user.js 4.1 KB

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