useUserSnapshotInjection.ts 963 B

1234567891011121314151617181920212223242526
  1. import {useUserStore} from "@/store/userStore";
  2. import {VHS_USER_SNAPSHOT} from "@/types/injectionSymbols";
  3. import {UserInfo} from "@/types/user";
  4. export const useProvideUserSnapshot = function (userSnapshotRefOrGetter: Ref<Partial<UserInfo>> | null = null) {
  5. // 有时志愿表会存储一部分当前用户信息,这部分信息命名为userSnapshot
  6. // 使用时,用快照信息去覆盖用户信息,即可得到当时保存志愿表时的用户信息。
  7. const userStore = useUserStore()
  8. const userSnapshot = computed(() => {
  9. const snapshot = toValue(userSnapshotRefOrGetter)
  10. return {
  11. ...userStore.userInfo,
  12. ...snapshot
  13. }
  14. })
  15. provide(VHS_USER_SNAPSHOT, userSnapshot)
  16. return userSnapshot
  17. }
  18. export const useInjectUserSnapshot = function () {
  19. // 默认值用currentUser填充
  20. const {userInfo} = storeToRefs(useUserStore())
  21. return inject(VHS_USER_SNAPSHOT, userInfo)
  22. }