You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import {
  5. login,
  6. logout,
  7. getInfo
  8. } from '@/api/login'
  9. import {
  10. getToken,
  11. setToken,
  12. removeToken
  13. } from '@/utils/auth'
  14. const baseUrl = config.baseUrl
  15. const user = {
  16. state: {
  17. token: getToken(),
  18. name: storage.get(constant.name),
  19. avatar: storage.get(constant.avatar),
  20. roles: storage.get(constant.roles),
  21. permissions: storage.get(constant.permissions)
  22. },
  23. mutations: {
  24. SET_TOKEN: (state, token) => {
  25. state.token = token
  26. },
  27. SET_NAME: (state, name) => {
  28. state.name = name
  29. storage.set(constant.name, name)
  30. },
  31. SET_AVATAR: (state, avatar) => {
  32. state.avatar = avatar
  33. storage.set(constant.avatar, avatar)
  34. },
  35. SET_ROLES: (state, roles) => {
  36. state.roles = roles
  37. storage.set(constant.roles, roles)
  38. },
  39. SET_PERMISSIONS: (state, permissions) => {
  40. state.permissions = permissions
  41. storage.set(constant.permissions, permissions)
  42. }
  43. },
  44. actions: {
  45. // 登录
  46. Login({
  47. commit
  48. }, 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({
  65. commit,
  66. state
  67. }) {
  68. return new Promise((resolve, reject) => {
  69. getInfo().then(res => {
  70. const user = res.user
  71. const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
  72. 'https://hly-oss-master.oss-cn-guangzhou.aliyuncs.com/pet/profile.jpg' : baseUrl + user.avatar
  73. // require("@/static/images/profile.jpg") : baseUrl + user.avatar
  74. const username = (user == null || user.userName == "" || user.userName ==
  75. null) ? "" : user.userName
  76. if (res.roles && res.roles.length > 0) {
  77. commit('SET_ROLES', res.roles)
  78. commit('SET_PERMISSIONS', res.permissions)
  79. } else {
  80. commit('SET_ROLES', ['ROLE_DEFAULT'])
  81. }
  82. commit('SET_NAME', username)
  83. commit('SET_AVATAR', avatar)
  84. resolve(res)
  85. }).catch(error => {
  86. reject(error)
  87. })
  88. })
  89. },
  90. // 退出系统
  91. LogOut({
  92. commit,
  93. state
  94. }) {
  95. return new Promise((resolve, reject) => {
  96. logout(state.token).then(() => {
  97. commit('SET_TOKEN', '')
  98. commit('SET_ROLES', [])
  99. commit('SET_PERMISSIONS', [])
  100. removeToken()
  101. storage.clean()
  102. resolve()
  103. }).catch(error => {
  104. reject(error)
  105. })
  106. })
  107. }
  108. }
  109. }
  110. export default user