user.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { login, logout, getInfo, getMenu } from '@/api/user'
  2. import { getToken, setToken, removeToken } from '@/utils/auth'
  3. import router, { resetRouter } from '@/router'
  4. const state = {
  5. token: getToken(),
  6. name: '',
  7. avatar: '',
  8. introduction: '',
  9. roles: []
  10. }
  11. const mutations = {
  12. SET_TOKEN: (state, token) => {
  13. state.token = token
  14. },
  15. SET_INTRODUCTION: (state, introduction) => {
  16. state.introduction = introduction
  17. },
  18. SET_NAME: (state, name) => {
  19. state.name = name
  20. },
  21. SET_AVATAR: (state, avatar) => {
  22. state.avatar = avatar
  23. },
  24. SET_ROLES: (state, roles) => {
  25. state.roles = roles
  26. },
  27. // 退出登录逻辑直接放在 mutations 中
  28. LOGOUT(state) {
  29. state.token = ''
  30. state.roles = []
  31. removeToken()
  32. resetRouter()
  33. }
  34. }
  35. const actions = {
  36. // 用户登录
  37. login({ commit }, userInfo) {
  38. const { username, password, type, captcha } = userInfo
  39. return new Promise((resolve, reject) => {
  40. login({ username: username.trim(), password: password, type: type, captcha: captcha.trim() }).then(response => {
  41. const { data } = response
  42. commit('SET_TOKEN', data.token)
  43. setToken(data.token,data.exp)
  44. resolve(response)
  45. }).catch(error => {
  46. reject(error)
  47. })
  48. })
  49. },
  50. // 获取用户身份信息
  51. getInfo({ commit, state }) {
  52. return new Promise((resolve, reject) => {
  53. getInfo(state.token).then(response => {
  54. const { data } = response
  55. // if (!data) {
  56. // reject('Verification failed, please Login again.')
  57. // }
  58. // 所有用户的身份都为admin
  59. const roles = ['admin']
  60. const { name, avatar, introduction } = data
  61. data.roles = roles
  62. // roles must be a non-empty array
  63. // 判断是否包含权限
  64. // if (!roles || roles.length <= 0) {
  65. // reject('getInfo: roles must be a non-null array!')
  66. // }
  67. commit('SET_ROLES', roles)
  68. commit('SET_NAME', name)
  69. commit('SET_AVATAR', avatar)
  70. commit('SET_INTRODUCTION', introduction)
  71. resolve(data)
  72. }).catch(error => {
  73. reject(error)
  74. })
  75. })
  76. },
  77. // 用户退出
  78. // logout({ commit, state, dispatch }) {
  79. // return new Promise((resolve, reject) => {
  80. // logout(state.token).then(() => {
  81. // commit('SET_TOKEN', '')
  82. // commit('SET_ROLES', [])
  83. // removeToken()
  84. // resetRouter()
  85. // // 重置已经访问过的视图
  86. // // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
  87. // dispatch('tagsView/delAllViews', null, { root: true })
  88. // resolve()
  89. // }).catch(error => {
  90. // reject(error)
  91. // })
  92. // })
  93. // },
  94. // 清理token
  95. resetToken({ commit }) {
  96. return new Promise(resolve => {
  97. commit('SET_TOKEN', '')
  98. commit('SET_ROLES', [])
  99. removeToken()
  100. resolve()
  101. })
  102. },
  103. // 用户切换时重置路由
  104. async changeRoles({ commit, dispatch }, role) {
  105. const token = role + '-token'
  106. commit('SET_TOKEN', token)
  107. setToken(token)
  108. const { roles } = await dispatch('getInfo')
  109. resetRouter()
  110. // generate accessible routes map based on roles
  111. const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
  112. // dynamically add accessible routes
  113. router.addRoutes(accessRoutes)
  114. // reset visited views and cached views
  115. dispatch('tagsView/delAllViews', null, { root: true })
  116. }
  117. }
  118. export default {
  119. namespaced: true,
  120. state,
  121. mutations,
  122. actions
  123. }