|
|
@@ -1,4 +1,4 @@
|
|
1
|
|
-import { getRegionTree } from '@/api/region'
|
|
|
1
|
+import cityCodeJson from '@/utils/json/cityCode.json'
|
|
2
|
2
|
|
|
3
|
3
|
/** 与平台 ruoyi-ui `utils/region.js` 逻辑一致 */
|
|
4
|
4
|
export function normalizeRegionTree(nodes) {
|
|
|
@@ -95,16 +95,57 @@ export function formatRegionDisplay(regionName) {
|
|
95
|
95
|
return (regionName || '').replace(/\//g, ' ')
|
|
96
|
96
|
}
|
|
97
|
97
|
|
|
|
98
|
+/**
|
|
|
99
|
+ * 将 cityCode.json 转为三级树(与后端 RegionTreeVO 结构一致)
|
|
|
100
|
+ */
|
|
|
101
|
+export function convertCityCodeToRegionTree(cityCodeList) {
|
|
|
102
|
+ return (cityCodeList || []).map((province) => {
|
|
|
103
|
+ const provinceCode = String(province.code)
|
|
|
104
|
+ const cities = (province.cityList || []).map((city) => {
|
|
|
105
|
+ const cityCode = String(city.code)
|
|
|
106
|
+ const areas = (city.areaList || []).map((area) => ({
|
|
|
107
|
+ code: String(area.code),
|
|
|
108
|
+ name: area.name,
|
|
|
109
|
+ type: 3
|
|
|
110
|
+ }))
|
|
|
111
|
+ const cityNode = {
|
|
|
112
|
+ code: cityCode,
|
|
|
113
|
+ name: city.name,
|
|
|
114
|
+ type: 2
|
|
|
115
|
+ }
|
|
|
116
|
+ if (areas.length) {
|
|
|
117
|
+ cityNode.children = areas
|
|
|
118
|
+ }
|
|
|
119
|
+ return cityNode
|
|
|
120
|
+ })
|
|
|
121
|
+ const provinceNode = {
|
|
|
122
|
+ code: provinceCode,
|
|
|
123
|
+ name: province.name,
|
|
|
124
|
+ type: 1
|
|
|
125
|
+ }
|
|
|
126
|
+ if (cities.length) {
|
|
|
127
|
+ provinceNode.children = cities
|
|
|
128
|
+ }
|
|
|
129
|
+ return provinceNode
|
|
|
130
|
+ })
|
|
|
131
|
+}
|
|
|
132
|
+
|
|
98
|
133
|
let cachedRawTree = null
|
|
99
|
134
|
let cachedCascaderTree = null
|
|
100
|
135
|
|
|
101
|
|
-/** 加载并缓存级联树(normalize 后供 up-cascader) */
|
|
102
|
|
-export async function loadRegionCascaderTree(force = false) {
|
|
|
136
|
+function getRegionTreeFromCityCode() {
|
|
|
137
|
+ if (!cachedRawTree) {
|
|
|
138
|
+ cachedRawTree = convertCityCodeToRegionTree(cityCodeJson)
|
|
|
139
|
+ }
|
|
|
140
|
+ return cachedRawTree
|
|
|
141
|
+}
|
|
|
142
|
+
|
|
|
143
|
+/** 加载并缓存级联树(本地 cityCode.json,normalize 后供 up-cascader) */
|
|
|
144
|
+export function loadRegionCascaderTree(force = false) {
|
|
103
|
145
|
if (!force && cachedCascaderTree) {
|
|
104
|
|
- return { raw: cachedRawTree, cascader: cachedCascaderTree }
|
|
|
146
|
+ return Promise.resolve({ raw: cachedRawTree, cascader: cachedCascaderTree })
|
|
105
|
147
|
}
|
|
106
|
|
- const res = await getRegionTree()
|
|
107
|
|
- cachedRawTree = res.data || []
|
|
|
148
|
+ cachedRawTree = getRegionTreeFromCityCode()
|
|
108
|
149
|
cachedCascaderTree = normalizeRegionTree(cachedRawTree)
|
|
109
|
|
- return { raw: cachedRawTree, cascader: cachedCascaderTree }
|
|
|
150
|
+ return Promise.resolve({ raw: cachedRawTree, cascader: cachedCascaderTree })
|
|
110
|
151
|
}
|