import store from '../store/index.js' import w_md5 from "../libs/zww-md5/w_md5.js" import { randomString } from '../utils/rocessor.js'; const baseurl = store.state.baseURL const getUpAppUrl = baseurl + '/api/version'; const getIosUpAppUrl = 'https://itunes.apple.com/cn/lookup?id=6474850968' // 请求升级获取数据地址 const iosAppStroeUrl = 'https://apps.apple.com/cn/app/%E6%98%93%E8%B6%A3cbb/id=6474850968'; // 获取app是否需要升级 export function getUpApp() { // 获取当前运行系统 let hj = uni.getSystemInfoSync().platform; let { AUTH_TOKEN, Timestamp } = store.state.md5; let NONCE = randomString(12); let URL = ''; if(hj === 'ios'){ URL = getIosUpAppUrl; } if(hj === 'android'){ URL = getUpAppUrl; } let MD5 = w_md5.hex_md5_32(Timestamp + NONCE + AUTH_TOKEN + URL); let tokenMD5 = `${Timestamp}|${NONCE}|${MD5}`; let header = { "Authori-zation": 'Bearer ' + uni .getStorageSync('token'), "X-YD-Req-Token": tokenMD5, "Access-Control-Max-Age":-1 }; // 获取仓库app数据对象 if (hj === 'ios') { uni.request({ url: getIosUpAppUrl, method: 'POST', data: {}, header, success: res => { let r = res.data; isUp(r, hj) }, fail: res => { store.commit('changeState', true); console.log(res, 'shib'); } }); // 设置显示数据 return } // 当前系统为安卓则显示数据 if (hj === 'android') { uni.request({ url: getUpAppUrl, method: 'GET', data: {}, header, success: res => { let r = res.data.data; console.log(r, '234') isUp(r, hj) }, fail: res => { store.commit('changeState', true); console.log(res, 'shib'); } }); } } //判断是否需要更新 function isUp(r, hj) { plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) { let version = ''; let arr = ''; if (hj == "android") { // 保存线上版本号 version = r.url; store.commit('setappversion', r.version); // 获取线上版本 arr = r.version.split('.'); } if (hj == "ios") { // 保存线上版本号 version = r.results[0].version; store.commit('setappversion', version); // 获取线上版本 arr = r.results[0].version.split('.'); } // 获取当前系统 const arr1 = wgtinfo.version.split('.'); for (let i = 0; i < arr.length; i++) { // 线上版本号 const x = +arr[i]; // 本地版本号 const y = +arr1[i]; // 判断线上版本是否小于本地版本 console.log(x, y, '数据'); if (x < y) { console.log('数据隐藏'); // 设置显示数据 store.commit('changeState', false); return } // 判断线上版本是否大于本地版本 else if (x > y) { uni.showModal({ title: '提示', content: '请更新应用', showCancel: false, success(e) { if (hj == "android") { downApp(version); } if (hj == "ios") { plus.runtime.openURL( iosAppStroeUrl ); } } }); return } // 判断是否本地版本等于线上版本 else if (x == y && i == arr.length - 1) { store.commit('changeState', true); return } } }); } // 下载app export function downApp(version) { console.log(version) uni.navigateTo({ url: '/pages/index/downLoad?apk=' + version, fail(err) { console.log(err); } }) // plus.nativeUI.showWaiting('下载升级包...'); // plus.downloader // .createDownload(version, {}, (d, status) => { // if (status == 200) { // installApp(d.filename); // 安装app // } else { // plus.nativeUI.alert('下载升级包失败!'); // } // plus.nativeUI.closeWaiting(); // }) // .start(); } // 安装app export function installApp(path) { plus.nativeUI.showWaiting('安装升级包...'); plus.runtime.install( path, {}, function() { plus.nativeUI.closeWaiting(); uni.showToast({ icon: 'none', title: '升级完成,准备重新载入' }); setTimeout(_ => { uni.hideToast(); plus.runtime.restart(); }, 1000); }, function(e) { plus.nativeUI.closeWaiting(); plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message); } ); }