feat:优化目录

This commit is contained in:
wangzhichao
2022-03-25 09:56:28 +08:00
parent 7546fb0efb
commit 82fa2f2c79
25 changed files with 14075 additions and 1448 deletions

View File

@@ -1,34 +0,0 @@
/*
* @Author: wangzhichiao<https://github.com/wzc570738205>
* @Date: 2020-04-22 12:50:27
* @LastEditors: wangzhichiao<https://github.com/wzc570738205>
* @LastEditTime: 2020-04-22 13:32:45
*/
// pages/address/address.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
address: '陕西省西安市雁塔区丈八沟街道高新四路高新大都荟710061 刘国良 13593464918 211381198512096810',
addressObj:{}
},
onShow: function (options) {
app.getAddressData()//保险起见,手动挂载数据
},
subimt: function () {
var address = app.smart(this.data.address)
console.log(address)
this.setData({
addressObj: address
})
},
bindName: function (e) {
this.data.address = e.detail.value
this.setData({
address: this.data.address
})
}
})

View File

@@ -1,5 +0,0 @@
{
"usingComponents": {
"cu-custom": "/colorui/components/cu-custom"
}
}

View File

@@ -1,32 +0,0 @@
<!--miniprogram/pages/add/add.wxml-->
<cu-custom bgColor="bg-gradual-blue">
<view slot="backText">返回</view>
<view slot="content">地址识别</view>
</cu-custom>
<form>
<textarea bindinput="bindName" value='{{address}}' placeholder="请输入地址"></textarea>
</form>
<view class="padding flex flex-direction">
<button class="cu-btn bg-blue margin-tb-sm lg" bindtap="subimt">识别</button>
</view>
<view class=" flex flex-direction" >
姓名:{{addressObj.name}}
</view>
<view class=" flex flex-direction" >
电话:{{addressObj.phone}}
</view>
<view class=" flex flex-direction" >
身份证:{{addressObj.idCard}}
</view>
<view class=" flex flex-direction" >
邮编:{{addressObj.zipCode}}
</view>
<view class=" flex flex-direction" >
省市区:{{addressObj.province}}/{{addressObj.city}}/{{addressObj.county}}/{{addressObj.street}}
</view>
<view class=" flex flex-direction" >
详细地址:{{addressObj.address}}
</view>

View File

@@ -1,25 +0,0 @@
/* pages/address/address.wxss */
/* miniprogram/pages/add/add.wxss */
@import "../../colorui/animation.wxss";
image[class*="gif-"] {
border-radius: 6rpx;
display: block;
}
.group-height {
min-height: 50rpx;
line-height: 50rpx !important;
}
.flex-tag {
align-items: center;
display: flex;
}
textarea{
width: 100%;
border: 1px solid #ccc;
font-size: 14px;
padding: 10px;
}

6
demo/css/bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

BIN
demo/img/smart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

274
demo/index.html Normal file
View File

