user.js 7.3 KB

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