ForceUpdate.ts 2.6 KB

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