wupengfei
8 天以前 b567da133517fb85c65c79f0fbb18f6871c6c1a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { flattenAreaTree, formatAreaListToTree } from '@12333/utils';
import { useQuery, useQueryClient } from '@tanstack/vue-query';
import { computed, onMounted, onUnmounted, ref } from 'vue';
import { AreaType, CategoryCode } from '@12333/constants';
import Taro, { EventChannel } from '@tarojs/taro';
import { useDictionaryDataSelect } from './dic';
 
export function useArea() {
  const queryClient = useQueryClient();
 
  const { dictionaryDataList } = useDictionaryDataSelect({
    categoryCode: CategoryCode.Area,
    staleTime: Infinity,
    all: true,
    maxDeep: AreaType.Area,
  });
 
  const areaList = computed(() => dictionaryDataList.value.map(convertDictionaryToAreaTreeNode));
 
  const areaTree = computed(() => formatAreaListToTree(areaList.value));
 
  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,
  };
}
 
function convertDictionaryToAreaTreeNode(
  item: API.SelectOptionStringGetDictionaryDataSelectQueryResultOption
) {
  return {
    children: [],
    areaCode: item.data?.code,
    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 { areaList } = useArea();
 
  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);
    }
  });
}