222 lines
5.5 KiB
JavaScript
222 lines
5.5 KiB
JavaScript
// +----------------------------------------------------------------------
|
||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||
// +----------------------------------------------------------------------
|
||
// | Author: CRMEB Team <admin@crmeb.com>
|
||
// +----------------------------------------------------------------------
|
||
import {
|
||
getUserInfo,
|
||
Appversion
|
||
} from "../../api/user.js";
|
||
import {
|
||
LOGIN_STATUS,
|
||
UID,
|
||
USER_INFO
|
||
} from '../../config/cache';
|
||
import Cache from '../../utils/cache';
|
||
// #ifdef APP-PLUS
|
||
import Updater from '@/uni_modules/guyue-updater/index';
|
||
import {
|
||
Toast
|
||
} from "../../libs/uniApi.js";
|
||
// #endif
|
||
|
||
|
||
const state = {
|
||
location: Cache.get('LOCATION_DATA', true) || {},
|
||
token: Cache.get(LOGIN_STATUS) || null,
|
||
uuid: uni.getStorageSync('uuid') || "",
|
||
backgroundColor: "#fff",
|
||
userInfo: (typeof Cache.get('USER_INFO') == 'string' ? JSON.parse(Cache.get('USER_INFO')) : Cache.get(
|
||
'USER_INFO')) || {},
|
||
uid: Cache.get(UID) || null,
|
||
globalData: uni.getStorageSync('GLOBAL_DATA') || {},
|
||
homeActive: false,
|
||
copyPwd: null,
|
||
pageFooter: uni.getStorageSync('pageFoot') || {},
|
||
keyColor: Cache.get('KEY_COLOR') || '_default',
|
||
viewColor: Cache.get('VIEW_COLOR') ||
|
||
'--view-theme: #E93323;--view-assist:#FF7612;--view-priceColor:#E93323;--view-bgColor:rgba(255, 118, 18,.1);--view-minorColor:rgba(233, 51, 35,.1);--view-bntColor11:#FDA923;--view-bntColor12:#FD6523;--view-bntColor21:#F11B09;--view-bntColor22:#F67A38;',
|
||
};
|
||
|
||
const mutations = {
|
||
setLocation(state, data) {
|
||
state.location = data
|
||
Cache.set('LOCATION_DATA', data);
|
||
},
|
||
LOGIN(state, opt) {
|
||
state.token = opt.token;
|
||
Cache.set(LOGIN_STATUS, opt.token, opt.time);
|
||
uni.removeStorageSync('auth_token');
|
||
},
|
||
SET_USERINFO(state, opt) {
|
||
state.userInfo = opt;
|
||
Cache.set(USER_INFO, opt);
|
||
},
|
||
SETUID(state, val) {
|
||
state.uid = val;
|
||
Cache.set(UID, val);
|
||
},
|
||
SETUUID(state, val) {
|
||
state.uuid = val;
|
||
uni.setStorageSync('uuid', val)
|
||
},
|
||
UPDATE_LOGIN(state, token) {
|
||
state.token = token;
|
||
},
|
||
|
||
LOGOUT(state) {
|
||
state.token = null;
|
||
state.uid = null
|
||
Cache.clear(LOGIN_STATUS);
|
||
Cache.clear(UID);
|
||
},
|
||
BACKGROUND_COLOR(state, color) {
|
||
state.color = color;
|
||
document.body.style.backgroundColor = color;
|
||
},
|
||
UPDATE_USERINFO(state, userInfo) {
|
||
userInfo.isNew && Cache.set('is_new_user', '1')
|
||
state.userInfo = userInfo;
|
||
},
|
||
OPEN_HOME(state) {
|
||
state.homeActive = true;
|
||
},
|
||
CLOSE_HOME(state) {
|
||
state.homeActive = false;
|
||
},
|
||
PARSE_PWD(state, pwd) {
|
||
state.copyPwd = pwd;
|
||
},
|
||
VIEW_COLOR(state, color) {
|
||
Cache.set('VIEW_COLOR', color)
|
||
state.viewColor = color;
|
||
},
|
||
KEY_COLOR(state, key) {
|
||
Cache.set('KEY_COLOR', key)
|
||
state.keyColor = key;
|
||
},
|
||
GLOBAL_DATA(state, key) {
|
||
uni.setStorageSync('GLOBAL_DATA', key);
|
||
state.globalData = key;
|
||
},
|
||
FOOT_UPLOAD(state, data) {
|
||
state.pageFooter = data
|
||
}
|
||
};
|
||
|
||
const actions = {
|
||
USERINFO({
|
||
state,
|
||
commit
|
||
}, force) {
|
||
if (state.userInfo !== null && !force)
|
||
return Promise.resolve(state.userInfo);
|
||
else
|
||
return new Promise(reslove => {
|
||
getUserInfo().then(res => {
|
||
commit("UPDATE_USERINFO", res.data);
|
||
Cache.set(USER_INFO, res.data);
|
||
reslove(res.data);
|
||
});
|
||
}).catch(() => {
|
||
|
||
});
|
||
},
|
||
async INIT_CONFIG({
|
||
state,
|
||
commit
|
||
}, data = false) {
|
||
if (data) {
|
||
uni.showLoading({
|
||
title: '加载中'
|
||
})
|
||
await uni.$u.sleep(500)
|
||
}
|
||
|
||
// #ifdef APP-PLUS
|
||
let os = uni.getSystemInfoSync();
|
||
let apptype;
|
||
if (os.osName == 'ios') {
|
||
apptype = 2
|
||
} else {
|
||
apptype = 1
|
||
}
|
||
// console.log({
|
||
// version: os.appWgtVersion,
|
||
// type: apptype,
|
||
// phone_brand:os.brand
|
||
// });
|
||
Appversion({
|
||
version: os.appWgtVersion,
|
||
type: apptype,
|
||
phone_brand: os.brand
|
||
}).then((res) => {
|
||
// console.log(res);
|
||
if (data) uni.hideLoading()
|
||
if (Object.keys(res.data.appInfo).length > 0) {
|
||
// if(res.data.appInfo.version) uni.showLoading({
|
||
// title: '检查更新中'
|
||
// })
|
||
// 版本更新
|
||
if (compareVersions(res.data.appInfo.version, os.appWgtVersion) == 1) {
|
||
try {
|
||
let info = res.data.appInfo || {};
|
||
let version = {
|
||
title: info.title || '发现新版本',
|
||
content: info.content || '修复了部分BUG',
|
||
versionName: info.version || '1.0.1',
|
||
brand: res.data.appInfo.phone_brand,
|
||
downUrl: info.dow_url || '',
|
||
force: info.force == 1 ? true : false, // 是否强制更新
|
||
quiet: info.quiet == 1 ? true : false // 是否静默更新
|
||
}
|
||
Updater.update(version, res.data.appInfo);
|
||
} catch (e) {
|
||
console.log(e);
|
||
Toast(e)
|
||
}
|
||
// uni.hideLoading();
|
||
} else {
|
||
if (data) Toast('已经是最新版本了')
|
||
}
|
||
|
||
} else {
|
||
if (data) Toast('已经是最新版本了')
|
||
}
|
||
}).catch((err) => {
|
||
console.log(err);
|
||
if (data) uni.hideLoading()
|
||
Toast(err.msg || err.message || err)
|
||
// console.log(err)
|
||
})
|
||
|
||
|
||
// #endif
|
||
}
|
||
};
|
||
|
||
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;
|
||
}
|
||
|
||
export default {
|
||
state,
|
||
mutations,
|
||
actions
|
||
}; |