user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { login, logout, getInfo, getMenu ,getImgCode } 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 { real_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', real_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. getImgCode() {
  79. return new Promise((resolve, reject) => {
  80. getImgCode({}).then(response => {
  81. resolve(response)
  82. }).catch(error => {
  83. reject(error)
  84. })
  85. })
  86. },
  87. // 用户退出
  88. // logout({ commit, state, dispatch }) {
  89. // return new Promise((resolve, reject) => {
  90. // logout(state.token).then(() => {
  91. // commit('SET_TOKEN', '')
  92. // commit('SET_ROLES', [])
  93. // removeToken()
  94. // resetRouter()
  95. // // 重置已经访问过的视图
  96. // // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
  97. // dispatch('tagsView/delAllViews', null, { root: true })
  98. // resolve()
  99. // }).catch(error => {
  100. // reject(error)
  101. // })
  102. // })
  103. // },
  104. // 清理token
  105. resetToken({ commit }) {
  106. return new Promise(resolve => {
  107. commit('SET_TOKEN', '')
  108. commit('SET_ROLES', [])
  109. removeToken()
  110. resolve()
  111. })
  112. },
  113. // 用户切换时重置路由
  114. async changeRoles({ commit, dispatch }, role) {
  115. const token = role + '-token'
  116. commit('SET_TOKEN', token)
  117. setToken(token)
  118. const { roles } = await dispatch('getInfo')
  119. resetRouter()
  120. // generate accessible routes map based on roles
  121. const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
  122. // dynamically add accessible routes
  123. router.addRoutes(accessRoutes)
  124. // reset visited views and cached views
  125. dispatch('tagsView/delAllViews', null, { root: true })
  126. }
  127. }
  128. export default {
  129. namespaced: true,
  130. state,
  131. mutations,
  132. actions
  133. }