ForceUpdate.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import Util from "../../../before/util/Util";
  2. import JsbSystem from "../../../mk/system/JsbSystem";
  3. import { UpdateState } from "../../data/AppData";
  4. import { AUDIO_TYPE } from "../../data/GameData";
  5. /** 最新版本下载 */
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class ForceUpdate extends cc.Component {
  9. @property(cc.Label)
  10. labUpdate: cc.Label = null
  11. @property(cc.Label)
  12. labFix: cc.Label = null
  13. @property(cc.Node)
  14. btnSure: cc.Node = null
  15. @property(cc.Node)
  16. btnCancel: cc.Node = null
  17. @property(cc.Sprite)
  18. fileProgress: cc.Sprite = null
  19. @property(cc.Label)
  20. labPro: cc.Label = null
  21. start() {
  22. this.setText()
  23. this.btnCancel.active = false;
  24. }
  25. clickDownLoad() {
  26. mk.audio.playEffect("ef_button_click")
  27. this.btnSure.active = false
  28. this.btnCancel.active = false
  29. this.fileProgress.node.parent.active = true
  30. let apkUrl = gData.appData.installUrl
  31. let arr = apkUrl.split('/')
  32. let apkName = arr[arr.length - 1]
  33. console.log('apkName ', apkName)
  34. let installUrl = apkName.replace('.apk', '');
  35. if (JsbSystem.isFileExist(installUrl)) {
  36. console.log('installUrl ', installUrl)
  37. //已下载过
  38. JsbSystem.installApk(installUrl);
  39. return null;
  40. }
  41. //未下载过
  42. JsbSystem.downFile2Local(apkUrl, apkName,
  43. (locaPath) => {
  44. setTimeout(() => {
  45. JsbSystem.installApk(installUrl)
  46. }, 1)
  47. },
  48. (locaPath) => {
  49. let str = '下载失败,请检查网络后重试'
  50. mk.tip.pop(str);
  51. this.fileProgress.node.parent.active = false
  52. this.fileProgress.fillRange = 0
  53. this.labPro.string = '0%'
  54. this.btnSure.active = true
  55. this.btnCancel.active = false
  56. },
  57. (bytesReceived, totalBytesReceived, totalBytesExpected) => {
  58. let pro = totalBytesReceived / totalBytesExpected
  59. this.setPro(pro)
  60. })
  61. }
  62. //更新进度
  63. setPro(pro) {
  64. this.labPro.string = `${Math.floor(pro * 100)}%`
  65. this.fileProgress.fillRange = pro
  66. }
  67. clickSkip() {
  68. mk.audio.playEffect(AUDIO_TYPE.button)
  69. mk.ui.closePanel(this.node.name);
  70. gData.appData.updateState = UpdateState.ForceUpdateFinish;
  71. }
  72. /** 设置文本 */
  73. setText() {
  74. this.labUpdate.string = gData.appData.updateDes;
  75. this.labFix.string = gData.appData.fixDes;
  76. }
  77. }