123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- const JMButton = require('JMButton');
- const UpdateProgress = require('UpdateProgress');
- cc.Class({
- editor: {
- menu: 'Launch/UpdateAlert'
- },
- extends: cc.Component,
- properties: {
- contentLabel: cc.Label,
- wifiNode: cc.Node,
- updateProgress: UpdateProgress,
- leftBtn: JMButton,
- rightBtn: JMButton,
- bannerNode: cc.Node,
- awardNode: cc.Node
- },
- // ================================================================================================
- onLoad () {
- // 弹框居中展示
- let y = cc.winSize.height / 2;
- let x = cc.winSize.width / 2;
- let newVec2 = this.node.convertToNodeSpaceAR(cc.v2(x, y));
- this.node.y = newVec2.y;
- this.node.x = newVec2.x;
- this.initStatusInfo();
- },
- onEnable () {
- cc.game.on('e_mgr_launch_version_update_downloading', this._handleVersionDownloading, this);
- cc.game.on('e_mgr_launch_version_update_pause', this._handleVersionPause, this);
- cc.game.on('e_mgr_launch_version_update_finished', this._handleVersionFinished, this);
- cc.game.on('e_mgr_launch_version_update_failed', this._handleVersionFailed, this);
- this._reloadData();
- this.reloadView();
- },
- onDisable () {
- cc.game.targetOff(this);
- },
- initStatusInfo () {
- this.status = undefined;
- // 强更按钮布局与回调
- this.forceBtnMap = {
- [JMC.UPDATE_STATUS.UNSTART]: [
- {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
- {active: true, text: '立即更新', cb: this.startDownloadOnClicked}
- ],
- [JMC.UPDATE_STATUS.DOWNLOADING]: [
- {active: false},
- {active: false}
- ],
- [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [
- {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
- {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
- ],
- [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [
- {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
- {active: true, text: '立即安装', cb: this.installOnClicked}
- ],
- [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [
- {active: true, text: '退出游戏', cb: this.exitGameOnClicked},
- {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
- ]
- };
- // 非强更
- this.unforceBtnMap = {
- [JMC.UPDATE_STATUS.UNSTART]: [
- {active: true, text: '暂不更新', cb: this.skipUpdateOnClicked},
- {active: true, text: '立即更新', cb: this.startDownloadOnClicked}
- ],
- [JMC.UPDATE_STATUS.DOWNLOADING]: [
- {active: true, text: '隐藏弹窗', pos: cc.v2(14, 1), cb: this.hideUpdateOnClicked},
- {active: false}
- ],
- [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [
- {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked},
- {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
- ],
- [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [
- {active: true, text: '暂不安装', cb: this.skipUpdateOnClicked},
- {active: true, text: '立即安装', cb: this.installOnClicked}
- ],
- [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [
- {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked},
- {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked}
- ]
- };
- },
- showAward () {
- if (!this.awardNode) {
- return;
- }
- // 绘制奖励
- if (this.awardListInfo.length == 1) {
- this.awardNode.getChildByName('gold').active = false;
- this.awardNode.getChildByName('diamond').active = false;
- this.awardNode.getChildByName('awardLine').active = false;
- let award0 = this.awardListInfo[0];
- this.awardNode.getChildByName('award').active = true;
- this.awardNode
- .getChildByName('award')
- .getChildByName('num')
- .getComponent(cc.Label).string = award0.count;
- if (award0.id == 2) {
- cc.loader.loadRes('res_texture/launch', cc.SpriteAtlas, (err, atlas) => {
- this.awardNode
- .getChildByName('award')
- .getComponent(cc.Sprite).spriteFrame = atlas.getSpriteFrame('launch_update_award_icon_02');
- });
- }
- } else {
- for (let i = 0; i < this.awardListInfo.length; i++) {
- let award = this.awardListInfo[i];
- if (award.id == 1) {
- this.awardNode
- .getChildByName('gold')
- .getChildByName('num')
- .getComponent(cc.Label).string = award.count;
- } else {
- this.awardNode
- .getChildByName('diamond')
- .getChildByName('num')
- .getComponent(cc.Label).string = award.count;
- }
- }
- }
- },
- showBanner (newAppBanner) {
- if (!this.bannerNode) {
- return;
- }
- const JMNetImage = require('JMNetImage');
- this.bannerNode.getChildByName('banner').getComponent(JMNetImage).iconUrl = newAppBanner;
- },
- reloadView () {
- let status = G.VersionUpdateMgr.status;
- // 刷新非WiFi的警告
- this.wifiNode.active = (cc.sys.getNetworkType() == cc.sys.NetworkType.WWAN) ? true : false;
- // 刷新进度条
- this.updateProgress.setDownloadStatus(status);
- let downloadedBytes = (G.VersionUpdateMgr.downloadProgress / 100) * G.VersionUpdateMgr.totalBytes;
- this.updateProgress.setProgress(downloadedBytes, G.VersionUpdateMgr.totalBytes);
- // 以下是状态更新了才刷新
- if (this.status === status)
- return;
- this.status = status;
- // 更新弹框描述
- this.contentLabel.string = this.infoMap[status];
- // 更新按钮
- let btnMap = this.isForceUpdate ? this.forceBtnMap : this.unforceBtnMap;
- let btnInfo = btnMap[status];
- // 设置左边的按钮
- let leftBtnInfo = btnInfo[0];
- if (leftBtnInfo.active) {
- this.leftBtn.node.active = true;
- this.leftBtn.title = leftBtnInfo.text;
- let cb = this.leftBtn.node.cb;
- if (cb) {
- this.leftBtn.node.off('click', cb, this);
- }
- this.leftBtn.node.cb = leftBtnInfo.cb;
- this.leftBtn.node.on('click', leftBtnInfo.cb, this);
- } else {
- this.leftBtn.node.active = false;
- }
- // 设置右侧按钮
- let rightBtnInfo = btnInfo[1];
- if (rightBtnInfo.active) {
- this.rightBtn.node.active = true;
- this.rightBtn.title = rightBtnInfo.text;
- let cb = this.rightBtn.node.cb;
- if (cb) {
- this.rightBtn.node.off('click', cb, this);
- }
- this.rightBtn.node.cb = rightBtnInfo.cb;
- this.rightBtn.node.on('click', rightBtnInfo.cb, this);
- } else {
- this.rightBtn.node.active = false;
- }
- },
- // 立即更新
- startDownloadOnClicked () {
- G.VersionUpdateMgr.download(this.newAppUrl, this.newAppMd5);
- },
- // 暂停下载
- pauseDownloadOnClicked () {
- G.VersionUpdateMgr.pauseDownload();
- },
- // 继续下载
- resumeDownloadOnClicked () {
- G.VersionUpdateMgr.resumeDownload();
- },
- // 退出游戏
- exitGameOnClicked () {
- if (!G.MiddleAuth.exitGame()) {
- cc.game.end();
- }
- },
- // 立即安装
- installOnClicked () {
- G.VersionUpdateMgr.install();
- },
- // 暂不更新
- skipUpdateOnClicked () {
- cc.game.emit('e_launch_update_show_progress');
- cc.game.emit('e_launch_enter_scene');
- this.node.active = false;
- },
- // 隐藏弹窗
- hideUpdateOnClicked () {
- cc.game.emit('e_launch_update_show_progress');
- cc.game.emit('e_launch_enter_scene');
- this.node.active = false;
- },
- _handleVersionDownloading () {
- this.reloadView();
- },
- _handleVersionPause () {
- this.reloadView();
- },
- _handleVersionFinished () {
- this.reloadView();
- },
- _handleVersionFailed () {
- this.reloadView();
- },
- _reloadData () {
- let remoteUpdateInfo = G.RemoteUpdateInfoMgr.remoteUpdateInfo;
- // 将版本信息接口返回的包大小转换成字节
- G.VersionUpdateMgr.totalBytes = remoteUpdateInfo.newAppSize * 1024 * 1024;
- // 内容信息
- this.infoMap = {
- [JMC.UPDATE_STATUS.UNSTART]: `发现更新内容,更新大小:${G.UpdateUtils.bytesToMb(G.VersionUpdateMgr.totalBytes).toFixed(1)}M,是否立即更新?`,
- [JMC.UPDATE_STATUS.DOWNLOADING]: '下载中,请保持网络畅通',
- [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: '检测到网络变更,下载被暂停',
- [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: '下载完成,立即启动游戏',
- [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: '下载失败,请保持网络畅通再试'
- };
- // 有无更新奖励判断
- this.haveAward = remoteUpdateInfo.newAppAward == '' ? false : true;
- // 有无banner判断
- this.haveBanner = remoteUpdateInfo.newAppBanner == '' ? false : true;
- // 是否强制更新判断
- this.isForceUpdate = remoteUpdateInfo.newAppForce == 0 ? false : true;
- // 是否静默更新判断
- this.isSilentUpdate = remoteUpdateInfo.newAppSilence == 0 ? false : true;
- // 转换奖励字符串成JSON
- if (this.haveAward) {
- this.awardListInfo = JSON.parse(remoteUpdateInfo.newAppAward);
- }
- this.newAppUrl = remoteUpdateInfo.newAppUrl;
- this.newAppMd5 = remoteUpdateInfo.newAppMd5;
- // 展示奖励
- if (this.haveAward) {
- this.showAward();
- }
- // 展示banner
- if (this.haveBanner) {
- this.showBanner(remoteUpdateInfo.newAppBanner);
- }
- this.status = undefined;
- }
- });
|