123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import { login, logout, getInfo, getMenu ,getImgCode } from '@/api/user'
- import { getToken, setToken, removeToken } from '@/utils/auth'
- import router, { resetRouter } from '@/router'
- const state = {
- token: getToken(),
- name: '',
- avatar: '',
- introduction: '',
- roles: []
- }
- const mutations = {
- SET_TOKEN: (state, token) => {
- state.token = token
- },
- SET_INTRODUCTION: (state, introduction) => {
- state.introduction = introduction
- },
- SET_NAME: (state, name) => {
- state.name = name
- },
- SET_AVATAR: (state, avatar) => {
- state.avatar = avatar
- },
- SET_ROLES: (state, roles) => {
- state.roles = roles
- },
- // 退出登录逻辑直接放在 mutations 中
- LOGOUT(state) {
- state.token = ''
- state.roles = []
- removeToken()
- resetRouter()
- }
- }
- const actions = {
- // 用户登录
- login({ commit }, userInfo) {
- const { username, password, type, captcha } = userInfo
- return new Promise((resolve, reject) => {
- login({ username: username.trim(), password: password, type: type, captcha: captcha.trim() }).then(response => {
- const { data } = response
- commit('SET_TOKEN', data.token)
- setToken(data.token,data.exp)
- resolve(response)
- }).catch(error => {
- reject(error)
- })
- })
- },
- // 获取用户身份信息
- getInfo({ commit, state }) {
- return new Promise((resolve, reject) => {
- getInfo(state.token).then(response => {
- const { data } = response
- // if (!data) {
- // reject('Verification failed, please Login again.')
- // }
- // 所有用户的身份都为admin
- const roles = ['admin']
- const { real_name, avatar, introduction } = data
- data.roles = roles
- // roles must be a non-empty array
- // 判断是否包含权限
- // if (!roles || roles.length <= 0) {
- // reject('getInfo: roles must be a non-null array!')
- // }
- commit('SET_ROLES', roles)
- commit('SET_NAME', real_name)
- commit('SET_AVATAR', avatar)
- commit('SET_INTRODUCTION', introduction)
- resolve(data)
- }).catch(error => {
- reject(error)
- })
- })
- },
- // 获取图形验证码
- getImgCode() {
- return new Promise((resolve, reject) => {
- getImgCode({}).then(response => {
- resolve(response)
- }).catch(error => {
- reject(error)
- })
- })
- },
- // 用户退出
- // logout({ commit, state, dispatch }) {
- // return new Promise((resolve, reject) => {
- // logout(state.token).then(() => {
- // commit('SET_TOKEN', '')
- // commit('SET_ROLES', [])
- // removeToken()
- // resetRouter()
- // // 重置已经访问过的视图
- // // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
- // dispatch('tagsView/delAllViews', null, { root: true })
- // resolve()
- // }).catch(error => {
- // reject(error)
- // })
- // })
- // },
- // 清理token
- resetToken({ commit }) {
- return new Promise(resolve => {
- commit('SET_TOKEN', '')
- commit('SET_ROLES', [])
- removeToken()
- resolve()
- })
- },
- // 用户切换时重置路由
- async changeRoles({ commit, dispatch }, role) {
- const token = role + '-token'
- commit('SET_TOKEN', token)
- setToken(token)
- const { roles } = await dispatch('getInfo')
- resetRouter()
- // generate accessible routes map based on roles
- const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
- // dynamically add accessible routes
- router.addRoutes(accessRoutes)
- // reset visited views and cached views
- dispatch('tagsView/delAllViews', null, { root: true })
- }
- }
- export default {
- namespaced: true,
- state,
- mutations,
- actions
- }
|