增加pinyin模块,解决按姓名排序的多音字问题
This commit is contained in:
parent
c0b8773e57
commit
cc9a0516c5
@ -58,6 +58,7 @@
|
|||||||
"lodash": "4.17.21",
|
"lodash": "4.17.21",
|
||||||
"pinia": "2.0.23",
|
"pinia": "2.0.23",
|
||||||
"pinia-plugin-persist-uni": "1.2.0",
|
"pinia-plugin-persist-uni": "1.2.0",
|
||||||
|
"pinyin-pro": "^3.27.0",
|
||||||
"uview-plus": "3.1.20",
|
"uview-plus": "3.1.20",
|
||||||
"vconsole": "3.15.1",
|
"vconsole": "3.15.1",
|
||||||
"vue": "3.2.45",
|
"vue": "3.2.45",
|
||||||
|
|||||||
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
@ -74,6 +74,9 @@ importers:
|
|||||||
pinia-plugin-persist-uni:
|
pinia-plugin-persist-uni:
|
||||||
specifier: 1.2.0
|
specifier: 1.2.0
|
||||||
version: 1.2.0(pinia@2.0.23(typescript@4.8.3)(vue@3.2.45))(vue@3.2.45)
|
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:
|
uview-plus:
|
||||||
specifier: 3.1.20
|
specifier: 3.1.20
|
||||||
version: 3.1.20
|
version: 3.1.20
|
||||||
@ -2946,6 +2949,9 @@ packages:
|
|||||||
typescript:
|
typescript:
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
pinyin-pro@3.27.0:
|
||||||
|
resolution: {integrity: sha512-Osdgjwe7Rm17N2paDMM47yW+jUIUH3+0RGo8QP39ZTLpTaJVDK0T58hOLaMQJbcMmAebVuK2ePunTEVEx1clNQ==}
|
||||||
|
|
||||||
pirates@4.0.7:
|
pirates@4.0.7:
|
||||||
resolution: {integrity: sha512-TfySrs/5nm8fQJDcBDuUng3VOUKsd7S+zqvbOTiGXHfxX4wK31ard+hoNuvkicM/2YFzlpDgABOevKSsB4G/FA==}
|
resolution: {integrity: sha512-TfySrs/5nm8fQJDcBDuUng3VOUKsd7S+zqvbOTiGXHfxX4wK31ard+hoNuvkicM/2YFzlpDgABOevKSsB4G/FA==}
|
||||||
engines: {node: '>= 6'}
|
engines: {node: '>= 6'}
|
||||||
@ -7572,6 +7578,8 @@ snapshots:
|
|||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
typescript: 4.8.3
|
typescript: 4.8.3
|
||||||
|
|
||||||
|
pinyin-pro@3.27.0: {}
|
||||||
|
|
||||||
pirates@4.0.7: {}
|
pirates@4.0.7: {}
|
||||||
|
|
||||||
pixelmatch@4.0.2:
|
pixelmatch@4.0.2:
|
||||||
|
|||||||
50
src/utils/pinyinUtil.ts
Normal file
50
src/utils/pinyinUtil.ts
Normal 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'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user