HotUpdateAlert.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // 大资源更新弹窗
  2. const JMButton = require('JMButton');
  3. const UpdateProgress = require('UpdateProgress');
  4. cc.Class({
  5. editor: {
  6. menu: 'Launch/HotUpdateAlert'
  7. },
  8. extends: cc.Component,
  9. properties: {
  10. contentLabel: cc.Label,
  11. wifiNode: cc.Node,
  12. updateProgress: UpdateProgress,
  13. countdownLabel: cc.Label,
  14. leftBtn: JMButton,
  15. rightBtn: JMButton
  16. },
  17. onLoad () {
  18. this.initStatusInfo();
  19. this.countdownLabel.node.active = false;
  20. this._countDownFinish = false;
  21. // 弹框居中展示
  22. let y = cc.winSize.height / 2;
  23. let x = cc.winSize.width / 2;
  24. let newVec2 = this.node.convertToNodeSpaceAR(cc.v2(x, y));
  25. this.node.y = newVec2.y;
  26. this.node.x = newVec2.x;
  27. },
  28. onEnable () {
  29. cc.game.on('e_mgr_launch_update_progression', this._handleUpdateProgression, this);
  30. cc.game.on('e_mgr_launch_update_finished', this._handleUpdateFinished, this);
  31. cc.game.on('e_mgr_launch_update_failed', this._handleUpdateFailed, this);
  32. this.reloadView();
  33. },
  34. onDisable () {
  35. cc.game.targetOff(this);
  36. },
  37. initStatusInfo () {
  38. this.status = undefined;
  39. let remoteUpdateInfo = G.RemoteUpdateInfoMgr.remoteUpdateInfo;
  40. this.isForceUpdate = remoteUpdateInfo.newResForce;
  41. // 非强更按钮布局与回调
  42. this.forceBtnMap = {
  43. [JMC.UPDATE_STATUS.UNSTART]: [
  44. {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
  45. {active: true, text: '立即更新', cb: this.startDownloadOnClicked}
  46. ],
  47. [JMC.UPDATE_STATUS.DOWNLOADING]: [
  48. {active: false},
  49. {active: false}
  50. ],
  51. [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [
  52. {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
  53. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  54. ],
  55. [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [
  56. {active: true, text: '重启游戏', cb: this.restartOnClicked},
  57. {active: false}
  58. ],
  59. [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [
  60. {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
  61. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  62. ]
  63. };
  64. // 非强更按钮布局与回调
  65. this.unforceBtnMap = {
  66. [JMC.UPDATE_STATUS.UNSTART]: [
  67. {active: true, text: '暂不更新', cb: this.skipUpdateOnClicked},
  68. {active: true, text: '立即更新', cb: this.startDownloadOnClicked}
  69. ],
  70. [JMC.UPDATE_STATUS.DOWNLOADING]: [
  71. {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked, countdown: true},
  72. {active: false}
  73. ],
  74. [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [
  75. {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked},
  76. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  77. ],
  78. [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [
  79. {active: true, text: '重启游戏', cb: this.restartOnClicked},
  80. {active: false}
  81. ],
  82. [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [
  83. {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked},
  84. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  85. ]
  86. };
  87. // 弹框描述内容
  88. this.infoMap = {
  89. [JMC.UPDATE_STATUS.UNSTART]: `发现更新,更新大小:${G.UpdateUtils.bytesToMb(G.HotUpdateMgr.realTotalBytes).toFixed(1)}M,是否立即更新?`,
  90. [JMC.UPDATE_STATUS.DOWNLOADING]: '下载中,请保持网络畅通',
  91. [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: '检测到网络变更,下载被暂停',
  92. [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: '下载完成,立即启动游戏',
  93. [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: '下载失败,请保持网络畅通再试'
  94. };
  95. },
  96. reloadView () {
  97. let status = G.HotUpdateMgr.status;
  98. // 刷新非WiFi的警告
  99. this.wifiNode.active = (cc.sys.getNetworkType() == cc.sys.NetworkType.WWAN) ? true : false;
  100. // 刷新进度条
  101. this.updateProgress.setDownloadStatus(status);
  102. this.updateProgress.setProgress(G.HotUpdateMgr.downloadedBytes, G.HotUpdateMgr.realTotalBytes);
  103. // 以下是状态更新了才刷新
  104. if (this.status === status)
  105. return;
  106. this.status = status;
  107. this._stStatusChange(status);
  108. // 更新弹框描述
  109. this.contentLabel.string = this.infoMap[status];
  110. // 更新按钮
  111. let btnMap = this.isForceUpdate ? this.forceBtnMap : this.unforceBtnMap;
  112. let btnInfo = btnMap[status];
  113. // 设置左边的按钮
  114. let leftBtnInfo = btnInfo[0];
  115. if (leftBtnInfo.active) {
  116. this.leftBtn.node.active = true;
  117. this.leftBtn.title = leftBtnInfo.text;
  118. let cb = this.leftBtn.node.cb;
  119. if (cb) {
  120. this.leftBtn.node.off('click', cb, this);
  121. }
  122. this.leftBtn.node.cb = leftBtnInfo.cb;
  123. this.leftBtn.node.on('click', leftBtnInfo.cb, this);
  124. if (leftBtnInfo.countdown && !this._countDownFinish) {
  125. this.countdownLabel.node.active = true;
  126. let times = 10;
  127. this.leftBtn.interactable = false;
  128. this.schedule(
  129. () => {
  130. times--;
  131. this.countdownLabel.string = `${times}秒后可点击`;
  132. if (times <= 0) {
  133. this.leftBtn.interactable = true;
  134. this.countdownLabel.node.active = false;
  135. this._countDownFinish = true;
  136. }
  137. },
  138. 1,
  139. 10
  140. );
  141. }
  142. } else {
  143. this.leftBtn.node.active = false;
  144. }
  145. // 设置右侧按钮
  146. let rightBtnInfo = btnInfo[1];
  147. if (rightBtnInfo.active) {
  148. this.rightBtn.node.active = true;
  149. this.rightBtn.title = rightBtnInfo.text;
  150. let cb = this.rightBtn.node.cb;
  151. if (cb) {
  152. this.rightBtn.node.off('click', cb, this);
  153. }
  154. this.rightBtn.node.cb = rightBtnInfo.cb;
  155. this.rightBtn.node.on('click', rightBtnInfo.cb, this);
  156. } else {
  157. this.rightBtn.node.active = false;
  158. }
  159. },
  160. // 退出游戏
  161. exitGameOnClicked () {
  162. if (!G.MiddleAuth.exitGame()) {
  163. cc.game.end();
  164. }
  165. },
  166. // 立即更新
  167. startDownloadOnClicked () {
  168. G.HotUpdateMgr.gotoUpdate();
  169. },
  170. // 继续下载
  171. resumeDownloadOnClicked () {
  172. G.HotUpdateMgr.gotoUpdate();
  173. },
  174. // 重启游戏
  175. restartOnClicked () {
  176. G.HotUpdateMgr.restartApp();
  177. },
  178. // 暂不更新
  179. skipUpdateOnClicked () {
  180. cc.game.emit('e_launch_update_show_progress'); // 在非更新场景时,展示顶部进度条
  181. cc.game.emit('e_launch_enter_scene'); // 在更新场景时,进入下一个场景
  182. this.node.active = false;
  183. },
  184. // 隐藏弹窗
  185. hideUpdateOnClicked () {
  186. cc.game.emit('e_launch_update_show_progress'); // 在非更新场景时,展示顶部进度条
  187. cc.game.emit('e_launch_enter_scene'); // 在更新场景时,进入下一个场景
  188. this.node.active = false;
  189. },
  190. _handleUpdateProgression () {
  191. this.reloadView();
  192. },
  193. _handleUpdateFinished () {
  194. this.reloadView();
  195. },
  196. _handleUpdateFailed () {
  197. this.reloadView();
  198. }
  199. });