CustomerWriteReport.php 49 KB

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