user.js 7.2 KB

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