123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- // 版本更新管理
- let VersionUpdateMgr = {
- init () {
- if (!cc.sys.isNative) {
- return;
- }
- let UpdateCfg = require('UpdateCfg');
- // 是否模拟版本更新
- this.isDebug = UpdateCfg.simulationVersionUpdate;
- this.isDebugFail = UpdateCfg.simulationVersionFail;
- // 下载状态
- this.status = JMC.UPDATE_STATUS.UNSTART;
- // 下载进度
- this.downloadProgress = this.getCurProgress();
- this.totalBytes = 0;
- cc.game.on('e_middle_update_download_start', this._handleDownloadStart, this);
- cc.game.on('e_middle_update_download_update_progress', this._handleDownloadUpdateProgress, this);
- cc.game.on('e_middle_update_download_pause', this._handleDownloadPause, this);
- cc.game.on('e_middle_update_download_finished', this._handleDownloadFinished, this);
- cc.game.on('e_middle_update_download_fail', this._handleDownloadFail, this);
- },
- getChannelId () {
- let ret = G.MiddleDevice.getChannelId();
- return ret;
- },
- checkDownloaded (newAppUrl, newAppMd5) {
- let path = this.getDownloadedPath(newAppUrl, newAppMd5);
- if (path && path.length > 0) {
- this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_FINISHED);
- this.status = JMC.UPDATE_STATUS.DOWNLOAD_FINISHED;
- this.downloadProgress = 100;
- return true;
- } else {
- return false;
- }
- },
- /**
- * 是否支持版本更新
- *
- * @author libo
- * @date 2019-03-26
- * @returns
- */
- isSupportUpdate () {
- if (this.isDebug) {
- return false;
- }
- return G.MiddleUpdate.isSupportUpdate();
- },
- /**
- * 暂停下载[暂未使用]
- *
- * @author libo
- * @date 2019-03-26
- */
- pauseDownload () {
- if (this.isDebug) {
- if (this.isDebugFail) {
- // 暂停定时器
- clearInterval(this.intervalID);
- }
- this.handleDownloadPause();
- return;
- }
- G.MiddleUpdate.pauseDownload();
- },
- /**
- * 继续下载
- *
- * @author libo
- * @date 2019-03-26
- */
- resumeDownload () {
- if (this.isDebug) {
- if (this.isDebugFail) {
- // 恢复定时器
- this.intervalID = setInterval(() => {
- this.handleDownloadUpdateProgress(this.downloadProgress + 1);
- if (this.downloadProgress == 20) {
- this.handleDownloadFail();
- clearInterval(this.intervalID);
- }
- if (this.downloadProgress >= 100) {
- this.handleDownloadFinished();
- clearInterval(this.intervalID);
- }
- }, 200);
- }
- }
- G.MiddleUpdate.resumeDownload();
- },
- /**
- * 下载新版本
- *
- * @author libo
- * @date 2019-03-26
- * @param {string} newAppUrl
- * @param {string} newAppMd5
- * @returns bool 开始下载成功?
- */
- download (newAppUrl, newAppMd5) {
- if (this.isDebug) {
- // 模拟下载 定时器
- this.handleDownloadStart();
- if (this.isDebugFail) {
- this.intervalID = setInterval(() => {
- this.handleDownloadUpdateProgress(this.downloadProgress + 1);
- if (this.downloadProgress == 20) {
- this.handleDownloadFail();
- clearInterval(this.intervalID);
- }
- if (this.downloadProgress >= 100) {
- this.handleDownloadFinished();
- clearInterval(this.intervalID);
- }
- }, 200);
- }
- return true;
- }
- // 1、当配置的下载链接不是apk后缀时,点击立即下载时跳转到网页
- // 2、当渠道包不支持版本更新功能时,点击立即下载时跳转到网页
- let isAPK = newAppUrl.indexOf('apk', newAppUrl.length - 'apk'.length) !== -1;
- let isSupportUpdate = G.VersionUpdateMgr.isSupportUpdate();
- if (!isAPK || !isSupportUpdate) {
- cc.sys.openURL(newAppUrl);
- return;
- }
- return G.MiddleUpdate.download(newAppUrl, newAppMd5);
- },
- /**
- * 安装新版本
- *
- * @author libo
- * @date 2019-03-26
- * @returns bool 可以安装?
- */
- install () {
- if (this.isDebug) {
- return true;
- }
- return G.MiddleUpdate.install();
- },
- /**
- * 获取已下载的包文件路径[暂未使用]
- *
- * @author libo
- * @date 2019-03-26
- * @param {string} newAppUrl
- * @param {string} newAppMd5
- * @returns string 包文件路径
- */
- getDownloadedPath (newAppUrl, newAppMd5) {
- if (this.isDebug) {
- return '这是一个路径,模拟的';
- }
- return G.MiddleUpdate.getDownloadedPath(newAppUrl, newAppMd5);
- },
- /**
- * 获取当前下载进度[暂未使用]
- *
- * @author libo
- * @date 2019-03-26
- * @returns number 下载进度
- */
- getCurProgress () {
- if (this.isDebug) {
- return this.downloadProgress;
- }
- return G.MiddleUpdate.getCurProgress();
- },
- /**
- * 设置是否仅Wifi下载
- *
- * @author libo
- * @date 2019-03-26
- * @param {bool} isWifi
- */
- setWifiOnlyDownload (isWifi) {
- if (this.isDebug) {
- return;
- }
- G.MiddleUpdate.setWifiOnlyDownload(isWifi);
- },
- /**
- * 设置是否静默下载
- *
- * @author libo
- * @date 2019-03-26
- * @param {bool} isSilence
- */
- setSilenceDownload (isSilence) {
- if (this.isDebug) {
- return;
- }
- G.MiddleUpdate.setSilenceDownload(isSilence);
- },
- /**
- * 下载开始[Java回调]
- *
- * @author libo
- * @date 2019-03-26
- */
- _handleDownloadStart () {
- this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOADING);
- this.status = JMC.UPDATE_STATUS.DOWNLOADING;
- cc.game.emit('e_mgr_launch_version_update_downloading');
- },
- /**
- * 下载进度[Java回调]
- *
- * @author libo
- * @date 2019-03-26
- * @param {number} progress 百分比进度
- */
- _handleDownloadUpdateProgress (eventData) {
- // eventData.data.progress: 进度 0~100
- this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOADING);
- this.status = JMC.UPDATE_STATUS.DOWNLOADING;
- this.downloadProgress = eventData.data.progress;
- cc.game.emit('e_mgr_launch_version_update_downloading');
- },
- /**
- * 下载暂停[Java回调]
- *
- * @author libo
- * @date 2019-03-26
- */
- _handleDownloadPause () {
- this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_PAUSE);
- this.status = JMC.UPDATE_STATUS.DOWNLOAD_PAUSE;
- cc.game.emit('e_mgr_launch_version_update_pause');
- },
- /**
- * 下载完成[Java回调]
- *
- * @author libo
- * @date 2019-03-26
- */
- _handleDownloadFinished () {
- this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_FINISHED);
- this.status = JMC.UPDATE_STATUS.DOWNLOAD_FINISHED;
- this.downloadProgress = 100;
- cc.game.emit('e_mgr_launch_version_update_finished');
- },
- /**
- * 下载失败[Java回调]
- *
- * @author libo
- * @date 2019-03-26
- */
- _handleDownloadFail (eventData) {
- // eventData.errorMsg: 失败原因。枚举 Downloads.java
- this._stUpdateStatusChange(JMC.UPDATE_STATUS.DOWNLOAD_FAILED, eventData.errorMsg);
- this.status = JMC.UPDATE_STATUS.DOWNLOAD_FAILED;
- cc.game.emit('e_mgr_launch_version_update_failed');
- },
- // 统计 版本更新状态改变
- _stUpdateStatusChange (status, reason) {
- if (this.status === status)
- return;
- }
- };
- module.exports = VersionUpdateMgr;
|