user.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { defineStore } from 'pinia';
  2. import { getBaseInfo, getUserInfo } 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 { updateEventList } from '@/utils/eventList'
  8. import { checkPersonBaseInfo } from '@/utils/check'
  9. // 默认用户信息
  10. const defaultBaseInfo = {
  11. avatar: '', // 头像
  12. nickname: '', // 昵称
  13. gender: 0, // 性别
  14. mobile: '', // 手机号
  15. point: 0, // 积分
  16. };
  17. // 默认账户信息
  18. const defaultAccountInfo = {
  19. accessToken: '',
  20. expiresTime: '',
  21. openid: '',
  22. refreshToken: '',
  23. userId: ''
  24. }
  25. const tabUrl = [
  26. 'pages/index/position',
  27. 'pages/index/communicate',
  28. 'pages/index/my'
  29. ]
  30. export const userStore = defineStore('user', {
  31. state: () => {
  32. const userLocal = uni.getStorageSync('user')
  33. const userInfo = userLocal ? JSON.parse(userLocal) : {}
  34. return {
  35. baseInfo: userInfo.baseInfo ?? {}, // 用户信息
  36. userInfo: userInfo.userInfo ?? {},
  37. isLogin: !!uni.getStorageSync('token'), // 登录状态
  38. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  39. lastUpdateTime: 0, // 上次更新时间
  40. accountInfo: { ...defaultAccountInfo }, // 账号信息
  41. }
  42. },
  43. actions: {
  44. setLogin (val) {
  45. this.isLogin = val
  46. },
  47. // 登录
  48. async handleSmsLogin (query, index = 0) {
  49. const apiList = [weChatLogin, smsLogin, passwordLogin]
  50. const { data, code } = await apiList[index](query)
  51. if (code === 0) {
  52. uni.showToast({
  53. title: '登录成功'
  54. })
  55. }
  56. this.accountInfo = data
  57. const res = await this.getInfo()
  58. this.getUserInfo()
  59. return Promise.resolve(res);
  60. },
  61. async handleRegister (query) {
  62. const { data, code } = await userRegister(query)
  63. if (code === 0) {
  64. uni.showToast({
  65. title: '注册成功'
  66. })
  67. }
  68. this.accountInfo = data
  69. const res = await this.getInfo()
  70. this.getUserInfo()
  71. return Promise.resolve(res);
  72. },
  73. // 扫码注册登录
  74. async handleShareUserRegister (query) {
  75. try {
  76. const { data, code } = await shareUserRegister(query)
  77. if (code === 0) {
  78. uni.showToast({
  79. title: '登录成功'
  80. })
  81. this.accountInfo = data
  82. await this.getInfo()
  83. this.getUserInfo()
  84. }
  85. return Promise.resolve(data);
  86. } catch (err) {
  87. uni.showToast({
  88. icon: 'none',
  89. title: err.msg
  90. })
  91. }
  92. },
  93. // 校验是否完善人才必填信息
  94. checkPersonBaseInfoFun(data) {
  95. const necessaryInfoReady = checkPersonBaseInfo(data)
  96. data.necessaryInfoReady = necessaryInfoReady
  97. if (necessaryInfoReady) closeAuthModal()
  98. else showAuthModal('necessaryInfo')
  99. uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'fddeaddc47868b');
  100. },
  101. // 获取人才信息
  102. async getInfo() {
  103. const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
  104. updateEventList() // 更新事件列表
  105. await this.checkPersonBaseInfoFun(data) // 校验是否完善人才必填信息
  106. if (code !== 0) {
  107. return;
  108. }
  109. if (!data) return
  110. const _data = await this.getFieldText(data)
  111. this.baseInfo = _data
  112. return Promise.resolve(data);
  113. },
  114. // 获取用户信息
  115. async getUserInfo() {
  116. const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
  117. if (code !== 0) {
  118. return;
  119. }
  120. this.userInfo = data;
  121. return Promise.resolve(data);
  122. },
  123. // 设置 token
  124. setToken(token = '', refreshToken = '') {
  125. if (token === '') {
  126. this.isLogin = false;
  127. this.refreshToken = ''
  128. uni.removeStorageSync('token');
  129. uni.removeStorageSync('refresh-token');
  130. } else {
  131. this.isLogin = true;
  132. uni.setStorageSync('token', token);
  133. this.refreshToken = refreshToken
  134. uni.setStorageSync('refresh-token', refreshToken);
  135. this.loginAfter();
  136. }
  137. return this.isLogin;
  138. },
  139. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  140. async updateUserData() {
  141. if (!this.isLogin) {
  142. this.resetUserData();
  143. return;
  144. }
  145. // 防抖,5 秒之内不刷新
  146. const nowTime = new Date().getTime();
  147. if (this.lastUpdateTime + 5000 > nowTime) {
  148. return;
  149. }
  150. this.lastUpdateTime = nowTime;
  151. // 获取最新信息
  152. return this.baseInfo;
  153. },
  154. // 重置用户默认数据
  155. resetUserData() {
  156. // 清空 token
  157. this.setToken();
  158. // 清空用户相关的缓存
  159. this.baseInfo = { ...defaultBaseInfo };
  160. this.userInfo = {}
  161. this.accountInfo = { ...defaultAccountInfo };
  162. },
  163. // 登录后,加载各种信息
  164. async loginAfter() {
  165. await this.updateUserData();
  166. },
  167. // 登出系统
  168. async handleLogout() {
  169. await logout()
  170. this.resetUserData();
  171. return !this.isLogin;
  172. },
  173. // 字典对应中文
  174. async getFieldText (data) {
  175. if (!data || !Object.keys(data).length) return {}
  176. if (data.birthday && data.birthday !== 0) {
  177. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  178. }
  179. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  180. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  181. }
  182. if (data.areaId && data.areaId !== 0) {
  183. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  184. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  185. }
  186. if (data.eduType && data.eduType !== 0) {
  187. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  188. }
  189. if (data.expType && data.expType !== 0) {
  190. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  191. }
  192. if (data.sex && data.sex !== 0) {
  193. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  194. }
  195. if (data.jobType && data.jobType !== 0) {
  196. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  197. }
  198. if (data.jobStatus && data.jobStatus !== 0) {
  199. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  200. }
  201. if (data.maritalStatus && data.maritalStatus !== 0) {
  202. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  203. }
  204. return data
  205. }
  206. },
  207. persist: {
  208. // enabled: true,
  209. // strategies: [
  210. // {
  211. // key: 'user-store'
  212. // }
  213. // ]
  214. storage: {
  215. setItem(key, value) {
  216. uni.setStorageSync(key, value)
  217. },
  218. getItem(key) {
  219. return uni.getStorageSync(key)
  220. },
  221. },
  222. }
  223. })