user.js 3.6 KB

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