user.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. removeSsoAdminToken
  14. } from '@/utils/auth'
  15. const baseUrl = config.baseUrl
  16. const isLoginSso = config.VITE_SSO_LOGIN
  17. const user = {
  18. state: {
  19. token: getToken(),
  20. name: storage.get(constant.name),
  21. avatar: storage.get(constant.avatar),
  22. roles: storage.get(constant.roles),
  23. permissions: storage.get(constant.permissions),
  24. userinfo: storage.get(constant.userinfo),
  25. },
  26. mutations: {
  27. SET_TOKEN: (state, token) => {
  28. state.token = token
  29. },
  30. SET_NAME: (state, name) => {
  31. state.name = name
  32. storage.set(constant.name, name)
  33. },
  34. SET_AVATAR: (state, avatar) => {
  35. state.avatar = avatar
  36. storage.set(constant.avatar, avatar)
  37. },
  38. SET_ROLES: (state, roles) => {
  39. state.roles = roles
  40. storage.set(constant.roles, roles)
  41. },
  42. SET_PERMISSIONS: (state, permissions) => {
  43. state.permissions = permissions
  44. storage.set(constant.permissions, permissions)
  45. },
  46. SET_USERINFO: (state, userinfo) => {
  47. state.userinfo = userinfo
  48. storage.set(constant.userinfo, userinfo)
  49. },
  50. },
  51. actions: {
  52. // 登录
  53. Login({
  54. commit
  55. }, userInfo) {
  56. const username = userInfo.username.trim()
  57. const password = userInfo.password
  58. const code = userInfo.code
  59. const uuid = userInfo.uuid
  60. return new Promise((resolve, reject) => {
  61. login(username, password, code, uuid).then(res => {
  62. setToken(res.token)
  63. commit('SET_TOKEN', res.token)
  64. resolve()
  65. }).catch(error => {
  66. reject(error)
  67. })
  68. })
  69. },
  70. // 获取用户信息
  71. GetInfo({
  72. commit,
  73. state
  74. }) {
  75. return new Promise((resolve, reject) => {
  76. getInfo().then(res => {
  77. const user = res.user
  78. const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
  79. require("@/static/images/profile.jpg") : baseUrl + user.avatar
  80. const username = (user == null || user.userName == "" || user.userName ==
  81. null) ? "" : user.userName
  82. if (res.roles && res.roles.length > 0) {
  83. commit('SET_ROLES', res.roles)
  84. commit('SET_PERMISSIONS', res.permissions)
  85. } else {
  86. commit('SET_ROLES', ['ROLE_DEFAULT'])
  87. }
  88. commit('SET_NAME', username)
  89. commit('SET_AVATAR', avatar)
  90. commit('SET_USERINFO', user)
  91. resolve(res)
  92. }).catch(error => {
  93. reject(error)
  94. })
  95. })
  96. },
  97. // 退出系统
  98. LogOut({
  99. commit,
  100. state
  101. }) {
  102. return new Promise((resolve, reject) => {
  103. logout(state.token).then(() => {
  104. commit('SET_TOKEN', '')
  105. commit('SET_ROLES', [])
  106. commit('SET_PERMISSIONS', [])
  107. commit('SET_USERINFO', '')
  108. // if (isLoginSso) {
  109. uni.setStorageSync('ssoLogout', true)
  110. // }
  111. removeToken()
  112. removeSsoAdminToken()
  113. storage.clean()
  114. resolve()
  115. }).catch(error => {
  116. reject(error)
  117. })
  118. })
  119. }
  120. }
  121. }
  122. export default user