增加pinyin模块,解决按姓名排序的多音字问题

This commit is contained in:
ywyonui 2025-09-11 21:38:03 +08:00
parent c0b8773e57
commit cc9a0516c5
4 changed files with 60 additions and 1 deletions

View File

@ -58,6 +58,7 @@
"lodash": "4.17.21",
"pinia": "2.0.23",
"pinia-plugin-persist-uni": "1.2.0",
"pinyin-pro": "^3.27.0",
"uview-plus": "3.1.20",
"vconsole": "3.15.1",
"vue": "3.2.45",

8
pnpm-lock.yaml generated
View File

@ -74,6 +74,9 @@ importers:
pinia-plugin-persist-uni:
specifier: 1.2.0
version: 1.2.0(pinia@2.0.23(typescript@4.8.3)(vue@3.2.45))(vue@3.2.45)
pinyin-pro:
specifier: ^3.27.0
version: 3.27.0
uview-plus:
specifier: 3.1.20
version: 3.1.20
@ -2946,6 +2949,9 @@ packages:
typescript:
optional: true
pinyin-pro@3.27.0:
resolution: {integrity: sha512-Osdgjwe7Rm17N2paDMM47yW+jUIUH3+0RGo8QP39ZTLpTaJVDK0T58hOLaMQJbcMmAebVuK2ePunTEVEx1clNQ==}
pirates@4.0.7:
resolution: {integrity: sha512-TfySrs/5nm8fQJDcBDuUng3VOUKsd7S+zqvbOTiGXHfxX4wK31ard+hoNuvkicM/2YFzlpDgABOevKSsB4G/FA==}
engines: {node: '>= 6'}
@ -7572,6 +7578,8 @@ snapshots:
optionalDependencies:
typescript: 4.8.3
pinyin-pro@3.27.0: {}
pirates@4.0.7: {}
pixelmatch@4.0.2:

View File

@ -1,7 +1,7 @@
const ip: string = "127.0.0.1:8897";
// const ip: string = "lzcxsx.cn";
const fwqip: string = "lzcxsx.cn";
//const ip: string = "zhxy.yufangzc.com";
// const ip: string = "zhxy.yufangzc.com";
//const fwqip: string = "zhxy.yufangzc.com";
//打包服务器接口代理标识
const SERVERAGENT: string = "/jzd-api";

50
src/utils/pinyinUtil.ts Normal file
View File

@ -0,0 +1,50 @@
// utils/pinyinUtils.ts
import { pinyin } from 'pinyin-pro';
/**
*
* @param text
* @returns
*/
export function chineseToPinyin(text: string): string {
try {
return pinyin(text, { toneType: 'none', type: 'array', mode: 'surname' }).join('');
} catch (error) {
console.error('拼音转换失败:', error);
return text;
}
}
/**
*
* @param text
* @returns
*/
export function getFirstLetter(text: string): string {
try {
return pinyin(text, { pattern: 'first', toneType: 'none' });
} catch (error) {
console.error('首字母提取失败:', error);
return text;
}
}
/**
*
* @param list
* @param key
* @returns
*/
export function sortChinese(list: any, key?: string) {
return list.sort((a:any, b: any) => {
const textA = key ? a[key] : a as unknown as string;
const textB = key ? b[key] : b as unknown as string;
const pinyinA = chineseToPinyin(textA);
const pinyinB = chineseToPinyin(textB);
return pinyinA.localeCompare(pinyinB, 'zh-Hans-CN', {
sensitivity: 'accent'
});
});
}