CustomerWriteReport.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\AreaMap;
  4. use App\Model\BasicType;
  5. use App\Model\BasicTypeAllUse;
  6. use App\Model\Customer;
  7. use App\Model\CustomerInfo;
  8. use App\Model\CustomerReport;
  9. use App\Model\CustomerReportDepart;
  10. use App\Model\FollowUpRecord;
  11. use App\Model\SalesOrder;
  12. use App\Model\SeeRange;
  13. use Illuminate\Console\Command;
  14. use Illuminate\Support\Facades\DB;
  15. class CustomerWriteReport extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'command:customer_write_report';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = 'Command description';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return mixed
  42. */
  43. public function handle()
  44. {
  45. echo "执行任务--------start---------------\n";
  46. try {
  47. $this->customerWrite();
  48. }catch (\Throwable $exception){
  49. echo "发生异常:" . $exception->getFile() . "|" . $exception->getMessage() . "|" . $exception->getLine() . "\n";
  50. }
  51. echo "执行任务--------end---------------\n";
  52. }
  53. private function customerWrite(){
  54. //今天到上月今天的时间戳
  55. $nowStamp = time();
  56. $lastMonth = date("Y-m-d 00:00:00", strtotime("-1 month"));
  57. $lastMonthStamp = strtotime($lastMonth);
  58. list($provinceMap, $cityMap, $countyMap) = $this->constructAddress();
  59. // dd($provinceMap, $cityMap, $countyMap);
  60. // dd(count($provinceMap), count($cityMap), count($countyMap));
  61. // $this->forInsertArea($provinceMap, $cityMap, $countyMap);dd(122);
  62. Customer::where("del_time",0)
  63. ->where('enter_time','>=', $lastMonthStamp)
  64. ->where('enter_time','<=', $nowStamp)
  65. ->orderBy('enter_time','asc')
  66. ->select('id as customer_id','title','model_type','consulting_product_new','enter_time','address2','top_depart_id','customer_from','car_type')
  67. ->chunkById(200, function ($data) use($provinceMap, $cityMap, $countyMap,$nowStamp){
  68. // 开启事务
  69. DB::transaction(function () use ($data, $provinceMap, $cityMap, $countyMap, $nowStamp) {
  70. $dataArray = $data->toArray();
  71. list($main, $main_depart) = $this->processingData($dataArray, $provinceMap, $cityMap, $countyMap, $nowStamp);
  72. if (! empty($main)) {
  73. $customer_ids = array_column($dataArray, 'customer_id');
  74. // 删除旧数据
  75. CustomerReport::where('del_time', 0)
  76. ->whereIn('customer_id', $customer_ids)
  77. ->update(['del_time' => $nowStamp]);
  78. CustomerReportDepart::where('del_time', 0)
  79. ->whereIn('customer_id', $customer_ids)
  80. ->update(['del_time' => $nowStamp]);
  81. // 写入新数据
  82. CustomerReport::insert($main);
  83. CustomerReportDepart::insert($main_depart);
  84. echo '200条写入中' . PHP_EOL;
  85. } else {
  86. echo '暂无数据写入' . PHP_EOL;
  87. }
  88. });
  89. });
  90. }
  91. private function processingData($data,$provinceMap,$cityMap,$countyMap,$time){
  92. $return = [];
  93. $customer_id = array_column($data,'customer_id');
  94. $range = SeeRange::where('del_time',0)
  95. ->whereIn('data_id', $customer_id)
  96. ->where('data_type',SeeRange::type_one)
  97. ->where('type',SeeRange::data_three)
  98. ->select('data_id as customer_id','param_id as top_depart_id')
  99. ->get()->toArray();
  100. $map = [];
  101. foreach ($range as $value){
  102. $map[$value['customer_id']][] = $value['top_depart_id'];
  103. }
  104. $isset_map = SalesOrder::where('del_time', 0)
  105. ->whereIn('customer_id', $customer_id)
  106. ->pluck('customer_id')
  107. ->unique()
  108. ->flip()
  109. ->map(fn() => 1)
  110. ->toArray();
  111. // 最新的跟进记录
  112. $latestRecordsArray = $detail_insert = [];
  113. $latestRecords = FollowUpRecord::whereIn('data_id', $customer_id)
  114. ->where('type', FollowUpRecord::type_one)
  115. ->where('del_time',0)
  116. ->select('data_id','follow_type','crt_id')
  117. ->orderBy('id','desc')
  118. ->get()->toArray();
  119. foreach ($latestRecords as $value){
  120. if(! isset($latestRecordsArray[$value['data_id']])){
  121. $latestRecordsArray[$value['data_id']] = [
  122. 'follow_type' => $value['follow_type'],
  123. 'num' => 1,
  124. ];
  125. }else{
  126. $latestRecordsArray[$value['data_id']]['num'] += 1;
  127. }
  128. $detail_insert[] = [
  129. 'customer_id' => $value['data_id'],
  130. 'top_depart_id' => 0,
  131. 'crt_time' => $time,
  132. 'type' => CustomerReportDepart::type_three,
  133. 'man_id' => $value['crt_id'],
  134. ];
  135. }
  136. $customer_info = CustomerInfo::where('del_time',0)
  137. ->whereIn('customer_id',$customer_id)
  138. ->whereIn('type',[CustomerInfo::type_one, CustomerInfo::type_two])
  139. ->select('type','contact_type','contact_info','customer_id','data_id')
  140. ->get()->toArray();
  141. $customer_info_map = $customer_info_map2 = [];
  142. foreach ($customer_info as $value){
  143. if($value['type'] == CustomerInfo::type_one){
  144. if(! $value['contact_info']) continue;
  145. if(! empty($customer_info_map[$value['customer_id']])) continue;
  146. $customer_info_map[$value['customer_id']] = $value['contact_info'];
  147. }else{
  148. if(! $value['data_id']) continue;
  149. if(! empty($customer_info_map2[$value['customer_id']]) && in_array($value['data_id'], $customer_info_map2[$value['customer_id']])) continue;
  150. $customer_info_map2[$value['customer_id']][] = $value['data_id'];
  151. }
  152. }
  153. $array = array_unique(array_merge_recursive(array_column($data,'customer_from'),array_column($data,'car_type')));
  154. $basic_map = BasicType::whereIn('id',$array)
  155. ->pluck('title','id')
  156. ->toArray();
  157. $basic_all_map = BasicTypeAllUse::where('type', BasicTypeAllUse::type_one)
  158. ->whereIn('id',array_unique(array_column($data,'consulting_product_new')))
  159. ->pluck('title','id')
  160. ->toArray();
  161. foreach ($data as $key => $value){
  162. $province = $province_code = $city = $city_code = "";
  163. $is_success = 0;
  164. if(! empty($value['address2'])){
  165. $tmp = $this->parseAddress($value['address2'], $provinceMap, $cityMap,$countyMap);
  166. if(empty($tmp['province']['label']) || empty($tmp['city']['label'])){
  167. // $tmp['origin'] = $value['address2'];
  168. // $a = [
  169. // "provice" => $tmp['province']['label'],
  170. // "provice_code" => $tmp['province']['value'],
  171. // "city" => $tmp['city']['label'],
  172. // "city_code" => $tmp['city']['value'],
  173. // "origin" => $value['address2']
  174. // ];
  175. // $return[] = $a;
  176. }else{
  177. $province = $tmp['province']['label'];
  178. $province_code = $tmp['province']['value'];
  179. $city = $tmp['city']['label'];
  180. $city_code = $tmp['city']['value'];
  181. $is_success = 1;
  182. }
  183. }
  184. $data[$key]['province'] = $province;
  185. $data[$key]['province_code'] = $province_code;
  186. $data[$key]['city'] = $city;
  187. $data[$key]['city_code'] = $city_code;
  188. $data[$key]['is_success'] = $is_success;
  189. $belong_depart_id = [$value['top_depart_id']];
  190. if(isset($map[$value['customer_id']])) $belong_depart_id = array_unique($map[$value['customer_id']]);
  191. foreach ($belong_depart_id as $b){
  192. $detail_insert[] = [
  193. 'customer_id' => $value['customer_id'],
  194. 'top_depart_id' => $b,
  195. 'crt_time' => $time,
  196. 'type' => CustomerReportDepart::type_one,
  197. 'man_id' => 0,
  198. ];
  199. }
  200. $customer_info2 = $customer_info_map2[$value['customer_id']] ?? [];
  201. if(! empty($customer_info2)){
  202. foreach ($customer_info2 as $v_i){
  203. $detail_insert[] = [
  204. 'customer_id' => $value['customer_id'],
  205. 'top_depart_id' => 0,
  206. 'crt_time' => $time,
  207. 'type' => CustomerReportDepart::type_two,
  208. 'man_id' => $v_i,
  209. ];
  210. }
  211. }
  212. $type = -1;
  213. $r_array = $latestRecordsArray[$value['customer_id']] ?? [];
  214. $num = $r_array['num'] ?? 0;
  215. if(isset($isset_map[$value['customer_id']])){
  216. $type = 3;
  217. }else{
  218. if(! empty($r_array)) $type = $r_array['follow_type'];
  219. }
  220. $data[$key]['type'] = $type;
  221. $data[$key]['follow_num'] = $num;
  222. $data[$key]['contact_info'] = $customer_info_map[$value['customer_id']] ?? "";
  223. $data[$key]['customer_from'] = $basic_map[$value['customer_from']] ?? "";
  224. $data[$key]['car_type'] = $customer_info_map[$value['car_type']] ?? "";
  225. $data[$key]['consulting_product_new_title'] = $basic_all_map[$value['consulting_product_new']] ?? "";
  226. $data[$key]['crt_time'] = $time;
  227. }
  228. return [$data, $detail_insert];
  229. }
  230. private function forInsertArea($provinceMap, $cityMap, $countyMap){
  231. $insert = [];
  232. foreach ($provinceMap as $key => $value){
  233. $insert[] = [
  234. 'key' => $key,
  235. 'label' => $value['label'],
  236. 'value' => $value['value'],
  237. 'province' => "",
  238. 'type' => 1,
  239. ];
  240. }
  241. foreach ($cityMap as $key => $value){
  242. $insert[] = [
  243. 'key' => $key,
  244. 'label' => $value['label'],
  245. 'value' => $value['value'],
  246. 'province' => $value['province'],
  247. 'type' => 2,
  248. ];
  249. }
  250. foreach ($countyMap as $key => $value){
  251. $insert[] = [
  252. 'key' => $key,
  253. 'label' => $value['city_label'],
  254. 'value' => $value['city_value'],
  255. 'province' => $value['province_value'],
  256. 'type' => 3,
  257. ];
  258. }
  259. AreaMap::where('del_time',0)
  260. ->where('is_create_by_human',0)
  261. ->update(['del_time' => time()]);
  262. AreaMap::insert($insert);
  263. }
  264. private function constructAddress($type = 0) {
  265. if(! empty($type)){
  266. $result = AreaMap::where('del_time',0)
  267. ->select('key','label','value','province','type')
  268. ->get()->toArray();
  269. $provinceMap = [];
  270. $cityMap = [];
  271. $countyMap = []; // 新增区县映射表
  272. foreach ($result as $value){
  273. if($value['type'] == 1){
  274. $provinceMap[$value['key']] = [
  275. 'label' => $value['label'],
  276. 'value' => $value['value'],
  277. ];
  278. }elseif($value['type'] == 2){
  279. $cityMap[$value['key']] = [
  280. 'label' => $value['label'],
  281. 'value' => $value['value'],
  282. 'province' => $value['province'],
  283. ];
  284. }else{
  285. $countyMap[$value['key']] = [
  286. 'city_label' => $value['label'],
  287. 'city_value' => $value['value'],
  288. 'province_value' => $value['value'],
  289. 'type' => 3,
  290. ];
  291. }
  292. }
  293. }else{
  294. $addressData = config('address3');
  295. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  296. $provinceMap = [];
  297. $cityMap = [];
  298. $countyMap = []; // 新增区县映射表
  299. // 添加直辖市特殊处理
  300. $municipalities = [
  301. '110000' => ['北京', '北京市'],
  302. '120000' => ['天津', '天津市'],
  303. '310000' => ['上海', '上海市'],
  304. '500000' => ['重庆', '重庆市']
  305. ];
  306. // 添加省级简称映射
  307. $provinceAbbr = [
  308. '内蒙古' => '内蒙古自治区',
  309. '新疆' => '新疆维吾尔自治区',
  310. '广西' => '广西壮族自治区',
  311. '宁夏' => '宁夏回族自治区',
  312. '西藏' => '西藏自治区',
  313. ];
  314. foreach ($addressData as $province) {
  315. $pValue = $province['value'];
  316. $pLabel = $province['label'];
  317. // 添加省份全称和简称
  318. $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
  319. $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
  320. $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
  321. $provinceMap[$pValue] = ['label' => $pLabel, 'value' => $pValue]; // 新增:通过编码直接访问
  322. // 添加省级简称
  323. if (isset($provinceAbbr[$shortProvince])) {
  324. $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
  325. }
  326. // 处理直辖市
  327. if (isset($municipalities[$pValue])) {
  328. $cityLabel = $municipalities[$pValue][1];
  329. $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
  330. 'label' => $cityLabel,
  331. 'value' => $pValue,
  332. 'province' => $pValue
  333. ];
  334. // 添加直辖市区县映射
  335. foreach ($province['children'][0]['children'] as $district) {
  336. $dLabel = $district['label'];
  337. $countyMap[$dLabel] = [
  338. 'city_label' => $cityLabel,
  339. 'city_value' => $pValue,
  340. 'province_value' => $pValue
  341. ];
  342. // 添加区县简称(去掉"区"字)
  343. $shortDistrict = preg_replace('/区$/', '', $dLabel);
  344. if ($shortDistrict !== $dLabel) {
  345. $countyMap[$shortDistrict] = $countyMap[$dLabel];
  346. }
  347. }
  348. continue;
  349. }
  350. // 处理普通城市和区县
  351. foreach ($province['children'] as $city) {
  352. $cValue = $city['value'];
  353. $cLabel = $city['label'];
  354. // 添加城市全称和简称
  355. $shortCity = preg_replace('/(市|地区|盟)$/u', '', $cLabel);
  356. $cityMap[$cLabel] = [
  357. 'label' => $cLabel,
  358. 'value' => $cValue,
  359. 'province' => $pValue
  360. ];
  361. $cityMap[$shortCity] = [
  362. 'label' => $cLabel,
  363. 'value' => $cValue,
  364. 'province' => $pValue
  365. ];
  366. // 添加自治州特殊处理(如恩施)
  367. $specialMapping = [
  368. '延边朝鲜族自治州' => '延边',
  369. '恩施土家族苗族自治州' => '恩施',
  370. '湘西土家族苗族自治州' => '湘西',
  371. '阿坝藏族羌族自治州' => '阿坝',
  372. '凉山彝族自治州' => '凉山',
  373. '甘孜藏族自治州' => '甘孜',
  374. '黔东南苗族侗族自治州' => '黔东南',
  375. '黔南布依族苗族自治州' => '黔南',
  376. '黔西南布依族苗族自治州' => '黔西南',
  377. '楚雄彝族自治州' => '楚雄',
  378. '红河哈尼族彝族自治州' => '红河',
  379. '文山壮族苗族自治州' => '文山',
  380. '西双版纳傣族自治州' => '西双版纳',
  381. '大理白族自治州' => '大理',
  382. '德宏傣族景颇族自治州' => '德宏',
  383. '怒江傈僳族自治州' => '怒江',
  384. '迪庆藏族自治州' => '迪庆',
  385. '临夏回族自治州' => '临夏',
  386. '甘南藏族自治州' => '甘南',
  387. '海南藏族自治州' => '海南',
  388. '海北藏族自治州' => '海北',
  389. '海西蒙古族藏族自治州' => '海西',
  390. '黄南藏族自治州' => '黄南',
  391. '果洛藏族自治州' => '果洛',
  392. '玉树藏族自治州' => '玉树',
  393. '伊犁哈萨克自治州' => '伊犁',
  394. '博尔塔拉蒙古自治州' => '博尔塔拉',
  395. '昌吉回族自治州' => '昌吉',
  396. // '巴音郭楞蒙古自治州' => '巴州',
  397. '巴音郭楞蒙古自治州' => '巴音郭楞',
  398. '克孜勒苏柯尔克孜自治州' => '克孜勒苏',
  399. ];
  400. // $specialMapping = array_flip($specialMapping);
  401. if (isset($specialMapping[$shortCity])) {
  402. $cityMap[$specialMapping[$shortCity]] = [
  403. 'label' => $shortCity,
  404. 'value' => $cValue,
  405. 'province' => $pValue
  406. ];
  407. }
  408. // if (strpos($cLabel, '自治州') !== false) {
  409. //// dump($province['children']);
  410. // $autonomousShort = str_replace(['自治州', '土家族', '苗族', '壮族'], '', $cLabel);
  411. // $cityMap[$autonomousShort] = [
  412. // 'label' => $cLabel,
  413. // 'value' => $cValue,
  414. // 'province' => $pValue
  415. // ];
  416. // }
  417. // 添加城市常见简称(特殊处理)
  418. $specialAbbr = [
  419. '成都' => '成都市',
  420. '杭州' => '杭州市',
  421. '广州' => '广州市',
  422. '深圳' => '深圳市',
  423. '武汉' => '武汉市',
  424. '南京' => '南京市',
  425. '苏州' => '苏州市',
  426. '宁波' => '宁波市',
  427. '青岛' => '青岛市',
  428. ];
  429. if (isset($specialAbbr[$shortCity])) {
  430. $cityMap[$shortCity] = [
  431. 'label' => $specialAbbr[$shortCity],
  432. 'value' => $cValue,
  433. 'province' => $pValue
  434. ];
  435. }
  436. // 添加区县映射
  437. if (isset($city['children'])) {
  438. foreach ($city['children'] as $county) {
  439. $countyLabel = $county['label'];
  440. $countyMap[$countyLabel] = [
  441. 'city_label' => $cLabel,
  442. 'city_value' => $cValue,
  443. 'province_value' => $pValue
  444. ];
  445. // 添加区县简称(去掉"区"/"县"字)
  446. $shortCounty = preg_replace('/(区|县|市|自治县|黎族自治县|黎族苗族自治县)$/u', '', $countyLabel);
  447. if ($shortCounty !== $countyLabel) {
  448. $countyMap[$shortCounty] = $countyMap[$countyLabel];
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }
  455. //dd($cityMap);
  456. return [$provinceMap, $cityMap, $countyMap]; // 返回三个映射表
  457. }
  458. function parseAddress($str, $provinceMap, $cityMap, $countyMap) {
  459. static $cache = [];
  460. if (isset($cache[$str])) return $cache[$str];
  461. $str = str_replace(['·', ' ', '-', '-'], '', trim($str));
  462. // 0. 处理空字符串
  463. if (empty($str)) {
  464. $result = [
  465. 'province' => ['label' => '', 'value' => ''],
  466. 'city' => ['label' => '', 'value' => '']
  467. ];
  468. $cache[$str] = $result;
  469. return $result;
  470. }
  471. // 1. 处理直辖市简称
  472. $municipalityShort = ['京', '沪', '津', '渝'];
  473. $municipalityMap = [
  474. '京' => '北京市',
  475. '沪' => '上海市',
  476. '津' => '天津市',
  477. '渝' => '重庆市'
  478. ];
  479. if (in_array(mb_substr($str, 0, 1), $municipalityShort)) {
  480. $cityName = $municipalityMap[mb_substr($str, 0, 1)];
  481. $city = $cityMap[$cityName] ?? null;
  482. if ($city) {
  483. $provinceInfo = $provinceMap[$city['province']] ?? null;
  484. $result = [
  485. 'province' => $provinceInfo ?: ['label' => $cityName, 'value' => $city['province']],
  486. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  487. ];
  488. $cache[$str] = $result;
  489. return $result;
  490. }
  491. }
  492. // 2. 完整字符串匹配城市
  493. if (isset($cityMap[$str])) {
  494. $city = $cityMap[$str];
  495. $provinceInfo = $provinceMap[$city['province']] ?? null;
  496. $result = [
  497. 'province' => $provinceInfo ?: ['label' => '', 'value' => $city['province']],
  498. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  499. ];
  500. $cache[$str] = $result;
  501. return $result;
  502. }
  503. // 3. 匹配区县(新增)
  504. if (isset($countyMap[$str])) {
  505. $county = $countyMap[$str];
  506. $cityInfo = [
  507. 'label' => $county['city_label'],
  508. 'value' => $county['city_value']
  509. ];
  510. $provinceInfo = $provinceMap[$county['province_value']] ?? null;
  511. $result = [
  512. 'province' => $provinceInfo ?: ['label' => '', 'value' => $county['province_value']],
  513. 'city' => $cityInfo
  514. ];
  515. $cache[$str] = $result;
  516. return $result;
  517. }
  518. // 4. 优先匹配3字省份(如内蒙古)
  519. if (mb_strlen($str) >= 3) {
  520. $prefix = mb_substr($str, 0, 3);
  521. if (isset($provinceMap[$prefix])) {
  522. return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $countyMap, $cache, $str);
  523. }
  524. }
  525. // 5. 匹配2字省份
  526. if (mb_strlen($str) >= 2) {
  527. $prefix = mb_substr($str, 0, 2);
  528. if (isset($provinceMap[$prefix])) {
  529. return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $countyMap, $cache, $str);
  530. }
  531. }
  532. // 6. 城市关键词扫描(加强版)
  533. $cityKeys = array_keys($cityMap);
  534. usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
  535. foreach ($cityKeys as $key) {
  536. if (mb_strpos($str, $key) !== false) {
  537. $city = $cityMap[$key];
  538. $provinceInfo = $provinceMap[$city['province']] ?? null;
  539. $result = [
  540. 'province' => $provinceInfo ?: ['label' => '', 'value' => $city['province']],
  541. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  542. ];
  543. $cache[$str] = $result;
  544. return $result;
  545. }
  546. }
  547. // 7. 区县关键词扫描(新增)
  548. $countyKeys = array_keys($countyMap);
  549. usort($countyKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
  550. foreach ($countyKeys as $key) {
  551. if (mb_strpos($str, $key) !== false) {
  552. $county = $countyMap[$key];
  553. $provinceInfo = $provinceMap[$county['province_value']] ?? null;
  554. $result = [
  555. 'province' => $provinceInfo ?: ['label' => '', 'value' => $county['province_value']],
  556. 'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
  557. ];
  558. $cache[$str] = $result;
  559. return $result;
  560. }
  561. }
  562. // 8. 最终匹配失败
  563. $result = [
  564. 'province' => ['label' => '', 'value' => ''],
  565. 'city' => ['label' => '', 'value' => '']
  566. ];
  567. $cache[$str] = $result;
  568. return $result;
  569. }
  570. // 优化省份匹配后的处理
  571. function handleMatchedProvince($provinceKey, $remainder, $provinceMap, $cityMap, $countyMap, &$cache, $originalStr) {
  572. $province = $provinceMap[$provinceKey];
  573. // 0. 处理空余部分
  574. if (empty($remainder)) {
  575. $result = [
  576. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  577. 'city' => ['label' => '', 'value' => '']
  578. ];
  579. $cache[$originalStr] = $result;
  580. return $result;
  581. }
  582. // 1. 尝试完整匹配城市
  583. if (isset($cityMap[$remainder])) {
  584. $city = $cityMap[$remainder];
  585. $result = [
  586. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  587. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  588. ];
  589. $cache[$originalStr] = $result;
  590. return $result;
  591. }
  592. // 2. 尝试匹配区县(新增)
  593. if (isset($countyMap[$remainder])) {
  594. $county = $countyMap[$remainder];
  595. $result = [
  596. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  597. 'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
  598. ];
  599. $cache[$originalStr] = $result;
  600. return $result;
  601. }
  602. // 3. 城市关键词扫描
  603. $cityKeys = array_keys($cityMap);
  604. usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
  605. foreach ($cityKeys as $key) {
  606. if (mb_strpos($remainder, $key) !== false) {
  607. $city = $cityMap[$key];
  608. $result = [
  609. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  610. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  611. ];
  612. $cache[$originalStr] = $result;
  613. return $result;
  614. }
  615. }
  616. // 4. 区县关键词扫描(新增)
  617. $countyKeys = array_keys($countyMap);
  618. usort($countyKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
  619. foreach ($countyKeys as $key) {
  620. if (mb_strpos($remainder, $key) !== false) {
  621. $county = $countyMap[$key];
  622. $result = [
  623. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  624. 'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
  625. ];
  626. $cache[$originalStr] = $result;
  627. return $result;
  628. }
  629. }
  630. // 5. 仅返回省份信息
  631. $result = [
  632. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  633. 'city' => ['label' => '', 'value' => '']
  634. ];
  635. $cache[$originalStr] = $result;
  636. return $result;
  637. }
  638. //带转换数据
  639. private function constructAddress3() {
  640. $special = ["710000", "810000", "820000"];
  641. $address2Data = config("address");
  642. $extra_data = [];
  643. foreach ($address2Data as $value){
  644. if(in_array($value['value'], $special)){
  645. $extra_data[] = $value;
  646. }
  647. }
  648. // 1. 加载新数据结构
  649. $address2Data = config("address2");
  650. $address2Data = json_decode($address2Data, true);
  651. // 2. 转换数据结构为旧格式
  652. $addressData = [];
  653. foreach ($address2Data as $province) {
  654. $provinceEntry = [
  655. 'value' => $province['code'],
  656. 'label' => $province['name'],
  657. 'children' => []
  658. ];
  659. if(! isset($province['city'])) continue;
  660. foreach ($province['city'] as $city) {
  661. $cityEntry = [
  662. 'value' => $city['code'],
  663. 'label' => $city['name'],
  664. 'children' => []
  665. ];
  666. foreach ($city['area'] as $area) {
  667. $cityEntry['children'][] = [
  668. 'value' => $area['code'],
  669. 'label' => $area['name']
  670. ];
  671. }
  672. $provinceEntry['children'][] = $cityEntry;
  673. }
  674. $addressData[] = $provinceEntry;
  675. }
  676. $addressData = array_merge_recursive($addressData, $extra_data);
  677. echo json_encode($addressData);die;
  678. $provinceMap = [];
  679. $cityMap = [];
  680. $countyMap = []; // 新增区县映射表
  681. // 添加直辖市特殊处理
  682. $municipalities = [
  683. '110000' => ['北京', '北京市'],
  684. '120000' => ['天津', '天津市'],
  685. '310000' => ['上海', '上海市'],
  686. '500000' => ['重庆', '重庆市']
  687. ];
  688. // 添加省级简称映射
  689. $provinceAbbr = [
  690. '内蒙古' => '内蒙古自治区',
  691. '新疆' => '新疆维吾尔自治区',
  692. '广西' => '广西壮族自治区',
  693. '宁夏' => '宁夏回族自治区',
  694. '西藏' => '西藏自治区'
  695. ];
  696. foreach ($addressData as $province) {
  697. $pValue = $province['value'];
  698. $pLabel = $province['label'];
  699. // 添加省份全称和简称
  700. $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
  701. $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
  702. $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
  703. $provinceMap[$pValue] = ['label' => $pLabel, 'value' => $pValue]; // 新增:通过编码直接访问
  704. // 添加省级简称
  705. if (isset($provinceAbbr[$shortProvince])) {
  706. $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
  707. }
  708. // 处理直辖市
  709. if (isset($municipalities[$pValue])) {
  710. $cityLabel = $municipalities[$pValue][1];
  711. $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
  712. 'label' => $cityLabel,
  713. 'value' => $pValue,
  714. 'province' => $pValue
  715. ];
  716. // 添加直辖市区县映射
  717. foreach ($province['children'][0]['children'] as $district) {
  718. $dLabel = $district['label'];
  719. $countyMap[$dLabel] = [
  720. 'city_label' => $cityLabel,
  721. 'city_value' => $pValue,
  722. 'province_value' => $pValue
  723. ];
  724. // 添加区县简称(去掉"区"字)
  725. $shortDistrict = preg_replace('/区$/', '', $dLabel);
  726. if ($shortDistrict !== $dLabel) {
  727. $countyMap[$shortDistrict] = $countyMap[$dLabel];
  728. }
  729. }
  730. continue;
  731. }
  732. // 处理普通城市和区县
  733. foreach ($province['children'] as $city) {
  734. $cValue = $city['value'];
  735. $cLabel = $city['label'];
  736. // 添加城市全称和简称
  737. $shortCity = preg_replace('/市$/', '', $cLabel);
  738. $cityMap[$cLabel] = [
  739. 'label' => $cLabel,
  740. 'value' => $cValue,
  741. 'province' => $pValue
  742. ];
  743. $cityMap[$shortCity] = [
  744. 'label' => $cLabel,
  745. 'value' => $cValue,
  746. 'province' => $pValue
  747. ];
  748. // 添加自治州特殊处理(如恩施)
  749. if (strpos($cLabel, '自治州') !== false) {
  750. $autonomousShort = str_replace(['自治州', '土家族', '苗族', '壮族'], '', $cLabel);
  751. $cityMap[$autonomousShort] = [
  752. 'label' => $cLabel,
  753. 'value' => $cValue,
  754. 'province' => $pValue
  755. ];
  756. }
  757. // 添加城市常见简称(特殊处理)
  758. $specialAbbr = [
  759. '成都' => '成都市',
  760. '杭州' => '杭州市',
  761. '广州' => '广州市',
  762. '深圳' => '深圳市',
  763. '武汉' => '武汉市',
  764. '南京' => '南京市',
  765. '苏州' => '苏州市',
  766. '宁波' => '宁波市',
  767. '青岛' => '青岛市'
  768. ];
  769. if (isset($specialAbbr[$shortCity])) {
  770. $cityMap[$shortCity] = [
  771. 'label' => $specialAbbr[$shortCity],
  772. 'value' => $cValue,
  773. 'province' => $pValue
  774. ];
  775. }
  776. // 添加区县映射
  777. if (isset($city['children'])) {
  778. foreach ($city['children'] as $county) {
  779. $countyLabel = $county['label'];
  780. $countyMap[$countyLabel] = [
  781. 'city_label' => $cLabel,
  782. 'city_value' => $cValue,
  783. 'province_value' => $pValue
  784. ];
  785. // 添加区县简称(去掉"区"/"县"字)
  786. $shortCounty = preg_replace('/(区|县|市)$/u', '', $countyLabel);
  787. if ($shortCounty !== $countyLabel) {
  788. $countyMap[$shortCounty] = $countyMap[$countyLabel];
  789. }
  790. }
  791. }
  792. }
  793. }
  794. return [$provinceMap, $cityMap, $countyMap]; // 返回三个映射表
  795. }
  796. private function constructAddress2(){
  797. $addressData = config('address');
  798. $provinceMap = [];
  799. $cityMap = [];
  800. // 添加直辖市特殊处理
  801. $municipalities = [
  802. '110000' => ['北京', '北京市'],
  803. '120000' => ['天津', '天津市'],
  804. '310000' => ['上海', '上海市'],
  805. '500000' => ['重庆', '重庆市']
  806. ];
  807. // 添加省级简称映射
  808. $provinceAbbr = [
  809. '内蒙古' => '内蒙古自治区',
  810. '新疆' => '新疆维吾尔自治区',
  811. '广西' => '广西壮族自治区',
  812. '宁夏' => '宁夏回族自治区',
  813. '西藏' => '西藏自治区'
  814. ];
  815. foreach ($addressData as $province) {
  816. $pValue = $province['value'];
  817. $pLabel = $province['label'];
  818. // 添加省份全称和简称
  819. $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
  820. $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
  821. $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
  822. // 添加省级简称
  823. if (isset($provinceAbbr[$shortProvince])) {
  824. $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
  825. }
  826. // 处理直辖市
  827. if (isset($municipalities[$pValue])) {
  828. $cityLabel = $municipalities[$pValue][1];
  829. $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
  830. 'label' => $cityLabel,
  831. 'value' => $pValue,
  832. 'province' => $pValue
  833. ];
  834. continue;
  835. }
  836. // 处理普通城市
  837. foreach ($province['children'] as $city) {
  838. $cValue = $city['value'];
  839. $cLabel = $city['label'];
  840. // 添加城市全称和简称
  841. $shortCity = preg_replace('/市$/', '', $cLabel);
  842. $cityMap[$cLabel] = [
  843. 'label' => $cLabel,
  844. 'value' => $cValue,
  845. 'province' => $pValue
  846. ];
  847. $cityMap[$shortCity] = [
  848. 'label' => $cLabel,
  849. 'value' => $cValue,
  850. 'province' => $pValue
  851. ];
  852. // 添加城市常见简称(特殊处理)
  853. $specialAbbr = [
  854. '成都' => '成都市',
  855. '杭州' => '杭州市',
  856. '广州' => '广州市',
  857. '深圳' => '深圳市',
  858. '武汉' => '武汉市',
  859. '南京' => '南京市'
  860. ];
  861. if (isset($specialAbbr[$shortCity])) {
  862. $cityMap[$shortCity] = [
  863. 'label' => $specialAbbr[$shortCity],
  864. 'value' => $cValue,
  865. 'province' => $pValue
  866. ];
  867. }
  868. }
  869. }
  870. return [$provinceMap, $cityMap];
  871. }
  872. function parseAddress2($str, $provinceMap, $cityMap) {
  873. static $cache = [];
  874. if (isset($cache[$str])) return $cache[$str];
  875. $str = str_replace(['·', ' ', '-'], '', trim($str));
  876. // 1. 处理直辖市简称
  877. $municipalityShort = ['京', '沪', '津', '渝'];
  878. $municipalityMap = [
  879. '京' => '北京市',
  880. '沪' => '上海市',
  881. '津' => '天津市',
  882. '渝' => '重庆市'
  883. ];
  884. if (in_array(mb_substr($str, 0, 1), $municipalityShort)) {
  885. $cityName = $municipalityMap[mb_substr($str, 0, 1)];
  886. $city = $cityMap[$cityName] ?? null;
  887. if ($city) {
  888. $result = [
  889. 'province' => ['label' => $city['label'], 'value' => $city['province']],
  890. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  891. ];
  892. $cache[$str] = $result;
  893. return $result;
  894. }
  895. }
  896. // 2. 完整字符串匹配城市(加强版)
  897. if (isset($cityMap[$str])) {
  898. $city = $cityMap[$str];
  899. $province = $provinceMap[$city['province']] ?? null;
  900. $result = [
  901. 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
  902. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  903. ];
  904. $cache[$str] = $result;
  905. return $result;
  906. }
  907. // 3. 优先匹配3字省份(如内蒙古)
  908. if (mb_strlen($str) >= 3) {
  909. $prefix = mb_substr($str, 0, 3);
  910. if (isset($provinceMap[$prefix])) {
  911. return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $cache, $str);
  912. }
  913. }
  914. // 4. 匹配2字省份
  915. if (mb_strlen($str) >= 2) {
  916. $prefix = mb_substr($str, 0, 2);
  917. if (isset($provinceMap[$prefix])) {
  918. return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $cache, $str);
  919. }
  920. }
  921. // 5. 城市关键词扫描(加强版)
  922. $cityKeys = array_keys($cityMap);
  923. usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
  924. foreach ($cityKeys as $key) {
  925. if (mb_strpos($str, $key) !== false) {
  926. $city = $cityMap[$key];
  927. $province = $provinceMap[$city['province']] ?? null;
  928. // 特殊处理:当匹配到城市时,尝试找回省份
  929. if (!$province) {
  930. $province = $provinceMap[$city['province']] ?? ['label' => '', 'value' => ''];
  931. }
  932. $result = [
  933. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  934. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  935. ];
  936. $cache[$str] = $result;
  937. return $result;
  938. }
  939. }
  940. // 6. 最终匹配失败
  941. $result = [
  942. 'province' => ['label' => '', 'value' => ''],
  943. 'city' => ['label' => '', 'value' => '']
  944. ];
  945. $cache[$str] = $result;
  946. return $result;
  947. }
  948. // 优化省份匹配后的处理
  949. function handleMatchedProvince2($provinceKey, $remainder, $provinceMap, $cityMap, &$cache, $originalStr) {
  950. $province = $provinceMap[$provinceKey];
  951. // 直辖市特殊处理
  952. $municipalities = ['110000', '120000', '310000', '500000'];
  953. if (in_array($province['value'], $municipalities)) {
  954. $city = $cityMap[$province['label']] ?? null;
  955. if ($city) {
  956. $result = [
  957. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  958. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  959. ];
  960. $cache[$originalStr] = $result;
  961. return $result;
  962. }
  963. }
  964. // 尝试匹配剩余部分
  965. if ($remainder !== '') {
  966. // 先尝试完整匹配
  967. if (isset($cityMap[$remainder])) {
  968. $city = $cityMap[$remainder];
  969. $result = [
  970. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  971. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  972. ];
  973. $cache[$originalStr] = $result;
  974. return $result;
  975. }
  976. // 再尝试关键词扫描
  977. $cityKeys = array_keys($cityMap);
  978. usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
  979. foreach ($cityKeys as $key) {
  980. if (mb_strpos($remainder, $key) !== false) {
  981. $city = $cityMap[$key];
  982. $result = [
  983. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  984. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  985. ];
  986. $cache[$originalStr] = $result;
  987. return $result;
  988. }
  989. }
  990. }
  991. // 仅返回省份信息
  992. $result = [
  993. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  994. 'city' => ['label' => '', 'value' => '']
  995. ];
  996. $cache[$originalStr] = $result;
  997. return $result;
  998. }
  999. private function constructAddress1(){
  1000. $addressData = config('address');
  1001. // 构建映射表
  1002. $provinceMap = [];
  1003. $cityMap = [];
  1004. // 直辖市特殊处理(直接映射到市)
  1005. $municipalities = [
  1006. '110000' => ['北京', '北京市'],
  1007. '120000' => ['天津', '天津市'],
  1008. '310000' => ['上海', '上海市'],
  1009. '500000' => ['重庆', '重庆市']
  1010. ];
  1011. foreach ($addressData as $province) {
  1012. $pValue = $province['value'];
  1013. $pLabel = $province['label'];
  1014. // 生成省份简称
  1015. $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
  1016. $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
  1017. $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
  1018. // 处理直辖市(省市同名)
  1019. if (isset($municipalities[$pValue])) {
  1020. $cityLabel = $municipalities[$pValue][1];
  1021. $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
  1022. 'label' => $cityLabel,
  1023. 'value' => $pValue, // 直辖市使用省级代码
  1024. 'province' => $pValue
  1025. ];
  1026. continue;
  1027. }
  1028. // 处理普通城市
  1029. foreach ($province['children'] as $city) {
  1030. $cValue = $city['value'];
  1031. $cLabel = $city['label'];
  1032. // 生成城市简称
  1033. $shortCity = preg_replace('/市$/', '', $cLabel);
  1034. $cityMap[$cLabel] = [
  1035. 'label' => $cLabel,
  1036. 'value' => $cValue,
  1037. 'province' => $pValue
  1038. ];
  1039. if ($shortCity !== $cLabel) {
  1040. $cityMap[$shortCity] = [
  1041. 'label' => $cLabel,
  1042. 'value' => $cValue,
  1043. 'province' => $pValue
  1044. ];
  1045. }
  1046. }
  1047. }
  1048. return [$provinceMap, $cityMap];
  1049. }
  1050. // 解析函数
  1051. function parseAddress1($str, $provinceMap, $cityMap) {
  1052. static $cache = []; // 结果缓存
  1053. if (isset($cache[$str])) return $cache[$str];
  1054. $str = str_replace(['·', ' '], '', trim($str));
  1055. // 1. 处理带分隔符的格式 (广东-佛山)
  1056. if (preg_match('/[-\s]/', $str)) {
  1057. $parts = preg_split('/[-\s]+/', $str);
  1058. foreach ($parts as $part) {
  1059. if ($result = $this->parseAddress($part, $provinceMap, $cityMap)) {
  1060. $cache[$str] = $result;
  1061. return $result;
  1062. }
  1063. }
  1064. }
  1065. // 2. 完整字符串匹配城市
  1066. if (isset($cityMap[$str])) {
  1067. $city = $cityMap[$str];
  1068. $province = $provinceMap[$city['province']] ?? null;
  1069. $result = [
  1070. 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
  1071. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  1072. ];
  1073. $cache[$str] = $result;
  1074. return $result;
  1075. }
  1076. // 3. 优先匹配3字省份 (内蒙古)
  1077. if (mb_strlen($str) >= 3) {
  1078. $prefix = mb_substr($str, 0, 3);
  1079. if (isset($provinceMap[$prefix])) {
  1080. return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $cache, $str);
  1081. }
  1082. }
  1083. // 4. 匹配2字省份 (江苏)
  1084. if (mb_strlen($str) >= 2) {
  1085. $prefix = mb_substr($str, 0, 2);
  1086. if (isset($provinceMap[$prefix])) {
  1087. return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $cache, $str);
  1088. }
  1089. }
  1090. // 5. 城市关键词扫描 (解决"成都双流"类问题)
  1091. $cityKeys = array_keys($cityMap);
  1092. usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
  1093. foreach ($cityKeys as $key) {
  1094. if (mb_strpos($str, $key) !== false) {
  1095. $city = $cityMap[$key];
  1096. $province = $provinceMap[$city['province']] ?? null;
  1097. $result = [
  1098. 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
  1099. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  1100. ];
  1101. $cache[$str] = $result;
  1102. return $result;
  1103. }
  1104. }
  1105. // 6. 最终匹配失败
  1106. $result = [
  1107. 'province' => ['label' => '', 'value' => ''],
  1108. 'city' => ['label' => '', 'value' => '']
  1109. ];
  1110. $cache[$str] = $result;
  1111. return $result;
  1112. }
  1113. // 省份匹配后的处理
  1114. function handleMatchedProvince1($provinceKey, $remainder, $provinceMap, $cityMap, &$cache, $originalStr) {
  1115. $province = $provinceMap[$provinceKey];
  1116. // 直辖市特殊处理
  1117. if (isset($provinceMap[$province['label']])) {
  1118. $city = $cityMap[$province['label']] ?? null;
  1119. if ($city) {
  1120. $result = [
  1121. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  1122. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  1123. ];
  1124. $cache[$originalStr] = $result;
  1125. return $result;
  1126. }
  1127. }
  1128. // 尝试匹配剩余部分
  1129. if ($remainder !== '' && isset($cityMap[$remainder])) {
  1130. $city = $cityMap[$remainder];
  1131. $result = [
  1132. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  1133. 'city' => ['label' => $city['label'], 'value' => $city['value']]
  1134. ];
  1135. $cache[$originalStr] = $result;
  1136. return $result;
  1137. }
  1138. // 仅返回省份信息
  1139. $result = [
  1140. 'province' => ['label' => $province['label'], 'value' => $province['value']],
  1141. 'city' => ['label' => '', 'value' => '']
  1142. ];
  1143. $cache[$originalStr] = $result;
  1144. return $result;
  1145. }
  1146. }