diff --git a/package.json b/package.json index 63acc46..d6ee231 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/store/modules/carts.ts b/src/store/modules/carts.ts index 4a7c0a1..58cfc86 100644 --- a/src/store/modules/carts.ts +++ b/src/store/modules/carts.ts @@ -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