user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import { isHttp, isEmpty } from "@/utils/validate"
  5. import { login, logout, getInfo } from '@/api/login'
  6. import { getToken, setToken, removeToken } from '@/utils/auth'
  7. import defAva from '@/static/images/xijiao/avator.png'
  8. const baseUrl = config.baseUrl
  9. const user = {
  10. state: {
  11. token: getToken(),
  12. id: storage.get(constant.id),
  13. name: storage.get(constant.name),
  14. avatar: storage.get(constant.avatar),
  15. roles: storage.get(constant.roles),
  16. permissions: storage.get(constant.permissions),
  17. person:storage.get(constant.person),
  18. },
  19. mutations: {
  20. SET_TOKEN: (state, token) => {
  21. state.token = token
  22. },
  23. SET_ID: (state, id) => {
  24. state.id = id
  25. storage.set(constant.id, id)
  26. },
  27. SET_NAME: (state, name) => {
  28. state.name = name
  29. storage.set(constant.name, name)
  30. },
  31. SET_AVATAR: (state, avatar) => {
  32. state.avatar = avatar
  33. storage.set(constant.avatar, avatar)
  34. },
  35. SET_ROLES: (state, roles) => {
  36. state.roles = roles
  37. storage.set(constant.roles, roles)
  38. },
  39. SET_PERMISSIONS: (state, permissions) => {
  40. state.permissions = permissions
  41. storage.set(constant.permissions, permissions)
  42. },
  43. SET_PERSON: (state, person) => {
  44. state.person = person
  45. storage.set(constant.person, person)
  46. },
  47. },
  48. actions: {
  49. // 登录
  50. Login({ commit }, userInfo) {
  51. const username = userInfo.username.trim()
  52. const password = userInfo.password
  53. const code = userInfo.code
  54. const uuid = userInfo.uuid
  55. return new Promise((resolve, reject) => {
  56. login(username, password, code, uuid).then(res => {
  57. setToken(res.data.access_token)
  58. commit('SET_TOKEN', res.data.access_token)
  59. resolve()
  60. }).catch(error => {
  61. reject(error)
  62. })
  63. })
  64. },
  65. // 获取用户信息
  66. GetInfo({ commit, state }) {
  67. return new Promise((resolve, reject) => {
  68. getInfo().then(res => {
  69. const user = res.user
  70. let avatar = user.avatar || ""
  71. if (!isHttp(avatar)) {
  72. avatar = (isEmpty(avatar)) ? defAva : baseUrl + avatar
  73. }
  74. const userid = (isEmpty(user) || isEmpty(user.userId)) ? "" : user.userId
  75. const username = (isEmpty(user) || isEmpty(user.userName)) ? "" : user.userName
  76. if (res.roles && res.roles.length > 0) {
  77. commit('SET_ROLES', res.roles)
  78. commit('SET_PERMISSIONS', res.permissions)
  79. } else {
  80. commit('SET_ROLES', ['ROLE_DEFAULT'])
  81. }
  82. commit('SET_ID', userid)
  83. commit('SET_NAME', username)
  84. commit('SET_AVATAR', avatar)
  85. resolve(res)
  86. }).catch(error => {
  87. reject(error)
  88. })
  89. })
  90. },
  91. // 退出系统
  92. LogOut({ commit, state }) {
  93. return new Promise((resolve, reject) => {
  94. logout(state.token).then(() => {
  95. commit('SET_TOKEN', '')
  96. commit('SET_ROLES', [])
  97. commit('SET_PERMISSIONS', [])
  98. removeToken()
  99. storage.clean()
  100. resolve()
  101. }).catch(error => {
  102. reject(error)
  103. })
  104. })
  105. }
  106. }
  107. }
  108. export default user