user.js 3.5 KB

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