UpdateAlert.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. const JMButton = require('JMButton');
  2. const UpdateProgress = require('UpdateProgress');
  3. cc.Class({
  4. editor: {
  5. menu: 'Launch/UpdateAlert'
  6. },
  7. extends: cc.Component,
  8. properties: {
  9. contentLabel: cc.Label,
  10. wifiNode: cc.Node,
  11. updateProgress: UpdateProgress,
  12. leftBtn: JMButton,
  13. rightBtn: JMButton,
  14. bannerNode: cc.Node,
  15. awardNode: cc.Node
  16. },
  17. // ================================================================================================
  18. onLoad () {
  19. // 弹框居中展示
  20. let y = cc.winSize.height / 2;
  21. let x = cc.winSize.width / 2;
  22. let newVec2 = this.node.convertToNodeSpaceAR(cc.v2(x, y));
  23. this.node.y = newVec2.y;
  24. this.node.x = newVec2.x;
  25. this.initStatusInfo();
  26. },
  27. onEnable () {
  28. cc.game.on('e_mgr_launch_version_update_downloading', this._handleVersionDownloading, this);
  29. cc.game.on('e_mgr_launch_version_update_pause', this._handleVersionPause, this);
  30. cc.game.on('e_mgr_launch_version_update_finished', this._handleVersionFinished, this);
  31. cc.game.on('e_mgr_launch_version_update_failed', this._handleVersionFailed, this);
  32. this._reloadData();
  33. this.reloadView();
  34. },
  35. onDisable () {
  36. cc.game.targetOff(this);
  37. },
  38. initStatusInfo () {
  39. this.status = undefined;
  40. // 强更按钮布局与回调
  41. this.forceBtnMap = {
  42. [JMC.UPDATE_STATUS.UNSTART]: [
  43. {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
  44. {active: true, text: '立即更新', cb: this.startDownloadOnClicked}
  45. ],
  46. [JMC.UPDATE_STATUS.DOWNLOADING]: [
  47. {active: false},
  48. {active: false}
  49. ],
  50. [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [
  51. {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
  52. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  53. ],
  54. [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [
  55. {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
  56. {active: true, text: '立即安装', cb: this.installOnClicked}
  57. ],
  58. [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [
  59. {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
  60. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  61. ]
  62. };
  63. // 非强更
  64. this.unforceBtnMap = {
  65. [JMC.UPDATE_STATUS.UNSTART]: [
  66. {active: true, text: '暂不更新', cb: this.skipUpdateOnClicked},
  67. {active: true, text: '立即更新', cb: this.startDownloadOnClicked}
  68. ],
  69. [JMC.UPDATE_STATUS.DOWNLOADING]: [
  70. {active: true, text: '隐藏弹窗', pos: cc.v2(14, 1), cb: this.hideUpdateOnClicked},
  71. {active: false}
  72. ],
  73. [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [
  74. {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked},
  75. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  76. ],
  77. [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [
  78. {active: true, text: '暂不安装', cb: this.skipUpdateOnClicked},
  79. {active: true, text: '立即安装', cb: this.installOnClicked}
  80. ],
  81. [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [
  82. {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked},
  83. {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
  84. ]
  85. };
  86. },
  87. showAward () {
  88. if (!this.awardNode) {
  89. return;
  90. }
  91. // 绘制奖励
  92. if (this.awardListInfo.length == 1) {
  93. this.awardNode.getChildByName('gold').active = false;
  94. this.awardNode.getChildByName('diamond').active = false;
  95. this.awardNode.getChildByName('awardLine').active = false;
  96. let award0 = this.awardListInfo[0];
  97. this.awardNode.getChildByName('award').active = true;
  98. this.awardNode
  99. .getChildByName('award')
  100. .getChildByName('num')
  101. .getComponent(cc.Label).string = award0.count;
  102. if (award0.id == 2) {
  103. cc.loader.loadRes('res_texture/launch', cc.SpriteAtlas, (err, atlas) => {
  104. this.awardNode
  105. .getChildByName('award')
  106. .getComponent(cc.Sprite).spriteFrame = atlas.getSpriteFrame('launch_update_award_icon_02');
  107. });
  108. }
  109. } else {
  110. for (let i = 0; i < this.awardListInfo.length; i++) {
  111. let award = this.awardListInfo[i];
  112. if (award.id == 1) {
  113. this.awardNode
  114. .getChildByName('gold')
  115. .getChildByName('num')
  116. .getComponent(cc.Label).string = award.count;
  117. } else {
  118. this.awardNode
  119. .getChildByName('diamond')
  120. .getChildByName('num')
  121. .getComponent(cc.Label).string = award.count;
  122. }
  123. }
  124. }
  125. },
  126. showBanner (newAppBanner) {
  127. if (!this.bannerNode) {
  128. return;
  129. }
  130. const JMNetImage = require('JMNetImage');
  131. this.bannerNode.getChildByName('banner').getComponent(JMNetImage).iconUrl = newAppBanner;
  132. },
  133. reloadView () {
  134. let status = G.VersionUpdateMgr.status;
  135. // 刷新非WiFi的警告
  136. this.wifiNode.active = (cc.sys.getNetworkType() == cc.sys.NetworkType.WWAN) ? true : false;
  137. // 刷新进度条
  138. this.updateProgress.setDownloadStatus(status);
  139. let downloadedBytes = (G.VersionUpdateMgr.downloadProgress / 100) * G.VersionUpdateMgr.totalBytes;
  140. this.updateProgress.setProgress(downloadedBytes, G.VersionUpdateMgr.totalBytes);
  141. // 以下是状态更新了才刷新
  142. if (this.status === status)
  143. return;
  144. this.status = status;
  145. // 更新弹框描述
  146. this.contentLabel.string = this.infoMap[status];
  147. // 更新按钮
  148. let btnMap = this.isForceUpdate ? this.forceBtnMap : this.unforceBtnMap;
  149. let btnInfo = btnMap[status];
  150. // 设置左边的按钮
  151. let leftBtnInfo = btnInfo[0];
  152. if (leftBtnInfo.active) {
  153. this.leftBtn.node.active = true;
  154. this.leftBtn.title = leftBtnInfo.text;
  155. let cb = this.leftBtn.node.cb;
  156. if (cb) {
  157. this.leftBtn.node.off('click', cb, this);
  158. }
  159. this.leftBtn.node.cb = leftBtnInfo.cb;
  160. this.leftBtn.node.on('click', leftBtnInfo.cb, this);
  161. } else {
  162. this.leftBtn.node.active = false;
  163. }
  164. // 设置右侧按钮
  165. let rightBtnInfo = btnInfo[1];
  166. if (rightBtnInfo.active) {
  167. this.rightBtn.node.active = true;
  168. this.rightBtn.title = rightBtnInfo.text;
  169. let cb = this.rightBtn.node.cb;
  170. if (cb) {
  171. this.rightBtn.node.off('click', cb, this);
  172. }
  173. this.rightBtn.node.cb = rightBtnInfo.cb;
  174. this.rightBtn.node.on('click', rightBtnInfo.cb, this);
  175. } else {
  176. this.rightBtn.node.active = false;
  177. }
  178. },
  179. // 立即更新
  180. startDownloadOnClicked () {
  181. G.VersionUpdateMgr.download(this.newAppUrl, this.newAppMd5);
  182. },
  183. // 暂停下载
  184. pauseDownloadOnClicked () {
  185. G.VersionUpdateMgr.pauseDownload();
  186. },
  187. // 继续下载
  188. resumeDownloadOnClicked () {
  189. G.VersionUpdateMgr.resumeDownload();
  190. },
  191. // 退出游戏
  192. exitGameOnClicked () {
  193. if (!G.MiddleAuth.exitGame()) {
  194. cc.game.end();
  195. }
  196. },
  197. // 立即安装
  198. installOnClicked () {
  199. G.VersionUpdateMgr.install();
  200. },
  201. // 暂不更新
  202. skipUpdateOnClicked () {
  203. cc.game.emit('e_launch_update_show_progress');
  204. cc.game.emit('e_launch_enter_scene');
  205. this.node.active = false;
  206. },
  207. // 隐藏弹窗
  208. hideUpdateOnClicked () {
  209. cc.game.emit('e_launch_update_show_progress');
  210. cc.game.emit('e_launch_enter_scene');
  211. this.node.active = false;
  212. },
  213. _handleVersionDownloading () {
  214. this.reloadView();
  215. },
  216. _handleVersionPause () {
  217. this.reloadView();
  218. },
  219. _handleVersionFinished () {
  220. this.reloadView();
  221. },
  222. _handleVersionFailed () {
  223. this.reloadView();
  224. },
  225. _reloadData () {
  226. let remoteUpdateInfo = G.RemoteUpdateInfoMgr.remoteUpdateInfo;
  227. // 将版本信息接口返回的包大小转换成字节
  228. G.VersionUpdateMgr.totalBytes = remoteUpdateInfo.newAppSize * 1024 * 1024;
  229. // 内容信息
  230. this.infoMap = {
  231. [JMC.UPDATE_STATUS.UNSTART]: `发现更新内容,更新大小:${G.UpdateUtils.bytesToMb(G.VersionUpdateMgr.totalBytes).toFixed(1)}M,是否立即更新?`,
  232. [JMC.UPDATE_STATUS.DOWNLOADING]: '下载中,请保持网络畅通',
  233. [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: '检测到网络变更,下载被暂停',
  234. [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: '下载完成,立即启动游戏',
  235. [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: '下载失败,请保持网络畅通再试'
  236. };
  237. // 有无更新奖励判断
  238. this.haveAward = remoteUpdateInfo.newAppAward == '' ? false : true;
  239. // 有无banner判断
  240. this.haveBanner = remoteUpdateInfo.newAppBanner == '' ? false : true;
  241. // 是否强制更新判断
  242. this.isForceUpdate = remoteUpdateInfo.newAppForce == 0 ? false : true;
  243. // 是否静默更新判断
  244. this.isSilentUpdate = remoteUpdateInfo.newAppSilence == 0 ? false : true;
  245. // 转换奖励字符串成JSON
  246. if (this.haveAward) {
  247. this.awardListInfo = JSON.parse(remoteUpdateInfo.newAppAward);
  248. }
  249. this.newAppUrl = remoteUpdateInfo.newAppUrl;
  250. this.newAppMd5 = remoteUpdateInfo.newAppMd5;
  251. // 展示奖励
  252. if (this.haveAward) {
  253. this.showAward();
  254. }
  255. // 展示banner
  256. if (this.haveBanner) {
  257. this.showBanner(remoteUpdateInfo.newAppBanner);
  258. }
  259. this.status = undefined;
  260. }
  261. });