地点添加拼音搜索

This commit is contained in:
hant 2025-03-02 16:04:10 +08:00
parent 1a9a5bea70
commit 282a1d79d3
6 changed files with 108 additions and 16 deletions

View File

@ -0,0 +1,40 @@
<?php
namespace app\admin\command;
use Overtrue\Pinyin\Converter;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use Overtrue\Pinyin\Pinyin;
class AreaPinyinCommand extends Command
{
protected function configure()
{
$this->setName('area:pinyin')
->setDescription('批量生成区域名称的拼音');
}
protected function execute(Input $input, Output $output)
{
$pinyin = new Pinyin();
// 读取 `area` 表所有数据
$areas = Db::name('areas')->where('id','>',1738)->select();
foreach ($areas as $area) {
$fullPinyin = strtolower(str_replace(' ','',$pinyin->name($area['short_name'],Converter::TONE_STYLE_NONE))); // 全拼
$abbrPinyin = strtolower(str_replace(' ','',$pinyin->abbr($area['short_name']))); // 首字母
// 更新数据库
Db::name('areas')
->where('id', $area['id'])
->update(['pinyin' => $fullPinyin, 'abbr' => $abbrPinyin]);
$output->writeln("更新: {$area['merge_name']} -> {$fullPinyin} ({$abbrPinyin})");
}
$output->writeln("拼音转换完成!");
}
}

View File

@ -19,9 +19,22 @@ class Area extends Backend
$this->success(data:[]); $this->success(data:[]);
}else{ }else{
$data = model('area') $data = model('area')
->where('merge_name','like','%'.$keyword.'%')
->where('level','=',3) ->where('level','=',3)
->where(function ($query)use ($keyword){
$query->where('merge_name','like','%'.$keyword.'%')
->whereOr('pinyin', 'like', "%{$keyword}%")
->whereOr('abbr', 'like', "%{$keyword}%");
})
->field('area_code, merge_name') ->field('area_code, merge_name')
->orderRaw(
"CASE
WHEN name LIKE '{$keyword}%' THEN 1
WHEN pinyin LIKE '{$keyword}%' THEN 2
WHEN abbr LIKE '{$keyword}%' THEN 3
ELSE 4
END"
)
->limit(0,10) ->limit(0,10)
->select(); ->select();
$this->success(data:$data); $this->success(data:$data);

View File

@ -17,4 +17,5 @@ return [
'app\admin\command\Min', 'app\admin\command\Min',
'app\admin\command\Addon', 'app\admin\command\Addon',
'app\admin\command\Api', 'app\admin\command\Api',
'app\admin\command\AreaPinyinCommand',
]; ];

View File

@ -22,7 +22,7 @@
"topthink/think-queue": "1.1.6", "topthink/think-queue": "1.1.6",
"topthink/think-helper": "^1.0.7", "topthink/think-helper": "^1.0.7",
"karsonzhang/fastadmin-addons": "~1.4.0", "karsonzhang/fastadmin-addons": "~1.4.0",
"overtrue/pinyin": "^3.0", "overtrue/pinyin": "^5.3",
"phpoffice/phpspreadsheet": "^1.29.1", "phpoffice/phpspreadsheet": "^1.29.1",
"overtrue/wechat": "^4.6", "overtrue/wechat": "^4.6",
"ext-json": "*", "ext-json": "*",

62
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "da8316e5b18922be071cdeaf00c4d962", "content-hash": "fe3829381cb87e4cad3751a2d797da6e",
"packages": [ "packages": [
{ {
"name": "composer/pcre", "name": "composer/pcre",
@ -790,31 +790,63 @@
}, },
{ {
"name": "overtrue/pinyin", "name": "overtrue/pinyin",
"version": "3.0.6", "version": "5.3.3",
"source": {
"type": "git",
"url": "https://github.com/overtrue/pinyin.git",
"reference": "bff15b27cf3e1cc416464b678576f4da9899692e"
},
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/overtrue/pinyin/3.0.6/overtrue-pinyin-3.0.6.zip", "url": "https://api.github.com/repos/overtrue/pinyin/zipball/bff15b27cf3e1cc416464b678576f4da9899692e",
"reference": "3b781d267197b74752daa32814d3a2cf5d140779", "reference": "bff15b27cf3e1cc416464b678576f4da9899692e",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"php": ">=5.3" "php": ">=8.0.2"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~4.8" "brainmaestro/composer-git-hooks": "^3.0",
"friendsofphp/php-cs-fixer": "^3.2",
"laravel/pint": "^1.10",
"nunomaduro/termwind": "^1.0|^2.0",
"phpunit/phpunit": "^10.0|^11.2"
}, },
"bin": [
"bin/pinyin"
],
"type": "library", "type": "library",
"extra": {
"hooks": {
"pre-push": [
"composer pint",
"composer test"
],
"pre-commit": [
"composer pint",
"composer test"
]
}
},
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Overtrue\\Pinyin\\": "src/" "Overtrue\\Pinyin\\": "src/"
} }
}, },
"notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"MIT" "MIT"
], ],
"authors": [ "authors": [
{ {
"name": "Carlos", "name": "overtrue",
"email": "anzhengchao@gmail.com",
"homepage": "http://github.com/overtrue" "homepage": "http://github.com/overtrue"
} }
], ],
@ -827,9 +859,15 @@
], ],
"support": { "support": {
"issues": "https://github.com/overtrue/pinyin/issues", "issues": "https://github.com/overtrue/pinyin/issues",
"source": "https://github.com/overtrue/pinyin/tree/master" "source": "https://github.com/overtrue/pinyin/tree/5.3.3"
}, },
"time": "2017-07-10T07:20:01+00:00" "funding": [
{
"url": "https://github.com/overtrue",
"type": "github"
}
],
"time": "2024-08-01T08:19:06+00:00"
}, },
{ {
"name": "overtrue/socialite", "name": "overtrue/socialite",
@ -2592,6 +2630,6 @@
"ext-pdo": "*", "ext-pdo": "*",
"ext-bcmath": "*" "ext-bcmath": "*"
}, },
"platform-dev": [], "platform-dev": {},
"plugin-api-version": "2.3.0" "plugin-api-version": "2.6.0"
} }

View File

@ -187,7 +187,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
console.error("请求失败"); console.error("请求失败");
} }
}); });
}, 200); // 500 毫秒防抖 }, 400); // 500 毫秒防抖
}); });
// 渲染城市下拉选项 // 渲染城市下拉选项
@ -246,7 +246,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
console.error("请求失败"); console.error("请求失败");
} }
}); });
}, 200); // 500 毫秒防抖 }, 400); // 500 毫秒防抖
}); });
// 渲染城市下拉选项 // 渲染城市下拉选项