UserTools.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import VillageApi, { VolunteerInfo } from "@/api/inhert/VillageApi";
  2. import { useSimpleDataLoader } from "@/components/composeabe/loader/SimpleDataLoader";
  3. import { useAuthStore } from "@/store/auth";
  4. import { useVillageStore } from "@/store/village";
  5. import { ref } from "vue";
  6. export function useUserTools() {
  7. const authStore = useAuthStore();
  8. const villageStore = useVillageStore();
  9. const volunteerInfo = ref<VolunteerInfo | null>(null);
  10. async function getIsVolunteer() {
  11. if (!authStore.isLogged) {
  12. return false;
  13. }
  14. try {
  15. volunteerInfo.value = await VillageApi.getVolunteerInfo();
  16. return volunteerInfo.value !== null;
  17. } catch (e) {
  18. console.error(e);
  19. return false;
  20. }
  21. }
  22. async function getIsJoinedVillage(villageId: number) {
  23. if (!authStore.isLogged) {
  24. return false;
  25. }
  26. if (villageStore.myJoinedVillages.length === 0) {
  27. await villageStore.loadMyJoinedVillages();
  28. }
  29. try {
  30. const isJoined = villageStore.myJoinedVillages.some(p => p.villageId === villageId) ?? false;
  31. return isJoined;
  32. } catch (error) {
  33. return false;
  34. }
  35. }
  36. async function getCanCollect(villageId: number) {
  37. if (!authStore.isLogged) {
  38. return false;
  39. }
  40. if (villageStore.myJoinedVillages.length === 0) {
  41. await villageStore.loadMyJoinedVillages();
  42. }
  43. try {
  44. const village = villageStore.myJoinedVillages.find(p => p.villageId === villageId);
  45. return Boolean(village !== undefined && village.collectFlag);
  46. } catch (error) {
  47. return false;
  48. }
  49. }
  50. return {
  51. getIsVolunteer,
  52. getIsJoinedVillage,
  53. getCanCollect,
  54. volunteerInfo,
  55. };
  56. }