village.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { ref } from 'vue'
  2. import { defineStore } from 'pinia'
  3. import type { VillageListItem } from '@/api/light/LightVillageApi';
  4. import FollowVillageApi from '@/api/light/FollowVillageApi';
  5. import LightVillageApi from '@/api/light/LightVillageApi';
  6. /**
  7. * 村庄各页面共享 数据
  8. */
  9. export const useVillageStore = defineStore('village', () => {
  10. const currentVillage = ref<VillageListItem | null>(null);
  11. const currentLonlat = ref<{ longitude: number, latitude: number } | null>(null);
  12. const myFollowVillages = ref<VillageListItem[]>([]);
  13. const myJoinedVillages = ref<VillageListItem[]>([]);
  14. async function setCurrentVillage(village: VillageListItem) {
  15. currentVillage.value = await LightVillageApi.getVillageDetails(village.id);
  16. console.log('切换当前激活村社', currentVillage.value);
  17. uni.setStorageSync('currentVillage', village.id);
  18. }
  19. function loadCurrentVillage() {
  20. return uni.getStorageSync('currentVillage') as number|unknown;
  21. }
  22. async function reloadVillageInfo() {
  23. try {
  24. if (!currentVillage.value)
  25. return null;
  26. const oldVillage = currentVillage.value.clone();
  27. await setCurrentVillage(oldVillage as VillageListItem);
  28. return oldVillage;
  29. } catch (error) {
  30. console.error(error);
  31. return null;
  32. }
  33. }
  34. function setCurrentLonlat(lonlat: { longitude: number, latitude: number }) {
  35. currentLonlat.value = lonlat;
  36. }
  37. async function loadMyFollowVillages() {
  38. const villages = await FollowVillageApi.getFollowVillageList();
  39. myFollowVillages.value = villages.list;
  40. }
  41. async function loadMyJoinedVillages() {
  42. const res = await FollowVillageApi.getClaimedVallageList();
  43. myJoinedVillages.value = res;
  44. }
  45. return {
  46. currentVillage,
  47. currentLonlat,
  48. myFollowVillages,
  49. myJoinedVillages,
  50. loadCurrentVillage,
  51. reloadVillageInfo,
  52. setCurrentVillage,
  53. setCurrentLonlat,
  54. loadMyFollowVillages,
  55. loadMyJoinedVillages,
  56. }
  57. })