user.js 3.0 KB

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