import Vue from 'vue' import { login, getLoginUser, logout } from '@/api/loginManage' import { ACCESS_TOKEN, ALL_APPS_MENU, DICT_TYPE_TREE_DATA, NOTICE_RECEIVED, DEFAULT_ISLOGIN, MODULE_CODE, REAL_NAME, } from '@/store/mutation-types' import store from '../index' import router from '../../router' const user = { state: { token: '', name: '', welcome: '', avatar: '', buttons: [], // 按钮权限 admintype: '', // 是否是超管 roles: [], info: {}, notices: [], //接收的通知公告 moduleCode:'',//模块code realname:'',//真是名称 }, mutations: { SET_TOKEN: (state, token) => { state.token = token }, SET_CODE: (state, code) => { Vue.ls.set(MODULE_CODE,code) state.moduleCode = code }, SET_REALNAME: (state, realname) => { Vue.ls.set(REAL_NAME,realname) state.realname = realname }, SET_NAME: (state, { name, welcome }) => { state.name = name state.welcome = welcome }, SET_AVATAR: (state, avatar) => { state.avatar = avatar }, SET_ROLES: (state, roles) => { state.roles = roles }, SET_INFO: (state, info) => { state.info = info }, SET_BUTTONS: (state, buttons) => { state.buttons = buttons }, SET_ADMINTYPE: (state, admintype) => { state.admintype = admintype }, SET_NOTICES: (state, notices) => { state.notices = notices }, }, actions: { // 登录 Login({ commit }, userInfo) { return new Promise((resolve, reject) => { login(userInfo).then(response => { console.log(response, 101) if (response.Code != 0) { reject(response.Message) return } const Data = response.Data if (Data.Status != 0) { reject('用户名或密码错误') return } const token = response.Data.Token const corpid = response.Data.User.CorpID const RelatedID = response.Data.User.RelatedID const RealName = response.Data.User.RealName Vue.ls.set(ACCESS_TOKEN, token, 1000*60*60*23.5) Vue.ls.set(DEFAULT_ISLOGIN, true,1000*60*60*23.5) commit('SET_TOKEN', token) commit('SET_REALNAME', RealName) // commit('SET_CORPID', corpid) resolve(Data) // eslint-disable-next-line handle-callback-err }).catch(error => { // eslint-disable-next-line prefer-promise-reject-errors reject('后端未启动或代理错误') }) }) }, // 获取用户信息 GetInfo({ commit }) { return new Promise((resolve, reject) => { getLoginUser().then(response => { if (response.success) { const data = response.data // console.log(data, 140) commit('SET_ADMINTYPE', data.adminType) commit('SET_ROLES', 1) commit('SET_BUTTONS', data.permissions) commit('SET_INFO', data) let modulecode = Vue.ls.get(MODULE_CODE) if(modulecode){ commit('SET_CODE', modulecode) }else{ let activeModule = data.apps.filter(x=>{ return x.active===true }) if(activeModule.length>0){ let code = activeModule[0].code commit('SET_CODE', code) } } resolve(data) } else { // eslint-disable-next-line no-undef reject(new Error(data.message)) } }).catch(error => { reject(error) }) }) }, // 登出 Logout({ commit, state }) { return new Promise((resolve) => { logout(state.token).then((res) => { resolve() }).catch(() => { resolve() }).finally(() => { commit('SET_TOKEN', '') commit('SET_ROLES', []) commit('SET_BUTTONS', []) commit('SET_ADMINTYPE', '') commit('SET_REALNAME', '') Vue.ls.remove(ACCESS_TOKEN) Vue.ls.remove(REAL_NAME) commit('SET_INFO', null) Vue.ls.set(DEFAULT_ISLOGIN, false) Vue.ls.remove(ALL_APPS_MENU) Vue.ls.remove(DICT_TYPE_TREE_DATA) }) }) }, // 切换应用菜单 MenuChange({ commit }, application) { return new Promise((resolve) => { commit('SET_CODE', application.code) resolve() }) }, } } export default user