user.js 912 字节
Newer Older
芊里 已提交
1
// 上次启动时的用户信息
DCloud_JSON's avatar
DCloud_JSON 已提交
2
let userInfoHistory = uni.getStorageSync('userInfo') || {};
芊里 已提交
3 4

let state = {
DCloud_JSON's avatar
DCloud_JSON 已提交
5
		//是否已经登录
DCloud_JSON's avatar
DCloud_JSON 已提交
6 7 8
		hasLogin: Boolean(Object.keys(userInfoHistory).length),
		//用户信息
		info: userInfoHistory
芊里 已提交
9 10 11 12 13 14 15 16 17 18
	},
	getters = {
		info(state) {
			return state.info;
		},
		hasLogin(state){
			return state.hasLogin;
		}
	},
	mutations = {
DCloud_JSON's avatar
DCloud_JSON 已提交
19
		login(state, info) { //登录成功后的操作
DCloud_JSON's avatar
DCloud_JSON 已提交
20
			//原有的结合传来的参数
芊里 已提交
21 22
			let _info = state.info;
			state.info = Object.assign({}, _info, info);
DCloud_JSON's avatar
DCloud_JSON 已提交
23
			//设置为已经登录
芊里 已提交
24
			state.hasLogin = true;
DCloud_JSON's avatar
DCloud_JSON 已提交
25
			//存储最新的用户数据到本地持久化存储
26
			uni.setStorageSync('userInfo', state.info);
芊里 已提交
27 28
		},
		logout(state) {
29
			state.info = {};
芊里 已提交
30
			state.hasLogin = false;
31
			uni.setStorageSync('userInfo', {});
L
23  
linju 已提交
32
			uni.setStorageSync('uni_id_token', '');
芊里 已提交
33 34 35 36 37 38 39 40 41 42 43
		}
	},
	actions = {

	}
export default {
	state,
	getters,
	mutations,
	actions
}