address_parse.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. const WXAPI = require('apifm-wxapi')
  2. var addressList = []; //地址列表
  3. var smartObj = {};
  4. /**
  5. * 解析邮编
  6. * @param event识别的地址
  7. * @returns <obj>
  8. */
  9. async function smart(event) {
  10. console.log('event1', event);
  11. event = stripscript(event); //过滤特殊字符
  12. console.log('event2', event);
  13. let obj = {};
  14. let copyaddress = JSON.parse(JSON.stringify(event));
  15. console.log('copyaddress', copyaddress);
  16. copyaddress = copyaddress.split(" ");
  17. console.log('地址转为数组:', copyaddress);
  18. for (let index = 0; index < copyaddress.length; index++) {
  19. const res = copyaddress[index];
  20. if (res) {
  21. if (res.length == 1) {
  22. res += "XX"; // 过滤掉一位的名字或者地址
  23. }
  24. let addressObj = await smatrAddress(res);
  25. obj = Object.assign(obj, addressObj);
  26. if (JSON.stringify(addressObj) === "{}") {
  27. obj.name = res.replace("XX", "");
  28. }
  29. }
  30. }
  31. return obj;
  32. }
  33. async function smatrAddress(event) {
  34. smartObj = {};
  35. let address = event;
  36. //address= event.replace(/\s/g, ''); //去除空格
  37. address = stripscript(address); //过滤特殊字符
  38. //身份证号匹配
  39. if (IdentityCodeValid(address)) {
  40. smartObj.idCard = address;
  41. address = address.replace(address, "");
  42. }
  43. //电话匹配
  44. let phone = address.match(
  45. /(86-[1][0-9]{10}) | (86[1][0-9]{10})|([1][0-9]{10})/g
  46. );
  47. if (phone) {
  48. smartObj.phone = phone[0];
  49. address = address.replace(phone[0], "");
  50. }
  51. console.log('smatrAddress:', address);
  52. let matchAddress = "";
  53. //省匹配 比如输入北京市朝阳区,会用北 北京 北京市 北京市朝 以此类推在addressList里的province中做匹配,会得到北京市 河北省 天津市等等;
  54. const resProvince = await WXAPI.province()
  55. const provinceList = resProvince.data
  56. let matchProvince = []; //粗略匹配上的省份
  57. // for (let begIndex = 0; begIndex < address.length; begIndex++) {
  58. matchAddress = "";
  59. for (let endIndex = 0; endIndex < address.length; endIndex++) {
  60. // if (endIndex > begIndex) {
  61. matchAddress = address.slice(0, endIndex + 2);
  62. provinceList.forEach(res => {
  63. if (res.name.indexOf(matchAddress) != -1) {
  64. matchProvince.push({
  65. province: res.name,
  66. provinceCode: res.id,
  67. matchValue: matchAddress
  68. });
  69. }
  70. });
  71. // }
  72. }
  73. // }
  74. //统计筛选初略统计出的省份
  75. matchProvince.forEach(res => {
  76. res.index = 0;
  77. matchProvince.forEach(el => {
  78. if (res.province == el.province) {
  79. el.index++;
  80. if (res.matchValue.length > el.matchValue.length) {
  81. el.matchValue = res.matchValue;
  82. }
  83. }
  84. });
  85. });
  86. if (matchProvince.length != 0) {
  87. let province = matchProvince.reduce((p, v) => (p.index < v.index ? v : p));
  88. smartObj.province = province.province;
  89. smartObj.provinceCode = province.provinceCode;
  90. address = address.replace(province.matchValue, "");
  91. }
  92. //市查找
  93. const resCity = await WXAPI.city()
  94. let cityList = resCity.data
  95. if (smartObj.provinceCode) {
  96. cityList = resCity.data.filter(ele => {
  97. return ele.pid == smartObj.provinceCode
  98. })
  99. }
  100. let matchCity = []; //粗略匹配上的市
  101. matchAddress = "";
  102. for (let endIndex = 0; endIndex < address.length; endIndex++) {
  103. matchAddress = address.slice(0, endIndex + 2)
  104. cityList.forEach(res => {
  105. if (res.name.indexOf(matchAddress) != -1) {
  106. matchCity.push({
  107. city: res.name,
  108. cityCode: res.id,
  109. pid: res.pid,
  110. matchValue: matchAddress,
  111. province: smartObj.province,
  112. provinceCode: smartObj.provinceCode
  113. });
  114. }
  115. });
  116. }
  117. //统计筛选初略统计出的市
  118. matchCity.forEach(res => {
  119. res.index = 0;
  120. matchCity.forEach(el => {
  121. if (res.city == el.city) {
  122. el.index++;
  123. if (res.matchValue.length > el.matchValue.length) {
  124. el.matchValue = res.matchValue;
  125. }
  126. }
  127. });
  128. });
  129. // 直辖市处理
  130. if (smartObj.province == '北京市') {
  131. matchCity.push({
  132. city: '北京市',
  133. cityCode: '110100000000',
  134. matchValue: '',
  135. province: smartObj.province,
  136. provinceCode: smartObj.provinceCode
  137. });
  138. }
  139. if (smartObj.province == '上海市') {
  140. matchCity.push({
  141. city: '上海市',
  142. cityCode: '310100000000',
  143. matchValue: '',
  144. province: smartObj.province,
  145. provinceCode: smartObj.provinceCode
  146. });
  147. }
  148. if (smartObj.province == '天津市') {
  149. matchCity.push({
  150. city: '天津市',
  151. cityCode: '120100000000',
  152. matchValue: '',
  153. province: smartObj.province,
  154. provinceCode: smartObj.provinceCode
  155. });
  156. }
  157. if (smartObj.province == '重庆市') {
  158. matchCity.push({
  159. city: '重庆市',
  160. cityCode: '500100000000',
  161. matchValue: '',
  162. province: smartObj.province,
  163. provinceCode: smartObj.provinceCode
  164. });
  165. }
  166. if (matchCity.length != 0) {
  167. let city = matchCity.reduce((p, v) => (p.index < v.index ? v : p));
  168. smartObj.city = city.city;
  169. smartObj.cityCode = city.cityCode;
  170. // smartObj.county = city.county;
  171. // smartObj.countyCode = city.countyCode;
  172. if (!smartObj.province) {
  173. const _province = provinceList.find(ele => {
  174. return ele.id == city.pid
  175. })
  176. smartObj.province = _province.name;
  177. smartObj.provinceCode = city.pid;
  178. }
  179. address = address.replace(city.matchValue, "");
  180. }
  181. //区县查找
  182. const resDistricts = await WXAPI.districts()
  183. let districtList = resDistricts.data
  184. if (smartObj.cityCode) {
  185. districtList = resDistricts.data.filter(ele => {
  186. return ele.pid == smartObj.cityCode
  187. })
  188. }
  189. console.log('smartObj', smartObj);
  190. let matchCounty = []; //粗略匹配上的区县
  191. matchAddress = "";
  192. for (let endIndex = 0; endIndex < address.length; endIndex++) {
  193. matchAddress = address.slice(0, endIndex + 2);
  194. districtList.forEach(res => {
  195. if (res.name.indexOf(matchAddress) != -1) {
  196. matchCounty.push({
  197. county: res.name,
  198. countyCode: res.id,
  199. city: smartObj.city,
  200. cityCode: smartObj.cityCode,
  201. matchValue: matchAddress,
  202. province: smartObj.province,
  203. provinceCode: smartObj.provinceCode
  204. });
  205. }
  206. });
  207. }
  208. //统计筛选初略统计出的区县
  209. matchCounty.forEach(res => {
  210. res.index = 0;
  211. matchCounty.forEach(el => {
  212. if (res.city == el.city) {
  213. el.index++;
  214. if (res.matchValue.length > el.matchValue.length) {
  215. el.matchValue = res.matchValue;
  216. }
  217. }
  218. });
  219. });
  220. if (matchCounty.length != 0) {
  221. let city = matchCounty.reduce((p, v) => (p.index < v.index ? v : p));
  222. smartObj.county = city.county;
  223. smartObj.countyCode = city.countyCode;
  224. if (!smartObj.province) {
  225. smartObj.province = city.province;
  226. smartObj.provinceCode = city.provinceCode;
  227. }
  228. if (!smartObj.city) {
  229. smartObj.city = city.city;
  230. smartObj.cityCode = city.cityCode;
  231. }
  232. address = address.replace(city.matchValue, "");
  233. }
  234. //街道查找
  235. let matchStreet = []; //粗略匹配上的街道查
  236. matchAddress = "";
  237. for (let endIndex = 0; endIndex < address.length; endIndex++) {
  238. matchAddress = address.slice(0, endIndex + 3);
  239. addressList.forEach(el => {
  240. if (el.name == smartObj.province) {
  241. if (
  242. smartObj.province == "北京市" ||
  243. smartObj.province == "天津市" ||
  244. smartObj.province == "上海市" ||
  245. smartObj.province == "重庆市"
  246. ) {
  247. //nothing
  248. } else {
  249. el.children.forEach(element => {
  250. if (element.name == smartObj.city) {
  251. element.children.forEach(item => {
  252. if (item.name == smartObj.county) {
  253. item.children.forEach(res => {
  254. if (res["street"].indexOf(matchAddress) != -1) {
  255. matchStreet.push({
  256. street: res.street,
  257. streetCode: res.code,
  258. matchValue: matchAddress
  259. });
  260. }
  261. });
  262. }
  263. });
  264. }
  265. });
  266. }
  267. }
  268. });
  269. }
  270. //统计筛选初略统计出的区县
  271. matchStreet.forEach(res => {
  272. res.index = 0;
  273. matchStreet.forEach(el => {
  274. if (res.city == el.city) {
  275. el.index++;
  276. if (res.matchValue.length > el.matchValue.length) {
  277. el.matchValue = res.matchValue;
  278. }
  279. }
  280. });
  281. });
  282. if (matchStreet.length != 0) {
  283. let city = matchStreet.reduce((p, v) => (p.index < v.index ? v : p));
  284. smartObj.street = city.street;
  285. smartObj.streetCode = city.streetCode;
  286. address = address.replace(city.matchValue, "");
  287. }
  288. //姓名查找
  289. if (smartObj.province) {
  290. smartObj.address = address;
  291. }
  292. return smartObj;
  293. }
  294. ////过滤特殊字符
  295. function stripscript(s) {
  296. s = s.replace(/\t/g, " ")
  297. s = s.replace(/(\d{3})-(\d{4})-(\d{4})/g, "$1$2$3");
  298. s = s.replace(/(\d{3}) (\d{4}) (\d{4})/g, "$1$2$3");
  299. var pattern = new RegExp(
  300. "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“’。,、?-]"
  301. );
  302. var rs = "";
  303. for (var i = 0; i < s.length; i++) {
  304. rs = rs + s.substr(i, 1).replace(pattern, " ");
  305. }
  306. rs = rs.replace(/[\r\n]/g, "");
  307. return rs;
  308. }
  309. function IdentityCodeValid(code) {
  310. let pass;
  311. var city = {
  312. 11: "北京",
  313. 12: "天津",
  314. 13: "河北",
  315. 14: "山西",
  316. 15: "内蒙古",
  317. 21: "辽宁",
  318. 22: "吉林",
  319. 23: "黑龙江 ",
  320. 31: "上海",
  321. 32: "江苏",
  322. 33: "浙江",
  323. 34: "安徽",
  324. 35: "福建",
  325. 36: "江西",
  326. 37: "山东",
  327. 41: "河南",
  328. 42: "湖北 ",
  329. 43: "湖南",
  330. 44: "广东",
  331. 45: "广西",
  332. 46: "海南",
  333. 50: "重庆",
  334. 51: "四川",
  335. 52: "贵州",
  336. 53: "云南",
  337. 54: "西藏 ",
  338. 61: "陕西",
  339. 62: "甘肃",
  340. 63: "青海",
  341. 64: "宁夏",
  342. 65: "新疆",
  343. 71: "台湾",
  344. 81: "香港",
  345. 82: "澳门",
  346. 91: "国外 "
  347. };
  348. var tip = "";
  349. pass = true;
  350. if (!code || !/^\d{17}(\d|X)$/i.test(code)) {
  351. tip = "身份证号格式错误";
  352. pass = false;
  353. } else if (!city[code.substr(0, 2)]) {
  354. tip = "地址编码错误";
  355. pass = false;
  356. } else {
  357. //18位身份证需要验证最后一位校验位
  358. if (code.length == 18) {
  359. code = code.split("");
  360. //∑(ai×Wi)(mod 11)
  361. //加权因子
  362. var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  363. //校验位
  364. var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
  365. var sum = 0;
  366. var ai = 0;
  367. var wi = 0;
  368. for (var i = 0; i < 17; i++) {
  369. ai = code[i];
  370. wi = factor[i];
  371. sum += ai * wi;
  372. }
  373. var last = parity[sum % 11];
  374. if (parity[sum % 11] != code[17]) {
  375. tip = "校验位错误";
  376. pass = false;
  377. }
  378. }
  379. }
  380. return pass;
  381. }
  382. module.exports = {
  383. smart: smart
  384. };