综合办公系统
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * @Author: ysh
  3. * @Date: 2025-01-16 11:17:09
  4. * @LastEditors: Please set LastEditors
  5. * @LastEditTime: 2025-03-03 14:31:51
  6. */
  7. import config from '@/config'
  8. import storage from '@/utils/storage'
  9. import constant from '@/utils/constant'
  10. import { login, logout, getInfo } from '@/api/login'
  11. import { getToken, setToken, removeToken } from '@/utils/auth'
  12. const baseUrl = config.baseUrl
  13. const user = {
  14. state: {
  15. token: getToken(),
  16. name: storage.get(constant.name),
  17. avatar: storage.get(constant.avatar),
  18. roles: storage.get(constant.roles),
  19. permissions: storage.get(constant.permissions),
  20. userId: storage.get(constant.userId),
  21. deptId: storage.get(constant.deptId),
  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. SET_USERID: (state, userId) => {
  44. state.userId = userId
  45. storage.set(constant.userId, userId)
  46. },
  47. SET_DEPTID: (state, deptId) => {
  48. state.deptId = deptId
  49. storage.set(constant.deptId, deptId)
  50. }
  51. },
  52. actions: {
  53. // 登录
  54. Login({ commit }, userInfo) {
  55. const username = userInfo.username.trim()
  56. const password = userInfo.password
  57. const code = userInfo.code
  58. const uuid = userInfo.uuid
  59. return new Promise((resolve, reject) => {
  60. login(username, password, code, uuid).then(res => {
  61. setToken(res.token)
  62. commit('SET_TOKEN', res.token)
  63. resolve()
  64. }).catch(error => {
  65. reject(error)
  66. })
  67. })
  68. },
  69. // 获取用户信息
  70. GetInfo({ commit, state }) {
  71. return new Promise((resolve, reject) => {
  72. getInfo().then(res => {
  73. const user = res.user
  74. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/user.png") : baseUrl + user.avatar
  75. const username = (user == null || user.nickName == "" || user.nickName == null) ? "" : user.nickName
  76. const userId = (user == null || user.userId == "" || user.userId == null) ? "" : user.userId
  77. if (res.roles && res.roles.length > 0) {
  78. commit('SET_ROLES', res.roles)
  79. commit('SET_PERMISSIONS', res.permissions)
  80. } else {
  81. commit('SET_ROLES', ['ROLE_DEFAULT'])
  82. }
  83. commit('SET_USERID', userId)
  84. commit('SET_DEPTID', user.deptId)
  85. commit('SET_NAME', username)
  86. commit('SET_AVATAR', avatar)
  87. resolve(res)
  88. }).catch(error => {
  89. reject(error)
  90. })
  91. })
  92. },
  93. // 退出系统
  94. LogOut({ commit, state }) {
  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