user.ts 2.9 KB

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