DownLoadUI.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import Loader from "../Loader";
  2. import AdM from "../manager/AdM";
  3. import GameM, { AUDIO_TYPE } from "../manager/GameM";
  4. import UiM, { PANEL_NAME } from "../manager/UiM";
  5. import { Utils } from "../utils/Utils";
  6. import EffectNode from "./EffectNode";
  7. /** 最新版本下载 */
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class DownLoadUI extends cc.Component {
  11. @property(cc.Toggle)
  12. skipToggle: cc.Toggle = null
  13. @property(cc.Label)
  14. labUpdate: cc.Label = null
  15. @property(cc.Label)
  16. labFix: cc.Label = null
  17. @property(cc.Node)
  18. btnSure: cc.Node = null
  19. @property(cc.Node)
  20. btnCancel: cc.Node = null
  21. @property(cc.Sprite)
  22. fileProgress: cc.Sprite = null
  23. @property(cc.Label)
  24. labPro: cc.Label = null
  25. /** 是否显示“不再提醒” */
  26. showToggle = false
  27. start() {
  28. this.setText()
  29. }
  30. init(showToggle) {
  31. this.skipToggle.node.active = showToggle
  32. this.showToggle = showToggle
  33. if (!showToggle) {
  34. this.btnSure.y = -366
  35. }
  36. }
  37. clickDownLoad() {
  38. GameM.audioM.playEffect(AUDIO_TYPE.button)
  39. this.btnSure.active = false
  40. this.btnCancel.active = false
  41. this.skipToggle.node.active = false
  42. this.fileProgress.node.parent.active = true
  43. let apkUrl = GameM.commonData.installUrl
  44. let arr = apkUrl.split('/')
  45. let apkName = arr[arr.length - 1]
  46. let installUrl = Utils.checkHasApk(apkName)
  47. //已下载过
  48. if (installUrl != '') {
  49. AdM.installApk(installUrl)
  50. }
  51. else {
  52. //未下载过
  53. Utils.downFile2Local(apkUrl, apkName,
  54. (locaPath) => {
  55. AdM.installApk(locaPath)
  56. },
  57. (locaPath) => {
  58. let str = '下载失败,请检查网络后重试'
  59. if (cc.director.getScene().name == 'Loader') {
  60. cc.find('Canvas').getComponent(Loader).PlayTip(str)
  61. }
  62. else {
  63. EffectNode.instance.PlayTip(str)
  64. }
  65. this.fileProgress.node.parent.active = false
  66. this.fileProgress.fillRange = 0
  67. this.labPro.string = '0%'
  68. this.btnSure.active = true
  69. this.btnCancel.active = true
  70. this.skipToggle.node.active = this.showToggle
  71. },
  72. (bytesReceived, totalBytesReceived, totalBytesExpected) => {
  73. let pro = totalBytesReceived / totalBytesExpected
  74. this.setPro(pro)
  75. })
  76. }
  77. }
  78. //更新进度
  79. setPro(pro) {
  80. this.labPro.string = `${Math.floor(pro * 100)}%`
  81. this.fileProgress.fillRange = pro
  82. }
  83. clickSkip() {
  84. GameM.audioM.playEffect(AUDIO_TYPE.button)
  85. if (this.skipToggle.isChecked) {
  86. let installVersion = GameM.commonData.installVersion
  87. GameM.globalStorage.setStorage('installVersion' + installVersion, 'skip', 1)
  88. }
  89. UiM.Instance.offPanel(PANEL_NAME.DownLoadUI)
  90. GameM.commonData.skipUpdate = true
  91. }
  92. /** 设置文本 */
  93. setText() {
  94. this.labUpdate.string = GameM.commonData.updateDes
  95. this.labFix.string = GameM.commonData.fixDes
  96. }
  97. }