import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		// 判断登录状态
		hasLogin: !!uni.getStorageSync('token'),
		// 设置登录token
		token: uni.getStorageSync('token') || '',
		// 存储用户信息
		userInfo: uni.getStorageSync('userInfo') || {},
		// 企业token
		enToken: uni.getStorageSync('enToken') || '',
		//企业信息
		enterprise: uni.getStorageSync('enterprise') || '',
		/*
		 * 登录者权限
		 * isAdministrator是否为超级管理员 true是，false不是
		 * dataField 数据域权限
		 * custom 节点权限
		 */
		access: uni.getStorageSync('access') || '',
		// 企业基本设置
		basicSet: uni.getStorageSync('basicSet') || '',
	},
	mutations: {
		// 企业基本设置
		commit_basicSet(state, basicSet) {
			state.basicSet = basicSet
			uni.setStorageSync('basicSet', basicSet);
		},
		// 登录者权限
		commit_access(state, access) {
			state.access = access
			uni.setStorageSync('access', access);
		},
		// 企业信息
		commit_enterprise(state, enterprise) {
			console.log(enterprise,'enterprise');
			state.enterprise = enterprise == '军海蓝'?'君子堂':enterprise
			uni.setStorageSync('enterprise', enterprise);
		},
		// 企业token
		commit_enToken(state, enToken) {
			state.enToken = enToken
			uni.setStorageSync('enToken', enToken);
		},
		// 存储用户信息
		commit_userInfo(state, userInfo) {
			state.userInfo = userInfo
			uni.setStorageSync('userInfo', userInfo);
		},
		// 设置登录token
		commit_token(state, token) {
			state.token = token
			uni.setStorageSync('token', token);
		},
		// 判断登录状态
		commit_hasLogin(state, hasLogin) {
			state.hasLogin = hasLogin
		},
	},
	actions: {
		logout({
			commit
		}) {
			uni.clearStorageSync();
			commit("commit_hasLogin", false);
			commit("commit_token", '');
			commit("commit_userInfo", '');
			commit("commit_enToken", '');
			commit("commit_enterprise", '');
			commit("commit_access", '');
			commit("commit_basicSet", '');
			uni.reLaunch({
				url: '/pages/login/index'
			})
		}
	}
})
export default store
