ef915a121e
可以在项目代码中直接使用 import um = require('UpdateManager') 然后调用相关接口.
129 lines
4.4 KiB
JavaScript
129 lines
4.4 KiB
JavaScript
/**
|
|
* 方便直接获取当前版本号等信息.
|
|
* 使用方式:
|
|
* // @ts-ignore
|
|
* import UpdateManager = require( "UpdateManager");
|
|
* 然后在代码中直接使用即可.
|
|
*/
|
|
class UpdateManager {
|
|
|
|
cur_ver_code = 0;
|
|
cur_ver_name = "1.0.1";
|
|
cur_ver_desc = "";
|
|
ver_type = "dev";
|
|
|
|
new_ver_code = 0;
|
|
new_ver_name = "1.0.1";
|
|
new_ver_desc = "";
|
|
new_ver_info = {};
|
|
|
|
// 记录当前版本的bundles. 如果有新版本, 则自动下载新bundles.
|
|
cur_bundles = {};
|
|
|
|
updateSuccess = "0";
|
|
|
|
constructor() {
|
|
const curVer = window.localStorage.getItem("cur_ver_info");
|
|
if (curVer) {
|
|
const verInfo = JSON.parse(curVer);
|
|
this.new_ver_code = this.cur_ver_code = verInfo.versionCode;
|
|
this.new_ver_name = this.cur_ver_name = verInfo.versionName;
|
|
this.ver_type = verInfo.versionType;
|
|
this.new_ver_desc = this.cur_ver_desc = verInfo.versionLog;
|
|
this.new_ver_info = verInfo;
|
|
this.cur_bundles = verInfo.bundleVers;
|
|
}
|
|
|
|
this.updateSuccess = window.localStorage.getItem("ver_updated") || 0;
|
|
this.fetchNewVerInfo();
|
|
}
|
|
|
|
// 此方法适用于非强制更新包. 需要设计信息界面,显示版本信息.
|
|
// 手动调用进行更新下载, 下载完成返回true, 此时需要重启游戏才能应用更新. 需要界面提示用户.
|
|
doUpdate() {
|
|
if (this.hasNewVersion()) {
|
|
window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
resetUpdateFlag() {
|
|
window.localStorage.setItem('ver_updated', '0');
|
|
}
|
|
|
|
getUpdateFlag() {
|
|
return this.updateSuccess == 1;
|
|
}
|
|
|
|
// 检测是否有新版本.
|
|
hasNewVersion() {
|
|
return this.new_ver_info && this.new_ver_info.versionCode && this.new_ver_info.versionCode != this.cur_ver_code;
|
|
}
|
|
|
|
fetchNewVerInfo() {
|
|
let url = window.updateUrl || false;
|
|
if (!url) {
|
|
return;
|
|
}
|
|
// 游戏启动后再请求更新,避免影响启动速度.
|
|
url += `?_t=${Math.random()}`;
|
|
cc.log("请求更新地址:", url);
|
|
this._get(url).then(version => {
|
|
if (!version) {
|
|
console.log("未检测到更新版本内容");
|
|
return;
|
|
}
|
|
if (version) {
|
|
cc.log('当前版本号:', this.cur_ver_code, this.cur_ver_name);
|
|
cc.log('请求更新内容:', version);
|
|
let verInfo = version;
|
|
if (typeof version === 'string') {
|
|
verInfo = JSON.parse(version);
|
|
}
|
|
this.new_ver_code = verInfo.versionCode;
|
|
this.new_ver_name = verInfo.versionName;
|
|
this.new_ver_desc = verInfo.versionLog;
|
|
|
|
this.new_ver_info = verInfo;
|
|
|
|
// 如果首次运行,本地未缓存版本信息,则进行缓存.
|
|
if (!this.cur_ver_code) {
|
|
cc.log("当前首次运行,记录版本信息");
|
|
window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info));
|
|
|
|
this.new_ver_code = this.cur_ver_code = verInfo.versionCode;
|
|
this.new_ver_name = this.cur_ver_name = verInfo.versionName;
|
|
this.ver_type = verInfo.versionType;
|
|
this.new_ver_desc = this.cur_ver_desc = verInfo.versionLog;
|
|
|
|
} else if (this.hasNewVersion() && verInfo.forceUpdate) {
|
|
window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info));
|
|
window.localStorage.setItem('ver_updated', '1');
|
|
cc.log("有新版本,且默认强制更新");
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// ajax 请求.
|
|
_get(url) {
|
|
return new Promise(resolve => {
|
|
const ajax = new XMLHttpRequest();
|
|
ajax.open("get", url, true);
|
|
ajax.setRequestHeader("Content-Type", "application/json;charset=utf-8");
|
|
ajax.onreadystatechange = function () {
|
|
if (ajax.readyState == 4) {
|
|
if (ajax.status == 200) {
|
|
resolve(ajax.responseText);
|
|
} else {
|
|
resolve(null);
|
|
}
|
|
}
|
|
}
|
|
ajax.send(null);
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = new UpdateManager(); |