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