user.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. import { userLogout } from '@/api/common';
  8. // 默认账户信息
  9. const defaultAccountInfo = {
  10. accessToken: '',
  11. expiresTime: '',
  12. openid: '',
  13. refreshToken: '',
  14. userId: ''
  15. }
  16. export const userStore = defineStore('user', {
  17. state: () => {
  18. const userLocal = uni.getStorageSync('user')
  19. const userInfo = userLocal ? JSON.parse(userLocal) : {}
  20. return {
  21. phone: null,
  22. baseInfo: userInfo.baseInfo ?? {}, // 用户信息
  23. userInfo: userInfo.userInfo ?? {},
  24. isLogin: !!uni.getStorageSync('token'), // 登录状态
  25. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  26. lastUpdateTime: 0, // 上次更新时间
  27. accountInfo: { ...defaultAccountInfo }, // 账号信息
  28. userAccountInfo: userInfo.userAccountInfo ?? {}, // 账户信息
  29. }
  30. },
  31. actions: {
  32. setLogin (val) {
  33. this.isLogin = val
  34. },
  35. // 登录
  36. async handleSmsLogin (query, index = 0, isGetData = true) {
  37. this.phone = query.phone
  38. const apiList = [smsLogin, passwordLogin]
  39. const { data, code } = await apiList[index](query)
  40. if (code === 0) {
  41. uni.showToast({
  42. title: '登录成功'
  43. })
  44. }
  45. this.accountInfo = data
  46. // 企业注册-审核被拒绝-重新提交-效验手机号不需要获取用户信息
  47. if (!isGetData) return
  48. this.getUserInfos()
  49. this.getAccountInfo()
  50. },
  51. async handleRegister (query) {
  52. this.phone = query.phone
  53. const { data, code } = await smsLogin(query)
  54. if (code === 0) {
  55. uni.showToast({
  56. title: '手机号验证成功'
  57. })
  58. }
  59. this.accountInfo = data
  60. closeAuthModal()
  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 handleUserLogout () {
  131. await userLogout()
  132. this.resetUserData()
  133. return !this.isLogin
  134. },
  135. // 字典对应中文
  136. async getFieldText (data) {
  137. if (!data || !Object.keys(data).length) return {}
  138. if (data.birthday && data.birthday !== 0) {
  139. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  140. }
  141. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  142. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  143. }
  144. if (data.areaId && data.areaId !== 0) {
  145. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  146. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  147. }
  148. if (data.eduType && data.eduType !== 0) {
  149. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  150. }
  151. if (data.expType && data.expType !== 0) {
  152. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  153. }
  154. if (data.sex && data.sex !== 0) {
  155. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  156. }
  157. if (data.jobType && data.jobType !== 0) {
  158. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  159. }
  160. if (data.jobStatus && data.jobStatus !== 0) {
  161. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  162. }
  163. if (data.maritalStatus && data.maritalStatus !== 0) {
  164. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  165. }
  166. return data
  167. }
  168. },
  169. persist: {
  170. storage: {
  171. setItem(key, value) {
  172. uni.setStorageSync(key, value)
  173. },
  174. getItem(key) {
  175. return uni.getStorageSync(key)
  176. },
  177. },
  178. }
  179. })