user.js 5.7 KB

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