const state = () => { return { access_token: '', refresh_token: '', userInfo: null, } } // getters const getters = { nickName(state) { return state.userInfo ? state.userInfo.nickName : '' }, tel(state) { return state.userInfo ? state.userInfo.tel : '' }, avatar(state) { return state.userInfo ? state.userInfo.image : '' }, loginName(state) { return state.userInfo ? state.userInfo.loginName : '' }, } // actions const actions = { clearLoginInfo({ commit }) { this.$elBusCookie.removeToken() commit('SET_USERINFO', null) }, async getUserInfo({ commit }) { const { code, data } = await this.$services.auth.getUserInfo() if (code === 0) { const routes = data.menus || [] commit('permission/SET_ROUTES', routes, { root: true }) commit('SET_USERINFO', { ...data }) } return { code, data } }, async login({ commit, dispatch }, payload) { const { code, data } = await this.$services.auth.login(payload) if (code === 0) { this.$elBusCookie.setToken(data.access_token, data.refresh_token) return await dispatch('getUserInfo') } return { code, data, } }, async smsLogin({ commit, dispatch }, payload) { const { code, data } = await this.$services.auth.smsLogin(payload) if (code === 0) { this.$elBusCookie.setToken(data.access_token, data.refresh_token) return await dispatch('getUserInfo') } return { code, data, } }, } // mutations const mutations = { SET_USERINFO(state, userInfo) { state.userInfo = userInfo }, } export default { namespaced: true, state, getters, actions, mutations, }