123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- const update ={
- namespaced:true,
- state: {
- downloadTask: '',
- device: 'android', //设备环境
- progress: {}, //下载进度
- },
- mutations: {
- setDevice(state,device)
- {
- state.device = device
- },
- downloadAPP(state, data) {
- // const param = {
- // android_link:'http://192.168.1.190:8068/api/version/download?id=89'
- // }
-
- var wgtUrl = 'http://192.168.1.50:8068/api/version/download?id='+ data.id;
- //判断安卓ios,判断是否第一次更包,整包更新还是资源包更新,只做ios?安卓也可以,统一一个方法,如果要做安卓,在这里做判断条件即可
- // var wgtUrl = 'http://godee.cdn.dtbctech.com/_UNI_6FC15A3.wgt'
- // if (state.device == 'ios') {
- // console.log('ios')
- // if (!wgtUrl) {
- // wgtUrl = data.ios_link;
- // }
- // }
- plus.nativeUI.showWaiting("下载更新文件...");
- state.downloadTask = uni.downloadFile({
- url: wgtUrl,
- success: (res) => {
- console.log('文件下载信息',res)
- if (res.statusCode === 200) {
- plus.nativeUI.showWaiting("安装更新文件...");
- plus.runtime.install(res.tempFilePath, {force:true}, function() {
- plus.nativeUI.closeWaiting();
- plus.nativeUI.alert("应用资源更新完成!", function() {
- plus.runtime.restart();
- });
- }, function(e) {
- plus.nativeUI.closeWaiting();
- plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
- if (e.code == 10) {
- alert('请清除临时目录');
- }
- });
-
- // commit('setDevice','android')
- // commit('installWgt', res.tempFilePath); //安装wgt包
- }
- },
- fail: (res) => {
- plus.nativeUI.alert("下载失败!");
- }
- });
- state.downloadTask.onProgressUpdate((res) => {
- if (state.progress.progress < res.progress) {
- state.progress = res
- }
- });
- },
- installWgt(state, path) {
- plus.nativeUI.showWaiting("安装更新文件...");
- plus.runtime.install(path, {force:true}, function() {
- plus.nativeUI.closeWaiting();
- plus.nativeUI.alert("应用资源更新完成!", function() {
- plus.runtime.restart();
- });
- }, function(e) {
- plus.nativeUI.closeWaiting();
- plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
- if (e.code == 10) {
- alert('请清除临时目录');
- }
- });
- },
- },
- actions: {
- setDevice({commit},device)
- {
- commit('setDevice',device)
- },
- updateApp({commit}, is_force) {
-
- // is_force在点击事件的时候传,0或1,分辨是否弹出提示信息
- // let the_date = new Date().toDateString();//利用时间戳更新缓存,看实际项目需要
- // console.log('时间',the_date)
- let device = uni.getSystemInfoSync().platform; //判断设备
- let type = 2;//资源包更新
- let json_path = '';
- if (device.toLowerCase() == 'android') {
- console.log('安卓?')
- json_path = 'android'
- } else {
- console.log('ios?')
- json_path = 'ios'
- // return;//如果不需要上ios
- }
- commit('setDevice',json_path)
- console.log('appid',plus.runtime.appid)
- console.log('version',plus.runtime.version)
- console.log('device',plus.device.imei)
-
- // state.device = json_path;
- plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
-
- uni.request({
- // url: 'http://139.159.237.94:8068/api/version/getnewversion', //检查更新的服务器地址
- url: 'http://192.168.1.50:8068/api/version/getnewversion', //检查更新的服务器地址
- data: {
- type:type,
- appid: plus.runtime.appid,
- //version_id: plus.runtime.version,
- //imei: plus.device.imei
- //这里只要是针对大后台,多个app管理配置传参
- },
- success: (result) => {
- console.log(widgetInfo)
- var data = result.data.update_list;
- console.log(data)
- // commit('downloadAPP', param)
- let cur_v = '1';
- let pass_v = '0';
- try {
- cur_v = parseInt(data.fileversionid.split('.').join('')); //转换为number类型进行比较,后台返回的最新版本号
- if (isNaN(cur_v)) {
- cur_v = 1;
- }
- pass_v = parseInt(widgetInfo.version.split('.').join('')); //app本地版本号
- if (isNaN(pass_v)) {
- pass_v = 0;
- }
- console.log(cur_v)
- console.log(pass_v)
- } catch (e) {}
- if (cur_v > pass_v && data.filename) {
- uni.showModal({
- title: '更新提示',
- content: data.msg || '检测到有数据包更新,是否进行更新?', //可后台配置更新文字返回显示:data.msg
- confirmText: '立即进行',
- success: function(res) {
- if (res.confirm) {
- commit('downloadAPP', data)
- } else if (res.cancel) {}
- }
- });
-
- } else {
- if (is_force) {
- uni.showToast({
- title: '当前已是最新版本',
- icon: 'none'
- })
- }
- }
- },
- fail(e) {
- console.error(e);
- if (e) {
- uni.showToast({
- title: '更新失败,请联系客服!',
- icon: 'none',
- })
- }
- }
- });
-
- });
- },
- }
- }
- export default update
|