1
0

user.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { login, logout, getInfo } from '@/api/login'
  2. import { getToken, setToken, removeToken } from '@/utils/auth'
  3. import { Notification } from 'element-ui'
  4. const user = {
  5. state: {
  6. token: getToken(),
  7. name: '',
  8. avatar: '',
  9. roles: [],
  10. permissions: [],
  11. dataPermissions: []
  12. },
  13. mutations: {
  14. SET_TOKEN: (state, token) => {
  15. state.token = token
  16. },
  17. SET_NAME: (state, name) => {
  18. state.name = name
  19. },
  20. SET_AVATAR: (state, avatar) => {
  21. state.avatar = avatar
  22. },
  23. SET_ROLES: (state, roles) => {
  24. state.roles = roles
  25. },
  26. SET_PERMISSIONS: (state, permissions) => {
  27. state.permissions = permissions
  28. },
  29. SET_DATA_PERMISSIONS: (state, dataPermissions) => {
  30. state.dataPermissions = dataPermissions
  31. }
  32. },
  33. actions: {
  34. // 登录
  35. Login({ commit }, userInfo) {
  36. const username = userInfo.username.trim()
  37. const password = userInfo.password
  38. const code = userInfo.code
  39. const uuid = userInfo.uuid
  40. return new Promise((resolve, reject) => {
  41. login(username, password, code, uuid)
  42. .then((res) => {
  43. if (res.code == 200) {
  44. setToken(res.token)
  45. commit('SET_TOKEN', res.token)
  46. resolve()
  47. } else {
  48. Notification({
  49. message: res.msg,
  50. type: 'error',
  51. duration: 2000,
  52. customClass: 'elemsg-notify'
  53. })
  54. }
  55. })
  56. .catch((error) => {
  57. reject(error)
  58. })
  59. })
  60. },
  61. // 获取用户信息
  62. GetInfo({ commit, state }) {
  63. return new Promise((resolve, reject) => {
  64. getInfo()
  65. .then((res) => {
  66. if (res.code == 200) {
  67. const user = res.user
  68. const avatar = user.avatar == '' ? require('@/assets/images/profile.jpg') : process.env.VUE_APP_BASE_API + user.avatar
  69. if (res.roles && res.roles.length > 0) {
  70. // 验证返回的roles是否是一个非空数组
  71. commit('SET_ROLES', res.roles)
  72. commit('SET_PERMISSIONS', res.permissions)
  73. } else {
  74. commit('SET_ROLES', ['ROLE_DEFAULT'])
  75. }
  76. let deptids = user.dataJurisdictionIds && user.dataJurisdictionIds.split(',')
  77. commit('SET_DATA_PERMISSIONS', deptids)
  78. commit('SET_NAME', user.userName)
  79. commit('SET_AVATAR', avatar)
  80. resolve(res)
  81. }
  82. })
  83. .catch((error) => {
  84. reject(error)
  85. })
  86. })
  87. },
  88. // 退出系统
  89. LogOut({ commit, state }) {
  90. return new Promise((resolve, reject) => {
  91. logout(state.token)
  92. .then(() => {
  93. commit('SET_TOKEN', '')
  94. commit('SET_ROLES', [])
  95. commit('SET_PERMISSIONS', [])
  96. removeToken()
  97. resolve()
  98. })
  99. .catch((error) => {
  100. reject(error)
  101. })
  102. })
  103. },
  104. // 前端 登出
  105. FedLogOut({ commit }) {
  106. return new Promise((resolve) => {
  107. commit('SET_TOKEN', '')
  108. removeToken()
  109. resolve()
  110. })
  111. }
  112. }
  113. }
  114. export default user