2021-11-25 07:57:23 +08:00
|
|
|
|
import Vue from 'vue'
|
|
|
|
|
import Vuex from 'vuex'
|
|
|
|
|
// import {request} from '@/common/js/request'
|
2021-11-26 09:47:23 +08:00
|
|
|
|
import { getUserInfo } from '@/api/member/userProfile.js'
|
2021-11-25 07:57:23 +08:00
|
|
|
|
|
|
|
|
|
Vue.use(Vuex)
|
|
|
|
|
|
|
|
|
|
const store = new Vuex.Store({
|
|
|
|
|
state: {
|
|
|
|
|
openExamine: false, // 是否开启审核状态。用于小程序、App 等审核时,关闭部分功能。TODO 芋艿:暂时没找到刷新的地方
|
|
|
|
|
token: '', // 用户身份 Token
|
|
|
|
|
userInfo: {}, // 用户基本信息
|
|
|
|
|
timerIdent: false, // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看
|
|
|
|
|
},
|
|
|
|
|
getters: {
|
|
|
|
|
hasLogin(state){
|
|
|
|
|
return !!state.token;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mutations: {
|
2021-11-26 09:47:23 +08:00
|
|
|
|
// 更新 state 的通用方法
|
|
|
|
|
setStateAttr(state, param) {
|
|
|
|
|
if (param instanceof Array) {
|
2021-11-25 07:57:23 +08:00
|
|
|
|
for(let item of param){
|
|
|
|
|
state[item.key] = item.val;
|
|
|
|
|
}
|
2021-11-26 09:47:23 +08:00
|
|
|
|
} else {
|
2021-11-25 07:57:23 +08:00
|
|
|
|
state[param.key] = param.val;
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-11-26 09:47:23 +08:00
|
|
|
|
// 更新token
|
|
|
|
|
setToken(state, data) {
|
|
|
|
|
// 设置 Token
|
|
|
|
|
const { token } = data;
|
2021-11-25 07:57:23 +08:00
|
|
|
|
state.token = token;
|
2021-11-26 09:47:23 +08:00
|
|
|
|
uni.setStorageSync('token', token);
|
|
|
|
|
|
|
|
|
|
// 加载用户信息
|
|
|
|
|
this.dispatch('obtainUserInfo');
|
2021-11-25 07:57:23 +08:00
|
|
|
|
},
|
|
|
|
|
// 退出登录
|
|
|
|
|
logout(state) {
|
2021-11-26 09:47:23 +08:00
|
|
|
|
// 清空 Token
|
2021-11-25 07:57:23 +08:00
|
|
|
|
state.token = '';
|
2021-11-26 09:47:23 +08:00
|
|
|
|
uni.removeStorageSync('token');
|
|
|
|
|
// 清空用户信息 TODO 芋艿:这里 setTimeout 的原因是,上面可能还有一些 request 请求。后续得优化下
|
2021-11-25 07:57:23 +08:00
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
state.userInfo = {};
|
|
|
|
|
}, 1100)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
actions: {
|
2021-11-26 09:47:23 +08:00
|
|
|
|
// 获得用户基本信息
|
|
|
|
|
async obtainUserInfo({state, commit}) {
|
|
|
|
|
const data = await getUserInfo();
|
2021-11-25 07:57:23 +08:00
|
|
|
|
commit('setStateAttr', {
|
2021-11-26 09:47:23 +08:00
|
|
|
|
key: 'userInfo',
|
2021-11-25 07:57:23 +08:00
|
|
|
|
val: data
|
2021-11-26 09:47:23 +08:00
|
|
|
|
});
|
2021-11-25 07:57:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default store
|