/**
 * 登录状态管理
 * 个人信息数据持久化
 */
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
import md5 from 'md5';
import {
	localStorage,
	sessionStorage,
	cookieStorage
} from 'js-storage';
const userKey = md5("userInfo"),
	sysTokenKey = md5('accessToken'),
	userTreeKey = md5('userTree');
export default new Vuex.Store({
	state: {
		hasLogin: false,
		userInfo: {},
		userTree:[],
		accessToken:''
	},
	mutations: {
		login(state) {
			state.accessToken = sessionStorage.get(sysTokenKey) || '';
			state.userInfo = sessionStorage.get(userKey) || {};
			state.hasLogin = state.accessToken || false;
		},
		saveToken(state, accessToken = '') {
			state.accessToken = accessToken;
			state.hasLogin = Boolean(state.accessToken);
			//保存accessToken
			sessionStorage.set(sysTokenKey, state.accessToken);
		},
		saveUser(state, info = {}) {
			state.userInfo = info;
			//保存userInfo
			sessionStorage.set(userKey, state.userInfo);
		},
		//new add
		saveUserTree(state, info = []) {
			state.userTree = info;
			//保存user tree
			sessionStorage.set(userTreeKey, state.userTree);
		},
		//退出登录
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			state.accessToken = '';
			sessionStorage.remove([userKey, sysTokenKey,userTreeKey]);
		}
	}
});
