ForceUpdate.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  24. clickDownLoad() {
  25. mk.audio.playEffect("ef_button_click")
  26. this.btnSure.active = false
  27. this.btnCancel.active = false
  28. this.fileProgress.node.parent.active = true
  29. let apkUrl = gData.appData.installUrl
  30. let arr = apkUrl.split('/')
  31. let apkName = arr[arr.length - 1]
  32. console.log('apkName ', apkName)
  33. let installUrl = apkName.replace('.apk', '');
  34. if (JsbSystem.isFileExist(installUrl)) {
  35. console.log('installUrl ', installUrl)
  36. //已下载过
  37. JsbSystem.installApk(installUrl);
  38. return null;
  39. }
  40. //未下载过
  41. JsbSystem.downFile2Local(apkUrl, apkName,
  42. (locaPath) => {
  43. setTimeout(() => {
  44. JsbSystem.installApk(installUrl)
  45. }, 1)
  46. },
  47. (locaPath) => {
  48. let str = '下载失败,请检查网络后重试'
  49. mk.tip.pop(str);
  50. this.fileProgress.node.parent.active = false
  51. this.fileProgress.fillRange = 0
  52. this.labPro.string = '0%'
  53. this.btnSure.active = true
  54. this.btnCancel.active = true
  55. },
  56. (bytesReceived, totalBytesReceived, totalBytesExpected) => {
  57. let pro = totalBytesReceived / totalBytesExpected
  58. this.setPro(pro)
  59. })
  60. }
  61. //更新进度
  62. setPro(pro) {
  63. this.labPro.string = `${Math.floor(pro * 100)}%`
  64. this.fileProgress.fillRange = pro
  65. }
  66. clickSkip() {
  67. mk.audio.playEffect(AUDIO_TYPE.button)
  68. mk.ui.closePanel(this.node.name);
  69. gData.appData.updateState = UpdateState.ForceUpdateFinish;
  70. }
  71. /** 设置文本 */
  72. setText() {
  73. this.labUpdate.string = gData.appData.updateDes;
  74. this.labFix.string = gData.appData.fixDes;
  75. }
  76. }