user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import { login, logout, getInfo } from '@/api/login'
  5. import { getToken, setToken, removeToken } from '@/utils/auth'
  6. const baseUrl = config.baseUrl
  7. const user = {
  8. state: {
  9. token: getToken(),
  10. name: storage.get(constant.name),
  11. avatar: storage.get(constant.avatar),
  12. roles: storage.get(constant.roles),
  13. permissions: storage.get(constant.permissions),
  14. id:storage.get(constant.id),
  15. user:storage.get("vuex_user")
  16. },
  17. mutations: {
  18. SET_TOKEN: (state, token) => {
  19. state.token = token
  20. },
  21. SET_NAME: (state, name) => {
  22. state.name = name
  23. storage.set(constant.name, name)
  24. },
  25. SET_AVATAR: (state, avatar) => {
  26. state.avatar = avatar
  27. storage.set(constant.avatar, avatar)
  28. },
  29. SET_ROLES: (state, roles) => {
  30. state.roles = roles
  31. storage.set(constant.roles, roles)
  32. },
  33. SET_PERMISSIONS: (state, permissions) => {
  34. state.permissions = permissions
  35. storage.set(constant.permissions, permissions)
  36. },
  37. SET_ID: (state, id) => {
  38. state.id = id
  39. storage.set(constant.id, id)
  40. },
  41. SET_USER: (state, user) => {
  42. state.user = user
  43. storage.set("vuex_user", user)
  44. }
  45. },
  46. actions: {
  47. // 登录
  48. Login({ commit }, userInfo) {
  49. const username = userInfo.username.trim()
  50. const password = userInfo.password
  51. const code = userInfo.code
  52. const uuid = userInfo.uuid
  53. return new Promise((resolve, reject) => {
  54. login(username, password, code, uuid).then(res => {
  55. setToken(res.token)
  56. commit('SET_TOKEN', res.token)
  57. resolve()
  58. }).catch(error => {
  59. reject(error)
  60. })
  61. })
  62. },
  63. // 获取用户信息
  64. GetInfo({ commit, state }) {
  65. return new Promise((resolve, reject) => {
  66. getInfo().then(res => {
  67. const user = res.user
  68. const userId = user.userId
  69. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  70. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  71. if (res.roles && res.roles.length > 0) {
  72. commit('SET_ROLES', res.roles)
  73. commit('SET_PERMISSIONS', res.permissions)
  74. } else {
  75. commit('SET_ROLES', ['ROLE_DEFAULT'])
  76. }
  77. commit('SET_USER',user)
  78. commit('SET_ID', userId)
  79. commit('SET_NAME', username)
  80. commit('SET_AVATAR', avatar)
  81. resolve(res)
  82. }).catch(error => {
  83. reject(error)
  84. })
  85. })
  86. },
  87. // 退出系统
  88. LogOut({ commit, state }) {
  89. return new Promise((resolve, reject) => {
  90. logout(state.token).then(() => {
  91. commit('SET_TOKEN', '')
  92. commit('SET_ROLES', [])
  93. commit('SET_PERMISSIONS', [])
  94. removeToken()
  95. storage.clean()
  96. resolve()
  97. }).catch(error => {
  98. reject(error)
  99. })
  100. })
  101. }
  102. }
  103. }
  104. export default user