user.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 || { phone }
  99. const necessaryInfoReady = checkPersonBaseInfo(data)
  100. data.necessaryInfoReady = necessaryInfoReady
  101. if (necessaryInfoReady) closeAuthModal()
  102. else showAuthModal('necessaryInfo')
  103. uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'fddeaddc47868b');
  104. },
  105. // 获取人才信息
  106. async getInfo() {
  107. const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
  108. updateEventList() // 更新事件列表
  109. this.checkPersonBaseInfoFun(data) // 校验是否完善人才必填信息
  110. if (code !== 0) {
  111. return;
  112. }
  113. if (!data) return
  114. const _data = await this.getFieldText(data)
  115. this.baseInfo = _data
  116. return Promise.resolve(data);
  117. },
  118. // 获取用户信息
  119. async getUserInfo() {
  120. const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
  121. if (code !== 0) {
  122. return;
  123. }
  124. this.userInfo = data;
  125. return Promise.resolve(data);
  126. },
  127. // 设置 token
  128. setToken(token = '', refreshToken = '') {
  129. if (token === '') {
  130. this.isLogin = false;
  131. this.refreshToken = ''
  132. uni.removeStorageSync('token');
  133. uni.removeStorageSync('refresh-token');
  134. } else {
  135. this.isLogin = true;
  136. uni.setStorageSync('token', token);
  137. this.refreshToken = refreshToken
  138. uni.setStorageSync('refresh-token', refreshToken);
  139. this.loginAfter();
  140. }
  141. return this.isLogin;
  142. },
  143. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  144. async updateUserData() {
  145. if (!this.isLogin) {
  146. this.resetUserData();
  147. return;
  148. }
  149. // 防抖,5 秒之内不刷新
  150. const nowTime = new Date().getTime();
  151. if (this.lastUpdateTime + 5000 > nowTime) {
  152. return;
  153. }
  154. this.lastUpdateTime = nowTime;
  155. // 获取最新信息
  156. return this.baseInfo;
  157. },
  158. // 重置用户默认数据
  159. resetUserData() {
  160. // 清空 token
  161. this.setToken();
  162. // 清空用户相关的缓存
  163. this.baseInfo = { ...defaultBaseInfo };
  164. this.userInfo = {}
  165. this.accountInfo = { ...defaultAccountInfo };
  166. },
  167. // 登录后,加载各种信息
  168. async loginAfter() {
  169. await this.updateUserData();
  170. },
  171. // 登出系统
  172. async handleLogout() {
  173. await logout()
  174. this.resetUserData();
  175. return !this.isLogin;
  176. },
  177. // 字典对应中文
  178. async getFieldText (data) {
  179. if (!data || !Object.keys(data).length) return {}
  180. if (data.birthday && data.birthday !== 0) {
  181. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  182. }
  183. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  184. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  185. }
  186. if (data.areaId && data.areaId !== 0) {
  187. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  188. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  189. }
  190. if (data.eduType && data.eduType !== 0) {
  191. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  192. }
  193. if (data.expType && data.expType !== 0) {
  194. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  195. }
  196. if (data.sex && data.sex !== 0) {
  197. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  198. }
  199. if (data.jobType && data.jobType !== 0) {
  200. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  201. }
  202. if (data.jobStatus && data.jobStatus !== 0) {
  203. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  204. }
  205. if (data.maritalStatus && data.maritalStatus !== 0) {
  206. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  207. }
  208. return data
  209. }
  210. },
  211. persist: {
  212. // enabled: true,
  213. // strategies: [
  214. // {
  215. // key: 'user-store'
  216. // }
  217. // ]
  218. storage: {
  219. setItem(key, value) {
  220. uni.setStorageSync(key, value)
  221. },
  222. getItem(key) {
  223. return uni.getStorageSync(key)
  224. },
  225. },
  226. }
  227. })