user.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @author https://vue-admin-beautiful.com (不想保留author可删除)
  3. * @description 登录、获取用户信息、退出登录、清除accessToken逻辑,不建议修改
  4. */
  5. import Vue from 'vue'
  6. import { getUserInfo, login, logout } from '@/api/user'
  7. import {
  8. getAccessToken,
  9. removeAccessToken,
  10. setAccessToken,
  11. } from '@/utils/accessToken'
  12. import { resetRouter } from '@/router'
  13. import { title, tokenName } from '@/config'
  14. const state = () => ({
  15. accessToken: getAccessToken(),
  16. username: '',
  17. avatar: '',
  18. permissions: [],
  19. })
  20. const getters = {
  21. accessToken: (state) => state.accessToken,
  22. username: (state) => state.username,
  23. avatar: (state) => state.avatar,
  24. permissions: (state) => state.permissions,
  25. }
  26. const mutations = {
  27. setAccessToken(state, accessToken) {
  28. state.accessToken = accessToken
  29. setAccessToken(accessToken)
  30. },
  31. setUsername(state, username) {
  32. state.username = username
  33. },
  34. setAvatar(state, avatar) {
  35. state.avatar = avatar
  36. },
  37. setPermissions(state, permissions) {
  38. state.permissions = permissions
  39. },
  40. }
  41. const actions = {
  42. setPermissions({ commit }, permissions) {
  43. commit('setPermissions', permissions)
  44. },
  45. async login({ commit }, userInfo) {
  46. const { data } = await login(userInfo)
  47. const accessToken = data[tokenName]
  48. if (accessToken) {
  49. commit('setAccessToken', accessToken)
  50. const hour = new Date().getHours()
  51. const thisTime =
  52. hour < 8
  53. ? '早上好'
  54. : hour <= 11
  55. ? '上午好'
  56. : hour <= 13
  57. ? '中午好'
  58. : hour < 18
  59. ? '下午好'
  60. : '晚上好'
  61. Vue.prototype.$baseNotify(`欢迎登录${title}`, `${thisTime}!`)
  62. } else {
  63. Vue.prototype.$baseMessage(
  64. `登录接口异常,未正确返回${tokenName}...`,
  65. 'error'
  66. )
  67. }
  68. },
  69. async getUserInfo({ commit, state }) {
  70. const { data } = await getUserInfo(state.accessToken)
  71. if (!data) {
  72. Vue.prototype.$baseMessage('验证失败,请重新登录...', 'error')
  73. return false
  74. }
  75. let { permissions, username, avatar } = data
  76. if (permissions && username && Array.isArray(permissions)) {
  77. commit('setPermissions', permissions)
  78. commit('setUsername', username)
  79. commit('setAvatar', avatar)
  80. return permissions
  81. } else {
  82. Vue.prototype.$baseMessage('用户信息接口异常', 'error')
  83. return false
  84. }
  85. },
  86. async logout({ dispatch }) {
  87. await logout(state.accessToken)
  88. await dispatch('resetAccessToken')
  89. await resetRouter()
  90. },
  91. resetAccessToken({ commit }) {
  92. commit('setPermissions', [])
  93. commit('setAccessToken', '')
  94. removeAccessToken()
  95. },
  96. }
  97. export default { state, getters, mutations, actions }