@@ -0,0 +1,274 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>地址智能识别</title>
<link rel="icon" type="image/x-icon" href="img/smart.png" />
<link rel="stylesheet" href="css/bootstrap.min.css" />
<script data-ad-client="ca-pub-9729613796142547" async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<style>
#value .key {
color: #ff5722;
display: inline-block;
width: 111px;
text-align: right;
}
#value .value {
color: #366500;
padding-left: 10px;
}
li {
padding: 5px;
}
li:hover {
cursor: pointer;
background: #8bc34aa1;
padding: 5px;
}
</style>
</head>
<body>
<div class="page-header">
<h1>
地址智能识别
<small>支持省市区街道/姓名/电话/邮编</small>
<a class="github-button" href="https://github.com/wzc570738205/smartParsePro" data-size="large"
data-show-count="true" aria-label="Star wzc570738205/smartParsePro on GitHub">Star</a>
</h1>
</div>
<div class="alert alert-danger" role="alert">地址、姓名、电话、邮编用空格分开!!</div>
<!-- <div>
特殊字符<code>~!@#$^&*()=|{}':;',\\[\\].<>/?~@#¥……&*()——|{}【】‘;:”“’。,、?-</code>
</div> -->
<ol>
<li>广东省珠海市香洲区盘山路28号幸福茶庄,陈景勇13593464918</li>
<li>马云,陕西省西安市雁塔区丈八沟街道高新四路南江国际 13593464918</li>
<li>陕西省西安市雁塔区丈八沟街道高新四路南江国际710061 刘国良 13593464918 </li>
<li>疆维吾尔自治区乌鲁木齐市沙依巴克区西虹东路463号 400-1808855 小红</li>
<li>
姓名:刘德华老表<br>
电话18149428888<br>
地址广东省珠海市香洲区盘山路28号幸福茶庄
</li>
<li>
姓名:阿红<br>
电话800-8585222<br>
收货地址新疆阿克苏温宿县博孜墩柯尔克孜族乡吾斯塘博村一组306号
</li>
<li>
姓名:马云<br>
联系电话800-8585222<br>
所在地区:河北省石家庄市新华区<br>
详细地址:中华北大街68号鹿城商务中心6号楼1413室
</li>
</ol>
<textarea class="form-control" onchange="getAddress()" onkeydown="getAddress()" cols="50" rows="5" id="textarea"
placeholder="请粘贴或者输入你的地址"></textarea>
<h4>省市区街道四级联动</h4>
<form class="form-inline">
<div class="form-group">
<label for="exampleInputName2">省/直辖市</label>
<select class="form-control" id="province">
<option>请选择</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail2"></label>
<select class="form-control" id="city">
<option>请选择</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail2">区/县</label>
<select class="form-control" id="county">
<option>请选择</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputEmail2">镇/乡/街道</label>
<select class="form-control" id="street">
<option>请选择</option>
</select>
</div>
</form>
<div id="response"></div>
<div id="value"></div>
</body>
</html>
<script src="js/jquery.min.js"></script>
<script src="js/pcasCode.js"></script>
<script src="js/zipCode.js"></script>
<script src="js/address_parse.js"></script>
<!-- <script src="dist/address_parse.js"></script> -->
<script src="js/bootstrap.min.js"></script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
<script src="js/axios.js"></script>
<script>
$('li').on('click', function (e) {
$('#textarea').val($(this).text().trim())
getAddress()
})
function getAddress() {
let parse_list = smart($('#textarea').val())
let html = ''
axios({
method: 'post',
url: "https://wangzc.wang/smAddress",
data: {
address: $('#textarea').val(),
},
})
.then(function (res) {
$('#response').html(
`<span style="color:green">接口调用状态码200当前调用次数` +
res.data.requestNumber +
`</span>`
)
html += `<p><span class="key">姓名</span>:<span class="value">${res.data.name}</span></p>`
html += `<p><span class="key">电话</span>:<span class="value">${res.data.phone}</span></p>`
html += `<p><span class="key">邮编</span>:<span class="value">${res.data.zipCode}</span></p>`
// html += `<p><span class="key">身份证号码</span>:<span class="value">${res.data.idCard}</span></p>`
html += `<p><span class="key">省</span>:<span class="value">${res.data.province}</span></p>`
html += `<p><span class="key">市</span>:<span class="value">${res.data.city}</span></p>`
html += `<p><span class="key">区</span>:<span class="value">${res.data.county}</span></p>`
html += `<p><span class="key">街道</span>:<span class="value">${res.data.street}</span></p>`
html += `<p><span class="key">详细地址</span>:<span class="value">${res.data.address}</span></p>`
$('#value').html(html)
})
.catch(function (err) {
$('#response').html(
`<span style="color:red">接口调用状态码500</span>`
)
})
$('#province').val(parse_list.provinceCode)
$('#city').val(parse_list.cityCode)
$('#county').val(parse_list.countyCode)
$('#street').val(parse_list.streetCode)
getCity($('#province').val(), parse_list)
getCounty($('#city').val(), parse_list)
getStreet($('#county').val(), parse_list)
}
addressList = pcassCode
///
addressList.forEach((item) => {
formatAddresList(item, 1, '')
})
//
let province = ''
addressList.forEach((res) => {
province += ` <option value='` + res.code + `'>` + res.name + `</option>`
})
$('#province').html(province)
getCity($('#province').val())
getCounty($('#city').val())
getStreet($('#county').val())
$('#province').change((res) => {
let provinceCode = $('#province').val()
getCity(provinceCode)
getCounty($('#city').val())
getStreet($('#county').val())
})
$('#city').change((res) => {
let cityCode = $('#city').val()
getCounty(cityCode)
getStreet($('#county').val())
})
$('#county').change((res) => {
let countyCode = $('#county').val()
getStreet(countyCode)
})
function getCity(provinceCode, parse_list) {
let cityList = []
let city = ''
addressList.forEach((res) => {
if (res.code == provinceCode) {
cityList = res.children
return
}
})
cityList.forEach((res) => {
city += ` <option value='` + res.code + `'>` + res.name + `</option>`
})
$('#city').html(city)
if (parse_list) {
$('#city').val(parse_list.cityCode)
}
}
function getCounty(cityCode, parse_list) {
let countyList = []
let county = ''
addressList.forEach((el) => {
el.children.forEach((res) => {
if (res.code == cityCode) {
countyList = res.children
return
}
})
})
countyList.forEach((res) => {
county += ` <option value='` + res.code + `'>` + res.name + `</option>`
})
$('#county').html(county)
if (parse_list) {
$('#county').val(parse_list.countyCode)
}
}
function getStreet(countyCode, parse_list) {
let streetList = []
let street = ''
addressList.forEach((el) => {
el.children.forEach((element) => {
element.children.forEach((res) => {
if (res.code == countyCode) {
streetList = res.children
return
}
})
})
})
streetList.forEach((res) => {
street += ` <option value='` + res.code + `'>` + res.name + `</option>`
})
$('#street').html(street)
if (parse_list) {
$('#street').val(parse_list.streetCode)
}
}
</script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-119026906-1"></script>
<script>
window.dataLayer = window.dataLayer || []
function gtag() {
dataLayer.push(arguments)
}
gtag('js', new Date())
gtag('config', 'UA-119026906-1')
</script>

