user.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import {
  5. login,
  6. logout,
  7. getInfo
  8. } from '@/api/login'
  9. import {
  10. getToken,
  11. setToken,
  12. removeToken
  13. } from '@/utils/auth'
  14. const baseUrl = config.baseUrl
  15. const user = {
  16. state: {
  17. token: getToken(),
  18. name: storage.get(constant.name),
  19. avatar: storage.get(constant.avatar),
  20. roles: storage.get(constant.roles),
  21. permissions: storage.get(constant.permissions)
  22. },
  23. mutations: {
  24. SET_TOKEN: (state, token) => {
  25. state.token = token
  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. },
  44. actions: {
  45. // 登录
  46. Login({
  47. commit
  48. }, userInfo) {
  49. const username = userInfo.username.trim()
  50. const password = userInfo.password
  51. const code = userInfo.code
  52. const uuid = userInfo.uuid
  53. return new Promise((resolve, reject) => {
  54. login(username, password, code, uuid).then(res => {
  55. setToken(res.token)
  56. uni.setStorageSync("certificationStatus",res.certificationStatus)
  57. commit('SET_TOKEN', res.token)
  58. resolve()
  59. }).catch(error => {
  60. reject(error)
  61. })
  62. })
  63. },
  64. // 获取用户信息
  65. GetInfo({
  66. commit,
  67. state
  68. }) {
  69. return new Promise((resolve, reject) => {
  70. getInfo().then(res => {
  71. const user = res.user
  72. const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
  73. require("@/static/images/profile.jpg") : baseUrl + user.avatar
  74. const username = (user == null || user.userName == "" || user.userName ==
  75. null) ? "" : 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_NAME', username)
  83. commit('SET_AVATAR', avatar)
  84. resolve(res)
  85. }).catch(error => {
  86. reject(error)
  87. })
  88. })
  89. },
  90. // 退出系统
  91. LogOut({
  92. commit,
  93. state
  94. }) {
  95. return new Promise((resolve, reject) => {
  96. logout(state.token).then(() => {
  97. commit('SET_TOKEN', '')
  98. commit('SET_ROLES', [])
  99. commit('SET_PERMISSIONS', [])
  100. removeToken()
  101. storage.clean()
  102. resolve()
  103. }).catch(error => {
  104. reject(error)
  105. })
  106. })
  107. }
  108. }
  109. }
  110. export default user