user.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. console.log('>>>>進來了', userInfo);
  42. const username = userInfo.username
  43. const password = userInfo.password
  44. const code = userInfo.code
  45. const uuid = userInfo.uuid
  46. return new Promise((resolve, reject) => {
  47. login(username, password, code, uuid).then((res: any) => {
  48. // console.log(login(),'接口调用了')
  49. setToken(res.token)
  50. commit('SET_TOKEN', res.token)
  51. resolve(res)
  52. }).catch(error => {
  53. reject(error)
  54. })
  55. })
  56. },
  57. // 获取用户信息
  58. GetInfo({ commit, state }) {
  59. return new Promise((resolve, reject) => {
  60. getInfo().then((res: any) => {
  61. const user = res.user
  62. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? "@/static/images/profile.jpg" : baseUrl + user.avatar
  63. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  64. if (res.roles && res.roles.length > 0) {
  65. commit('SET_ROLES', res.roles)
  66. commit('SET_PERMISSIONS', res.permissions)
  67. } else {
  68. commit('SET_ROLES', ['ROLE_DEFAULT'])
  69. }
  70. commit('SET_NAME', username)
  71. commit('SET_AVATAR', avatar)
  72. resolve(res)
  73. }).catch(error => {
  74. reject(error)
  75. })
  76. })
  77. },
  78. // 退出系统
  79. LogOut({ commit, state }) {
  80. return new Promise((resolve, reject) => {
  81. logout().then((res) => {
  82. commit('SET_TOKEN', '')
  83. commit('SET_ROLES', [])
  84. commit('SET_PERMISSIONS', [])
  85. removeToken()
  86. storage.clean()
  87. resolve(res)
  88. }).catch(error => {
  89. reject(error)
  90. })
  91. })
  92. }
  93. }
  94. }
  95. export default user