AddressController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Http\Controllers;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Http\Request;
  5. use App\Handlers\IdentifyAddress;
  6. use App\Handlers\Address;
  7. use App\Handlers\AddressParse;
  8. use App\Handlers\DetailParse;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. class AddressController extends Controller{
  12. private $userRepository;
  13. public function __construct(IdentifyAddress $userRepo)
  14. {
  15. // $this->userRepository = $userRepo;
  16. $this->userRepository = new AddressParse();
  17. }
  18. public function getTest(Request $request){
  19. // 解析包含用户信息的字符串
  20. $string = $request->input('str');//'深圳市龙华区龙华街道1980科技文化产业园3栋317 张三 13800138000 518000 120113196808214821';
  21. $r = Address::smart($string);
  22. return $r;
  23. print_r($r);
  24. // 解析不包含用户信息的字符串
  25. $string = '吉林省长春市朝阳区开运街领秀朝阳小区333弄8号502';
  26. $r = Address::smart($string, $user = false);
  27. print_r($r);
  28. // 测试
  29. // $test = array(
  30. // '北京市东城区宵云路36号国航大厦一层',
  31. // '甘肃省东乡族自治县布楞沟村1号',
  32. // '成都市双流区宵云路36号国航大厦一层',
  33. // '内蒙古自治区乌兰察布市公安局交警支队车管所',
  34. // '长春市朝阳区宵云路36号国航大厦一层',
  35. // '成都市高新区天府软件园B区科技大楼',
  36. // '双流区郑通路社保局区52050号',
  37. // '岳市岳阳楼区南湖求索路碧灏花园A座1101',
  38. // '四川省 凉山州美姑县东方网肖小区18号院',
  39. // '重庆攀枝花市东区机场路3中学校',
  40. // '渝北区渝北中学51200街道地址',
  41. // '13566892356天津天津市红桥区水木天成1区临湾路9-3-1101',
  42. // '苏州市昆山市青阳北路时代名苑20号311室',
  43. // '崇州市崇阳镇金鸡万人小区兴盛路105-107',
  44. // '四平市双辽市辽北街道',
  45. // '梧州市奥奇丽路10-9号A幢地层(礼迅贸易有限公司)卢丽丽',
  46. // '江西省抚州市东乡区孝岗镇恒安东路125号1栋3单元502室 13511112222 吴刚',
  47. // '清远市清城区石角镇美林湖大东路口佰仹公司 郑万顺 15345785872',
  48. // );
  49. //
  50. // foreach ($test as $key => $v) {
  51. // $r = Address::smart($v, $user = false);
  52. // print_r($r);
  53. // }
  54. }
  55. public static function smart_parse(Request $request)
  56. {
  57. //解析结果
  58. $parse = [];
  59. $address=$request->input('discernDel');
  60. //1. 过滤掉收货地址中的常用说明字符,排除干扰词
  61. $search = ['收货地址', '地址', '收货人', '收件人', '收货', '邮编', '电话', '身份证号码', '身份证号', '身份证', ':', ':', ';', ';', ',', ',', '。', ];
  62. $replace = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
  63. $address = str_replace($search, $replace, $address);
  64. //2. 把空白字符(包括空格\r\n\t)都换成一个空格
  65. $address = preg_replace('/\s{1,}/', ' ', $address);
  66. //3. 去除手机号码中的短横线 如136-3333-6666 主要针对苹果手机
  67. $address = preg_replace('/0-|0?(\d{3})-(\d{4})-(\d{4})/', '$1$2$3', $address);
  68. //4. 提取中国境内身份证号码
  69. preg_match('/\d{18}|\d{17}X/i', $address, $match);
  70. if ($match && $match[0]) {
  71. $parse['idno'] = strtoupper($match[0]);
  72. $address = str_replace($match[0], '', $address);
  73. }
  74. //5. 提取11位手机号码或者7位以上座机号
  75. preg_match('/\d{7,11}|\d{3,4}-\d{6,8}/', $address, $match);
  76. if ($match && $match[0]) {
  77. $parse['mobile'] = $match[0];
  78. $address = str_replace($match[0], '', $address);
  79. }
  80. //6. 提取6位邮编 邮编也可用后面解析出的省市区地址从数据库匹配出
  81. preg_match('/\d{6}/', $address, $match);
  82. if ($match && $match[0]) {
  83. $parse['postcode'] = $match[0];
  84. $address = str_replace($match[0], '', $address);
  85. }
  86. //再次把2个及其以上的空格合并成一个,并TRIM
  87. $address = trim(preg_replace('/ {2,}/', ' ', $address));
  88. //按照空格切分 长度长的为地址 短的为姓名 因为不是基于自然语言分析,所以采取统计学上高概率的方案
  89. $split_arr = explode(' ', $address);
  90. if (count($split_arr) > 1) {
  91. $parse['name'] = $split_arr[0];
  92. foreach ($split_arr as $value) {
  93. if (strlen($value) < strlen($parse['name'])) {
  94. $parse['name'] = $value;
  95. }
  96. }
  97. $address = trim(str_replace($parse['name'], '', $address));
  98. }
  99. $address_detail = DetailParse::detail_parse($address);
  100. $parse['address_detail'] = $address_detail;
  101. $parse['detail'] = $address;
  102. //parse['detail']详细地址可以传入另一个文件的函数,用来解析出:省,市,区,街道地址
  103. return $parse;
  104. }
  105. /**
  106. * Function:地址识别
  107. * Author:cyw0413
  108. * @param Request $request
  109. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|
  110. * \Laravel\Lumen\Http\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  111. */
  112. public function getDiscern(Request $request)
  113. {
  114. $discernDel = $request->input('discernDel');
  115. return $this->userRepository->getAddress($discernDel);
  116. try{
  117. DB::beginTransaction();
  118. $address = $this->userRepository->getDiscern($discernDel);
  119. DB::commit();
  120. }catch (\Exception $e){
  121. DB::rollBack();
  122. $msg = "信息提示:".$e->getMessage().",行:".$e->getLine();
  123. return response(['code' => 0, 'msg'=>$msg]);
  124. }
  125. return response(['code'=>1,'msg'=>$address]); //地址识别完成
  126. }
  127. public function DealAddress(Request $request){
  128. $data=[];
  129. $details=$request->input('details');
  130. $r = Address::smart($details);
  131. return $this->success([
  132. 'phone'=>array_key_exists('mobile',$r)?$r['mobile']:'',
  133. 'name'=>array_key_exists('name',$r)?$r['name']:'',
  134. 'province'=>$r['province'],
  135. 'city'=>$r['city'],
  136. 'area'=>$r['region'],
  137. 'street'=>$r['street'],
  138. ]);
  139. preg_match_all("/1((34[0-8]\d{7})|((3[0-3|5-9])|(4[5-7|9])|(5[0-3|5-9])|(66)|(7[2-3|5-8])|(8[0-9])|(9[1|8|9]))\d{8})/", $details,$phones);
  140. if($phones){
  141. if($phones[0]){
  142. $data['phone']=$phones[0][0];
  143. }
  144. }
  145. $name=trim(mb_substr($details,'0',mb_strpos($details,$phones[0][0])));
  146. $regex = "/\ |\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\,|\。|\。|\“|\‘|\?|\!|\【|\】|\;|\:|\¥|\(|\)|\'|\`|\-|\=|\\\|\|/";
  147. $data['name'] = preg_replace($regex,"",$name);
  148. $client = new Client();
  149. $url = 'https://restapi.amap.com/v5/place/text?parameters';
  150. $array = [
  151. 'query' => [
  152. 'keywords' => $details,
  153. 'output' => 'json',
  154. 'key' => '4e0ce153265426f2d2ceef069e5eb552',
  155. ]
  156. ];
  157. $response = $client->request('GET', $url, $array);
  158. $ad = json_decode($response->getBody()->getContents());
  159. if ($ad->pois){
  160. $data['province']=$ad->pois[0]->pname;
  161. $data['city']=$ad->pois[0]->cityname;
  162. $data['area']=$ad->pois[0]->adname;
  163. }
  164. return $this->success($data);
  165. }
  166. }