user.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. setPubKey,
  3. getPubKey,
  4. getToken,
  5. setToken,
  6. setUid,
  7. getUid,
  8. removeAll
  9. } from '@/utils/auth.js'
  10. import {
  11. encrypt
  12. } from '@/utils/jsencrypt.js'
  13. const user = {
  14. state: {
  15. // 用户认证token
  16. token: getToken(),
  17. // 用户ID
  18. userId: getUid(),
  19. // 用户名
  20. userName: null,
  21. // 验证码
  22. codeUrl: null,
  23. // 验证码登录参数
  24. uid: null,
  25. // 公钥
  26. publicKey: getPubKey(),
  27. },
  28. mutations: {
  29. SET_TOKEN: (state, value) => {
  30. state.token = value
  31. },
  32. SET_USER_ID: (state, value) => {
  33. state.userId = value
  34. },
  35. SET_USER_NAME: (state, value) => {
  36. state.userName = value
  37. },
  38. SET_CODE_IMG: (state, value) => {
  39. state.codeUrl = 'data:image/gif;base64,' + value
  40. },
  41. SET_UID: (state, value) => {
  42. state.uid = value
  43. },
  44. SET_PUBKEY: (state, value) => {
  45. state.publicKey = value
  46. },
  47. },
  48. actions: {
  49. // 获取验证码
  50. getCodeImage({
  51. commit,
  52. dispatch
  53. }) {
  54. return new Promise((resolve, reject) => {
  55. uni.$http.get('/user/getCodeImage').then(res => {
  56. if (res.data.code === 200) {
  57. const {
  58. verifyCode,
  59. uid
  60. } = res.data.data
  61. commit('SET_CODE_IMG', verifyCode)
  62. commit('SET_UID', uid)
  63. dispatch('getPubKey')
  64. resolve(res)
  65. }
  66. })
  67. })
  68. },
  69. // 获取公钥
  70. getPubKey({
  71. commit
  72. }) {
  73. return new Promise((resolve, reject) => {
  74. uni.$http.get('/user/getPublicKey').then(res => {
  75. if (res.data.code === 200) {
  76. const key = res.data.data.replace('\r\n', '')
  77. commit('SET_PUBKEY', key)
  78. setPubKey(key)
  79. resolve(res)
  80. }
  81. })
  82. })
  83. },
  84. // 用户登录
  85. login({
  86. commit,
  87. state
  88. }, data) {
  89. const pwd = encrypt(data.password, data.publicKey)
  90. const obj = {
  91. userName: data.userName,
  92. password: pwd,
  93. verifyCode: data.verifyCode,
  94. uid: state.uid
  95. }
  96. return new Promise((resolve, reject) => {
  97. uni.$http.post('/user/phoneUserLogin', obj).then(res => {
  98. if (res.data.code === 200) {
  99. const token = res.data.data.token
  100. const {
  101. userId,
  102. userName
  103. } = res.data.data.user
  104. commit('SET_TOKEN', token)
  105. setToken(token)
  106. commit('SET_USER_ID', userId)
  107. setUid(userId)
  108. commit('SET_USER_NAME', userName)
  109. }
  110. resolve(res)
  111. }).catch(err => {
  112. reject(err)
  113. })
  114. })
  115. },
  116. // 退出登录
  117. logout({
  118. commit
  119. }) {
  120. return new Promise((resolve, reject) => {
  121. uni.$http.post('/user/userLoginOut').then(res => {
  122. if (res.data.code === 200) {
  123. commit('SET_TOKEN', '')
  124. commit('SET_USER_ID', '')
  125. removeAll()
  126. }
  127. resolve(res)
  128. }).catch(err => {
  129. reject(err)
  130. })
  131. })
  132. }
  133. }
  134. }
  135. const {
  136. state,
  137. mutations,
  138. actions
  139. } = user
  140. export default {
  141. namespaced: true,
  142. state,
  143. mutations,
  144. actions
  145. }