【缺陷修复】购物车提交时的配送方式校验

pull/131/head
puhui999 2024-12-19 15:45:32 +08:00
parent 164589f634
commit 34be99fccb
1 changed files with 52 additions and 30 deletions

View File

@ -109,7 +109,6 @@
import { computed, reactive } from 'vue'; import { computed, reactive } from 'vue';
import { fen2yuan } from '@/sheep/hooks/useGoods'; import { fen2yuan } from '@/sheep/hooks/useGoods';
import { isEmpty } from '@/sheep/helper/utils'; import { isEmpty } from '@/sheep/helper/utils';
import { DeliveryTypeEnum } from '@/sheep/util/const';
const sys_navBar = sheep.$platform.navbar; const sys_navBar = sheep.$platform.navbar;
const cart = sheep.$store('cart'); const cart = sheep.$store('cart');
@ -158,37 +157,60 @@
}); });
} }
/** 校验配送方式 */ /**
function validateDeliveryType(spuIds) { * 校验配送方式冲突
return new Promise(async (resolve, reject) => { *
const { data } = await SpuApi.getSpuListByIds(spuIds.join(',')); * @param {string[]} spuIds - 商品ID数组
if (isEmpty(data)) { * @returns {Promise<void>}
reject('获取商品信息失败!!!'); * @throws {Error} 当配送方式冲突或获取商品信息失败时抛出错误
return; */
async function validateDeliveryType(spuIds) {
//
const { data: spuList } = await SpuApi.getSpuListByIds(spuIds.join(','));
if (isEmpty(spuList)) {
sheep.$helper.toast('未找到商品信息');
throw new Error('未找到商品信息');
} }
let onlyExpress = false; // //
let onlyPickup = false; // const deliveryTypesList = spuList.map(item => item.deliveryTypes);
// TODO @puhui999A B A B //
const deliveryTypes = data.map((item) => item.deliveryTypes); const hasConflict = checkDeliveryConflicts(deliveryTypesList);
for (const deliveryType of deliveryTypes) { if (hasConflict) {
// sheep.$helper.toast('选中商品支持的配送方式冲突,不允许提交');
if (deliveryType.length > 1) { throw new Error('选中商品支持的配送方式冲突,不允许提交');
continue;
}
//
if (deliveryType[0] === DeliveryTypeEnum.EXPRESS.type) {
onlyExpress = true;
} else if (deliveryType[0] === DeliveryTypeEnum.PICK_UP.type) {
onlyPickup = true;
} }
} }
if (onlyExpress || onlyPickup) {
reject('选中商品存在只支持特定配送方式的情况不允许提交!!!'); /**
sheep.$helper.toast('选中商品存在只支持特定配送方式的情况不允许提交!!!'); * 检查配送方式列表中是否存在冲突
return; * @description
* 示例场景:
* A 商品支持[快递, 自提]
* B 商品支持[快递]
* C 商品支持[自提]
*
* 对比结果:
* A B不冲突 (有交集快递)
* A C不冲突 (有交集自提)
* B C冲突 (无交集)
* @param {Array<Array<number>>} deliveryTypesList - 配送方式列表的数组
* @returns {boolean} 是否存在冲突
*/
function checkDeliveryConflicts(deliveryTypesList) {
for (let i = 0; i < deliveryTypesList.length - 1; i++) {
const currentTypes = deliveryTypesList[i];
for (let j = i + 1; j < deliveryTypesList.length; j++) {
const nextTypes = deliveryTypesList[j];
//
const hasNoIntersection = !currentTypes.some(type =>
nextTypes.includes(type),
);
if (hasNoIntersection) {
return true;
} }
resolve(); }
}); }
return false;
} }
function onNumberChange(e, cartItem) { function onNumberChange(e, cartItem) {