user.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {login, logout, getInfo, socialLogin, socialLogin2} from '@/api/login'
  2. import { getToken, setToken, removeToken } from '@/utils/auth'
  3. const user = {
  4. state: {
  5. token: getToken(),
  6. id: 0, // 用户编号
  7. name: '',
  8. avatar: '',
  9. roles: [],
  10. permissions: []
  11. },
  12. mutations: {
  13. SET_ID: (state, id) => {
  14. state.id = id
  15. },
  16. SET_TOKEN: (state, token) => {
  17. state.token = token
  18. },
  19. SET_NAME: (state, name) => {
  20. state.name = name
  21. },
  22. SET_AVATAR: (state, avatar) => {
  23. state.avatar = avatar
  24. },
  25. SET_ROLES: (state, roles) => {
  26. state.roles = roles
  27. },
  28. SET_PERMISSIONS: (state, permissions) => {
  29. state.permissions = permissions
  30. }
  31. },
  32. actions: {
  33. // 登录
  34. Login({ commit }, userInfo) {
  35. const username = userInfo.username.trim()
  36. const password = userInfo.password
  37. const code = userInfo.code
  38. const uuid = userInfo.uuid
  39. return new Promise((resolve, reject) => {
  40. login(username, password, code, uuid).then(res => {
  41. res = res.data;
  42. setToken(res.token)
  43. commit('SET_TOKEN', res.token)
  44. resolve()
  45. }).catch(error => {
  46. reject(error)
  47. })
  48. })
  49. },
  50. // 社交登录
  51. SocialLogin({ commit }, userInfo) {
  52. const code = userInfo.code
  53. const state = userInfo.state
  54. const type = userInfo.type
  55. return new Promise((resolve, reject) => {
  56. socialLogin(type, code, state).then(res => {
  57. res = res.data;
  58. setToken(res.token)
  59. commit('SET_TOKEN', res.token)
  60. resolve()
  61. }).catch(error => {
  62. reject(error)
  63. })
  64. })
  65. },
  66. // 社交登录
  67. SocialLogin2({ commit }, userInfo) {
  68. const code = userInfo.code
  69. const state = userInfo.state
  70. const type = userInfo.type
  71. const username = userInfo.username.trim()
  72. const password = userInfo.password
  73. return new Promise((resolve, reject) => {
  74. socialLogin2(type, code, state, username, password).then(res => {
  75. res = res.data;
  76. setToken(res.token)
  77. commit('SET_TOKEN', res.token)
  78. resolve()
  79. }).catch(error => {
  80. reject(error)
  81. })
  82. })
  83. },
  84. // 获取用户信息
  85. GetInfo({ commit, state }) {
  86. return new Promise((resolve, reject) => {
  87. getInfo(state.token).then(res => {
  88. // 没有 data 数据,赋予个默认值
  89. if (!res) {
  90. res = {
  91. data: {
  92. roles: [],
  93. user: {
  94. id: '',
  95. avatar: '',
  96. userName: ''
  97. }
  98. }
  99. }
  100. }
  101. res = res.data; // 读取 data 数据
  102. const user = res.user
  103. const avatar = user.avatar === "" ? require("@/assets/images/profile.jpg") : user.avatar;
  104. if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
  105. commit('SET_ROLES', res.roles)
  106. commit('SET_PERMISSIONS', res.permissions)
  107. } else {
  108. commit('SET_ROLES', ['ROLE_DEFAULT'])
  109. }
  110. commit('SET_ID', user.id)
  111. commit('SET_NAME', user.userName)
  112. commit('SET_AVATAR', avatar)
  113. resolve(res)
  114. }).catch(error => {
  115. reject(error)
  116. })
  117. })
  118. },
  119. // 退出系统
  120. LogOut({ commit, state }) {
  121. return new Promise((resolve, reject) => {
  122. logout(state.token).then(() => {
  123. commit('SET_TOKEN', '')
  124. commit('SET_ROLES', [])
  125. commit('SET_PERMISSIONS', [])
  126. removeToken()
  127. resolve()
  128. }).catch(error => {
  129. reject(error)
  130. })
  131. })
  132. },
  133. // 前端 登出
  134. FedLogOut({ commit }) {
  135. return new Promise(resolve => {
  136. commit('SET_TOKEN', '')
  137. removeToken()
  138. resolve()
  139. })
  140. }
  141. }
  142. }
  143. export default user