user.js 3.1 KB

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