格式化代码

This commit is contained in:
wangzhichao
2020-04-20 19:18:01 +08:00
parent ee11dca971
commit 17d61c8032

View File

@@ -1,62 +1,37 @@
var addressList = []; //地址列表 var addressList = []; //地址列表
var zipCodeList = []; //邮编列表 var zipCodeList = []; //邮编列表
var zipCode = require("./zipCode.js");
console.log('正在加载省市区数据...') console.log("正在加载省市区数据...");
const wx_getaddress = () => { const wx_getaddress = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
wx.request({ wx.request({
url: 'https://api.weixin.qq.com/tcb/databasequery?access_token=32_tfGuNB4ZZ42kgDKxXEkQqkFFS15LLvGxHl3-nIpwbNoxRfz26vmwgXSNQy9EpbtJ1DH1nE0pxV6_qb2tSyghmc8oah6LJoMcycfVgismyu1OMFdzZS-_FXyzBl1ZLFiFRmWkwgC8g2PROTb6KMFgAFAZBU', url:
"https://api.weixin.qq.com/tcb/databasequery?access_token=32_tfGuNB4ZZ42kgDKxXEkQqkFFS15LLvGxHl3-nIpwbNoxRfz26vmwgXSNQy9EpbtJ1DH1nE0pxV6_qb2tSyghmc8oah6LJoMcycfVgismyu1OMFdzZS-_FXyzBl1ZLFiFRmWkwgC8g2PROTb6KMFgAFAZBU",
data: { data: {
"env": "wangzhichao", env: "wangzhichao",
"query": "db.collection('address').where({})\n.get()" query: "db.collection('address').where({})\n.get()"
}, },
method: 'POST', method: "POST",
success: function (res) { success: function(res) {
let data = JSON.parse(res.data.data[0]).data let data = JSON.parse(res.data.data[0]).data;
let arr = []; let arr = [];
data.forEach(e => { data.forEach(e => {
arr.push(JSON.parse(e)) arr.push(JSON.parse(e));
}) });
resolve(arr) resolve(arr);
} }
}) });
}); });
}; };
var smart ; wx_getaddress().then(res => {
wx_getaddress().then((res) => {
addressList = res; addressList = res;
addressList.forEach(item => { addressList.forEach(item => {
formatAddresList(item, 1, ''); formatAddresList(item, 1, "");
}); });
zipCodeList = zipCodeFormat(zipCode.variable); zipCodeList = zipCodeFormat(zipCode.variable);
console.log('省市区数据挂载完毕!!') console.log("省市区数据挂载完毕!!");
});
})
//var pcassCode = require("./pcasCode.js");
var zipCode = require("./zipCode.js");
//获取地址以及邮编json
/* const getJson = new Promise((res, rej) => {
$.getJSON("./json/pcas-code.json", data_address => {
$.getJSON("./json/zip-code.json", data_code => {
res({
'address': data_address,
'code': data_code
})
})
})
})
getJson.then((res) => {
addressList = res.address;
addressList.forEach(item => {
formatAddresList(item, 1, '')
})
zipCodeList = zipCodeFormat(res.code);
}) */
//addressList = pcassCode.variable;
/** /**
* 地址数据处理 * 地址数据处理
@@ -72,7 +47,7 @@ function formatAddresList(addressList, index, province) {
} }
if (index === 2) { if (index === 2) {
//市 //市
if (addressList.name == '市辖区') { if (addressList.name == "市辖区") {
addressList.name = province.name; addressList.name = province.name;
} }
addressList.city = addressList.name; addressList.city = addressList.name;
@@ -123,17 +98,17 @@ function smart(event) {
event = stripscript(event); //过滤特殊字符 event = stripscript(event); //过滤特殊字符
let obj = {}; let obj = {};
let copyaddress = JSON.parse(JSON.stringify(event)); let copyaddress = JSON.parse(JSON.stringify(event));
copyaddress = copyaddress.split(' '); copyaddress = copyaddress.split(" ");
copyaddress.forEach((res, index) => { copyaddress.forEach((res, index) => {
if (res) { if (res) {
if (res.length == 1) { if (res.length == 1) {
res += 'XX'; // 过滤掉一位的名字或者地址 res += "XX"; // 过滤掉一位的名字或者地址
} }
let addressObj = smatrAddress(res); let addressObj = smatrAddress(res);
obj = Object.assign(obj, addressObj); obj = Object.assign(obj, addressObj);
if (JSON.stringify(addressObj) === '{}') { if (JSON.stringify(addressObj) === "{}") {
obj.name = res.replace('XX', ''); obj.name = res.replace("XX", "");
} }
} }
}); });
@@ -148,7 +123,7 @@ function smatrAddress(event) {
//身份证号匹配 //身份证号匹配
if (IdentityCodeValid(address)) { if (IdentityCodeValid(address)) {
smartObj.idCard = address; smartObj.idCard = address;
address = address.replace(address, ''); address = address.replace(address, "");
} }
//电话匹配 //电话匹配
@@ -157,7 +132,7 @@ function smatrAddress(event) {
); );
if (phone) { if (phone) {
smartObj.phone = phone[0]; smartObj.phone = phone[0];
address = address.replace(phone[0], ''); address = address.replace(phone[0], "");
} }
//邮编匹配 //邮编匹配
@@ -166,19 +141,19 @@ function smatrAddress(event) {
let num = address.indexOf(res); let num = address.indexOf(res);
let code = address.slice(num, num + 6); let code = address.slice(num, num + 6);
smartObj.zipCode = code; smartObj.zipCode = code;
address = address.replace(code, ''); address = address.replace(code, "");
} }
}); });
let matchAddress = ''; let matchAddress = "";
//省匹配 比如输入北京市朝阳区,会用北 北京 北京市 北京市朝 以此类推在addressList里的province中做匹配会得到北京市 河北省 天津市等等; //省匹配 比如输入北京市朝阳区,会用北 北京 北京市 北京市朝 以此类推在addressList里的province中做匹配会得到北京市 河北省 天津市等等;
let matchProvince = []; //粗略匹配上的省份 let matchProvince = []; //粗略匹配上的省份
// for (let begIndex = 0; begIndex < address.length; begIndex++) { // for (let begIndex = 0; begIndex < address.length; begIndex++) {
matchAddress = ''; matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) { for (let endIndex = 0; endIndex < address.length; endIndex++) {
// if (endIndex > begIndex) { // if (endIndex > begIndex) {
matchAddress = address.slice(0, endIndex + 2); matchAddress = address.slice(0, endIndex + 2);
addressList.forEach(res => { addressList.forEach(res => {
if (res['province'].indexOf(matchAddress) != -1) { if (res["province"].indexOf(matchAddress) != -1) {
matchProvince.push({ matchProvince.push({
province: res.province, province: res.province,
provinceCode: res.code, provinceCode: res.code,
@@ -206,25 +181,25 @@ function smatrAddress(event) {
let province = matchProvince.reduce((p, v) => (p.index < v.index ? v : p)); let province = matchProvince.reduce((p, v) => (p.index < v.index ? v : p));
smartObj.province = province.province; smartObj.province = province.province;
smartObj.provinceCode = province.provinceCode; smartObj.provinceCode = province.provinceCode;
address = address.replace(province.matchValue, ''); address = address.replace(province.matchValue, "");
} }
//市查找 //市查找
let matchCity = []; //粗略匹配上的市 let matchCity = []; //粗略匹配上的市
matchAddress = ''; matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) { for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 2); matchAddress = address.slice(0, endIndex + 2);
addressList.forEach(el => { addressList.forEach(el => {
// if (el.name == smartObj.province) { // if (el.name == smartObj.province) {
if (el.code == smartObj.provinceCode || !smartObj.provinceCode) { if (el.code == smartObj.provinceCode || !smartObj.provinceCode) {
if ( if (
smartObj.province == '北京市' || smartObj.province == "北京市" ||
smartObj.province == '天津市' || smartObj.province == "天津市" ||
smartObj.province == '上海市' || smartObj.province == "上海市" ||
smartObj.province == '重庆市' smartObj.province == "重庆市"
) { ) {
el.children.forEach(item => { el.children.forEach(item => {
item.children.forEach(res => { item.children.forEach(res => {
if (res['county'].indexOf(matchAddress) != -1) { if (res["county"].indexOf(matchAddress) != -1) {
matchCity.push({ matchCity.push({
county: res.county, county: res.county,
countyCode: res.code, countyCode: res.code,
@@ -239,7 +214,7 @@ function smatrAddress(event) {
}); });
} else { } else {
el.children.forEach(res => { el.children.forEach(res => {
if (res['city'].indexOf(matchAddress) != -1) { if (res["city"].indexOf(matchAddress) != -1) {
matchCity.push({ matchCity.push({
city: res.city, city: res.city,
cityCode: res.code, cityCode: res.code,
@@ -277,29 +252,29 @@ function smatrAddress(event) {
smartObj.province = city.province; smartObj.province = city.province;
smartObj.provinceCode = city.provinceCode; smartObj.provinceCode = city.provinceCode;
} }
address = address.replace(city.matchValue, ''); address = address.replace(city.matchValue, "");
} }
//区县查找 //区县查找
let matchCounty = []; //粗略匹配上的区县 let matchCounty = []; //粗略匹配上的区县
matchAddress = ''; matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) { for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 2); matchAddress = address.slice(0, endIndex + 2);
addressList.forEach(el => { addressList.forEach(el => {
// if (el.name == smartObj.province) { // if (el.name == smartObj.province) {
if ( if (
smartObj.province == '北京市' || smartObj.province == "北京市" ||
smartObj.province == '天津市' || smartObj.province == "天津市" ||
smartObj.province == '上海市' || smartObj.province == "上海市" ||
smartObj.province == '重庆市' smartObj.province == "重庆市"
) { ) {
//nothing //nothing
} else { } else {
el.children.forEach(item => { el.children.forEach(item => {
// if (item.name == smartObj.city) { // if (item.name == smartObj.city) {
item.children.forEach(res => { item.children.forEach(res => {
if (res['county'].indexOf(matchAddress) != -1) { if (res["county"].indexOf(matchAddress) != -1) {
//省/市 || 省 //省/市 || 省
if (smartObj.province) { if (smartObj.province) {
if (res.code.slice(0, 2) == smartObj.provinceCode) { if (res.code.slice(0, 2) == smartObj.provinceCode) {
matchCounty.push({ matchCounty.push({
@@ -355,21 +330,21 @@ function smatrAddress(event) {
smartObj.city = city.city; smartObj.city = city.city;
smartObj.cityCode = city.cityCode; smartObj.cityCode = city.cityCode;
} }
address = address.replace(city.matchValue, ''); address = address.replace(city.matchValue, "");
} }
//街道查找 //街道查找
let matchStreet = []; //粗略匹配上的街道查 let matchStreet = []; //粗略匹配上的街道查
matchAddress = ''; matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) { for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 3); matchAddress = address.slice(0, endIndex + 3);
addressList.forEach(el => { addressList.forEach(el => {
if (el.name == smartObj.province) { if (el.name == smartObj.province) {
if ( if (
smartObj.province == '北京市' || smartObj.province == "北京市" ||
smartObj.province == '天津市' || smartObj.province == "天津市" ||
smartObj.province == '上海市' || smartObj.province == "上海市" ||
smartObj.province == '重庆市' smartObj.province == "重庆市"
) { ) {
//nothing //nothing
} else { } else {
@@ -378,7 +353,7 @@ function smatrAddress(event) {
element.children.forEach(item => { element.children.forEach(item => {
if (item.name == smartObj.county) { if (item.name == smartObj.county) {
item.children.forEach(res => { item.children.forEach(res => {
if (res['street'].indexOf(matchAddress) != -1) { if (res["street"].indexOf(matchAddress) != -1) {
matchStreet.push({ matchStreet.push({
street: res.street, street: res.street,
streetCode: res.code, streetCode: res.code,
@@ -412,7 +387,7 @@ function smatrAddress(event) {
let city = matchStreet.reduce((p, v) => (p.index < v.index ? v : p)); let city = matchStreet.reduce((p, v) => (p.index < v.index ? v : p));
smartObj.street = city.street; smartObj.street = city.street;
smartObj.streetCode = city.streetCode; smartObj.streetCode = city.streetCode;
address = address.replace(city.matchValue, ''); address = address.replace(city.matchValue, "");
} }
//姓名查找 //姓名查找
if (smartObj.province) { if (smartObj.province) {
@@ -423,76 +398,76 @@ function smatrAddress(event) {
} }
////过滤特殊字符 ////过滤特殊字符
function stripscript(s) { function stripscript(s) {
s = s.replace(/(\d{3})-(\d{4})-(\d{4})/g, '$1$2$3'); s = s.replace(/(\d{3})-(\d{4})-(\d{4})/g, "$1$2$3");
s = s.replace(/(\d{3}) (\d{4}) (\d{4})/g, '$1$2$3'); s = s.replace(/(\d{3}) (\d{4}) (\d{4})/g, "$1$2$3");
var pattern = new RegExp( var pattern = new RegExp(
"[`~!@#$^&*()=|{}':;',\\[\\].<>/?~@#¥……&*()——|{}【】‘;:”“’。,、?-]" "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~@#¥……&*()——|{}【】‘;:”“’。,、?-]"
); );
var rs = ''; var rs = "";
for (var i = 0; i < s.length; i++) { for (var i = 0; i < s.length; i++) {
rs = rs + s.substr(i, 1).replace(pattern, ' '); rs = rs + s.substr(i, 1).replace(pattern, " ");
} }
rs = rs.replace(/[\r\n]/g, ''); rs = rs.replace(/[\r\n]/g, "");
return rs; return rs;
} }
function IdentityCodeValid(code) { function IdentityCodeValid(code) {
let pass; let pass;
var city = { var city = {
11: '北京', 11: "北京",
12: '天津', 12: "天津",
13: '河北', 13: "河北",
14: '山西', 14: "山西",
15: '内蒙古', 15: "内蒙古",
21: '辽宁', 21: "辽宁",
22: '吉林', 22: "吉林",
23: '黑龙江 ', 23: "黑龙江 ",
31: '上海', 31: "上海",
32: '江苏', 32: "江苏",
33: '浙江', 33: "浙江",
34: '安徽', 34: "安徽",
35: '福建', 35: "福建",
36: '江西', 36: "江西",
37: '山东', 37: "山东",
41: '河南', 41: "河南",
42: '湖北 ', 42: "湖北 ",
43: '湖南', 43: "湖南",
44: '广东', 44: "广东",
45: '广西', 45: "广西",
46: '海南', 46: "海南",
50: '重庆', 50: "重庆",
51: '四川', 51: "四川",
52: '贵州', 52: "贵州",
53: '云南', 53: "云南",
54: '西藏 ', 54: "西藏 ",
61: '陕西', 61: "陕西",
62: '甘肃', 62: "甘肃",
63: '青海', 63: "青海",
64: '宁夏', 64: "宁夏",
65: '新疆', 65: "新疆",
71: '台湾', 71: "台湾",
81: '香港', 81: "香港",
82: '澳门', 82: "澳门",
91: '国外 ' 91: "国外 "
}; };
var tip = ''; var tip = "";
pass = true; pass = true;
if (!code || !/^\d{17}(\d|X)$/i.test(code)) { if (!code || !/^\d{17}(\d|X)$/i.test(code)) {
tip = '身份证号格式错误'; tip = "身份证号格式错误";
pass = false; pass = false;
} else if (!city[code.substr(0, 2)]) { } else if (!city[code.substr(0, 2)]) {
tip = '地址编码错误'; tip = "地址编码错误";
pass = false; pass = false;
} else { } else {
//18位身份证需要验证最后一位校验位 //18位身份证需要验证最后一位校验位
if (code.length == 18) { if (code.length == 18) {
code = code.split(''); code = code.split("");
//∑(ai×Wi)(mod 11) //∑(ai×Wi)(mod 11)
//加权因子 //加权因子
var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
//校验位 //校验位
var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
var sum = 0; var sum = 0;
var ai = 0; var ai = 0;
var wi = 0; var wi = 0;
@@ -503,7 +478,7 @@ function IdentityCodeValid(code) {
} }
var last = parity[sum % 11]; var last = parity[sum % 11];
if (parity[sum % 11] != code[17]) { if (parity[sum % 11] != code[17]) {
tip = '校验位错误'; tip = "校验位错误";
pass = false; pass = false;
} }
} }
@@ -513,4 +488,4 @@ function IdentityCodeValid(code) {
module.exports = { module.exports = {
method: smart method: smart
} };