riku
2024-11-12 befd1f21839939b54254bd316bbd158b136fcd15
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const app = getApp();
// 切换账号时,重置可选行政区划
app.globalData.onLaunch.push(() => {
  remoteLocations = [];
});
 
const _locations = [
  {
    label: '上海市',
    value: '31',
    children: [
      {
        label: '上海市',
        value: '3100',
        children: [
          {
            label: '全部区县',
            value: '-3100',
            children: [{ label: '全部街镇', value: '-310000' }],
          },
          {
            label: '金山区',
            value: '310116',
            children: [
              { label: '全部街镇', value: '-310116' },
              { label: '张堰镇', value: '310116103' },
              { label: '亭林镇', value: '310116104' },
              { label: '吕巷镇', value: '310116105' },
              { label: '廊下镇', value: '310116107' },
              { label: '高新区', value: '310116503' },
              { label: '金山卫镇', value: '310116109' },
              { label: '漕泾镇', value: '310116112' },
              { label: '山阳镇', value: '310116113' },
              { label: '石化街道', value: '310116001' },
              { label: '朱泾镇', value: '310116101' },
              { label: '枫泾镇', value: '310116102' },
              { label: '碳谷绿湾', value: '9000' },
            ],
          },
          {
            label: '徐汇区',
            value: '310104',
            children: [
              { label: '全部街镇', value: '-310104' },
              { label: '漕河泾新兴技术开发区', value: '310104501' },
              { label: '湖南路街道', value: '310104004' },
              { label: '天平路街道', value: '310104003' },
              { label: '虹梅路街道', value: '310104012' },
              { label: '枫林路街道', value: '310104008' },
              { label: '斜土路街道', value: '310104007' },
              { label: '长桥街道', value: '310104010' },
              { label: '田林街道', value: '310104011' },
              { label: '康健新村街道', value: '310104013' },
              { label: '徐家汇街道', value: '310104014' },
              { label: '凌云路街道', value: '310104015' },
              { label: '龙华街道', value: '310104016' },
              { label: '漕河泾街道', value: '310104017' },
              { label: '华泾镇', value: '310104103' },
            ],
          },
          {
            label: '静安区',
            value: '310106',
            children: [
              { label: '全部街镇', value: '-310106' },
              { label: '大宁路街道', value: '310106019' },
              { label: '彭浦新村街道', value: '310106020' },
              { label: '临汾路街道', value: '310106021' },
              { label: '芷江西路街道', value: '310106022' },
              { label: '彭浦镇', value: '310106101' },
              { label: '江宁路街道', value: '310106006' },
              { label: '石门二路街道', value: '310106011' },
              { label: '南京西路街道', value: '310106012' },
              { label: '静安寺街道', value: '310106013' },
              { label: '曹家渡街道', value: '310106014' },
              { label: '天目西路街道', value: '310106015' },
              { label: '北站街道', value: '310106016' },
              { label: '宝山路街道', value: '310106017' },
              { label: '共和新路街道', value: '310106018' },
            ],
          },
          {
            label: '普陀区',
            value: '310107',
            children: [
              { label: '全部街镇', value: '-310107' },
              { label: '曹杨新村街道', value: '310107005' },
              { label: '万里街道', value: '310107021' },
              { label: '真如镇街道', value: '310107022' },
              { label: '长征镇', value: '310107102' },
              { label: '桃浦镇', value: '310107103' },
              { label: '石泉路街道', value: '310107017' },
              { label: '甘泉路街道', value: '310107016' },
              { label: '长寿路街道', value: '310107015' },
              { label: '长风新村街道', value: '310107014' },
              { label: '宜川路街道', value: '310107020' },
            ],
          },
          {
            label: '闵行区',
            value: '310112',
            children: [
              { label: '全部街镇', value: '-310112' },
              { label: '江川路街道', value: '310112001' },
              { label: '古美街道', value: '310112006' },
              { label: '新虹街道', value: '310112008' },
              { label: '浦锦街道', value: '310112009' },
              { label: '莘庄镇', value: '310112101' },
              { label: '七宝镇', value: '310112102' },
              { label: '颛桥镇', value: '310112103' },
              { label: '华漕镇', value: '310112106' },
              { label: '虹桥镇', value: '310112107' },
              { label: '梅陇镇', value: '310112108' },
              { label: '吴泾镇', value: '310112110' },
              { label: '马桥镇', value: '310112112' },
              { label: '浦江镇', value: '310112114' },
              { label: '莘庄工业区', value: '310112501' },
            ],
          },
          {
            label: '长宁区',
            value: '310105',
            children: [
              { label: '全部街镇', value: '-310105' },
              { label: '华阳路街道', value: '310105001' },
              { label: '江苏路街道', value: '310105002' },
              { label: '新华路街道', value: '310105004' },
              { label: '周家桥街道', value: '310105005' },
              { label: '天山路街道', value: '310105006' },
              { label: '仙霞新村街道', value: '310105008' },
              { label: '虹桥街道', value: '310105009' },
              { label: '程家桥街道', value: '310105010' },
              { label: '北新泾街道', value: '310105011' },
              { label: '新泾镇', value: '310105102' },
            ],
          },
        ],
      },
    ],
  },
];
 
var remoteLocations = [];
 
function locations() {
  if (remoteLocations.length > 0) return remoteLocations;
  // 获取用户配置信息
  const userConfig = app.globalData.userConfig;
  if (!userConfig) {
    remoteLocations = _locations;
    return remoteLocations;
  }
  let configList = [
    userConfig.ucProvinceCode,
    userConfig.ucCityCode,
    userConfig.ucDistrictCode,
    userConfig.ucTownCode,
  ];
  remoteLocations = _recursiveSearch(configList, 0, _locations);
  return remoteLocations;
}
 
/**
 * 递归查询行政区划
 */
function _recursiveSearch(conditions, cIndex, srcList, type = 'value') {
  let res;
  const condition = conditions[cIndex];
  if (condition) {
    for (let i = 0; i < srcList.length; i++) {
      const e = srcList[i];
      const option = type == 'value' ? e.value : e.label;
      if (option == condition) {
        if (cIndex < conditions.length - 1 && e.children) {
          const result = _recursiveSearch(conditions, ++cIndex, e.children);
          res = [
            {
              label: e.label,
              value: e.value,
              children: result,
            },
          ];
        } else {
          res = [
            {
              label: e.label,
              value: e.value,
            },
          ];
        }
        break;
      }
    }
    if (!res) res = srcList;
    return res;
  } else {
    return srcList;
  }
}
 
function defaultValue() {
  const _values = _getFirst([], locations());
  return _values;
}
 
function _getFirst(values, item) {
  if (item.length == 0) {
    return values;
  } else if (item[0].children) {
    _getFirst(values, item[0].children);
  }
  values.unshift({
    label: item[0].label,
    value: item[0].value,
  });
  return values;
}
 
function getTownValue(conditions, type = 'value') {
  const res = _recursiveSearch(conditions, 0, _locations, type);
  const fun = v => {
    if (v.children) {
      return fun(v.children);
    } else {
      return v.value;
    }
  };
  return fun(res);
}
export { locations, defaultValue, getTownValue };