增加个人信息的加载

This commit is contained in:
YunaiV
2021-11-26 09:47:23 +08:00
parent 86ef156de4
commit 2da6a746e4
13 changed files with 295 additions and 99 deletions

View File

@ -1,6 +1,7 @@
import Vue from 'vue'
import Vuex from 'vuex'
// import {request} from '@/common/js/request'
import { getUserInfo } from '@/api/member/userProfile.js'
Vue.use(Vuex)
@ -10,7 +11,6 @@ const store = new Vuex.Store({
token: '', // 用户身份 Token
userInfo: {}, // 用户基本信息
timerIdent: false, // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看
orderCount: {}, // 订单数量
},
getters: {
hasLogin(state){
@ -18,73 +18,45 @@ const store = new Vuex.Store({
}
},
mutations: {
//更新state数据
setStateAttr(state, param){
if(param instanceof Array){
// 更新 state 的通用方法
setStateAttr(state, param) {
if (param instanceof Array) {
for(let item of param){
state[item.key] = item.val;
}
}else{
} else {
state[param.key] = param.val;
}
},
//更新token
setToken(state, data){
const {token, tokenExpired} = data;
// 更新token
setToken(state, data) {
// 设置 Token
const { token } = data;
state.token = token;
uni.setStorageSync('uniIdToken', token);
uni.setStorageSync('tokenExpired', tokenExpired);
this.dispatch('getUserInfo'); //更新用户信息
this.dispatch('getCartCount');//更新购物车数量
uni.$emit('refreshCart');//刷新购物车
this.dispatch('getOrderCount'); //更新订单数量
uni.setStorageSync('token', token);
// 加载用户信息
this.dispatch('obtainUserInfo');
},
// 退出登录
logout(state) {
// 清空 Token
state.token = '';
uni.removeStorageSync('uniIdToken');
this.dispatch('getCartCount');//更新购物车数量
uni.$emit('refreshCart');//刷新购物车
this.dispatch('getOrderCount'); //更新订单数量
uni.removeStorageSync('token');
// 清空用户信息 TODO 芋艿:这里 setTimeout 的原因是,上面可能还有一些 request 请求。后续得优化下
setTimeout(()=>{
state.userInfo = {};
}, 1100)
},
},
actions: {
//更新用户信息
async getUserInfo({state, commit}){
const res = await request('user', 'get', {}, {
checkAuthInvalid: false
});
if(res.status === 1){
const userInfo = res.data;
commit('setStateAttr', {
key: 'userInfo',
val: userInfo
})
}
},
//更新用户订单数量
async getOrderCount({state, commit}){
let data = {
c0: 0,
c1: 0,
c2: 0,
c3: 0
}
if(state.token){
try {
const res = await request('order', 'getOrderCount');
data = res;
}catch (err){
console.error('更新用户订单数量 => ', err);
}
}
// 获得用户基本信息
async obtainUserInfo({state, commit}) {
const data = await getUserInfo();
commit('setStateAttr', {
key: 'orderCount',
key: 'userInfo',
val: data
})
});
}
}
})