user.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { login, logout, getInfo } from '@/api/login'
  2. import { getToken, setToken, removeToken } from '@/utils/auth'
  3. import Cookies from 'js-cookie'
  4. const user = {
  5. state: {
  6. token: getToken(),
  7. id: '',
  8. name: '',
  9. avatar: '',
  10. roles: [],
  11. permissions: [],
  12. nickName: '',
  13. deptId: '',
  14. deptName: '',
  15. orgId: '',
  16. orgName: ''
  17. },
  18. mutations: {
  19. SET_TOKEN: (state, token) => {
  20. state.token = token
  21. },
  22. SET_ID: (state, id) => {
  23. state.id = id
  24. },
  25. SET_NAME: (state, name) => {
  26. state.name = name
  27. },
  28. SET_AVATAR: (state, avatar) => {
  29. state.avatar = avatar
  30. },
  31. SET_ROLES: (state, roles) => {
  32. state.roles = roles
  33. },
  34. SET_PERMISSIONS: (state, permissions) => {
  35. state.permissions = permissions
  36. },
  37. NICKNAME: (state, nickName) => {
  38. state.nickName = nickName
  39. },
  40. DEPTID: (state, deptId) => {
  41. state.deptId = deptId
  42. },
  43. DEPTNAME: (state, deptName) => {
  44. state.deptName = deptName
  45. },
  46. ORGID: (state, orgId) => {
  47. state.orgId = orgId
  48. },
  49. ORGNAME: (state, orgName) => {
  50. state.orgName = orgName
  51. },
  52. },
  53. actions: {
  54. // 登录
  55. Login({ commit }, 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({ commit, state }) {
  72. return new Promise((resolve, reject) => {
  73. getInfo().then(res => {
  74. const user = res.user
  75. const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
  76. if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
  77. commit('SET_ROLES', res.roles)
  78. Cookies.set('roles',res.roles,{ expires: 30 });
  79. commit('SET_PERMISSIONS', res.permissions)
  80. } else {
  81. commit('SET_ROLES', ['ROLE_DEFAULT'])
  82. }
  83. commit('SET_NAME', user.userName)
  84. commit('SET_ID', user.userId)
  85. commit('SET_AVATAR', avatar)
  86. commit('NICKNAME', user.nickName)
  87. commit('DEPTID', user.deptId)
  88. commit('DEPTNAME', user.deptName)
  89. commit('ORGID', user.orgId)
  90. commit('ORGNAME', user.orgName)
  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. resolve()
  106. }).catch(error => {
  107. reject(error)
  108. })
  109. })
  110. },
  111. // 前端 登出
  112. FedLogOut({ commit }) {
  113. return new Promise(resolve => {
  114. commit('SET_TOKEN', '')
  115. removeToken()
  116. resolve()
  117. })
  118. }
  119. }
  120. }
  121. export default user