483
demo/js/address_parse.js Normal file
View File

@@ -0,0 +1,483 @@
var addressList = []; //地址列表
var zipCodeList = []; //邮编列表
//获取地址以及邮编json
addressList = pcassCode;
addressList.forEach((item) => {
formatAddresList(item, 1, "");
});
zipCodeList = zipCodeFormat(zipCode);
/**
* 地址数据处理
* @param addressList-各级数据对象
* @param index-对应的省/市/县区/街道
* @param province-只有直辖市会处理为 北京市北京市
* @returns <array>
*/
function formatAddresList(addressList, index, province) {
if (index === 1) {
//省
addressList.province = addressList.name;
addressList.type = "province";
}
if (index === 2) {
//市
if (addressList.name == "市辖区") {
addressList.name = province.name;
}
addressList.city = addressList.name;
addressList.type = "city";
}
if (index === 3) {
//区或者县
addressList.county = addressList.name;
addressList.type = "county";
}
if (index === 4) {
//街道
addressList.street = addressList.name;
addressList.type = "street";
}
if (addressList.children) {
index++;
addressList.children.forEach((res) => {
formatAddresList(res, index, addressList);
});
}
}
/**
* 解析邮编
* @param
* @returns <array>
*/
function zipCodeFormat(zipCode) {
let list = [];
zipCode.forEach((el) => {
if (el.child) {
el.child.forEach((event) => {
if (event.child) {
event.child.forEach((element) => {
list.push(element.zipcode);
});
}
});
}
});
return list;
}
var smartObj = {};
/**
* 解析邮编
* @param event识别的地址
* @returns <obj>
*/
function smart(event) {
let copy = event;
let _phone = copy.match(
/((\d{2,4}[-_—])\d{3,8}([-_—]?\d{3,8})?([-_—]?\d{1,7})?)|(0?1\d{10})/g
);
if(_phone && _phone.length>0){
event = event.replace(_phone[0],'')
}
event = stripscript(event); //过滤特殊字符
let obj = {};
let copyaddress = JSON.parse(JSON.stringify(event));
copyaddress = copyaddress.split(" ");
copyaddress.forEach((res, index) => {
if (res) {
if (res.length == 1) {
res += "XX"; // 过滤掉一位的名字或者地址
}
let addressObj = smatrAddress(res);
obj = Object.assign(obj, addressObj);
if (JSON.stringify(addressObj) === "{}") {
obj.name = res.replace("XX", "");
}
}
});
if (!obj.phone && _phone) {
_phone.forEach((e) => {
if (e.length >= 8) {
obj.phone = e;
}
});
}
return obj;
}
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, "");
}
//电话匹配
let phone = address.match(
/(86-[1][0-9]{10}) | (86[1][0-9]{10})|([1][0-9]{10})/g
);
if (phone) {
smartObj.phone = phone[0];
address = address.replace(phone[0], "");
}
//邮编匹配
zipCodeList.forEach((res) => {
if (address.indexOf(res) != -1) {
let num = address.indexOf(res);
let code = address.slice(num, num + 6);
smartObj.zipCode = code;
address = address.replace(code, "");
}
});
let matchAddress = "";
//省匹配 比如输入北京市朝阳区,会用北 北京 北京市 北京市朝 以此类推在addressList里的province中做匹配会得到北京市 河北省 天津市等等;
let 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);
addressList.forEach((res) => {
if (res["province"].indexOf(matchAddress) != -1) {
matchProvince.push({
province: res.province,
provinceCode: res.code,
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) {
let province = matchProvince.reduce((p, v) => (p.index < v.index ? v : p));
smartObj.province = province.province;
smartObj.provinceCode = province.provinceCode;
address = address.replace(province.matchValue, "");
}
//市查找
let matchCity = []; //粗略匹配上的市
matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 2);
addressList.forEach((el) => {
// if (el.name == smartObj.province) {
if (el.code == smartObj.provinceCode || !smartObj.provinceCode) {
if (
smartObj.province == "北京市" ||
smartObj.province == "天津市" ||
smartObj.province == "上海市" ||
smartObj.province == "重庆市"
) {
el.children.forEach((item) => {
item.children.forEach((res) => {
if (res["county"].indexOf(matchAddress) != -1) {
matchCity.push({
county: res.county,
countyCode: res.code,
city: item.city,
cityCode: item.code,
matchValue: matchAddress,
province: el.province,
provinceCode: el.code,
});
}
});
});
} else {
el.children.forEach((res) => {
if (res["city"].indexOf(matchAddress) != -1) {
matchCity.push({
city: res.city,
cityCode: res.code,
matchValue: matchAddress,
province: el.province,
provinceCode: el.code,
});
}
});
}
}
// }
});
}
//统计筛选初略统计出的市
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 (matchCity.length != 0) {
let city = matchCity.reduce((p, v) => (p.index < v.index ? v : p));
smartObj.city = city.city;
smartObj.cityCode = city.cityCode;
smartObj.county = city.county;
smartObj.countyCode = city.countyCode;
if (!smartObj.province) {
smartObj.province = city.province;
smartObj.provinceCode = city.provinceCode;
}
address = address.replace(city.matchValue, "");
}
//区县查找
let matchCounty = []; //粗略匹配上的区县
matchAddress = "";
for (let endIndex = 0; endIndex < address.length; endIndex++) {
matchAddress = address.slice(0, endIndex + 2);
addressList.forEach((el) => {
// if (el.name == smartObj.province) {
if (
smartObj.province == "北京市" ||
smartObj.province == "天津市" ||
smartObj.province == "上海市" ||
smartObj.province == "重庆市"
) {
//nothing
} else {
el.children.forEach((item) => {
// if (item.name == smartObj.city) {
item.children.forEach((res) => {
if (res["county"].indexOf(matchAddress) != -1) {
//省/市 || 省
if (smartObj.province) {
if (res.code.slice(0, 4) == smartObj.cityCode) {
matchCounty.push({
county: res.county,
countyCode: res.code,
city: item.city,
cityCode: item.code,
matchValue: matchAddress,
province: el.province,
provinceCode: el.code,
});
}
} else if (!smartObj.province && !smartObj.city) {
matchCounty.push({
county: res.county,
countyCode: res.code,
city: item.city,
cityCode: item.code,
matchValue: matchAddress,
province: el.province,
provinceCode: el.code,
});
}
}
});
// }
});
}
// }
});
}
//统计筛选初略统计出的区县
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) {
let city = matchCounty.reduce((p, v) => (p.index < v.index ? v : p));
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, "");
}
//街道查找
let 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"].indexOf(matchAddress) != -1) {
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) {
let city = matchStreet.reduce((p, v) => (p.index < v.index ? v : p));
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.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 rs = "";
for (var i = 0; i < s.length; i++) {
rs = rs + s.substr(i, 1).replace(pattern, " ");
}
rs = rs.replace(/[\r\n]/g, "");
return rs;
}
function IdentityCodeValid(code) {
let pass;
var 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: "国外 ",
};
var tip = "";
pass = true;
if (!code || !/^\d{17}(\d|X)$/i.test(code)) {
tip = "身份证号格式错误";
pass = false;
} else if (!city[code.substr(0, 2)]) {
tip = "地址编码错误";
pass = false;
} else {
//18位身份证需要验证最后一位校验位
if (code.length == 18) {
code = code.split("");
//∑(ai×Wi)(mod 11)
//加权因子
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 sum = 0;
var ai = 0;
var wi = 0;
for (var i = 0; i < 17; i++) {
ai = code[i];
wi = factor[i];
sum += ai * wi;
}
var last = parity[sum % 11];
if (parity[sum % 11] != code[17]) {
tip = "校验位错误";
pass = false;
}
}
}
return pass;
}
window.smart = smart;

3
demo/js/axios.js Normal file

File diff suppressed because one or more lines are too long

7
demo/js/bootstrap.min.js vendored Normal file

File diff suppressed because one or more lines are too long

2
demo/js/jquery.min.js vendored Normal file

File diff suppressed because one or more lines are too long

1
demo/js/pcasCode.js Normal file

File diff suppressed because one or more lines are too long

13477
demo/js/zipCode.js Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long