user.js 6.9 KB

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