diff --git a/application/admin/addresmart/Address.php b/application/admin/addresmart/Address.php index 2e15427..cb566ab 100644 --- a/application/admin/addresmart/Address.php +++ b/application/admin/addresmart/Address.php @@ -20,32 +20,34 @@ class Address } $titles = Item::where('status',1)->column('title'); - $type = self::extractServiceTypes($string,$titles)[0] ?? ''; + $parser = new AppointmentParser(); + $result = $parser->parse($string); - $string = str_replace($type,'',$string); - if ($user) { - $decompose = self::decompose($string); - $re = $decompose; - } else { - $re['addr'] = $string; + if ($result){ + $type = self::extractServiceTypes($result['project']??'',$titles,false); + $re = [ + 'addr' => $result['address'], + 'name' => $result['name'], + 'mobile' => $result['phone'], + 'plan_time' => $result['datetime'], + ]; + }else{ + if ($user) { + $decompose = self::decompose($string); + $re = $decompose; + } else { + $re['addr'] = $string; + } + $type = self::extractServiceTypes($string,$titles)[0] ?? ''; } - $fuzz = self::fuzz($re['addr']); - $parse = self::parse($fuzz['a1'], $fuzz['a2'], $fuzz['a3']); - - $re['province'] = $parse['province'] ?? ''; - $re['city'] = $parse['city'] ?? ''; - $re['region'] = $parse['region'] ?? ''; $re['item'] = [ 'id'=> $name_items_map[$type] ?? 0, 'item' => $type ?? '' ]; - $re['street'] = ($fuzz['street']) ?: ''; - $re['street'] = str_replace([$re['region'], $re['city'], $re['province']], ['', '', ''], $re['street']); - return $re; } public static function extractChineseWords($str) { @@ -327,7 +329,7 @@ class Address $matched = []; foreach ($cleaned as $service) { - if (mb_stripos($chatText, $service) !== false) { + if (mb_stripos($service,$chatText) !== false) { if ($returnAll) { $matched[] = $service; } else { @@ -335,7 +337,6 @@ class Address } } } - return $returnAll ? $matched : null; } diff --git a/application/admin/addresmart/AppointmentParser.php b/application/admin/addresmart/AppointmentParser.php new file mode 100644 index 0000000..b328e43 --- /dev/null +++ b/application/admin/addresmart/AppointmentParser.php @@ -0,0 +1,86 @@ +isFormatA($text)) { + return $this->extractFormatA($text); + } + + if ($this->isFormatB($text)) { + return $this->extractFormatB($text); + } + + return null; + } + + // --- Format A --- + private function isFormatA(string $text): bool + { + $requiredFields = ['姓名:', '电话:', '地址:', '日期:', '时间:', '商品名称:']; + foreach ($requiredFields as $field) { + if (mb_strpos($text, $field) === false) { + return false; + } + } + return true; + } + + private function extractFormatA(string $text): array + { + $date = $this->match('/日期:(\d{4}-\d{2}-\d{2})/', $text); + $timeRange = $this->match('/时间:([\d:]+)~[\d:]+/', $text); + $datetime = $date && $timeRange ? "$date $timeRange" : null; + + return [ + 'name' => $this->match('/姓名:(.+)/u', $text), + 'phone' => $this->match('/电话:(\d{11})/', $text), + 'address' => $this->match('/地址:(.+)/u', $text), + 'date' => $date, + 'time' => $timeRange, + 'datetime' => $datetime, + 'project' => $this->match('/商品名称:(.+)/u', $text), + ]; + } + + + // --- Format B --- + private function isFormatB(string $text): bool + { + $requiredFields = ['预约项目:', '预约地址:', '联系人:', '联系电话:', '预约时间:']; + foreach ($requiredFields as $field) { + if (mb_strpos($text, $field) === false) { + return false; + } + } + return true; + } + + private function extractFormatB(string $text): array + { + $date = $this->match('/预约时间:(\d{4}-\d{2}-\d{2})/', $text); + $timeRange = $this->match('/预约时间:\d{4}-\d{2}-\d{2} ([\d:]+)-[\d:]+/', $text); + $datetime = $date && $timeRange ? "$date $timeRange" : null; + + return [ + 'name' => $this->match('/联系人:([^\n]+)/u', $text), + 'phone' => $this->match('/联系电话:.*?(\d{11})/', $text), + 'address' => $this->match('/预约地址:(.+)/u', $text), + 'date' => $date, + 'time' => $timeRange, + 'datetime' => $datetime, + 'project' => $this->match('/预约项目:(.+)/u', $text), + ]; + } + + // --- 通用正则匹配 --- + private function match(string $pattern, string $text): ?string + { + if (preg_match($pattern, $text, $matches)) { + return trim($matches[1]); + } + return null; + } +} diff --git a/public/assets/js/backend/order.js b/public/assets/js/backend/order.js index 001e1f0..6fa0f47 100644 --- a/public/assets/js/backend/order.js +++ b/public/assets/js/backend/order.js @@ -485,6 +485,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cascader'], function if (data.area_id) { $("#area_id").val(data.area_id); } + if (data.plan_time) { + $("#time").val(data.plan_time); + } if (data.addr && data.addr !== '') { $("#c-address").val(data.addr); $("#area_name").val(data.addr);