新增商品按字母搜索

This commit is contained in:
gyq
2026-05-15 13:40:42 +08:00
parent a719807b0e
commit 380a8c6572
2 changed files with 24 additions and 2 deletions

View File

@@ -59,6 +59,7 @@
"path-browserify": "^1.0.1",
"path-to-regexp": "^8.2.0",
"pinia": "^2.3.1",
"pinyin-match": "^1.2.10",
"qrcode": "^1.5.4",
"qs": "^6.14.0",
"sockjs-client": "^1.6.1",

View File

@@ -8,6 +8,7 @@ import shopUserApi from '@/api/account/shopUser'
import limitTimeDiscountApi from '@/api/market/limitTimeDiscount.js'
import { BigNumber } from "bignumber.js";
import _ from "lodash";
import PinyinMatch from 'pinyin-match'
// 导入工具库及相关类型
import {
@@ -308,11 +309,17 @@ export const useCartsStore = defineStore("carts", () => {
console.log('代客下单页面商品缓存.获取当前店铺可用的限时折扣', limitDiscountRes.value);
// 1. 先把 query.name 提取出来,接口调用时不传,避免后端限制
const searchName = query.name || '';
const queryParams = { ...query };
delete queryParams.name;
// 2. 调用接口(不带 name 参数,拿全量数据,前端自己过滤)
const res = await productApi.getPage({
page: 1,
size: 999,
status: "on_sale",
...query,
...queryParams,
});
interface ProductItem {
@@ -332,7 +339,21 @@ export const useCartsStore = defineStore("carts", () => {
limitDiscountPrice: number;
}
goods.value = (res.records as ProductItem[]).map((item: ProductItem): GoodsWithDiscount => {
// 3. 【核心:拼音 + 汉字 模糊过滤】
let filteredList = res.records as ProductItem[];
if (searchName) {
filteredList = filteredList.filter(item => {
if (!item.name) return false;
// 汉字模糊搜索
const hasName = item.name.includes(searchName);
// 拼音/首字母搜索zs / zhangsan / 张三 都支持)
const hasPinyin = PinyinMatch.match(item.name, searchName);
return hasName || hasPinyin;
});
}
// 4. 价格处理(完全不变)
goods.value = filteredList.map((item: ProductItem): GoodsWithDiscount => {
item.salePrice = item.lowPrice
item.memberPrice = item.lowMemberPrice