VersionUpdateMgr.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // 版本更新管理
  2. let VersionUpdateMgr = {
  3. init () {
  4. if (!cc.sys.isNative) {
  5. return;
  6. }
  7. let UpdateCfg = require('UpdateCfg');
  8. // 是否模拟版本更新
  9. this.isDebug = UpdateCfg.simulationVersionUpdate;
  10. this.isDebugFail = UpdateCfg.simulationVersionFail;
  11. // 下载状态
  12. this.status = JMC.UPDATE_STATUS.UNSTART;
  13. // 下载进度
  14. this.downloadProgress = this.getCurProgress();
  15. this.totalBytes = 0;
  16. cc.game.on('e_middle_update_download_start', this._handleDownloadStart, this);
  17. cc.game.on('e_middle_update_download_update_progress', this._handleDownloadUpdateProgress, this);
  18. cc.game.on('e_middle_update_download_pause', this._handleDownloadPause, this);
  19. cc.game.on('e_middle_update_download_finished', this._handleDownloadFinished, this);
  20. cc.game.on('e_middle_update_download_fail', this._handleDownloadFail, this);
  21. },
  22. getChannelId () {
  23. let ret = G.MiddleDevice.getChannelId();
  24. return ret;
  25. },
  26. checkDownloaded (newAppUrl, newAppMd5) {
  27. let path = this.getDownloadedPath(newAppUrl, newAppMd5);
  28. if (path && path.length > 0) {
  29. this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_FINISHED);
  30. this.status = JMC.UPDATE_STATUS.DOWNLOAD_FINISHED;
  31. this.downloadProgress = 100;
  32. return true;
  33. } else {
  34. return false;
  35. }
  36. },
  37. /**
  38. * 是否支持版本更新
  39. *
  40. * @author libo
  41. * @date 2019-03-26
  42. * @returns
  43. */
  44. isSupportUpdate () {
  45. if (this.isDebug) {
  46. return false;
  47. }
  48. return G.MiddleUpdate.isSupportUpdate();
  49. },
  50. /**
  51. * 暂停下载[暂未使用]
  52. *
  53. * @author libo
  54. * @date 2019-03-26
  55. */
  56. pauseDownload () {
  57. if (this.isDebug) {
  58. if (this.isDebugFail) {
  59. // 暂停定时器
  60. clearInterval(this.intervalID);
  61. }
  62. this.handleDownloadPause();
  63. return;
  64. }
  65. G.MiddleUpdate.pauseDownload();
  66. },
  67. /**
  68. * 继续下载
  69. *
  70. * @author libo
  71. * @date 2019-03-26
  72. */
  73. resumeDownload () {
  74. if (this.isDebug) {
  75. if (this.isDebugFail) {
  76. // 恢复定时器
  77. this.intervalID = setInterval(() => {
  78. this.handleDownloadUpdateProgress(this.downloadProgress + 1);
  79. if (this.downloadProgress == 20) {
  80. this.handleDownloadFail();
  81. clearInterval(this.intervalID);
  82. }
  83. if (this.downloadProgress >= 100) {
  84. this.handleDownloadFinished();
  85. clearInterval(this.intervalID);
  86. }
  87. }, 200);
  88. }
  89. }
  90. G.MiddleUpdate.resumeDownload();
  91. },
  92. /**
  93. * 下载新版本
  94. *
  95. * @author libo
  96. * @date 2019-03-26
  97. * @param {string} newAppUrl
  98. * @param {string} newAppMd5
  99. * @returns bool 开始下载成功?
  100. */
  101. download (newAppUrl, newAppMd5) {
  102. if (this.isDebug) {
  103. // 模拟下载 定时器
  104. this.handleDownloadStart();
  105. if (this.isDebugFail) {
  106. this.intervalID = setInterval(() => {
  107. this.handleDownloadUpdateProgress(this.downloadProgress + 1);
  108. if (this.downloadProgress == 20) {
  109. this.handleDownloadFail();
  110. clearInterval(this.intervalID);
  111. }
  112. if (this.downloadProgress >= 100) {
  113. this.handleDownloadFinished();
  114. clearInterval(this.intervalID);
  115. }
  116. }, 200);
  117. }
  118. return true;
  119. }
  120. // 1、当配置的下载链接不是apk后缀时,点击立即下载时跳转到网页
  121. // 2、当渠道包不支持版本更新功能时,点击立即下载时跳转到网页
  122. let isAPK = newAppUrl.indexOf('apk', newAppUrl.length - 'apk'.length) !== -1;
  123. let isSupportUpdate = G.VersionUpdateMgr.isSupportUpdate();
  124. if (!isAPK || !isSupportUpdate) {
  125. cc.sys.openURL(newAppUrl);
  126. return;
  127. }
  128. return G.MiddleUpdate.download(newAppUrl, newAppMd5);
  129. },
  130. /**
  131. * 安装新版本
  132. *
  133. * @author libo
  134. * @date 2019-03-26
  135. * @returns bool 可以安装?
  136. */
  137. install () {
  138. if (this.isDebug) {
  139. return true;
  140. }
  141. return G.MiddleUpdate.install();
  142. },
  143. /**
  144. * 获取已下载的包文件路径[暂未使用]
  145. *
  146. * @author libo
  147. * @date 2019-03-26
  148. * @param {string} newAppUrl
  149. * @param {string} newAppMd5
  150. * @returns string 包文件路径
  151. */
  152. getDownloadedPath (newAppUrl, newAppMd5) {
  153. if (this.isDebug) {
  154. return '这是一个路径,模拟的';
  155. }
  156. return G.MiddleUpdate.getDownloadedPath(newAppUrl, newAppMd5);
  157. },
  158. /**
  159. * 获取当前下载进度[暂未使用]
  160. *
  161. * @author libo
  162. * @date 2019-03-26
  163. * @returns number 下载进度
  164. */
  165. getCurProgress () {
  166. if (this.isDebug) {
  167. return this.downloadProgress;
  168. }
  169. return G.MiddleUpdate.getCurProgress();
  170. },
  171. /**
  172. * 设置是否仅Wifi下载
  173. *
  174. * @author libo
  175. * @date 2019-03-26
  176. * @param {bool} isWifi
  177. */
  178. setWifiOnlyDownload (isWifi) {
  179. if (this.isDebug) {
  180. return;
  181. }
  182. G.MiddleUpdate.setWifiOnlyDownload(isWifi);
  183. },
  184. /**
  185. * 设置是否静默下载
  186. *
  187. * @author libo
  188. * @date 2019-03-26
  189. * @param {bool} isSilence
  190. */
  191. setSilenceDownload (isSilence) {
  192. if (this.isDebug) {
  193. return;
  194. }
  195. G.MiddleUpdate.setSilenceDownload(isSilence);
  196. },
  197. /**
  198. * 下载开始[Java回调]
  199. *
  200. * @author libo
  201. * @date 2019-03-26
  202. */
  203. _handleDownloadStart () {
  204. this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOADING);
  205. this.status = JMC.UPDATE_STATUS.DOWNLOADING;
  206. cc.game.emit('e_mgr_launch_version_update_downloading');
  207. },
  208. /**
  209. * 下载进度[Java回调]
  210. *
  211. * @author libo
  212. * @date 2019-03-26
  213. * @param {number} progress 百分比进度
  214. */
  215. _handleDownloadUpdateProgress (eventData) {
  216. // eventData.data.progress: 进度 0~100
  217. this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOADING);
  218. this.status = JMC.UPDATE_STATUS.DOWNLOADING;
  219. this.downloadProgress = eventData.data.progress;
  220. cc.game.emit('e_mgr_launch_version_update_downloading');
  221. },
  222. /**
  223. * 下载暂停[Java回调]
  224. *
  225. * @author libo
  226. * @date 2019-03-26
  227. */
  228. _handleDownloadPause () {
  229. this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_PAUSE);
  230. this.status = JMC.UPDATE_STATUS.DOWNLOAD_PAUSE;
  231. cc.game.emit('e_mgr_launch_version_update_pause');
  232. },
  233. /**
  234. * 下载完成[Java回调]
  235. *
  236. * @author libo
  237. * @date 2019-03-26
  238. */
  239. _handleDownloadFinished () {
  240. this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_FINISHED);
  241. this.status = JMC.UPDATE_STATUS.DOWNLOAD_FINISHED;
  242. this.downloadProgress = 100;
  243. cc.game.emit('e_mgr_launch_version_update_finished');
  244. },
  245. /**
  246. * 下载失败[Java回调]
  247. *
  248. * @author libo
  249. * @date 2019-03-26
  250. */
  251. _handleDownloadFail (eventData) {
  252. // eventData.errorMsg: 失败原因。枚举 Downloads.java
  253. this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_FAILED, eventData.errorMsg);
  254. this.status = JMC.UPDATE_STATUS.DOWNLOAD_FAILED;
  255. cc.game.emit('e_mgr_launch_version_update_failed');
  256. },
  257. // 统计 版本更新状态改变
  258. _stUpdateStatusChange (status, reason) {
  259. if (this.status === status)
  260. return;
  261. }
  262. };
  263. module.exports = VersionUpdateMgr;