feat(shopro_huifu): 完善银行支行搜索功能 - 添加建设银行等更多银行支行数据到本地数据库 - 优化支行搜索算法,支持省市范围匹配和关键词分词搜索 - 新增支行联行号手动输入功能,提供更好的用户体验 - 改进搜索提示文案,明确说明搜索和匹配规则 - 修复支行搜索验证逻辑,支持多种匹配模式 ```
282 行
9.4 KiB
PHP
282 行
9.4 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro_huifu\library\onboard;
|
|
|
|
use think\Db;
|
|
|
|
class HuifuOnboardBankBranch
|
|
{
|
|
protected static $jsonCache;
|
|
|
|
/**
|
|
* @return array<int, array{branch_code:string,branch_name:string,bank_name?:string,prov_id?:string,area_id?:string}>
|
|
*/
|
|
public static function search($keyword, $cityCode = '', $limit = 20)
|
|
{
|
|
$keyword = trim((string) $keyword);
|
|
if ($keyword === '') {
|
|
return [];
|
|
}
|
|
$limit = max(1, min(50, (int) $limit));
|
|
$cityCode = HuifuOnboardAreaHelper::toCode($cityCode);
|
|
$provCode = self::provinceFromCity($cityCode);
|
|
|
|
$rows = self::collectSearch($keyword, $cityCode, $provCode, $limit);
|
|
if ($rows) {
|
|
return $rows;
|
|
}
|
|
if ($cityCode !== '') {
|
|
$rows = self::collectSearch($keyword, '', $provCode, $limit);
|
|
if ($rows) {
|
|
return $rows;
|
|
}
|
|
}
|
|
return self::collectSearch($keyword, '', '', $limit);
|
|
}
|
|
|
|
protected static function collectSearch($keyword, $cityCode, $provCode, $limit)
|
|
{
|
|
$rows = self::searchDatabase($keyword, $cityCode, $provCode, $limit);
|
|
if (count($rows) >= $limit) {
|
|
return $rows;
|
|
}
|
|
|
|
$jsonRows = self::searchJson($keyword, $cityCode, $provCode, $limit - count($rows));
|
|
$existing = array_column($rows, 'branch_code');
|
|
foreach ($jsonRows as $row) {
|
|
if (in_array($row['branch_code'], $existing, true)) {
|
|
continue;
|
|
}
|
|
$rows[] = $row;
|
|
if (count($rows) >= $limit) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return self::rankMatches($rows, $keyword);
|
|
}
|
|
|
|
protected static function searchDatabase($keyword, $cityCode, $provCode, $limit)
|
|
{
|
|
try {
|
|
$query = Db::name('shopro_huifu_bank_branch')->where('status', 1);
|
|
self::applyAreaScope($query, $cityCode, $provCode);
|
|
$query->where(function ($q) use ($keyword) {
|
|
self::applyKeywordScope($q, $keyword);
|
|
});
|
|
$list = $query->limit($limit * 3)->select();
|
|
if (!$list) {
|
|
return [];
|
|
}
|
|
$rows = [];
|
|
foreach ($list as $item) {
|
|
$rows[] = [
|
|
'branch_code' => (string) $item['branch_code'],
|
|
'branch_name' => (string) $item['branch_name'],
|
|
'bank_name' => (string) ($item['bank_name'] ?? ''),
|
|
'prov_id' => (string) ($item['prov_id'] ?? ''),
|
|
'area_id' => (string) ($item['area_id'] ?? ''),
|
|
];
|
|
}
|
|
return array_slice(self::rankMatches($rows, $keyword), 0, $limit);
|
|
} catch (\Throwable $e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
protected static function searchJson($keyword, $cityCode = '', $provCode = '', $limit = 20)
|
|
{
|
|
$limit = max(0, (int) $limit);
|
|
if ($limit <= 0) {
|
|
return [];
|
|
}
|
|
$cityCode = HuifuOnboardAreaHelper::toCode($cityCode);
|
|
$provCode = HuifuOnboardAreaHelper::toCode($provCode);
|
|
$tokens = self::keywordTokens($keyword);
|
|
$all = self::loadJson();
|
|
$rows = [];
|
|
foreach ($all as $item) {
|
|
if (!self::matchAreaScope($item, $cityCode, $provCode)) {
|
|
continue;
|
|
}
|
|
if (!self::matchKeywordTokens($item, $tokens, $keyword)) {
|
|
continue;
|
|
}
|
|
$rows[] = $item;
|
|
}
|
|
return array_slice(self::rankMatches($rows, $keyword), 0, $limit);
|
|
}
|
|
|
|
protected static function applyAreaScope($query, $cityCode, $provCode)
|
|
{
|
|
$cityCode = HuifuOnboardAreaHelper::toCode($cityCode);
|
|
$provCode = HuifuOnboardAreaHelper::toCode($provCode);
|
|
if ($cityCode !== '') {
|
|
$query->where('area_id', $cityCode);
|
|
return;
|
|
}
|
|
if ($provCode !== '') {
|
|
$query->where('prov_id', $provCode);
|
|
}
|
|
}
|
|
|
|
protected static function matchAreaScope(array $item, $cityCode, $provCode)
|
|
{
|
|
$cityCode = HuifuOnboardAreaHelper::toCode($cityCode);
|
|
$provCode = HuifuOnboardAreaHelper::toCode($provCode);
|
|
if ($cityCode !== '') {
|
|
return HuifuOnboardAreaHelper::toCode($item['area_id'] ?? '') === $cityCode;
|
|
}
|
|
if ($provCode !== '') {
|
|
return HuifuOnboardAreaHelper::toCode($item['prov_id'] ?? '') === $provCode;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected static function applyKeywordScope($query, $keyword)
|
|
{
|
|
$tokens = self::keywordTokens($keyword);
|
|
if (!$tokens) {
|
|
$query->whereLike('branch_name', '%' . $keyword . '%');
|
|
return;
|
|
}
|
|
foreach ($tokens as $token) {
|
|
$query->where(function ($q) use ($token) {
|
|
$q->whereLike('branch_name', '%' . $token . '%')
|
|
->whereOr('branch_code', 'like', '%' . $token . '%')
|
|
->whereOr('bank_name', 'like', '%' . $token . '%');
|
|
});
|
|
}
|
|
}
|
|
|
|
protected static function normalizeKeyword($keyword)
|
|
{
|
|
$keyword = trim((string) $keyword);
|
|
$keyword = str_replace(['理行', '支理'], '支行', $keyword);
|
|
$keyword = preg_replace('/([\x{4e00}-\x{9fff}])支行/u', '$1 支行', $keyword);
|
|
$keyword = preg_replace('/([\x{4e00}-\x{9fff}])分行/u', '$1 分行', $keyword);
|
|
return trim(preg_replace('/\s+/u', ' ', $keyword));
|
|
}
|
|
|
|
protected static function keywordTokens($keyword)
|
|
{
|
|
$keyword = self::normalizeKeyword($keyword);
|
|
if ($keyword === '') {
|
|
return [];
|
|
}
|
|
if (preg_match('/^\d{8,20}$/', $keyword)) {
|
|
return [$keyword];
|
|
}
|
|
$parts = preg_split('/[\s,,、\/]+/u', $keyword, -1, PREG_SPLIT_NO_EMPTY);
|
|
$tokens = [];
|
|
foreach ($parts ?: [$keyword] as $part) {
|
|
$part = trim($part);
|
|
if ($part === '') {
|
|
continue;
|
|
}
|
|
$tokens[] = $part;
|
|
$expanded = self::expandKeywordAliases($part);
|
|
foreach ($expanded as $alias) {
|
|
if (!in_array($alias, $tokens, true)) {
|
|
$tokens[] = $alias;
|
|
}
|
|
}
|
|
}
|
|
return array_values(array_unique($tokens));
|
|
}
|
|
|
|
protected static function expandKeywordAliases($token)
|
|
{
|
|
$map = [
|
|
'建行' => ['建设银行', '建设'],
|
|
'农行' => ['农业银行', '农业'],
|
|
'工行' => ['工商银行', '工商'],
|
|
'中行' => ['中国银行', '中国'],
|
|
'交行' => ['交通银行', '交通'],
|
|
'理行' => ['支行'],
|
|
];
|
|
$aliases = [];
|
|
foreach ($map as $short => $fullList) {
|
|
if (mb_strpos($token, $short, 0, 'UTF-8') !== false) {
|
|
$aliases = array_merge($aliases, $fullList);
|
|
}
|
|
}
|
|
return $aliases;
|
|
}
|
|
|
|
protected static function matchKeywordTokens(array $item, array $tokens, $keyword)
|
|
{
|
|
if (preg_match('/^\d{8,20}$/', $keyword)) {
|
|
return strpos((string) ($item['branch_code'] ?? ''), $keyword) !== false;
|
|
}
|
|
$hay = mb_strtolower(
|
|
($item['branch_name'] ?? '') . ' ' . ($item['bank_name'] ?? '') . ' ' . ($item['branch_code'] ?? ''),
|
|
'UTF-8'
|
|
);
|
|
if (!$tokens) {
|
|
return mb_strpos($hay, mb_strtolower($keyword, 'UTF-8'), 0, 'UTF-8') !== false;
|
|
}
|
|
foreach ($tokens as $token) {
|
|
if (mb_strpos($hay, mb_strtolower($token, 'UTF-8'), 0, 'UTF-8') === false) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected static function rankMatches(array $rows, $keyword)
|
|
{
|
|
if (!$rows) {
|
|
return [];
|
|
}
|
|
$keywordLower = mb_strtolower(trim((string) $keyword), 'UTF-8');
|
|
usort($rows, function ($a, $b) use ($keywordLower) {
|
|
return self::matchScore($b, $keywordLower) <=> self::matchScore($a, $keywordLower);
|
|
});
|
|
return $rows;
|
|
}
|
|
|
|
protected static function matchScore(array $item, $keywordLower)
|
|
{
|
|
$name = mb_strtolower((string) ($item['branch_name'] ?? ''), 'UTF-8');
|
|
$code = (string) ($item['branch_code'] ?? '');
|
|
if ($keywordLower !== '' && $name === $keywordLower) {
|
|
return 1000;
|
|
}
|
|
if ($keywordLower !== '' && mb_strpos($name, $keywordLower, 0, 'UTF-8') === 0) {
|
|
return 900;
|
|
}
|
|
if ($keywordLower !== '' && mb_strpos($name, $keywordLower, 0, 'UTF-8') !== false) {
|
|
return 800;
|
|
}
|
|
if ($keywordLower !== '' && strpos($code, $keywordLower) !== false) {
|
|
return 700;
|
|
}
|
|
return 100;
|
|
}
|
|
|
|
protected static function provinceFromCity($cityCode)
|
|
{
|
|
$cityCode = HuifuOnboardAreaHelper::toCode($cityCode);
|
|
if (strlen($cityCode) < 2) {
|
|
return '';
|
|
}
|
|
return substr($cityCode, 0, 2) . '0000';
|
|
}
|
|
|
|
protected static function loadJson()
|
|
{
|
|
if (self::$jsonCache !== null) {
|
|
return self::$jsonCache;
|
|
}
|
|
$file = ADDON_PATH . 'shopro_huifu/data/bank_branches.json';
|
|
if (!is_file($file)) {
|
|
self::$jsonCache = [];
|
|
return self::$jsonCache;
|
|
}
|
|
$decoded = json_decode(file_get_contents($file), true);
|
|
self::$jsonCache = is_array($decoded) ? $decoded : [];
|
|
return self::$jsonCache;
|
|
}
|
|
}
|