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

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 { fen2yuan } from '@/sheep/hooks/useGoods';
import { isEmpty } from '@/sheep/helper/utils';
import { DeliveryTypeEnum } from '@/sheep/util/const';
const sys_navBar = sheep.$platform.navbar;
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(','));
if (isEmpty(data)) {
reject('获取商品信息失败!!!');
return;
}
let onlyExpress = false; //
let onlyPickup = false; //
// TODO @puhui999A B A B
const deliveryTypes = data.map((item) => item.deliveryTypes);
for (const deliveryType of deliveryTypes) {
//
if (deliveryType.length > 1) {
continue;
}
//
if (deliveryType[0] === DeliveryTypeEnum.EXPRESS.type) {
onlyExpress = true;
} else if (deliveryType[0] === DeliveryTypeEnum.PICK_UP.type) {
onlyPickup = true;
/**
* 校验配送方式冲突
*
* @param {string[]} spuIds - 商品ID数组
* @returns {Promise<void>}
* @throws {Error} 当配送方式冲突或获取商品信息失败时抛出错误
*/
async function validateDeliveryType(spuIds) {
//
const { data: spuList } = await SpuApi.getSpuListByIds(spuIds.join(','));
if (isEmpty(spuList)) {
sheep.$helper.toast('未找到商品信息');
throw new Error('未找到商品信息');
}
//
const deliveryTypesList = spuList.map(item => item.deliveryTypes);
//
const hasConflict = checkDeliveryConflicts(deliveryTypesList);
if (hasConflict) {
sheep.$helper.toast('选中商品支持的配送方式冲突,不允许提交');
throw new Error('选中商品支持的配送方式冲突,不允许提交');
}
}
/**
* 检查配送方式列表中是否存在冲突
* @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('选中商品存在只支持特定配送方式的情况不允许提交!!!');
sheep.$helper.toast('选中商品存在只支持特定配送方式的情况不允许提交!!!');
return;
}
resolve();
});
}
return false;
}
function onNumberChange(e, cartItem) {