1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| export default class update { api = null version = '1.0.0' status = '' updateInfo = {} autoInstall=false constructor(api,autoInstall=false,choooseVersion='1.0.40') { this.api = api this.autoInstall=autoInstall this.choooseVersion = choooseVersion this.init() } async init() { this.status = await this.checkVersion() if(this.autoInstall){ this.doUpdate(true) } } getVersion() { return new Promise((resolve, reject) => { plus.runtime.getProperty(plus.runtime.appid, widgetInfo => { if (widgetInfo) { if(widgetInfo.version == this.choooseVersion){ uni.setStorageSync('chooseVersion',true) } resolve(widgetInfo.version) } reject(widgetInfo) }) }) } async checkVersion() { let version = await this.getVersion() this.version = version return new Promise((resolve, reject) => { this.api().then(res => { if (res) { this.updateInfo = res console.log(res) let { version: versionOl, update_size } = res if (versionOl == version) { resolve('') } let versionArr = version.split('.').map(x=>Number(x)) let versionOlArr = versionOl.split('.').map(x=>Number(x)) console.log(versionOlArr, versionArr) if (versionArr && versionOlArr) { if (versionOlArr[0] > versionArr[0] && update_size==2) { resolve('版本更新') } if (versionOlArr[0] == versionArr[0]) { if (versionOlArr[1] > versionArr[1]) { resolve('修复更新') } if (versionOlArr[0] == versionArr[0]) { if (versionOlArr[2] > versionArr[2]) { resolve('修复更新') } } } } reject('获取版本号失败') } }) }) } doFullUpdate() { let url = this.updateInfo.download_url plus.runtime.openURL(url); } doUpdate(autoInstall) { let that = this if(this.status == '版本更新'){ this.doFullUpdate() } if(this.status == '修复更新'){ if(autoInstall){ uni.showModal({ title:"更新提示", content:this.updateInfo.version_desc +'\n更新版本号:' +this.updateInfo.version, success(res){ if(res.confirm){ that.install() } } }) }else{ this.install() } } } install(){ uni.showLoading({ title:"下载更新中", icon:"none", mask:true }) uni.downloadFile({ url: this.updateInfo.update_file, success: downloadResult => { uni.hideLoading(); console.log(downloadResult); uni.showLoading({ title: "正在安装...", icon: "none", mask:true }); if (downloadResult.statusCode === 200) { plus.runtime.install( downloadResult.tempFilePath, {}, function () { uni.hideLoading(); console.log("install success..."); uni.showModal({ title: "提示", content: "更新已安装,是否立即重启?", success(res) { if (res.confirm) { plus.runtime.restart(); } } }); }, function (e) { uni.hideLoading(); console.error("install fail...", e); } ); } } }); } }
|