OfficeApp/store/modules/config.js
weipengfei 4f39a6ba29 更新
2023-08-31 10:45:23 +08:00

76 lines
1.9 KiB
JavaScript

import Cache from '@/utils/cache';
import { getConfig } from "@/api/config.js";
import Updater from '@/uni_modules/guyue-updater/index';
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 = {
eyeType: Cache.get('eyeType') || true, // 小眼睛
request: Cache.get('request') || true, // 网络请求
config: JSON.parse(Cache.get('config')||'{}')
};
const mutations = {
SET_EYE_TYPE(state){
state.eyeType=!state.eyeType;
Cache.set('eyeType', state.eyeType);
},
SET_REQUEST(state, data=true){
state.request = data;
Cache.set('request', state.request);
},
SET_CONFIG(state, data){
state.config = {...data};
Cache.set('config', JSON.stringify(state.config));
}
};
const actions = {
async initConfig({ state, commit }) {
let res = await getConfig();
commit('SET_CONFIG', res.data);
let os = uni.getSystemInfoSync();
// 版本更新
if(compareVersions(res.data.version, os.appVersion)==1){
try{
let info = res.data.versionInfo||{};
info = {
title: info.title||'发现新版本',
content: info.content||'修复了部分BUG',
versionName: info.versionName||'1.0.1',
downUrl: info.downUrl||'https://lihai001.oss-cn-chengdu.aliyuncs.com/__UNI__B5B1EDD__20230816174515.apk',
force: info.force||false, // 是否强制更新
quiet: info.quiet||false // 是否静默更新
}
Updater.update(info);
}catch(e){
console.log(e);
}
}
}
};
export default {
state,
mutations,
actions
};