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.

115 lines
2.5 KiB

1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week 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. require("@/static/images/profile.jpg") : baseUrl + user.avatar
  73. const username = (user == null || user.userName == "" || user.userName ==
  74. null) ? "" : user.userName
  75. if (res.roles && res.roles.length > 0) {
  76. commit('SET_ROLES', res.roles)
  77. commit('SET_PERMISSIONS', res.permissions)
  78. } else {
  79. commit('SET_ROLES', ['ROLE_DEFAULT'])
  80. }
  81. commit('SET_NAME', username)
  82. commit('SET_AVATAR', avatar)
  83. resolve(res)
  84. }).catch(error => {
  85. reject(error)
  86. })
  87. })
  88. },
  89. // 退出系统
  90. LogOut({
  91. commit,
  92. state
  93. }) {
  94. return new Promise((resolve, reject) => {
  95. logout(state.token).then(() => {
  96. commit('SET_TOKEN', '')
  97. commit('SET_ROLES', [])
  98. commit('SET_PERMISSIONS', [])
  99. removeToken()
  100. storage.clean()
  101. resolve()
  102. }).catch(error => {
  103. reject(error)
  104. })
  105. })
  106. }
  107. }
  108. }
  109. export default user