| import { flattenAreaTree, formatAreaListToTree } from '@12333/utils'; | 
| import { useQuery, useQueryClient } from '@tanstack/vue-query'; | 
| 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'; | 
| import * as dictionaryServices from '@12333/services/apiV2/dictionary'; | 
|   | 
| export function useArea() { | 
|   const queryClient = useQueryClient(); | 
|   | 
|   const { areaList } = useAllAreaList(); | 
|   | 
|   // const areaList = computed(() => dictionaryDataList.value.map(convertDictionaryToAreaTreeNode)); | 
|   | 
|   function getAreaFromCompleteAreaList(areaCode: string) { | 
|     return areaList.value.find((x) => x.areaCode === areaCode); | 
|   } | 
|   | 
|   function getAreaByAreaCode(areaCode: string) { | 
|     return areaList.value.find((x) => x.areaCode === areaCode); | 
|   } | 
|   | 
|   return { | 
|     completeAreaList: areaList, | 
|     areaList, | 
|     // 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 { data: areaTree } = useQuery({ | 
|     queryKey: ['dictionaryServices/getAreaSelect', maxLayer], | 
|     queryFn: () => { | 
|       return dictionaryServices.getAreaSelect( | 
|         { | 
|           maxDeep: unref(maxLayer), | 
|         }, | 
|         { showLoading: false } | 
|       ); | 
|     }, | 
|     placeholderData: () => [] as API.GetAreaSelectQueryResultOption[], | 
|     staleTime: Infinity, | 
|   }); | 
|   | 
|   // const areaTree = computed(() => formatAreaListToTree(areaList.value, null, unref(maxLayer))); | 
|   console.log('areaTree: ', areaTree); | 
|   | 
|   return { areaTree: areaTree }; | 
| } | 
|   | 
| function convertDictionaryToAreaTreeNode( | 
|   item: API.SelectOptionStringGetDictionaryDataSelectQueryResultOption | 
| ) { | 
|   return { | 
|     areaCode: item.data?.code, | 
|     parentCode: item.data?.parentCode, | 
|     areaName: item.label, | 
|     layer: Number(item.data?.field4), | 
|     quickQuery: item.data?.field2, | 
|     sort: item.data?.sort, | 
|     id: item.data?.id, | 
|   } as API.AreaTreeNode; | 
| } | 
|   | 
| export function useAllAreaList() { | 
|   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); | 
|     return areaItem?.areaCode ?? ''; | 
|   }; | 
|   | 
|   const findAreaNameFromCode = (areaCode: string) => { | 
|     const areaItem = areaList.value.find((x) => x.areaCode === areaCode); | 
|     return areaItem?.areaName ?? ''; | 
|   }; | 
|   | 
|   const findAreaItemFromCode = (areaCode: string) => { | 
|     const areaItem = areaList.value.find((x) => x.areaCode === areaCode); | 
|     return areaItem; | 
|   }; | 
|   | 
|   return { | 
|     areaList, | 
|     findAreaCodeFromName, | 
|     findAreaNameFromCode, | 
|     findAreaItemFromCode, | 
|   }; | 
| } | 
|   | 
| // export function useProvinceList() { | 
| //   const { provinceList } = useArea(); | 
|   | 
| //   return { | 
| //     provinceList, | 
| //   }; | 
| // } | 
|   | 
| export const globalEventEmitter = new Taro.Events(); | 
|   | 
| export function useEvent(eventName: string, fn: TaroGeneral.EventCallback) { | 
|   onMounted(() => { | 
|     if (globalEventEmitter?.on) { | 
|       globalEventEmitter.on(eventName, fn); | 
|     } | 
|   }); | 
|   | 
|   onUnmounted(() => { | 
|     if (globalEventEmitter?.off) { | 
|       globalEventEmitter.off(eventName, fn); | 
|     } | 
|   }); | 
| } |