user.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { defineStore } from 'pinia';
  2. import { getBaseInfo, getUserInfo, getUserAccountInfo } from '@/api/user';
  3. import { smsLogin, passwordLogin, weChatLogin, logout, userRegister, shareUserRegister } from '@/api/common'
  4. import { closeAuthModal, showAuthModal } from '@/hooks/useModal'
  5. import { timesTampChange } from '@/utils/date'
  6. import { getBaseInfoDictOfName } from '@/utils/getText'
  7. // 默认账户信息
  8. const defaultAccountInfo = {
  9. accessToken: '',
  10. expiresTime: '',
  11. openid: '',
  12. refreshToken: '',
  13. userId: ''
  14. }
  15. export const userStore = defineStore('user', {
  16. state: () => {
  17. const userLocal = uni.getStorageSync('user')
  18. const userInfo = userLocal ? JSON.parse(userLocal) : {}
  19. return {
  20. phone: null,
  21. baseInfo: userInfo.baseInfo ?? {}, // 用户信息
  22. userInfo: userInfo.userInfo ?? {},
  23. isLogin: !!uni.getStorageSync('token'), // 登录状态
  24. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  25. lastUpdateTime: 0, // 上次更新时间
  26. accountInfo: { ...defaultAccountInfo }, // 账号信息
  27. userAccountInfo: userInfo.userAccountInfo ?? {}, // 账户信息
  28. }
  29. },
  30. actions: {
  31. setLogin (val) {
  32. this.isLogin = val
  33. },
  34. // 登录
  35. async handleSmsLogin (query, index = 0) {
  36. this.phone = query.phone
  37. const apiList = [smsLogin, passwordLogin]
  38. const { data, code } = await apiList[index](query)
  39. if (code === 0) {
  40. uni.showToast({
  41. title: '登录成功'
  42. })
  43. }
  44. this.accountInfo = data
  45. this.getUserInfos()
  46. this.getAccountInfo()
  47. closeAuthModal()
  48. },
  49. async handleRegister (query) {
  50. this.phone = query.phone
  51. const { data, code } = await userRegister(query)
  52. if (code === 0) {
  53. uni.showToast({
  54. title: '手机号验证成功'
  55. })
  56. }
  57. this.accountInfo = data
  58. const res = await this.getInfo()
  59. this.getUserInfos()
  60. return Promise.resolve(res);
  61. },
  62. // 获取用户信息
  63. async getUserInfos() {
  64. const { code, data } = await getUserInfo();
  65. if (code !== 0) {
  66. return;
  67. }
  68. this.userInfo = data;
  69. return Promise.resolve(data);
  70. },
  71. // 获取账户信息
  72. async getAccountInfo () {
  73. const { code, data } = await getUserAccountInfo();
  74. if (code !== 0) {
  75. return;
  76. }
  77. this.userAccountInfo = data;
  78. return Promise.resolve(data);
  79. },
  80. // 设置 token
  81. setToken(token = '', refreshToken = '') {
  82. if (token === '') {
  83. this.isLogin = false;
  84. this.refreshToken = ''
  85. uni.removeStorageSync('token');
  86. uni.removeStorageSync('refresh-token');
  87. } else {
  88. this.isLogin = true;
  89. uni.setStorageSync('token', token);
  90. this.refreshToken = refreshToken
  91. uni.setStorageSync('refresh-token', refreshToken);
  92. this.loginAfter();
  93. }
  94. return this.isLogin;
  95. },
  96. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  97. async updateUserData() {
  98. if (!this.isLogin) {
  99. this.resetUserData();
  100. return;
  101. }
  102. // 防抖,5 秒之内不刷新
  103. const nowTime = new Date().getTime();
  104. if (this.lastUpdateTime + 5000 > nowTime) {
  105. return;
  106. }
  107. this.lastUpdateTime = nowTime;
  108. },
  109. // 重置用户默认数据
  110. resetUserData() {
  111. // 清空 token
  112. this.setToken();
  113. // 清空用户相关的缓存
  114. this.userInfo = {}
  115. this.phone = ''
  116. this.userAccountInfo = {}
  117. this.accountInfo = { ...defaultAccountInfo };
  118. },
  119. // 登录后,加载各种信息
  120. async loginAfter() {
  121. await this.updateUserData();
  122. },
  123. // 登出系统
  124. async handleLogout() {
  125. await logout(uni.getStorageSync('token'))
  126. this.resetUserData();
  127. return !this.isLogin;
  128. },
  129. // 字典对应中文
  130. async getFieldText (data) {
  131. if (!data || !Object.keys(data).length) return {}
  132. if (data.birthday && data.birthday !== 0) {
  133. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  134. }
  135. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  136. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  137. }
  138. if (data.areaId && data.areaId !== 0) {
  139. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  140. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  141. }
  142. if (data.eduType && data.eduType !== 0) {
  143. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  144. }
  145. if (data.expType && data.expType !== 0) {
  146. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  147. }
  148. if (data.sex && data.sex !== 0) {
  149. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  150. }
  151. if (data.jobType && data.jobType !== 0) {
  152. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  153. }
  154. if (data.jobStatus && data.jobStatus !== 0) {
  155. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  156. }
  157. if (data.maritalStatus && data.maritalStatus !== 0) {
  158. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  159. }
  160. return data
  161. }
  162. },
  163. persist: {
  164. storage: {
  165. setItem(key, value) {
  166. uni.setStorageSync(key, value)
  167. },
  168. getItem(key) {
  169. return uni.getStorageSync(key)
  170. },
  171. },
  172. }
  173. })