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

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