user.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. },
  15. mutations: {
  16. SET_TOKEN: (state, token) => {
  17. state.token = token
  18. },
  19. SET_NAME: (state, name) => {
  20. state.name = name
  21. storage.set(constant.name, name)
  22. },
  23. SET_AVATAR: (state, avatar) => {
  24. state.avatar = avatar
  25. storage.set(constant.avatar, avatar)
  26. },
  27. SET_ROLES: (state, roles) => {
  28. state.roles = roles
  29. storage.set(constant.roles, roles)
  30. },
  31. SET_PERMISSIONS: (state, permissions) => {
  32. state.permissions = permissions
  33. storage.set(constant.permissions, permissions)
  34. }
  35. },
  36. actions: {
  37. // 登录
  38. Login({ commit }, userInfo) {
  39. const username = userInfo.username.trim()
  40. const password = userInfo.password
  41. const code = userInfo.code
  42. const uuid = userInfo.uuid
  43. const type = userInfo.type
  44. return new Promise((resolve, reject) => {
  45. login(username, password, code, uuid,type).then(res => {
  46. setToken(res.token)
  47. commit('SET_TOKEN', res.token)
  48. resolve()
  49. }).catch(error => {
  50. reject(error)
  51. })
  52. })
  53. },
  54. // 获取用户信息
  55. GetInfo({ commit, state }) {
  56. return new Promise((resolve, reject) => {
  57. getInfo().then(res => {
  58. const user = res.user
  59. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  60. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  61. if (res.roles && res.roles.length > 0) {
  62. commit('SET_ROLES', res.roles)
  63. commit('SET_PERMISSIONS', res.permissions)
  64. } else {
  65. commit('SET_ROLES', ['ROLE_DEFAULT'])
  66. }
  67. commit('SET_NAME', username)
  68. commit('SET_AVATAR', avatar)
  69. resolve(res)
  70. }).catch(error => {
  71. reject(error)
  72. })
  73. })
  74. },
  75. // 退出系统
  76. LogOut({ commit, state }) {
  77. return new Promise((resolve, reject) => {
  78. logout(state.token).then(() => {
  79. commit('SET_TOKEN', '')
  80. commit('SET_ROLES', [])
  81. commit('SET_PERMISSIONS', [])
  82. removeToken()
  83. storage.clean()
  84. resolve()
  85. }).catch(error => {
  86. reject(error)
  87. })
  88. })
  89. }
  90. }
  91. }
  92. export default user