|
@@ -0,0 +1,1264 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Console\Commands;
|
|
|
+
|
|
|
+use App\Model\AreaMap;
|
|
|
+use App\Model\BasicType;
|
|
|
+use App\Model\BasicTypeAllUse;
|
|
|
+use App\Model\Customer;
|
|
|
+use App\Model\CustomerInfo;
|
|
|
+use App\Model\CustomerReport;
|
|
|
+use App\Model\CustomerReportDepart;
|
|
|
+use App\Model\FollowUpRecord;
|
|
|
+use App\Model\SalesOrder;
|
|
|
+use App\Model\SeeRange;
|
|
|
+use Illuminate\Console\Command;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class CustomerWriteReport extends Command
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * The name and signature of the console command.
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $signature = 'command:customer_write_report';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The console command description.
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $description = 'Command description';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create a new command instance.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ parent::__construct();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Execute the console command.
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function handle()
|
|
|
+ {
|
|
|
+ echo "执行任务--------start---------------\n";
|
|
|
+
|
|
|
+ try {
|
|
|
+ $this->customerWrite();
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ echo "发生异常:" . $exception->getFile() . "|" . $exception->getMessage() . "|" . $exception->getLine() . "\n";
|
|
|
+ }
|
|
|
+
|
|
|
+ echo "执行任务--------end---------------\n";
|
|
|
+ }
|
|
|
+
|
|
|
+ private function customerWrite(){
|
|
|
+ //今天到上月今天的时间戳
|
|
|
+ $nowStamp = time();
|
|
|
+ $lastMonth = date("Y-m-d 00:00:00", strtotime("-1 month"));
|
|
|
+ $lastMonthStamp = strtotime($lastMonth);
|
|
|
+
|
|
|
+ list($provinceMap, $cityMap, $countyMap) = $this->constructAddress();
|
|
|
+// dd($provinceMap, $cityMap, $countyMap);
|
|
|
+// dd(count($provinceMap), count($cityMap), count($countyMap));
|
|
|
+// $this->forInsertArea($provinceMap, $cityMap, $countyMap);dd(122);
|
|
|
+ Customer::where("del_time",0)
|
|
|
+ ->where('enter_time','>=', $lastMonthStamp)
|
|
|
+ ->where('enter_time','<=', $nowStamp)
|
|
|
+ ->orderBy('enter_time','asc')
|
|
|
+ ->select('id as customer_id','title','model_type','consulting_product_new','enter_time','address2','top_depart_id','customer_from','car_type')
|
|
|
+ ->chunk(200, function ($data) use($provinceMap, $cityMap, $countyMap,$nowStamp){
|
|
|
+ // 开启事务
|
|
|
+ DB::transaction(function () use ($data, $provinceMap, $cityMap, $countyMap, $nowStamp) {
|
|
|
+ $dataArray = $data->toArray();
|
|
|
+ list($main, $main_depart) = $this->processingData($dataArray, $provinceMap, $cityMap, $countyMap, $nowStamp);
|
|
|
+ if (! empty($main)) {
|
|
|
+ $customer_ids = array_column($dataArray, 'customer_id');
|
|
|
+
|
|
|
+ // 删除旧数据
|
|
|
+ CustomerReport::where('del_time', 0)
|
|
|
+ ->whereIn('customer_id', $customer_ids)
|
|
|
+ ->update(['del_time' => $nowStamp]);
|
|
|
+ CustomerReportDepart::where('del_time', 0)
|
|
|
+ ->whereIn('customer_id', $customer_ids)
|
|
|
+ ->update(['del_time' => $nowStamp]);
|
|
|
+
|
|
|
+ // 写入新数据
|
|
|
+ CustomerReport::insert($main);
|
|
|
+ CustomerReportDepart::insert($main_depart);
|
|
|
+
|
|
|
+ echo '写入中' . PHP_EOL;
|
|
|
+ } else {
|
|
|
+ echo '无' . PHP_EOL;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private function processingData($data,$provinceMap,$cityMap,$countyMap,$time){
|
|
|
+ $return = [];
|
|
|
+
|
|
|
+ $customer_id = array_column($data,'customer_id');
|
|
|
+ $range = SeeRange::where('del_time',0)
|
|
|
+ ->whereIn('data_id', $customer_id)
|
|
|
+ ->where('data_type',SeeRange::type_one)
|
|
|
+ ->where('type',SeeRange::data_three)
|
|
|
+ ->select('data_id as customer_id','param_id as top_depart_id')
|
|
|
+ ->get()->toArray();
|
|
|
+ $map = [];
|
|
|
+ foreach ($range as $value){
|
|
|
+ $map[$value['customer_id']][] = $value['top_depart_id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $isset_map = SalesOrder::where('del_time', 0)
|
|
|
+ ->whereIn('customer_id', $customer_id)
|
|
|
+ ->pluck('customer_id')
|
|
|
+ ->unique()
|
|
|
+ ->flip()
|
|
|
+ ->map(fn() => 1)
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ // 最新的跟进记录
|
|
|
+ $latestRecords = FollowUpRecord::whereIn('data_id', $customer_id)
|
|
|
+ ->where('type', FollowUpRecord::type_one)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->whereIn('id', function ($query) use ($customer_id) {
|
|
|
+ $query->select(DB::raw('MAX(id)'))
|
|
|
+ ->from('follow_up_record')
|
|
|
+ ->whereColumn('data_id', 'follow_up_record.data_id')
|
|
|
+ ->whereIn('data_id', $customer_id)
|
|
|
+ ->where('type', FollowUpRecord::type_one)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->groupBy('data_id');
|
|
|
+ })
|
|
|
+ ->get();
|
|
|
+ $latestRecordsArray = $latestRecords->keyBy('data_id')
|
|
|
+ ->map(fn($item) => $item->follow_type)
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ $customer_info = CustomerInfo::where('del_time',0)
|
|
|
+ ->whereIn('customer_id',$customer_id)
|
|
|
+ ->whereIn('type',[CustomerInfo::type_one])
|
|
|
+ ->select('type','contact_type','contact_info','customer_id')
|
|
|
+ ->get()->toArray();
|
|
|
+ $customer_info_map = [];
|
|
|
+ foreach ($customer_info as $value){
|
|
|
+ if(! $value['contact_info']) continue;
|
|
|
+ if(! empty($customer_info_map[$value['customer_id']])) continue;
|
|
|
+ $customer_info_map[$value['customer_id']] = $value['contact_info'];
|
|
|
+ }
|
|
|
+ $array = array_unique(array_merge_recursive(array_column($data,'customer_from'),array_column($data,'car_type')));
|
|
|
+ $basic_map = BasicType::whereIn('id',$array)
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ $basic_all_map = BasicTypeAllUse::where('type', BasicTypeAllUse::type_one)
|
|
|
+ ->whereIn('id',array_unique(array_column($data,'consulting_product_new')))
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ $detail_insert = [];
|
|
|
+ foreach ($data as $key => $value){
|
|
|
+ $province = $province_code = $city = $city_code = "";
|
|
|
+ $is_success = 0;
|
|
|
+ if(! empty($value['address2'])){
|
|
|
+ $tmp = $this->parseAddress($value['address2'], $provinceMap, $cityMap,$countyMap);
|
|
|
+ if(empty($tmp['province']['label']) || empty($tmp['city']['label'])){
|
|
|
+// $tmp['origin'] = $value['address2'];
|
|
|
+// $a = [
|
|
|
+// "provice" => $tmp['province']['label'],
|
|
|
+// "provice_code" => $tmp['province']['value'],
|
|
|
+// "city" => $tmp['city']['label'],
|
|
|
+// "city_code" => $tmp['city']['value'],
|
|
|
+// "origin" => $value['address2']
|
|
|
+// ];
|
|
|
+// $return[] = $a;
|
|
|
+ }else{
|
|
|
+ $province = $tmp['province']['label'];
|
|
|
+ $province_code = $tmp['province']['value'];
|
|
|
+ $city = $tmp['city']['label'];
|
|
|
+ $city_code = $tmp['city']['value'];
|
|
|
+ $is_success = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $data[$key]['province'] = $province;
|
|
|
+ $data[$key]['province_code'] = $province_code;
|
|
|
+ $data[$key]['city'] = $city;
|
|
|
+ $data[$key]['city_code'] = $city_code;
|
|
|
+ $data[$key]['is_success'] = $is_success;
|
|
|
+ $belong_depart_id = [$value['top_depart_id']];
|
|
|
+ if(isset($map[$value['customer_id']])) $belong_depart_id = array_unique($map[$value['customer_id']]);
|
|
|
+ foreach ($belong_depart_id as $b){
|
|
|
+ $detail_insert[] = [
|
|
|
+ 'customer_id' => $value['customer_id'],
|
|
|
+ 'top_depart_id' => $b,
|
|
|
+ 'crt_time' => $time
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $type = -1;
|
|
|
+ if(isset($isset_map[$value['customer_id']])){
|
|
|
+ $type = 3;
|
|
|
+ }else{
|
|
|
+ if(isset($latestRecordsArray[$value['customer_id']])) $type = $latestRecordsArray[$value['customer_id']];
|
|
|
+ }
|
|
|
+
|
|
|
+ $data[$key]['type'] = $type;
|
|
|
+ $data[$key]['contact_info'] = $customer_info_map[$value['customer_id']] ?? "";
|
|
|
+ $data[$key]['customer_from'] = $basic_map[$value['customer_from']] ?? "";
|
|
|
+ $data[$key]['car_type'] = $customer_info_map[$value['car_type']] ?? "";
|
|
|
+ $data[$key]['consulting_product_new'] = $basic_all_map[$value['consulting_product_new']] ?? "";
|
|
|
+ $data[$key]['crt_time'] = $time;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [$data, $detail_insert];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function forInsertArea($provinceMap, $cityMap, $countyMap){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($provinceMap as $key => $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'key' => $key,
|
|
|
+ 'label' => $value['label'],
|
|
|
+ 'value' => $value['value'],
|
|
|
+ 'province' => "",
|
|
|
+ 'type' => 1,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ foreach ($cityMap as $key => $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'key' => $key,
|
|
|
+ 'label' => $value['label'],
|
|
|
+ 'value' => $value['value'],
|
|
|
+ 'province' => $value['province'],
|
|
|
+ 'type' => 2,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ foreach ($countyMap as $key => $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'key' => $key,
|
|
|
+ 'label' => $value['city_label'],
|
|
|
+ 'value' => $value['city_value'],
|
|
|
+ 'province' => $value['province_value'],
|
|
|
+ 'type' => 3,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ AreaMap::where('del_time',0)
|
|
|
+ ->where('is_create_by_human',0)
|
|
|
+ ->update(['del_time' => time()]);
|
|
|
+ AreaMap::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function constructAddress($type = 0) {
|
|
|
+ if(! empty($type)){
|
|
|
+ $result = AreaMap::where('del_time',0)
|
|
|
+ ->select('key','label','value','province','type')
|
|
|
+ ->get()->toArray();
|
|
|
+ $provinceMap = [];
|
|
|
+ $cityMap = [];
|
|
|
+ $countyMap = []; // 新增区县映射表
|
|
|
+ foreach ($result as $value){
|
|
|
+ if($value['type'] == 1){
|
|
|
+ $provinceMap[$value['key']] = [
|
|
|
+ 'label' => $value['label'],
|
|
|
+ 'value' => $value['value'],
|
|
|
+ ];
|
|
|
+ }elseif($value['type'] == 2){
|
|
|
+ $cityMap[$value['key']] = [
|
|
|
+ 'label' => $value['label'],
|
|
|
+ 'value' => $value['value'],
|
|
|
+ 'province' => $value['province'],
|
|
|
+ ];
|
|
|
+ }else{
|
|
|
+ $countyMap[$value['key']] = [
|
|
|
+ 'city_label' => $value['label'],
|
|
|
+ 'city_value' => $value['value'],
|
|
|
+ 'province_value' => $value['value'],
|
|
|
+ 'type' => 3,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $addressData = config('address3');
|
|
|
+ if(is_string($addressData)) $addressData = json_decode($addressData, true);
|
|
|
+ $provinceMap = [];
|
|
|
+ $cityMap = [];
|
|
|
+ $countyMap = []; // 新增区县映射表
|
|
|
+
|
|
|
+ // 添加直辖市特殊处理
|
|
|
+ $municipalities = [
|
|
|
+ '110000' => ['北京', '北京市'],
|
|
|
+ '120000' => ['天津', '天津市'],
|
|
|
+ '310000' => ['上海', '上海市'],
|
|
|
+ '500000' => ['重庆', '重庆市']
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加省级简称映射
|
|
|
+ $provinceAbbr = [
|
|
|
+ '内蒙古' => '内蒙古自治区',
|
|
|
+ '新疆' => '新疆维吾尔自治区',
|
|
|
+ '广西' => '广西壮族自治区',
|
|
|
+ '宁夏' => '宁夏回族自治区',
|
|
|
+ '西藏' => '西藏自治区',
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($addressData as $province) {
|
|
|
+ $pValue = $province['value'];
|
|
|
+ $pLabel = $province['label'];
|
|
|
+
|
|
|
+ // 添加省份全称和简称
|
|
|
+ $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
|
|
|
+ $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ $provinceMap[$pValue] = ['label' => $pLabel, 'value' => $pValue]; // 新增:通过编码直接访问
|
|
|
+
|
|
|
+ // 添加省级简称
|
|
|
+ if (isset($provinceAbbr[$shortProvince])) {
|
|
|
+ $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理直辖市
|
|
|
+ if (isset($municipalities[$pValue])) {
|
|
|
+ $cityLabel = $municipalities[$pValue][1];
|
|
|
+ $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
|
|
|
+ 'label' => $cityLabel,
|
|
|
+ 'value' => $pValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加直辖市区县映射
|
|
|
+ foreach ($province['children'][0]['children'] as $district) {
|
|
|
+ $dLabel = $district['label'];
|
|
|
+ $countyMap[$dLabel] = [
|
|
|
+ 'city_label' => $cityLabel,
|
|
|
+ 'city_value' => $pValue,
|
|
|
+ 'province_value' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加区县简称(去掉"区"字)
|
|
|
+ $shortDistrict = preg_replace('/区$/', '', $dLabel);
|
|
|
+ if ($shortDistrict !== $dLabel) {
|
|
|
+ $countyMap[$shortDistrict] = $countyMap[$dLabel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理普通城市和区县
|
|
|
+ foreach ($province['children'] as $city) {
|
|
|
+ $cValue = $city['value'];
|
|
|
+ $cLabel = $city['label'];
|
|
|
+
|
|
|
+ // 添加城市全称和简称
|
|
|
+ $shortCity = preg_replace('/(市|地区|盟)$/u', '', $cLabel);
|
|
|
+ $cityMap[$cLabel] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ $cityMap[$shortCity] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加自治州特殊处理(如恩施)
|
|
|
+ $specialMapping = [
|
|
|
+ '延边朝鲜族自治州' => '延边',
|
|
|
+ '恩施土家族苗族自治州' => '恩施',
|
|
|
+ '湘西土家族苗族自治州' => '湘西',
|
|
|
+ '阿坝藏族羌族自治州' => '阿坝',
|
|
|
+ '凉山彝族自治州' => '凉山',
|
|
|
+ '甘孜藏族自治州' => '甘孜',
|
|
|
+ '黔东南苗族侗族自治州' => '黔东南',
|
|
|
+ '黔南布依族苗族自治州' => '黔南',
|
|
|
+ '黔西南布依族苗族自治州' => '黔西南',
|
|
|
+ '楚雄彝族自治州' => '楚雄',
|
|
|
+ '红河哈尼族彝族自治州' => '红河',
|
|
|
+ '文山壮族苗族自治州' => '文山',
|
|
|
+ '西双版纳傣族自治州' => '西双版纳',
|
|
|
+ '大理白族自治州' => '大理',
|
|
|
+ '德宏傣族景颇族自治州' => '德宏',
|
|
|
+ '怒江傈僳族自治州' => '怒江',
|
|
|
+ '迪庆藏族自治州' => '迪庆',
|
|
|
+ '临夏回族自治州' => '临夏',
|
|
|
+ '甘南藏族自治州' => '甘南',
|
|
|
+ '海南藏族自治州' => '海南',
|
|
|
+ '海北藏族自治州' => '海北',
|
|
|
+ '海西蒙古族藏族自治州' => '海西',
|
|
|
+ '黄南藏族自治州' => '黄南',
|
|
|
+ '果洛藏族自治州' => '果洛',
|
|
|
+ '玉树藏族自治州' => '玉树',
|
|
|
+ '伊犁哈萨克自治州' => '伊犁',
|
|
|
+ '博尔塔拉蒙古自治州' => '博尔塔拉',
|
|
|
+ '昌吉回族自治州' => '昌吉',
|
|
|
+// '巴音郭楞蒙古自治州' => '巴州',
|
|
|
+ '巴音郭楞蒙古自治州' => '巴音郭楞',
|
|
|
+ '克孜勒苏柯尔克孜自治州' => '克孜勒苏',
|
|
|
+ ];
|
|
|
+// $specialMapping = array_flip($specialMapping);
|
|
|
+
|
|
|
+ if (isset($specialMapping[$shortCity])) {
|
|
|
+ $cityMap[$specialMapping[$shortCity]] = [
|
|
|
+ 'label' => $shortCity,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+// if (strpos($cLabel, '自治州') !== false) {
|
|
|
+//// dump($province['children']);
|
|
|
+// $autonomousShort = str_replace(['自治州', '土家族', '苗族', '壮族'], '', $cLabel);
|
|
|
+// $cityMap[$autonomousShort] = [
|
|
|
+// 'label' => $cLabel,
|
|
|
+// 'value' => $cValue,
|
|
|
+// 'province' => $pValue
|
|
|
+// ];
|
|
|
+// }
|
|
|
+
|
|
|
+ // 添加城市常见简称(特殊处理)
|
|
|
+ $specialAbbr = [
|
|
|
+ '成都' => '成都市',
|
|
|
+ '杭州' => '杭州市',
|
|
|
+ '广州' => '广州市',
|
|
|
+ '深圳' => '深圳市',
|
|
|
+ '武汉' => '武汉市',
|
|
|
+ '南京' => '南京市',
|
|
|
+ '苏州' => '苏州市',
|
|
|
+ '宁波' => '宁波市',
|
|
|
+ '青岛' => '青岛市',
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (isset($specialAbbr[$shortCity])) {
|
|
|
+ $cityMap[$shortCity] = [
|
|
|
+ 'label' => $specialAbbr[$shortCity],
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加区县映射
|
|
|
+ if (isset($city['children'])) {
|
|
|
+ foreach ($city['children'] as $county) {
|
|
|
+ $countyLabel = $county['label'];
|
|
|
+ $countyMap[$countyLabel] = [
|
|
|
+ 'city_label' => $cLabel,
|
|
|
+ 'city_value' => $cValue,
|
|
|
+ 'province_value' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加区县简称(去掉"区"/"县"字)
|
|
|
+ $shortCounty = preg_replace('/(区|县|市|自治县|黎族自治县|黎族苗族自治县)$/u', '', $countyLabel);
|
|
|
+ if ($shortCounty !== $countyLabel) {
|
|
|
+ $countyMap[$shortCounty] = $countyMap[$countyLabel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+//dd($cityMap);
|
|
|
+ return [$provinceMap, $cityMap, $countyMap]; // 返回三个映射表
|
|
|
+ }
|
|
|
+
|
|
|
+ function parseAddress($str, $provinceMap, $cityMap, $countyMap) {
|
|
|
+ static $cache = [];
|
|
|
+ if (isset($cache[$str])) return $cache[$str];
|
|
|
+
|
|
|
+ $str = str_replace(['·', ' ', '-', '-'], '', trim($str));
|
|
|
+
|
|
|
+ // 0. 处理空字符串
|
|
|
+ if (empty($str)) {
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => '', 'value' => ''],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 处理直辖市简称
|
|
|
+ $municipalityShort = ['京', '沪', '津', '渝'];
|
|
|
+ $municipalityMap = [
|
|
|
+ '京' => '北京市',
|
|
|
+ '沪' => '上海市',
|
|
|
+ '津' => '天津市',
|
|
|
+ '渝' => '重庆市'
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (in_array(mb_substr($str, 0, 1), $municipalityShort)) {
|
|
|
+ $cityName = $municipalityMap[mb_substr($str, 0, 1)];
|
|
|
+ $city = $cityMap[$cityName] ?? null;
|
|
|
+ if ($city) {
|
|
|
+ $provinceInfo = $provinceMap[$city['province']] ?? null;
|
|
|
+ $result = [
|
|
|
+ 'province' => $provinceInfo ?: ['label' => $cityName, 'value' => $city['province']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 完整字符串匹配城市
|
|
|
+ if (isset($cityMap[$str])) {
|
|
|
+ $city = $cityMap[$str];
|
|
|
+ $provinceInfo = $provinceMap[$city['province']] ?? null;
|
|
|
+ $result = [
|
|
|
+ 'province' => $provinceInfo ?: ['label' => '', 'value' => $city['province']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 匹配区县(新增)
|
|
|
+ if (isset($countyMap[$str])) {
|
|
|
+ $county = $countyMap[$str];
|
|
|
+ $cityInfo = [
|
|
|
+ 'label' => $county['city_label'],
|
|
|
+ 'value' => $county['city_value']
|
|
|
+ ];
|
|
|
+ $provinceInfo = $provinceMap[$county['province_value']] ?? null;
|
|
|
+
|
|
|
+ $result = [
|
|
|
+ 'province' => $provinceInfo ?: ['label' => '', 'value' => $county['province_value']],
|
|
|
+ 'city' => $cityInfo
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 优先匹配3字省份(如内蒙古)
|
|
|
+ if (mb_strlen($str) >= 3) {
|
|
|
+ $prefix = mb_substr($str, 0, 3);
|
|
|
+ if (isset($provinceMap[$prefix])) {
|
|
|
+ return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $countyMap, $cache, $str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 匹配2字省份
|
|
|
+ if (mb_strlen($str) >= 2) {
|
|
|
+ $prefix = mb_substr($str, 0, 2);
|
|
|
+ if (isset($provinceMap[$prefix])) {
|
|
|
+ return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $countyMap, $cache, $str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 城市关键词扫描(加强版)
|
|
|
+ $cityKeys = array_keys($cityMap);
|
|
|
+ usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
|
|
|
+
|
|
|
+ foreach ($cityKeys as $key) {
|
|
|
+ if (mb_strpos($str, $key) !== false) {
|
|
|
+ $city = $cityMap[$key];
|
|
|
+ $provinceInfo = $provinceMap[$city['province']] ?? null;
|
|
|
+
|
|
|
+ $result = [
|
|
|
+ 'province' => $provinceInfo ?: ['label' => '', 'value' => $city['province']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. 区县关键词扫描(新增)
|
|
|
+ $countyKeys = array_keys($countyMap);
|
|
|
+ usort($countyKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
|
|
|
+
|
|
|
+ foreach ($countyKeys as $key) {
|
|
|
+ if (mb_strpos($str, $key) !== false) {
|
|
|
+ $county = $countyMap[$key];
|
|
|
+ $provinceInfo = $provinceMap[$county['province_value']] ?? null;
|
|
|
+
|
|
|
+ $result = [
|
|
|
+ 'province' => $provinceInfo ?: ['label' => '', 'value' => $county['province_value']],
|
|
|
+ 'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 8. 最终匹配失败
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => '', 'value' => ''],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 优化省份匹配后的处理
|
|
|
+ function handleMatchedProvince($provinceKey, $remainder, $provinceMap, $cityMap, $countyMap, &$cache, $originalStr) {
|
|
|
+ $province = $provinceMap[$provinceKey];
|
|
|
+
|
|
|
+ // 0. 处理空余部分
|
|
|
+ if (empty($remainder)) {
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 尝试完整匹配城市
|
|
|
+ if (isset($cityMap[$remainder])) {
|
|
|
+ $city = $cityMap[$remainder];
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 尝试匹配区县(新增)
|
|
|
+ if (isset($countyMap[$remainder])) {
|
|
|
+ $county = $countyMap[$remainder];
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 城市关键词扫描
|
|
|
+ $cityKeys = array_keys($cityMap);
|
|
|
+ usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
|
|
|
+
|
|
|
+ foreach ($cityKeys as $key) {
|
|
|
+ if (mb_strpos($remainder, $key) !== false) {
|
|
|
+ $city = $cityMap[$key];
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 区县关键词扫描(新增)
|
|
|
+ $countyKeys = array_keys($countyMap);
|
|
|
+ usort($countyKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
|
|
|
+
|
|
|
+ foreach ($countyKeys as $key) {
|
|
|
+ if (mb_strpos($remainder, $key) !== false) {
|
|
|
+ $county = $countyMap[$key];
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 仅返回省份信息
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //带转换数据
|
|
|
+ private function constructAddress3() {
|
|
|
+ $special = ["710000", "810000", "820000"];
|
|
|
+ $address2Data = config("address");
|
|
|
+ $extra_data = [];
|
|
|
+ foreach ($address2Data as $value){
|
|
|
+ if(in_array($value['value'], $special)){
|
|
|
+ $extra_data[] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 加载新数据结构
|
|
|
+ $address2Data = config("address2");
|
|
|
+ $address2Data = json_decode($address2Data, true);
|
|
|
+
|
|
|
+ // 2. 转换数据结构为旧格式
|
|
|
+ $addressData = [];
|
|
|
+ foreach ($address2Data as $province) {
|
|
|
+ $provinceEntry = [
|
|
|
+ 'value' => $province['code'],
|
|
|
+ 'label' => $province['name'],
|
|
|
+ 'children' => []
|
|
|
+ ];
|
|
|
+ if(! isset($province['city'])) continue;
|
|
|
+
|
|
|
+ foreach ($province['city'] as $city) {
|
|
|
+ $cityEntry = [
|
|
|
+ 'value' => $city['code'],
|
|
|
+ 'label' => $city['name'],
|
|
|
+ 'children' => []
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($city['area'] as $area) {
|
|
|
+ $cityEntry['children'][] = [
|
|
|
+ 'value' => $area['code'],
|
|
|
+ 'label' => $area['name']
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $provinceEntry['children'][] = $cityEntry;
|
|
|
+ }
|
|
|
+
|
|
|
+ $addressData[] = $provinceEntry;
|
|
|
+ }
|
|
|
+
|
|
|
+ $addressData = array_merge_recursive($addressData, $extra_data);
|
|
|
+ echo json_encode($addressData);die;
|
|
|
+
|
|
|
+ $provinceMap = [];
|
|
|
+ $cityMap = [];
|
|
|
+ $countyMap = []; // 新增区县映射表
|
|
|
+
|
|
|
+ // 添加直辖市特殊处理
|
|
|
+ $municipalities = [
|
|
|
+ '110000' => ['北京', '北京市'],
|
|
|
+ '120000' => ['天津', '天津市'],
|
|
|
+ '310000' => ['上海', '上海市'],
|
|
|
+ '500000' => ['重庆', '重庆市']
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加省级简称映射
|
|
|
+ $provinceAbbr = [
|
|
|
+ '内蒙古' => '内蒙古自治区',
|
|
|
+ '新疆' => '新疆维吾尔自治区',
|
|
|
+ '广西' => '广西壮族自治区',
|
|
|
+ '宁夏' => '宁夏回族自治区',
|
|
|
+ '西藏' => '西藏自治区'
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($addressData as $province) {
|
|
|
+ $pValue = $province['value'];
|
|
|
+ $pLabel = $province['label'];
|
|
|
+
|
|
|
+ // 添加省份全称和简称
|
|
|
+ $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
|
|
|
+ $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ $provinceMap[$pValue] = ['label' => $pLabel, 'value' => $pValue]; // 新增:通过编码直接访问
|
|
|
+
|
|
|
+ // 添加省级简称
|
|
|
+ if (isset($provinceAbbr[$shortProvince])) {
|
|
|
+ $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理直辖市
|
|
|
+ if (isset($municipalities[$pValue])) {
|
|
|
+ $cityLabel = $municipalities[$pValue][1];
|
|
|
+ $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
|
|
|
+ 'label' => $cityLabel,
|
|
|
+ 'value' => $pValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加直辖市区县映射
|
|
|
+ foreach ($province['children'][0]['children'] as $district) {
|
|
|
+ $dLabel = $district['label'];
|
|
|
+ $countyMap[$dLabel] = [
|
|
|
+ 'city_label' => $cityLabel,
|
|
|
+ 'city_value' => $pValue,
|
|
|
+ 'province_value' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加区县简称(去掉"区"字)
|
|
|
+ $shortDistrict = preg_replace('/区$/', '', $dLabel);
|
|
|
+ if ($shortDistrict !== $dLabel) {
|
|
|
+ $countyMap[$shortDistrict] = $countyMap[$dLabel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理普通城市和区县
|
|
|
+ foreach ($province['children'] as $city) {
|
|
|
+ $cValue = $city['value'];
|
|
|
+ $cLabel = $city['label'];
|
|
|
+
|
|
|
+ // 添加城市全称和简称
|
|
|
+ $shortCity = preg_replace('/市$/', '', $cLabel);
|
|
|
+ $cityMap[$cLabel] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ $cityMap[$shortCity] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加自治州特殊处理(如恩施)
|
|
|
+ if (strpos($cLabel, '自治州') !== false) {
|
|
|
+ $autonomousShort = str_replace(['自治州', '土家族', '苗族', '壮族'], '', $cLabel);
|
|
|
+ $cityMap[$autonomousShort] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加城市常见简称(特殊处理)
|
|
|
+ $specialAbbr = [
|
|
|
+ '成都' => '成都市',
|
|
|
+ '杭州' => '杭州市',
|
|
|
+ '广州' => '广州市',
|
|
|
+ '深圳' => '深圳市',
|
|
|
+ '武汉' => '武汉市',
|
|
|
+ '南京' => '南京市',
|
|
|
+ '苏州' => '苏州市',
|
|
|
+ '宁波' => '宁波市',
|
|
|
+ '青岛' => '青岛市'
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (isset($specialAbbr[$shortCity])) {
|
|
|
+ $cityMap[$shortCity] = [
|
|
|
+ 'label' => $specialAbbr[$shortCity],
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加区县映射
|
|
|
+ if (isset($city['children'])) {
|
|
|
+ foreach ($city['children'] as $county) {
|
|
|
+ $countyLabel = $county['label'];
|
|
|
+ $countyMap[$countyLabel] = [
|
|
|
+ 'city_label' => $cLabel,
|
|
|
+ 'city_value' => $cValue,
|
|
|
+ 'province_value' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加区县简称(去掉"区"/"县"字)
|
|
|
+ $shortCounty = preg_replace('/(区|县|市)$/u', '', $countyLabel);
|
|
|
+ if ($shortCounty !== $countyLabel) {
|
|
|
+ $countyMap[$shortCounty] = $countyMap[$countyLabel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [$provinceMap, $cityMap, $countyMap]; // 返回三个映射表
|
|
|
+ }
|
|
|
+
|
|
|
+ private function constructAddress2(){
|
|
|
+ $addressData = config('address');
|
|
|
+ $provinceMap = [];
|
|
|
+ $cityMap = [];
|
|
|
+
|
|
|
+ // 添加直辖市特殊处理
|
|
|
+ $municipalities = [
|
|
|
+ '110000' => ['北京', '北京市'],
|
|
|
+ '120000' => ['天津', '天津市'],
|
|
|
+ '310000' => ['上海', '上海市'],
|
|
|
+ '500000' => ['重庆', '重庆市']
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加省级简称映射
|
|
|
+ $provinceAbbr = [
|
|
|
+ '内蒙古' => '内蒙古自治区',
|
|
|
+ '新疆' => '新疆维吾尔自治区',
|
|
|
+ '广西' => '广西壮族自治区',
|
|
|
+ '宁夏' => '宁夏回族自治区',
|
|
|
+ '西藏' => '西藏自治区'
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($addressData as $province) {
|
|
|
+ $pValue = $province['value'];
|
|
|
+ $pLabel = $province['label'];
|
|
|
+
|
|
|
+ // 添加省份全称和简称
|
|
|
+ $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
|
|
|
+ $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+
|
|
|
+ // 添加省级简称
|
|
|
+ if (isset($provinceAbbr[$shortProvince])) {
|
|
|
+ $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理直辖市
|
|
|
+ if (isset($municipalities[$pValue])) {
|
|
|
+ $cityLabel = $municipalities[$pValue][1];
|
|
|
+ $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
|
|
|
+ 'label' => $cityLabel,
|
|
|
+ 'value' => $pValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理普通城市
|
|
|
+ foreach ($province['children'] as $city) {
|
|
|
+ $cValue = $city['value'];
|
|
|
+ $cLabel = $city['label'];
|
|
|
+
|
|
|
+ // 添加城市全称和简称
|
|
|
+ $shortCity = preg_replace('/市$/', '', $cLabel);
|
|
|
+ $cityMap[$cLabel] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ $cityMap[$shortCity] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 添加城市常见简称(特殊处理)
|
|
|
+ $specialAbbr = [
|
|
|
+ '成都' => '成都市',
|
|
|
+ '杭州' => '杭州市',
|
|
|
+ '广州' => '广州市',
|
|
|
+ '深圳' => '深圳市',
|
|
|
+ '武汉' => '武汉市',
|
|
|
+ '南京' => '南京市'
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (isset($specialAbbr[$shortCity])) {
|
|
|
+ $cityMap[$shortCity] = [
|
|
|
+ 'label' => $specialAbbr[$shortCity],
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [$provinceMap, $cityMap];
|
|
|
+ }
|
|
|
+
|
|
|
+ function parseAddress2($str, $provinceMap, $cityMap) {
|
|
|
+ static $cache = [];
|
|
|
+ if (isset($cache[$str])) return $cache[$str];
|
|
|
+
|
|
|
+ $str = str_replace(['·', ' ', '-'], '', trim($str));
|
|
|
+
|
|
|
+ // 1. 处理直辖市简称
|
|
|
+ $municipalityShort = ['京', '沪', '津', '渝'];
|
|
|
+ $municipalityMap = [
|
|
|
+ '京' => '北京市',
|
|
|
+ '沪' => '上海市',
|
|
|
+ '津' => '天津市',
|
|
|
+ '渝' => '重庆市'
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (in_array(mb_substr($str, 0, 1), $municipalityShort)) {
|
|
|
+ $cityName = $municipalityMap[mb_substr($str, 0, 1)];
|
|
|
+ $city = $cityMap[$cityName] ?? null;
|
|
|
+ if ($city) {
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $city['label'], 'value' => $city['province']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 完整字符串匹配城市(加强版)
|
|
|
+ if (isset($cityMap[$str])) {
|
|
|
+ $city = $cityMap[$str];
|
|
|
+ $province = $provinceMap[$city['province']] ?? null;
|
|
|
+ $result = [
|
|
|
+ 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 优先匹配3字省份(如内蒙古)
|
|
|
+ if (mb_strlen($str) >= 3) {
|
|
|
+ $prefix = mb_substr($str, 0, 3);
|
|
|
+ if (isset($provinceMap[$prefix])) {
|
|
|
+ return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $cache, $str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 匹配2字省份
|
|
|
+ if (mb_strlen($str) >= 2) {
|
|
|
+ $prefix = mb_substr($str, 0, 2);
|
|
|
+ if (isset($provinceMap[$prefix])) {
|
|
|
+ return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $cache, $str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 城市关键词扫描(加强版)
|
|
|
+ $cityKeys = array_keys($cityMap);
|
|
|
+ usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
|
|
|
+
|
|
|
+ foreach ($cityKeys as $key) {
|
|
|
+ if (mb_strpos($str, $key) !== false) {
|
|
|
+ $city = $cityMap[$key];
|
|
|
+ $province = $provinceMap[$city['province']] ?? null;
|
|
|
+
|
|
|
+ // 特殊处理:当匹配到城市时,尝试找回省份
|
|
|
+ if (!$province) {
|
|
|
+ $province = $provinceMap[$city['province']] ?? ['label' => '', 'value' => ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 最终匹配失败
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => '', 'value' => ''],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 优化省份匹配后的处理
|
|
|
+ function handleMatchedProvince2($provinceKey, $remainder, $provinceMap, $cityMap, &$cache, $originalStr) {
|
|
|
+ $province = $provinceMap[$provinceKey];
|
|
|
+
|
|
|
+ // 直辖市特殊处理
|
|
|
+ $municipalities = ['110000', '120000', '310000', '500000'];
|
|
|
+ if (in_array($province['value'], $municipalities)) {
|
|
|
+ $city = $cityMap[$province['label']] ?? null;
|
|
|
+ if ($city) {
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试匹配剩余部分
|
|
|
+ if ($remainder !== '') {
|
|
|
+ // 先尝试完整匹配
|
|
|
+ if (isset($cityMap[$remainder])) {
|
|
|
+ $city = $cityMap[$remainder];
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 再尝试关键词扫描
|
|
|
+ $cityKeys = array_keys($cityMap);
|
|
|
+ usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
|
|
|
+
|
|
|
+ foreach ($cityKeys as $key) {
|
|
|
+ if (mb_strpos($remainder, $key) !== false) {
|
|
|
+ $city = $cityMap[$key];
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 仅返回省份信息
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function constructAddress1(){
|
|
|
+ $addressData = config('address');
|
|
|
+ // 构建映射表
|
|
|
+ $provinceMap = [];
|
|
|
+ $cityMap = [];
|
|
|
+
|
|
|
+ // 直辖市特殊处理(直接映射到市)
|
|
|
+ $municipalities = [
|
|
|
+ '110000' => ['北京', '北京市'],
|
|
|
+ '120000' => ['天津', '天津市'],
|
|
|
+ '310000' => ['上海', '上海市'],
|
|
|
+ '500000' => ['重庆', '重庆市']
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($addressData as $province) {
|
|
|
+ $pValue = $province['value'];
|
|
|
+ $pLabel = $province['label'];
|
|
|
+
|
|
|
+ // 生成省份简称
|
|
|
+ $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
|
|
|
+ $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+ $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
|
|
|
+
|
|
|
+ // 处理直辖市(省市同名)
|
|
|
+ if (isset($municipalities[$pValue])) {
|
|
|
+ $cityLabel = $municipalities[$pValue][1];
|
|
|
+ $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
|
|
|
+ 'label' => $cityLabel,
|
|
|
+ 'value' => $pValue, // 直辖市使用省级代码
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理普通城市
|
|
|
+ foreach ($province['children'] as $city) {
|
|
|
+ $cValue = $city['value'];
|
|
|
+ $cLabel = $city['label'];
|
|
|
+
|
|
|
+ // 生成城市简称
|
|
|
+ $shortCity = preg_replace('/市$/', '', $cLabel);
|
|
|
+ $cityMap[$cLabel] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($shortCity !== $cLabel) {
|
|
|
+ $cityMap[$shortCity] = [
|
|
|
+ 'label' => $cLabel,
|
|
|
+ 'value' => $cValue,
|
|
|
+ 'province' => $pValue
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [$provinceMap, $cityMap];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析函数
|
|
|
+ function parseAddress1($str, $provinceMap, $cityMap) {
|
|
|
+ static $cache = []; // 结果缓存
|
|
|
+ if (isset($cache[$str])) return $cache[$str];
|
|
|
+
|
|
|
+ $str = str_replace(['·', ' '], '', trim($str));
|
|
|
+
|
|
|
+ // 1. 处理带分隔符的格式 (广东-佛山)
|
|
|
+ if (preg_match('/[-\s]/', $str)) {
|
|
|
+ $parts = preg_split('/[-\s]+/', $str);
|
|
|
+ foreach ($parts as $part) {
|
|
|
+ if ($result = $this->parseAddress($part, $provinceMap, $cityMap)) {
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 完整字符串匹配城市
|
|
|
+ if (isset($cityMap[$str])) {
|
|
|
+ $city = $cityMap[$str];
|
|
|
+ $province = $provinceMap[$city['province']] ?? null;
|
|
|
+ $result = [
|
|
|
+ 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 优先匹配3字省份 (内蒙古)
|
|
|
+ if (mb_strlen($str) >= 3) {
|
|
|
+ $prefix = mb_substr($str, 0, 3);
|
|
|
+ if (isset($provinceMap[$prefix])) {
|
|
|
+ return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $cache, $str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 匹配2字省份 (江苏)
|
|
|
+ if (mb_strlen($str) >= 2) {
|
|
|
+ $prefix = mb_substr($str, 0, 2);
|
|
|
+ if (isset($provinceMap[$prefix])) {
|
|
|
+ return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $cache, $str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 城市关键词扫描 (解决"成都双流"类问题)
|
|
|
+ $cityKeys = array_keys($cityMap);
|
|
|
+ usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
|
|
|
+
|
|
|
+ foreach ($cityKeys as $key) {
|
|
|
+ if (mb_strpos($str, $key) !== false) {
|
|
|
+ $city = $cityMap[$key];
|
|
|
+ $province = $provinceMap[$city['province']] ?? null;
|
|
|
+ $result = [
|
|
|
+ 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 最终匹配失败
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => '', 'value' => ''],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$str] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 省份匹配后的处理
|
|
|
+ function handleMatchedProvince1($provinceKey, $remainder, $provinceMap, $cityMap, &$cache, $originalStr) {
|
|
|
+ $province = $provinceMap[$provinceKey];
|
|
|
+
|
|
|
+ // 直辖市特殊处理
|
|
|
+ if (isset($provinceMap[$province['label']])) {
|
|
|
+ $city = $cityMap[$province['label']] ?? null;
|
|
|
+ if ($city) {
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试匹配剩余部分
|
|
|
+ if ($remainder !== '' && isset($cityMap[$remainder])) {
|
|
|
+ $city = $cityMap[$remainder];
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => $city['label'], 'value' => $city['value']]
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 仅返回省份信息
|
|
|
+ $result = [
|
|
|
+ 'province' => ['label' => $province['label'], 'value' => $province['value']],
|
|
|
+ 'city' => ['label' => '', 'value' => '']
|
|
|
+ ];
|
|
|
+ $cache[$originalStr] = $result;
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+}
|