CustomerWriteReport.php 50 KB

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