useUserSnapshotInjection.js 957 B

12345678910111213141516171819202122232425262728
  1. import {computed} from 'vue';
  2. import {useUserStore} from "@/hooks/useUserStore";
  3. import {injectLocal, provideLocal, toValue} from "@vueuse/core";
  4. const key = Symbol('USER_SNAPSHOT')
  5. export const useProvideUserSnapshot = function (userSnapshotRefOrGetter) {
  6. // 有时志愿表会存储一部分当前用户信息,这部分信息命名为userSnapshot
  7. // 使用时,用快照信息去覆盖用户信息,即可得到当时保存志愿表时的用户信息。
  8. const {currentUser} = useUserStore()
  9. const userSnapshot = computed(() => {
  10. const snapshot = toValue(userSnapshotRefOrGetter)
  11. return {
  12. ...currentUser.value,
  13. ...snapshot
  14. }
  15. })
  16. provideLocal(key, {userSnapshot})
  17. return userSnapshot
  18. }
  19. export const useInjectUserSnapshot = function () {
  20. // 默认值用currentUser填充
  21. const {currentUser} = useUserStore()
  22. return injectLocal(key, {userSnapshot: currentUser})
  23. }