user.js 3.5 KB

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