zhengyiming
5 天以前 9e975eee3ebe9cc133435ca3fb2dbf8458efc748
packages/hooks/area.ts
@@ -1,23 +1,17 @@
import { flattenAreaTree, formatAreaListToTree } from '@12333/utils';
import { useQuery, useQueryClient } from '@tanstack/vue-query';
import { computed, onMounted, onUnmounted, ref } from 'vue';
import { computed, MaybeRef, onMounted, onUnmounted, ref, unref } from 'vue';
import { AreaType, CategoryCode } from '@12333/constants';
import Taro, { EventChannel } from '@tarojs/taro';
import { useDictionaryDataSelect } from './dic';
import axios from 'axios';
export function useArea() {
  const queryClient = useQueryClient();
  const { dictionaryDataList } = useDictionaryDataSelect({
    categoryCode: CategoryCode.Area,
    staleTime: Infinity,
    all: true,
    maxDeep: AreaType.Area,
  });
  const { areaList } = useAllAreaList();
  const areaList = computed(() => dictionaryDataList.value.map(convertDictionaryToAreaTreeNode));
  const areaTree = computed(() => formatAreaListToTree(areaList.value));
  // const areaList = computed(() => dictionaryDataList.value.map(convertDictionaryToAreaTreeNode));
  function getAreaFromCompleteAreaList(areaCode: string) {
    return areaList.value.find((x) => x.areaCode === areaCode);
@@ -30,20 +24,33 @@
  return {
    completeAreaList: areaList,
    areaList,
    completeAreaTree: areaTree,
    provinceList: computed(() => areaList.value.filter((x) => x.layer === AreaType.Province)),
    // completeAreaTree: areaTree,
    // provinceList: computed(() => areaList.value.filter((x) => x.layer === AreaType.Province)),
    getAreaFromCompleteAreaList,
    getAreaByAreaCode,
  };
}
type UseAreaTreeOptions = {
  maxLayer?: MaybeRef<AreaType>;
};
export function useAreaTree(options = {} as UseAreaTreeOptions) {
  const { maxLayer = AreaType.Area } = options;
  const { areaList } = useAllAreaList();
  const areaTree = computed(() => formatAreaListToTree(areaList.value, null, unref(maxLayer)));
  return { areaTree: areaTree };
}
function convertDictionaryToAreaTreeNode(
  item: API.SelectOptionStringGetDictionaryDataSelectQueryResultOption
) {
  return {
    children: [],
    areaCode: item.data?.code,
    parentCode: '',
    parentCode: item.data?.parentCode,
    areaName: item.label,
    layer: Number(item.data?.field4),
    quickQuery: item.data?.field2,
@@ -53,7 +60,19 @@
}
export function useAllAreaList() {
  const { areaList } = useArea();
  const { data: areaList } = useQuery({
    queryKey: ['area.txt'],
    queryFn() {
      return axios
        .get<API.AreaTreeNode[]>(
          'https://parkmanagement.oss-cn-hangzhou.aliyuncs.com/12333/area.txt'
        )
        .then((res) => res.data);
    },
    placeholderData: () => [] as API.AreaTreeNode[],
    staleTime: Infinity,
  });
  const findAreaCodeFromName = (areaName: string) => {
    const areaItem = areaList.value.find((x) => x.areaName === areaName);
@@ -78,13 +97,13 @@
  };
}
export function useProvinceList() {
  const { provinceList } = useArea();
// export function useProvinceList() {
//   const { provinceList } = useArea();
  return {
    provinceList,
  };
}
//   return {
//     provinceList,
//   };
// }
export const globalEventEmitter = new Taro.Events();