address_parse.js
12.2 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
const WXAPI = require("apifm-wxapi");
const addressList = []; // 地址列表
let smartObj = {};
/**
* 解析邮编
* @param event 识别的地址
* @returns <obj>
*/
async function smart(event) {
console.log("event1", event);
event = stripscript(event); // 过滤特殊字符
console.log("event2", event);
let obj = {};
let copyaddress = structuredClone(JSON.stringify(event));
console.log("copyaddress", copyaddress);
copyaddress = copyaddress.split(" ");
console.log("地址转为数组:", copyaddress);
for (let res of copyaddress) {
if (res) {
if (res.length === 1) {
res += "XX"; // 过滤掉一位的名字或者地址
}
const addressObj = await smatrAddress(res);
obj = Object.assign(obj, addressObj);
if (JSON.stringify(addressObj) === "{}") {
obj.name = res.replace("XX", "");
}
}
}
return obj;
}
async function smatrAddress(event) {
smartObj = {};
let address = event;
// address= event.replace(/\s/g, ''); //去除空格
address = stripscript(address); // 过滤特殊字符
// 身份证号匹配
if (IdentityCodeValid(address)) {
smartObj.idCard = address;
address = address.replace(address, "");
}
// 电话匹配
const phone = address.match(
/(86-1\d{10}) | (861\d{10})|(1\d{10})/g
);
if (phone) {
smartObj.phone = phone[0];
address = address.replace(phone[0], "");
}
console.log("smatrAddress:", address);
let matchAddress = "";
// 省匹配 比如输入北京市朝阳区,会用北 北京 北京市 北京市朝 以此类推在addressList里的province中做匹配,会得到北京市 河北省 天津市等等;
const resProvince = await WXAPI.provinceV2();
const provinceList = resProvince.data;
const matchProvince = []; // 粗略匹配上的省份
// for (let begIndex = 0; begIndex < address.length; begIndex++) {
matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) {
// if (endIndex > begIndex) {
matchAddress = address.slice(0, endIndex + 2);
provinceList.forEach(res => {
if (res.name.includes(matchAddress)) {
matchProvince.push({
province: res.name,
provinceCode: res.id,
matchValue: matchAddress
});
}
});
// }
}
// }
// 统计筛选初略统计出的省份
matchProvince.forEach(res => {
res.index = 0;
matchProvince.forEach(el => {
if (res.province === el.province) {
el.index++;
if (res.matchValue.length > el.matchValue.length) {
el.matchValue = res.matchValue;
}
}
});
});
if (matchProvince.length > 0) {
smartObj.province = province.province;
smartObj.provinceCode = province.provinceCode;
address = address.replace(province.matchValue, "");
}
// 市查找
const resCity = await WXAPI.cityV2();
let cityList = resCity.data;
if (smartObj.provinceCode) {
cityList = resCity.data.filter(ele => {
return ele.pid === smartObj.provinceCode;
});
}
const matchCity = []; // 粗略匹配上的市
matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 2);
cityList.forEach(res => {
if (res.name.includes(matchAddress)) {
matchCity.push({
city: res.name,
cityCode: res.id,
pid: res.pid,
matchValue: matchAddress,
province: smartObj.province,
provinceCode: smartObj.provinceCode
});
}
});
}
// 统计筛选初略统计出的市
matchCity.forEach(res => {
res.index = 0;
matchCity.forEach(el => {
if (res.city === el.city) {
el.index++;
if (res.matchValue.length > el.matchValue.length) {
el.matchValue = res.matchValue;
}
}
});
});
// 直辖市处理
if (smartObj.province === "北京市") {
matchCity.push({
city: "北京市",
cityCode: "110100000000",
matchValue: "",
province: smartObj.province,
provinceCode: smartObj.provinceCode
});
}
if (smartObj.province === "上海市") {
matchCity.push({
city: "上海市",
cityCode: "310100000000",
matchValue: "",
province: smartObj.province,
provinceCode: smartObj.provinceCode
});
}
if (smartObj.province === "天津市") {
matchCity.push({
city: "天津市",
cityCode: "120100000000",
matchValue: "",
province: smartObj.province,
provinceCode: smartObj.provinceCode
});
}
if (smartObj.province === "重庆市") {
matchCity.push({
city: "重庆市",
cityCode: "500100000000",
matchValue: "",
province: smartObj.province,
provinceCode: smartObj.provinceCode
});
}
if (matchCity.length > 0) {
smartObj.city = city.city;
smartObj.cityCode = city.cityCode;
// smartObj.county = city.county;
// smartObj.countyCode = city.countyCode;
if (!smartObj.province) {
const _province = provinceList.find(ele => {
return ele.id === city.pid;
});
smartObj.province = _province.name;
smartObj.provinceCode = city.pid;
}
address = address.replace(city.matchValue, "");
}
// 区县查找
const resDistricts = await WXAPI.districts();
let districtList = resDistricts.data;
if (smartObj.cityCode) {
districtList = resDistricts.data.filter(ele => {
return ele.pid === smartObj.cityCode;
});
}
console.log("smartObj", smartObj);
const matchCounty = []; // 粗略匹配上的区县
matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 2);
districtList.forEach(res => {
if (res.name.includes(matchAddress)) {
matchCounty.push({
county: res.name,
countyCode: res.id,
city: smartObj.city,
cityCode: smartObj.cityCode,
matchValue: matchAddress,
province: smartObj.province,
provinceCode: smartObj.provinceCode
});
}
});
}
// 统计筛选初略统计出的区县
matchCounty.forEach(res => {
res.index = 0;
matchCounty.forEach(el => {
if (res.city === el.city) {
el.index++;
if (res.matchValue.length > el.matchValue.length) {
el.matchValue = res.matchValue;
}
}
});
});
if (matchCounty.length > 0) {
const city ={};
smartObj.county = city.county;
smartObj.countyCode = city.countyCode;
if (!smartObj.province) {
smartObj.province = city.province;
smartObj.provinceCode = city.provinceCode;
}
if (!smartObj.city) {
smartObj.city = city.city;
smartObj.cityCode = city.cityCode;
}
address = address.replace(city.matchValue, "");
}
// 街道查找
const matchStreet = []; // 粗略匹配上的街道查
matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 3);
addressList.forEach(el => {
if (el.name === smartObj.province) {
if (
smartObj.province === "北京市" ||
smartObj.province === "天津市" ||
smartObj.province === "上海市" ||
smartObj.province === "重庆市"
) {
// nothing
} else {
el.children.forEach(element => {
if (element.name === smartObj.city) {
element.children.forEach(item => {
if (item.name === smartObj.county) {
item.children.forEach(res => {
if (res.street.includes(matchAddress)) {
matchStreet.push({
street: res.street,
streetCode: res.code,
matchValue: matchAddress
});
}
});
}
});
}
});
}
}
});
}
// 统计筛选初略统计出的区县
matchStreet.forEach(res => {
res.index = 0;
matchStreet.forEach(el => {
if (res.city === el.city) {
el.index++;
if (res.matchValue.length > el.matchValue.length) {
el.matchValue = res.matchValue;
}
}
});
});
if (matchStreet.length > 0) {
const city = {};
smartObj.street = city.street;
smartObj.streetCode = city.streetCode;
address = address.replace(city.matchValue, "");
}
// 姓名查找
if (smartObj.province) {
smartObj.address = address;
}
return smartObj;
}
// //过滤特殊字符
function stripscript(s) {
s = s.replaceAll("\t", " ");
s = s.replaceAll(/(\d{3})-(\d{4})-(\d{4})/g, "$1$2$3");
s = s.replaceAll(/(\d{3}) (\d{4}) (\d{4})/g, "$1$2$3");
const pattern = new RegExp();
let rs = "";
for (let i = 0; i < s.length; i++) {
rs = rs + s.slice(i, 1).replace(pattern, " ");
}
rs = rs.replaceAll(/[\r\n]/g, "");
return rs;
}
function IdentityCodeValid(code) {
let pass;
const city = {
11: "北京",
12: "天津",
13: "河北",
14: "山西",
15: "内蒙古",
21: "辽宁",
22: "吉林",
23: "黑龙江 ",
31: "上海",
32: "江苏",
33: "浙江",
34: "安徽",
35: "福建",
36: "江西",
37: "山东",
41: "河南",
42: "湖北 ",
43: "湖南",
44: "广东",
45: "广西",
46: "海南",
50: "重庆",
51: "四川",
52: "贵州",
53: "云南",
54: "西藏 ",
61: "陕西",
62: "甘肃",
63: "青海",
64: "宁夏",
65: "新疆",
71: "台湾",
81: "香港",
82: "澳门",
91: "国外 "
};
let tip = "";
pass = true;
if (!code || !/^\d{17}[\dX]$/i.test(code)) {
tip = "身份证号格式错误";
pass = false;
} else if (city[code.slice(0, 2)]) {
// 18位身份证需要验证最后一位校验位
if (code.length === 18) {
code = code.split("");
// ∑(ai×Wi)(mod 11)
// 加权因子
const factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
// 校验位
const parity = [ 1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2 ];
let sum = 0;
let ai = 0;
let wi = 0;
for (let i = 0; i < 17; i++) {
ai = code[i];
wi = factor[i];
sum += ai * wi;
}
const last = parity[sum % 11];
if (parity[sum % 11] !== code[17]) {
tip = "校验位错误";
pass = false;
}
}
} else {
tip = "地址编码错误";
pass = false;
}
return pass;
}
module.exports = {
smart
};