user.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {login, logout, getInfo, socialLogin, smsLogin} from '@/api/login'
  2. import {setToken, removeToken} 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 captchaVerification = userInfo.captchaVerification
  37. const socialCode = userInfo.socialCode
  38. const socialState = userInfo.socialState
  39. const socialType = userInfo.socialType
  40. return new Promise((resolve, reject) => {
  41. login(username, password, captchaVerification, socialType, socialCode, socialState).then(res => {
  42. res = res.data;
  43. // 设置 token
  44. setToken(res)
  45. resolve()
  46. }).catch(error => {
  47. reject(error)
  48. })
  49. })
  50. },
  51. // 社交登录
  52. SocialLogin({ commit }, userInfo) {
  53. const code = userInfo.code
  54. const state = userInfo.state
  55. const type = userInfo.type
  56. return new Promise((resolve, reject) => {
  57. socialLogin(type, code, state).then(res => {
  58. res = res.data;
  59. // 设置 token
  60. setToken(res)
  61. resolve()
  62. }).catch(error => {
  63. reject(error)
  64. })
  65. })
  66. },
  67. // 短信登录
  68. SmsLogin({ commit }, userInfo) {
  69. const mobile = userInfo.mobile.trim()
  70. const mobileCode = userInfo.mobileCode
  71. return new Promise((resolve, reject) => {
  72. smsLogin(mobile,mobileCode).then(res => {
  73. res = res.data;
  74. // 设置 token
  75. setToken(res)
  76. resolve()
  77. }).catch(error => {
  78. reject(error)
  79. })
  80. })
  81. },
  82. // 获取用户信息
  83. GetInfo({ commit, state }) {
  84. return new Promise((resolve, reject) => {
  85. getInfo().then(res => {
  86. // 没有 data 数据,赋予个默认值
  87. if (!res) {
  88. res = {
  89. data: {
  90. roles: [],
  91. user: {
  92. id: '',
  93. avatar: '',
  94. userName: '',
  95. nickname: ''
  96. }
  97. }
  98. }
  99. }
  100. res = res.data; // 读取 data 数据
  101. const user = res.user
  102. const avatar = ( user.avatar === "" || user.avatar == null ) ? require("@/assets/images/profile.jpg") : user.avatar;
  103. if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
  104. commit('SET_ROLES', res.roles)
  105. commit('SET_PERMISSIONS', res.permissions)
  106. } else {
  107. commit('SET_ROLES', ['ROLE_DEFAULT'])
  108. }
  109. commit('SET_ID', user.id)
  110. commit('SET_NAME', user.userName)
  111. commit('SET_NICKNAME', user.nickname)
  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_ROLES', [])
  124. commit('SET_PERMISSIONS', [])
  125. removeToken()
  126. resolve()
  127. }).catch(error => {
  128. reject(error)
  129. })
  130. })
  131. }
  132. }
  133. }
  134. export default user