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

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) {
let onlyExpress = false; // //
let onlyPickup = false; // const { data: spuList } = await SpuApi.getSpuListByIds(spuIds.join(','));
// TODO @puhui999A B A B if (isEmpty(spuList)) {
const deliveryTypes = data.map((item) => item.deliveryTypes); sheep.$helper.toast('未找到商品信息');
for (const deliveryType of deliveryTypes) { throw new Error('未找到商品信息');
// }
if (deliveryType.length > 1) { //
continue; const deliveryTypesList = spuList.map(item => item.deliveryTypes);
} //
// const hasConflict = checkDeliveryConflicts(deliveryTypesList);
if (deliveryType[0] === DeliveryTypeEnum.EXPRESS.type) { if (hasConflict) {
onlyExpress = true; sheep.$helper.toast('选中商品支持的配送方式冲突,不允许提交');
} else if (deliveryType[0] === DeliveryTypeEnum.PICK_UP.type) { throw new Error('选中商品支持的配送方式冲突,不允许提交');
onlyPickup = true; }
}
/**
* 检查配送方式列表中是否存在冲突
* @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;
} }
} }
if (onlyExpress || onlyPickup) { }
reject('选中商品存在只支持特定配送方式的情况不允许提交!!!'); return false;
sheep.$helper.toast('选中商品存在只支持特定配送方式的情况不允许提交!!!');
return;
}
resolve();
});
} }
function onNumberChange(e, cartItem) { function onNumberChange(e, cartItem) {