user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. roleList: null,
  23. // 最后登陆时间
  24. lastLoginTime: null,
  25. // 验证码
  26. codeUrl: null,
  27. // 验证码登录参数
  28. uid: null,
  29. // 公钥
  30. publicKey: getPubKey(),
  31. },
  32. mutations: {
  33. SET_TOKEN: (state, value) => {
  34. state.token = value
  35. },
  36. SET_USER_ID: (state, value) => {
  37. state.userId = value
  38. },
  39. SET_USER_NAME: (state, value) => {
  40. state.userName = value
  41. },
  42. SET_ROLE_LIST: (state, value) => {
  43. state.roleList = value
  44. },
  45. SET_LAST_LOGINTIME: (state, value) => {
  46. state.lastLoginTime = value
  47. },
  48. SET_CODE_IMG: (state, value) => {
  49. state.codeUrl = 'data:image/gif;base64,' + value
  50. },
  51. SET_UID: (state, value) => {
  52. state.uid = value
  53. },
  54. SET_PUBKEY: (state, value) => {
  55. state.publicKey = value
  56. },
  57. },
  58. actions: {
  59. // 获取验证码
  60. getCodeImage({
  61. commit,
  62. dispatch
  63. }) {
  64. return new Promise((resolve, reject) => {
  65. uni.$http.get('/user/getCodeImage').then(res => {
  66. if (res.data.code === 200) {
  67. const {
  68. verifyCode,
  69. uid
  70. } = res.data.data
  71. commit('SET_CODE_IMG', verifyCode)
  72. commit('SET_UID', uid)
  73. dispatch('getPubKey')
  74. resolve(res)
  75. }
  76. })
  77. })
  78. },
  79. // 获取公钥
  80. getPubKey({
  81. commit
  82. }) {
  83. return new Promise((resolve, reject) => {
  84. uni.$http.get('/user/getPublicKey').then(res => {
  85. if (res.data.code === 200) {
  86. const key = res.data.data.replace('\r\n', '')
  87. commit('SET_PUBKEY', key)
  88. setPubKey(key)
  89. resolve(res)
  90. }
  91. })
  92. })
  93. },
  94. // 用户登录
  95. login({
  96. commit,
  97. state
  98. }, data) {
  99. const pwd = encrypt(data.password, data.publicKey)
  100. const obj = {
  101. userName: data.userName,
  102. password: pwd,
  103. verifyCode: data.verifyCode,
  104. uid: state.uid
  105. }
  106. return new Promise((resolve, reject) => {
  107. uni.$http.post('/user/phoneUserLogin', obj).then(res => {
  108. if (res.data.code === 200) {
  109. const token = res.data.data.token
  110. const {
  111. userId,
  112. userName,
  113. lastLoginTime,
  114. roleList
  115. } = res.data.data.user
  116. commit('SET_TOKEN', token)
  117. setToken(token)
  118. commit('SET_USER_ID', userId)
  119. setUid(userId)
  120. commit('SET_USER_NAME', userName)
  121. commit('SET_ROLE_LIST', roleList)
  122. commit('SET_LAST_LOGINTIME', lastLoginTime)
  123. }
  124. resolve(res)
  125. }).catch(err => {
  126. reject(err)
  127. })
  128. })
  129. },
  130. // 退出登录
  131. logout({
  132. commit
  133. }) {
  134. return new Promise((resolve, reject) => {
  135. uni.$http.post('/user/userLoginOut').then(res => {
  136. if (res.data.code === 200) {
  137. commit('SET_TOKEN', '')
  138. commit('SET_USER_ID', '')
  139. removeAll()
  140. }
  141. resolve(res)
  142. }).catch(err => {
  143. reject(err)
  144. })
  145. })
  146. },
  147. //修改密码
  148. updatePassword({
  149. state,
  150. commit
  151. }, data) {
  152. const pwd = encrypt(data.password, state.publicKey)
  153. const newPwd = encrypt(data.newPassword, state.publicKey)
  154. const obj = {
  155. password: pwd,
  156. newPassword: newPwd,
  157. userId: state.userId,
  158. userName: state.userName
  159. }
  160. return new Promise((resolve, reject) => {
  161. uni.$http.post('/user/updatePassWord', obj).then(res => {
  162. if (res.data.code === 200) {
  163. commit('SET_TOKEN', '')
  164. commit('SET_USER_ID', '')
  165. removeAll()
  166. }
  167. resolve(res)
  168. }).catch(err => {
  169. reject(err)
  170. })
  171. })
  172. },
  173. }
  174. }
  175. const {
  176. state,
  177. mutations,
  178. actions
  179. } = user
  180. export default {
  181. namespaced: true,
  182. state,
  183. mutations,
  184. actions
  185. }