OfficeApp/store/modules/config.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

import Cache from '@/utils/cache';
2023-08-26 18:41:06 +08:00
import { getConfig } from "@/api/config.js";
2023-08-30 18:09:49 +08:00
import Updater from '@/uni_modules/guyue-updater/index';
2023-08-26 18:41:06 +08:00
2023-08-31 10:45:23 +08:00
function compareVersions(version1, version2) {
const arr1 = version1.split('.').map(Number);
const arr2 = version2.split('.').map(Number);
for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) {
const num1 = i < arr1.length ? arr1[i] : 0;
const num2 = i < arr2.length ? arr2[i] : 0;
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
}
const state = {
2023-08-11 13:51:58 +08:00
eyeType: Cache.get('eyeType') || true, // 小眼睛
request: Cache.get('request') || true, // 网络请求
2023-08-30 11:19:26 +08:00
config: JSON.parse(Cache.get('config')||'{}')
};
const mutations = {
SET_EYE_TYPE(state){
state.eyeType=!state.eyeType;
Cache.set('eyeType', state.eyeType);
2023-08-05 15:16:05 +08:00
},
SET_REQUEST(state, data=true){
state.request = data;
Cache.set('request', state.request);
2023-08-11 13:51:58 +08:00
},
2023-08-26 18:41:06 +08:00
SET_CONFIG(state, data){
2023-08-29 14:18:28 +08:00
state.config = {...data};
2023-08-30 11:19:26 +08:00
Cache.set('config', JSON.stringify(state.config));
}
};
const actions = {
2023-08-31 15:49:26 +08:00
async initConfig({ state, commit }, data = false) {
2023-08-26 18:41:06 +08:00
let res = await getConfig();
commit('SET_CONFIG', res.data);
2023-08-31 10:45:23 +08:00
let os = uni.getSystemInfoSync();
2023-08-31 15:49:26 +08:00
// #ifdef APP-PLUS
if(data) uni.showLoading({
title: '检查更新中'
})
2023-08-30 18:09:49 +08:00
// 版本更新
2023-08-31 10:45:23 +08:00
if(compareVersions(res.data.version, os.appVersion)==1){
2023-08-30 18:09:49 +08:00
try{
2023-08-31 15:49:26 +08:00
let info = res.data.version_info||{};
2023-08-30 18:09:49 +08:00
info = {
title: info.title||'发现新版本',
content: info.content||'修复了部分BUG',
2023-08-31 15:49:26 +08:00
versionName: info.version||'1.0.1',
downUrl: info.dow_url||'',
force: info.force==1?true:false, // 是否强制更新
quiet: info.quiet==1?true:false // 是否静默更新
2023-08-30 18:09:49 +08:00
}
2023-08-31 10:45:23 +08:00
Updater.update(info);
2023-08-30 18:09:49 +08:00
}catch(e){
console.log(e);
}
2023-08-31 15:49:26 +08:00
if(data) uni.hideLoading();
}else if(data){
uni.hideLoading();
uni.showToast({
title: '已经是最新版本了',
icon: 'none'
})
2023-08-30 18:09:49 +08:00
}
2023-08-31 15:49:26 +08:00
// #endif
2023-08-11 13:51:58 +08:00
}
};
2023-08-31 10:45:23 +08:00
export default {
state,
mutations,
actions
};