user.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { defineStore } from 'pinia';
  2. import { getUserInfo, getUserAccountInfo } from '@/api/user';
  3. import { smsLogin, passwordLogin, logout } 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. import { getUserRegisterEnterpriseApply } from '@/api/enterprise'
  9. // 默认账户信息
  10. const defaultAccountInfo = {
  11. accessToken: '',
  12. expiresTime: '',
  13. openid: '',
  14. refreshToken: '',
  15. userId: ''
  16. }
  17. export const userStore = defineStore('user', {
  18. state: () => {
  19. const userLocal = uni.getStorageSync('user')
  20. const userInfo = userLocal ? JSON.parse(userLocal) : {}
  21. return {
  22. phone: null,
  23. isPersonal: false,
  24. baseInfo: userInfo.baseInfo ?? {}, // 用户信息
  25. userInfo: userInfo.userInfo ?? {},
  26. isLogin: !!uni.getStorageSync('token'), // 登录状态
  27. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  28. lastUpdateTime: 0, // 上次更新时间
  29. accountInfo: { ...defaultAccountInfo }, // 账号信息
  30. userAccountInfo: userInfo.userAccountInfo ?? {}, // 账户信息
  31. }
  32. },
  33. actions: {
  34. setLogin (val) {
  35. this.isLogin = val
  36. },
  37. // 登录
  38. async handleSmsLogin (query, index = 0, isGetData = true) {
  39. this.phone = query.phone
  40. const apiList = [smsLogin, passwordLogin]
  41. const { data, code } = await apiList[index](query)
  42. if (code === 0) {
  43. uni.showToast({
  44. title: '登录成功'
  45. })
  46. }
  47. this.isPersonal = false
  48. this.accountInfo = data
  49. uni.removeStorageSync('isPersonalToken') // 注册企业时个人登录
  50. // uni.removeStorageSync('entRegisterData') // 企业注册申请中的数据
  51. // 企业注册-审核被拒绝-重新提交-效验手机号不需要获取用户信息
  52. if (!isGetData) return
  53. this.getUserInfos()
  54. this.getAccountInfo()
  55. },
  56. async handleRegister (query) {
  57. this.phone = query.phone
  58. const { data } = await smsLogin(query)
  59. this.isPersonal = true
  60. this.accountInfo = data
  61. uni.setStorageSync('isPersonalToken', true); // 注册企业时个人登录
  62. },
  63. // 获取用户信息
  64. async getUserInfos() {
  65. const { code, data } = await getUserInfo();
  66. if (code !== 0) {
  67. return;
  68. }
  69. this.userInfo = data;
  70. return Promise.resolve(data);
  71. },
  72. // 获取账户信息
  73. async getAccountInfo () {
  74. const { code, data } = await getUserAccountInfo();
  75. if (code !== 0) {
  76. return;
  77. }
  78. this.userAccountInfo = data;
  79. return Promise.resolve(data);
  80. },
  81. // 查看用户是否有在申请中的数据
  82. async getPersonalApplyingData () {
  83. const { data } = await getUserRegisterEnterpriseApply()
  84. const bool = data && Object.keys(data).length > 0
  85. uni.setStorageSync('entRegisterData', bool ? JSON.stringify(data) : "") // 企业注册申请中的数据
  86. return Promise.resolve({ code: bool ? 1 : 0 , data: bool ? JSON.stringify(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. // 重置用户默认数据
  118. resetUserData() {
  119. // 清空 token
  120. this.setToken();
  121. // 清空用户相关的缓存
  122. this.userInfo = {}
  123. this.phone = ''
  124. this.userAccountInfo = {}
  125. this.accountInfo = { ...defaultAccountInfo };
  126. },
  127. // 登录后,加载各种信息
  128. async loginAfter() {
  129. await this.updateUserData();
  130. },
  131. // 登出系统
  132. async handleLogout() {
  133. try {
  134. await logout(uni.getStorageSync('token'))
  135. this.resetUserData()
  136. } catch (error) {
  137. console.log('handleLogout:error', error)
  138. // if (error?.msg === '企业登录授权错误,请重新登录')
  139. this.resetUserData()
  140. }
  141. // return !this.isLogin;
  142. },
  143. // 账号注册-回到首页
  144. async handleUserLogout () {
  145. await userLogout()
  146. uni.removeStorageSync('isPersonalToken') // 注册企业时个人登录
  147. uni.removeStorageSync('entRegisterData') // 企业注册申请中的数据
  148. this.resetUserData()
  149. return !this.isLogin
  150. },
  151. // 字典对应中文
  152. async getFieldText (data) {
  153. if (!data || !Object.keys(data).length) return {}
  154. if (data.birthday && data.birthday !== 0) {
  155. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  156. }
  157. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  158. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  159. }
  160. if (data.areaId && data.areaId !== 0) {
  161. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  162. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  163. }
  164. if (data.eduType && data.eduType !== 0) {
  165. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  166. }
  167. if (data.expType && data.expType !== 0) {
  168. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  169. }
  170. if (data.sex && data.sex !== 0) {
  171. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  172. }
  173. if (data.jobType && data.jobType !== 0) {
  174. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  175. }
  176. if (data.jobStatus && data.jobStatus !== 0) {
  177. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  178. }
  179. if (data.maritalStatus && data.maritalStatus !== 0) {
  180. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  181. }
  182. return data
  183. }
  184. },
  185. persist: {
  186. storage: {
  187. setItem(key, value) {
  188. uni.setStorageSync(key, value)
  189. },
  190. getItem(key) {
  191. return uni.getStorageSync(key)
  192. },
  193. },
  194. }
  195. })