user.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. nickName:'',
  9. deptId:'',
  10. avatar: '',
  11. roles: [],
  12. permissions: [],
  13. jiBie:'',
  14. deptName:'',
  15. agentFlag:false,
  16. userId:'',
  17. levelId:''
  18. },
  19. mutations: {
  20. SET_TOKEN: (state, token) => {
  21. state.token = token
  22. },
  23. SET_NAME: (state, name) => {
  24. state.name = name
  25. },
  26. SET_NICKNAME: (state, nickName) => {
  27. state.nickName = nickName
  28. },
  29. SET_AVATAR: (state, avatar) => {
  30. state.avatar = avatar
  31. },
  32. SET_ROLES: (state, roles) => {
  33. state.roles = roles
  34. },
  35. SET_PERMISSIONS: (state, permissions) => {
  36. state.permissions = permissions
  37. },
  38. SET_DEPT:(state, deptId)=>{
  39. state.deptId=deptId
  40. },
  41. SET_JIBIE:(state, jiBie)=>{
  42. state.jiBie = jiBie
  43. },
  44. SET_DEPTNAME:(state, deptName)=>{
  45. state.deptName = deptName
  46. },
  47. SET_AGENTFLAG:(state, agentFlag)=>{
  48. state.agentFlag = state.jiBie != 0 ? false : agentFlag === "1" ? true : false;
  49. },
  50. SET_USERID:(state, userId)=>{
  51. state.userId = userId
  52. },
  53. SET_LEVELID:(state, levelId)=>{
  54. state.levelId = levelId
  55. }
  56. },
  57. actions: {
  58. // 登录
  59. Login({ commit }, userInfo) {
  60. const username = userInfo.username.trim()
  61. const password = userInfo.password
  62. const code = userInfo.code
  63. const uuid = userInfo.uuid
  64. return new Promise((resolve, reject) => {
  65. login(username, password, code, uuid).then(res => {
  66. setToken(res.token)
  67. commit('SET_TOKEN', res.token)
  68. resolve()
  69. }).catch(error => {
  70. reject(error)
  71. })
  72. })
  73. },
  74. // 获取用户信息
  75. GetInfo({ commit, state }) {
  76. return new Promise((resolve, reject) => {
  77. getInfo(state.token).then(res => {
  78. const user = res.user
  79. const avatar = user.avatar == "" ? require("@/assets/image/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
  80. if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
  81. commit('SET_ROLES', res.roles)
  82. commit('SET_PERMISSIONS', res.permissions)
  83. } else {
  84. commit('SET_ROLES', ['ROLE_DEFAULT'])
  85. }
  86. commit('SET_NAME', user.userName)
  87. commit('SET_NICKNAME', user.nickName)
  88. commit('SET_AVATAR', avatar)
  89. commit('SET_DEPT', user.deptId)
  90. commit("SET_USERID",user.userId)
  91. commit("SET_JIBIE",user.dept.jiBie)
  92. commit("SET_DEPTNAME",user.dept.deptName)
  93. commit("SET_AGENTFLAG",user.agentFlag)
  94. resolve(res)
  95. }).catch(error => {
  96. reject(error)
  97. })
  98. })
  99. },
  100. // 退出系统
  101. LogOut({ commit, state }) {
  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_DEPT', '')
  108. removeToken()
  109. resolve()
  110. }).catch(error => {
  111. reject(error)
  112. })
  113. })
  114. },
  115. // 前端 登出
  116. FedLogOut({ commit }) {
  117. return new Promise(resolve => {
  118. commit('SET_TOKEN', '')
  119. removeToken()
  120. resolve()
  121. })
  122. }
  123. }
  124. }
  125. export default user