123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace App\Handlers;
- use GuzzleHttp\Client;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Log;
- use TencentCloud\Common\Credential;
- use TencentCloud\Common\Profile\ClientProfile;
- use TencentCloud\Common\Profile\HttpProfile;
- use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
- use TencentCloud\Ocr\V20181119\OcrClient;
- class IdentifyAddress {
- /**
- * Function:识别地址
- * Author:cyw0413
- */
- public function getDiscern($discernDel)
- {
- if (empty($discernDel)) {
- throw new \Exception("请传入要识别的地址");
- }
- $discernDel_left = explode ('[', $discernDel);
- if (!isset($discernDel_left[1])) {
- throw new \Exception("你填写的地址规则错误,手机号码应该用[]");
- }
- $discernDel_right = explode (']', $discernDel_left[1]);
- if (!isset($discernDel_right[1]) || empty($discernDel_right[1])) {
- throw new \Exception("你填写的地址规则错误,手机号码应该用[]");
- }
- $name = $discernDel_left[0];
- if (empty($name)) {
- throw new \Exception("你填写的姓名有误!");
- }
- $mobile = $discernDel_right[0];
- if (empty($mobile) ) {//|| checkMobile($mobile) == 0
- throw new \Exception("你填写的手机号码格式有误!");
- }
- $address = trim ($discernDel_right[1]);
- if (empty($address)) {
- throw new \Exception("你填写的地址不能为空");
- }
- $var_address = $this->getAddressArrar($address);
- $var_address['name'] = $name;
- $var_address['mobile'] = $mobile;
- return $var_address;
- }
- /**
- * Function:地址的处理
- * Author:cyw0413
- * @param $address
- * @return array
- * @throws \Exception
- */
- function getAddressArrar($address){
- // 获取所有地址递归列表
- $regions = $this->getRegions();
- // 初始化数据
- $province = $city = $district = [];
- // 先查找省份-第一级地区
- $province = $this->checkAddress($address, $regions);
- if($province){
- $province_arr = ['110000','300000','404100','310000']; //4个市辖区如果地址不存在二级(市辖区,县),则特殊处理
- if(!isset($province['region_code'])){
- throw new \Exception("请正确填写省份(市辖区)");
- }
- // 查找城市-第二级地区
- $city = $this->checkAddress($address, $province['list']);
- //这里只处理4个市辖区,可能还有多种情况,待发现
- if(in_array($province['region_code'],$province_arr) && count($city['list']) == 0){
- $city = $this->checkAddress('市辖区', $province['list']);
- if($city){
- // 查找地区-第三级地区
- $district = $this->checkAddress($address, $city['list']);
- //如果没有找到,则查找另外一个二级地区
- if(!isset($district['region_code'])){
- $city = $this->checkAddress('县', $province['list']);
- // 查找地区-第三级地区
- $district = $this->checkAddress($address, $city['list']);
- }
- }
- }else{
- if($city){
- // 查找地区-第三级地区
- $district = $this->checkAddress($address, $city['list']);
- }
- }
- }else{
- //省份不填,报错误
- throw new \Exception("省份没填写,请检查");
- }
- return $this->getAddressInfo($address, $province, $city, $district);
- }
- /**
- * 匹配正确的城市地址
- * @param $address
- * @param $city_list
- * @param int $force
- * @param int $str_len
- * @return array
- **/
- function checkAddress($address, $city_list, $force=false, $str_len=2){
- $num = 0;
- $list = array();
- $result = array();
- // 遍历所有可能存在的城市
- foreach ($city_list as $city_key=>$city){
- $city_name = mb_substr($city['region_name'], 0, $str_len,'utf-8');
- // 判断是否存包含当前地址字符
- $city_arr = explode($city_name, $address);
- // 如果存在相关字眼,保存该地址的所有子地址
- if(count($city_arr) >= 2){
- // 必须名称长度同时达到当前比对长度
- if(strlen($city['region_name']) < $str_len){
- continue;
- }
- $num ++;
- if(isset($city['child'])){
- $list = $list + $city['child'];
- }
- $result[] = array(
- 'region_code' => $city_key,
- 'region_name' => $city['region_name'],
- 'list' =>$list,
- );
- }
- }
- // 如果有多个存在,则加大字符匹配长度
- if($num > 1 || $force){
- $region_name1 = $result[0]['region_name'];
- $region_name2 = $result[1]['region_name'];
- if(strlen($region_name1) == strlen($region_name2) && strlen($region_name1) == $str_len){
- $region_id1 = $result[0]['region_code'];
- $region_id2 = $result[1]['region_code'];
- $index = $region_id1 > $region_id2 ? 1 : 0;
- $result = $result[$index];
- return $result;
- }
- return $this->checkAddress($address, $city_list, $force, $str_len+1);
- } else {
- $result[0]['list'] = $list;
- return $result[0];
- }
- }
- /**
- * 根据原地址返回详细信息
- * @param $address
- * @param $province
- * @param $city
- * @param $area
- * @return array
- **/
- function getAddressInfo($address, $province, $city, $district){
- // 查找最后出现的地址 - 截取详细信息
- if(!isset($province['region_name'])){
- throw new \Exception("请检查并正确填写省份(市辖区)");
- }
- if(!isset($city['region_name'])){
- throw new \Exception("请检查并正确填写城市");
- }
- if(!isset($district['region_name'])){
- throw new \Exception("请检查并正确填写区域(县/区/镇)");
- }
- $find_str = '';
- if($province['region_name']){
- $find_str = $province['region_name'];
- if($city['region_name']){
- $find_str = $city['region_name'];
- if(isset($district['region_name']) && $district['region_name']){
- $find_str = $district['region_name'];
- }
- }
- }
- // 截取详细的信息
- $find_str_len = mb_strlen($find_str,'utf-8');
- for($i=0; $i<$find_str_len-1; $i++){
- $substr = mb_substr($find_str,0,$find_str_len - $i, 'utf-8');
- $end_index = mb_strpos($address, $substr);
- if ($end_index){
- $address = mb_substr($address, $end_index + mb_strlen($substr) , mb_strlen($address) - $end_index);
- }
- }
- !empty($find_str) && $find_str = '|\S*' . $find_str;
- $area['info'] = preg_replace("/\s*|,|,|:|:{$find_str}/i", '', $address);
- if(empty($area['info'])){
- throw new \Exception("详细地址不存在,请检查");
- }
- return $address = [
- 'province' => $province['region_code'],
- 'city' => $city['region_code'],
- 'district' => $district['region_code'],
- 'info' => $area['info']
- ];
- }
- }
|