From aa82851361259546608fc270225e8fa0467d5991 Mon Sep 17 00:00:00 2001 From: gyq <875626088@qq.com> Date: Mon, 13 Oct 2025 13:30:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 300 +-------------- src/utils/coupon-utils.js | 742 +++++++++++++++++++------------------- 2 files changed, 365 insertions(+), 677 deletions(-) diff --git a/package-lock.json b/package-lock.json index 448e642..59e3d79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "vite-electron", "version": "2.0.7", + "hasInstallScript": true, "dependencies": { "@element-plus/icons-vue": "^2.3.1", "axios": "^1.6.2", @@ -21,7 +22,6 @@ "pinia-plugin-persistedstate": "^3.2.1", "qrcode": "^1.5.3", "reconnecting-websocket": "^4.4.0", - "serialport": "^12.0.0", "speak-tts": "^2.0.8", "swiper": "^11.1.1", "uuid": "^10.0.0", @@ -1646,252 +1646,6 @@ "win32" ] }, - "node_modules/@serialport/binding-mock": { - "version": "10.2.2", - "resolved": "https://registry.npmmirror.com/@serialport/binding-mock/-/binding-mock-10.2.2.tgz", - "integrity": "sha512-HAFzGhk9OuFMpuor7aT5G1ChPgn5qSsklTFOTUX72Rl6p0xwcSVsRtG/xaGp6bxpN7fI9D/S8THLBWbBgS6ldw==", - "dependencies": { - "@serialport/bindings-interface": "^1.2.1", - "debug": "^4.3.3" - }, - "engines": { - "node": ">=12.0.0" - } - }, - "node_modules/@serialport/bindings-cpp": { - "version": "12.0.1", - "resolved": "https://registry.npmmirror.com/@serialport/bindings-cpp/-/bindings-cpp-12.0.1.tgz", - "integrity": "sha512-r2XOwY2dDvbW7dKqSPIk2gzsr6M6Qpe9+/Ngs94fNaNlcTRCV02PfaoDmRgcubpNVVcLATlxSxPTIDw12dbKOg==", - "hasInstallScript": true, - "dependencies": { - "@serialport/bindings-interface": "1.2.2", - "@serialport/parser-readline": "11.0.0", - "debug": "4.3.4", - "node-addon-api": "7.0.0", - "node-gyp-build": "4.6.0" - }, - "engines": { - "node": ">=16.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/bindings-cpp/node_modules/@serialport/parser-delimiter": { - "version": "11.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-delimiter/-/parser-delimiter-11.0.0.tgz", - "integrity": "sha512-aZLJhlRTjSmEwllLG7S4J8s8ctRAS0cbvCpO87smLvl3e4BgzbVgF6Z6zaJd3Aji2uSiYgfedCdNc4L6W+1E2g==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/bindings-cpp/node_modules/@serialport/parser-readline": { - "version": "11.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-readline/-/parser-readline-11.0.0.tgz", - "integrity": "sha512-rRAivhRkT3YO28WjmmG4FQX6L+KMb5/ikhyylRfzWPw0nSXy97+u07peS9CbHqaNvJkMhH1locp2H36aGMOEIA==", - "dependencies": { - "@serialport/parser-delimiter": "11.0.0" - }, - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/bindings-cpp/node_modules/debug": { - "version": "4.3.4", - "resolved": "https://registry.npmmirror.com/debug/-/debug-4.3.4.tgz", - "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dependencies": { - "ms": "2.1.2" - }, - "engines": { - "node": ">=6.0" - }, - "peerDependenciesMeta": { - "supports-color": { - "optional": true - } - } - }, - "node_modules/@serialport/bindings-cpp/node_modules/ms": { - "version": "2.1.2", - "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" - }, - "node_modules/@serialport/bindings-cpp/node_modules/node-addon-api": { - "version": "7.0.0", - "resolved": "https://registry.npmmirror.com/node-addon-api/-/node-addon-api-7.0.0.tgz", - "integrity": "sha512-vgbBJTS4m5/KkE16t5Ly0WW9hz46swAstv0hYYwMtbG7AznRhNyfLRe8HZAiWIpcHzoO7HxhLuBQj9rJ/Ho0ZA==" - }, - "node_modules/@serialport/bindings-cpp/node_modules/node-gyp-build": { - "version": "4.6.0", - "resolved": "https://registry.npmmirror.com/node-gyp-build/-/node-gyp-build-4.6.0.tgz", - "integrity": "sha512-NTZVKn9IylLwUzaKjkas1e4u2DLNcV4rdYagA4PWdPwW87Bi7z+BznyKSRwS/761tV/lzCGXplWsiaMjLqP2zQ==", - "bin": { - "node-gyp-build": "bin.js", - "node-gyp-build-optional": "optional.js", - "node-gyp-build-test": "build-test.js" - } - }, - "node_modules/@serialport/bindings-interface": { - "version": "1.2.2", - "resolved": "https://registry.npmmirror.com/@serialport/bindings-interface/-/bindings-interface-1.2.2.tgz", - "integrity": "sha512-CJaUd5bLvtM9c5dmO9rPBHPXTa9R2UwpkJ0wdh9JCYcbrPWsKz+ErvR0hBLeo7NPeiFdjFO4sonRljiw4d2XiA==", - "engines": { - "node": "^12.22 || ^14.13 || >=16" - } - }, - "node_modules/@serialport/parser-byte-length": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-byte-length/-/parser-byte-length-12.0.0.tgz", - "integrity": "sha512-0ei0txFAj+s6FTiCJFBJ1T2hpKkX8Md0Pu6dqMrYoirjPskDLJRgZGLqoy3/lnU1bkvHpnJO+9oJ3PB9v8rNlg==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-cctalk": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-cctalk/-/parser-cctalk-12.0.0.tgz", - "integrity": "sha512-0PfLzO9t2X5ufKuBO34DQKLXrCCqS9xz2D0pfuaLNeTkyGUBv426zxoMf3rsMRodDOZNbFblu3Ae84MOQXjnZw==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-delimiter": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-delimiter/-/parser-delimiter-12.0.0.tgz", - "integrity": "sha512-gu26tVt5lQoybhorLTPsH2j2LnX3AOP2x/34+DUSTNaUTzu2fBXw+isVjQJpUBFWu6aeQRZw5bJol5X9Gxjblw==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-inter-byte-timeout": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-inter-byte-timeout/-/parser-inter-byte-timeout-12.0.0.tgz", - "integrity": "sha512-GnCh8K0NAESfhCuXAt+FfBRz1Cf9CzIgXfp7SdMgXwrtuUnCC/yuRTUFWRvuzhYKoAo1TL0hhUo77SFHUH1T/w==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-packet-length": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-packet-length/-/parser-packet-length-12.0.0.tgz", - "integrity": "sha512-p1hiCRqvGHHLCN/8ZiPUY/G0zrxd7gtZs251n+cfNTn+87rwcdUeu9Dps3Aadx30/sOGGFL6brIRGK4l/t7MuQ==", - "engines": { - "node": ">=8.6.0" - } - }, - "node_modules/@serialport/parser-readline": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-readline/-/parser-readline-12.0.0.tgz", - "integrity": "sha512-O7cywCWC8PiOMvo/gglEBfAkLjp/SENEML46BXDykfKP5mTPM46XMaX1L0waWU6DXJpBgjaL7+yX6VriVPbN4w==", - "dependencies": { - "@serialport/parser-delimiter": "12.0.0" - }, - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-ready": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-ready/-/parser-ready-12.0.0.tgz", - "integrity": "sha512-ygDwj3O4SDpZlbrRUraoXIoIqb8sM7aMKryGjYTIF0JRnKeB1ys8+wIp0RFMdFbO62YriUDextHB5Um5cKFSWg==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-regex": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-regex/-/parser-regex-12.0.0.tgz", - "integrity": "sha512-dCAVh4P/pZrLcPv9NJ2mvPRBg64L5jXuiRxIlyxxdZGH4WubwXVXY/kBTihQmiAMPxbT3yshSX8f2+feqWsxqA==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-slip-encoder": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-slip-encoder/-/parser-slip-encoder-12.0.0.tgz", - "integrity": "sha512-0APxDGR9YvJXTRfY+uRGhzOhTpU5akSH183RUcwzN7QXh8/1jwFsFLCu0grmAUfi+fItCkR+Xr1TcNJLR13VNA==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/parser-spacepacket": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/parser-spacepacket/-/parser-spacepacket-12.0.0.tgz", - "integrity": "sha512-dozONxhPC/78pntuxpz/NOtVps8qIc/UZzdc/LuPvVsqCoJXiRxOg6ZtCP/W58iibJDKPZPAWPGYeZt9DJxI+Q==", - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/stream": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/@serialport/stream/-/stream-12.0.0.tgz", - "integrity": "sha512-9On64rhzuqKdOQyiYLYv2lQOh3TZU/D3+IWCR5gk0alPel2nwpp4YwDEGiUBfrQZEdQ6xww0PWkzqth4wqwX3Q==", - "dependencies": { - "@serialport/bindings-interface": "1.2.2", - "debug": "4.3.4" - }, - "engines": { - "node": ">=12.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/@serialport/stream/node_modules/debug": { - "version": "4.3.4", - "resolved": "https://registry.npmmirror.com/debug/-/debug-4.3.4.tgz", - "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dependencies": { - "ms": "2.1.2" - }, - "engines": { - "node": ">=6.0" - }, - "peerDependenciesMeta": { - "supports-color": { - "optional": true - } - } - }, - "node_modules/@serialport/stream/node_modules/ms": { - "version": "2.1.2", - "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" - }, "node_modules/@sindresorhus/is": { "version": "4.6.0", "resolved": "https://registry.npmmirror.com/@sindresorhus/is/-/is-4.6.0.tgz", @@ -3570,6 +3324,7 @@ "version": "4.4.3", "resolved": "https://registry.npmmirror.com/debug/-/debug-4.4.3.tgz", "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "dev": true, "dependencies": { "ms": "^2.1.3" }, @@ -5832,7 +5587,8 @@ "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true }, "node_modules/nanoid": { "version": "3.3.11", @@ -6881,54 +6637,6 @@ "randombytes": "^2.1.0" } }, - "node_modules/serialport": { - "version": "12.0.0", - "resolved": "https://registry.npmmirror.com/serialport/-/serialport-12.0.0.tgz", - "integrity": "sha512-AmH3D9hHPFmnF/oq/rvigfiAouAKyK/TjnrkwZRYSFZxNggJxwvbAbfYrLeuvq7ktUdhuHdVdSjj852Z55R+uA==", - "dependencies": { - "@serialport/binding-mock": "10.2.2", - "@serialport/bindings-cpp": "12.0.1", - "@serialport/parser-byte-length": "12.0.0", - "@serialport/parser-cctalk": "12.0.0", - "@serialport/parser-delimiter": "12.0.0", - "@serialport/parser-inter-byte-timeout": "12.0.0", - "@serialport/parser-packet-length": "12.0.0", - "@serialport/parser-readline": "12.0.0", - "@serialport/parser-ready": "12.0.0", - "@serialport/parser-regex": "12.0.0", - "@serialport/parser-slip-encoder": "12.0.0", - "@serialport/parser-spacepacket": "12.0.0", - "@serialport/stream": "12.0.0", - "debug": "4.3.4" - }, - "engines": { - "node": ">=16.0.0" - }, - "funding": { - "url": "https://opencollective.com/serialport/donate" - } - }, - "node_modules/serialport/node_modules/debug": { - "version": "4.3.4", - "resolved": "https://registry.npmmirror.com/debug/-/debug-4.3.4.tgz", - "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dependencies": { - "ms": "2.1.2" - }, - "engines": { - "node": ">=6.0" - }, - "peerDependenciesMeta": { - "supports-color": { - "optional": true - } - } - }, - "node_modules/serialport/node_modules/ms": { - "version": "2.1.2", - "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" - }, "node_modules/set-blocking": { "version": "2.0.0", "resolved": "https://registry.npmmirror.com/set-blocking/-/set-blocking-2.0.0.tgz", diff --git a/src/utils/coupon-utils.js b/src/utils/coupon-utils.js index bcad2d8..cb454ab 100644 --- a/src/utils/coupon-utils.js +++ b/src/utils/coupon-utils.js @@ -1,10 +1,6 @@ -import { - BigNumber -} from "bignumber.js"; +import { BigNumber } from "bignumber.js"; import _ from "lodash"; - - /** * 返回商品单价 * @param goods 商品 @@ -12,19 +8,19 @@ import _ from "lodash"; * @param {Object} shopInfo */ export function returnGoodsPrice(goods, user, shopInfo) { - if (!goods) { - return 0 - } - if (goods.discount_sale_amount * 1 > 0) { - return goods.discount_sale_amount; - } - if (shopInfo && !shopInfo.isMemberPrice) { - return goods.salePrice; - } - if (user.isVip && goods.memberPrice * 1 <= goods.salePrice * 1 && goods.memberPrice * 1 > 0) { - return goods.memberPrice; - } - return goods.salePrice; + if (!goods) { + return 0; + } + if (goods.discount_sale_amount * 1 > 0) { + return goods.discount_sale_amount; + } + if (shopInfo && !shopInfo.isMemberPrice) { + return goods.salePrice; + } + if (user.isVip && goods.memberPrice * 1 <= goods.salePrice * 1 && goods.memberPrice * 1 > 0) { + return goods.memberPrice; + } + return goods.salePrice; } /** @@ -32,15 +28,15 @@ export function returnGoodsPrice(goods, user, shopInfo) { * @param arr 商品列表 */ export function returnGoodsGroupMap(arr) { - let map = {}; - arr.forEach((v) => { - const key = v.productId + "_" + v.skuId; - if (!map[key]) { - map[key] = []; - } - map[key].push(v); - }); - return map; + let map = {}; + arr.forEach((v) => { + const key = v.productId + "_" + v.skuId; + if (!map[key]) { + map[key] = []; + } + map[key].push(v); + }); + return map; } /** @@ -48,17 +44,17 @@ export function returnGoodsGroupMap(arr) { * @param coupon */ export function returnCoupType(coupon) { - const couponTypes = { - 1: "满减券", - 2: "商品券", - 3: "折扣券", - 4: "第二件半价券", - 5: "消费送券", - 6: "买一送一券", - 7: "固定价格券", - 8: "免配送费券", - }; - return couponTypes[coupon.type] || "未知类型"; + const couponTypes = { + 1: "满减券", + 2: "商品券", + 3: "折扣券", + 4: "第二件半价券", + 5: "消费送券", + 6: "买一送一券", + 7: "固定价格券", + 8: "免配送费券", + }; + return couponTypes[coupon.type] || "未知类型"; } /** @@ -68,37 +64,37 @@ export function returnCoupType(coupon) { * @param user 用户信息 */ export function returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user) { - const types = [2, 4, 6]; - // 收集已抵扣商品并关联对应的优惠券类型 - const goodsCouponGoods = selCoupon - .filter((v) => types.includes(v.type)) - .reduce((prev, cur) => { - // 给每个抵扣商品添加所属优惠券类型 - const goodsWithType = cur.discount.hasDiscountGoodsArr.map(goods => ({ - ...goods, - couponType: cur.type // 记录该商品是被哪种类型的优惠券抵扣的 - })); - prev.push(...goodsWithType); - return prev; - }, []); - const arr = _.cloneDeep(canDikouGoodsArr) - .map((v) => { - const findCart = goodsCouponGoods.find((carts) => carts.id == v.id); - if (findCart) { - // 根据优惠券类型判断扣减数量 - if ([4, 6].includes(findCart.couponType)) { - // 类型4(第二件半价)或6(买一送一),数量减2 - v.num -= 2; - } else { - // 其他类型(如类型2商品券),按原逻辑扣减对应数量 - v.num -= findCart.num; - } - } - return v; - }) - .filter((v) => v.num > 0); // 过滤掉数量<=0的商品 + const types = [2, 4, 6]; + // 收集已抵扣商品并关联对应的优惠券类型 + const goodsCouponGoods = selCoupon + .filter((v) => types.includes(v.type)) + .reduce((prev, cur) => { + // 给每个抵扣商品添加所属优惠券类型 + const goodsWithType = cur.discount.hasDiscountGoodsArr.map((goods) => ({ + ...goods, + couponType: cur.type, // 记录该商品是被哪种类型的优惠券抵扣的 + })); + prev.push(...goodsWithType); + return prev; + }, []); + const arr = _.cloneDeep(canDikouGoodsArr) + .map((v) => { + const findCart = goodsCouponGoods.find((carts) => carts.id == v.id); + if (findCart) { + // 根据优惠券类型判断扣减数量 + if ([4, 6].includes(findCart.couponType)) { + // 类型4(第二件半价)或6(买一送一),数量减2 + v.num -= 2; + } else { + // 其他类型(如类型2商品券),按原逻辑扣减对应数量 + v.num -= findCart.num; + } + } + return v; + }) + .filter((v) => v.num > 0); // 过滤掉数量<=0的商品 - return arr; + return arr; } /** @@ -118,177 +114,178 @@ export function returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user) { * @returns {Object} - { canUse: boolean, reason: string } 可用状态及不可用原因 */ export function returnCouponCanUse(args) { - let { - canDikouGoodsArr, - coupon, - goodsOrderPrice, - user, - selCoupon, - shopInfo - } = args; - // 优惠券未启用 - if (!coupon.use) { - return { - canUse: false, - reason:coupon.noUseRestrictions|| "不在可用时间段内" - }; - } + let { canDikouGoodsArr, coupon, goodsOrderPrice, user, selCoupon, shopInfo } = args; + // 优惠券未启用 + if (!coupon.use) { + return { + canUse: false, + reason: coupon.noUseRestrictions || "不在可用时间段内", + }; + } - // 计算门槛金额 - let fullAmount = goodsOrderPrice; - canDikouGoodsArr = returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user, shopInfo); - //优惠券指定门槛商品列表 - let canCalcGoodsArr = [...canDikouGoodsArr] - //部分商品参与门槛计算 - if (coupon.thresholdFoods.length) { - canCalcGoodsArr = canDikouGoodsArr.filter((v) => { - return coupon.thresholdFoods.find((food) => food.id == v.productId); - }); - fullAmount = canCalcGoodsArr.reduce((pre, cur) => { - return pre + returnGoodsPrice(cur, user, shopInfo) * cur.num; - }, 0); - } + // 计算门槛金额 + let fullAmount = goodsOrderPrice; + canDikouGoodsArr = returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user, shopInfo); + //优惠券指定门槛商品列表 + let canCalcGoodsArr = [...canDikouGoodsArr]; + //部分商品参与门槛计算 + if (coupon.thresholdFoods.length) { + canCalcGoodsArr = canDikouGoodsArr.filter((v) => { + return coupon.thresholdFoods.find((food) => food.id == v.productId); + }); + fullAmount = canCalcGoodsArr.reduce((pre, cur) => { + return pre + returnGoodsPrice(cur, user, shopInfo) * cur.num; + }, 0); + } + // 是否全部商品可用 + const isDikouAll = coupon.useFoods.length === 0; + // 订单可用商品列表 + let canUseGoodsArr = []; + if (!isDikouAll) { + canUseGoodsArr = canDikouGoodsArr.filter((v) => { + return coupon.useFoods.find((food) => food.id == v.productId); + }); + } + if (user.isVip && !coupon.vipPriceShare) { + return { + canUse: false, + reason: "非会员可用", + }; + } + if (selCoupon.length > 0 && !selCoupon[0].otherCouponShare) { + return { + canUse: false, + reason: "当前选中的券不可与其他券同享", + }; + } + if (selCoupon.length > 0 && !coupon.otherCouponShare) { + return { + canUse: false, + reason: "当前选中的券不可与其他券同享", + }; + } + // 满减券和折扣券计算门槛金额是否满足 + if ([1, 3].includes(coupon.type)) { + if (canCalcGoodsArr.length <= 0) { + return { + canUse: false, + reason: "没有可参与计算门槛的商品", + }; + } + // 不满足门槛金额 + if (fullAmount < coupon.fullAmount) { + return { + canUse: false, + reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元`, + }; + } + } + // 商品兑换券,第二件半价和买一送一判断是否有可用商品 + if ([2, 4, 5].includes(coupon.type)) { + console.log("商品兑换券", fullAmount, coupon.fullAmount); + if (coupon.type == 2 && fullAmount < coupon.fullAmount) { + console.log("商品兑换券", coupon); + return { + canUse: false, + reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元`, + }; + } + // 没有符合条件的商品 + if (isDikouAll && canDikouGoodsArr.length === 0) { + return { + canUse: false, + reason: "没有符合条件的商品", + }; + } + if (!isDikouAll && canUseGoodsArr.length === 0) { + return { + canUse: false, + reason: "没有符合条件的商品", + }; + } + } + //商品兑换券是否达到门槛金额 + if (coupon.type == 2 && goodsOrderPrice < coupon.fullAmount) { + return { + canUse: false, + reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元`, + }; + } - // 是否全部商品可用 - const isDikouAll = coupon.useFoods.length === 0; - // 订单可用商品列表 - let canUseGoodsArr = []; - if (!isDikouAll) { - canUseGoodsArr = canDikouGoodsArr.filter((v) => { - return coupon.useFoods.find((food) => food.id == v.productId); - }); - } - if (user.isVip && !coupon.vipPriceShare) { - return { - canUse: false, - reason: "非会员可用" - }; - } - if (selCoupon.length > 0 && !selCoupon[0].otherCouponShare) { - return { - canUse: false, - reason: "当前选中的券不可与其他券同享" - }; - } - if (selCoupon.length > 0 && !coupon.otherCouponShare) { - return { - canUse: false, - reason: "当前选中的券不可与其他券同享" - }; - } - // 满减券和折扣券计算门槛金额是否满足 - if ([1, 3].includes(coupon.type)) { - if (canCalcGoodsArr.length <= 0) { - return { - canUse: false, - reason: "没有可参与计算门槛的商品" - }; - } - // 不满足门槛金额 - if (fullAmount < coupon.fullAmount) { - return { - canUse: false, - reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元` - }; - } - } - // 商品兑换券,第二件半价和买一送一判断是否有可用商品 - if ([2, 4, 5].includes(coupon.type)) { - // 没有符合条件的商品 - if (isDikouAll && canDikouGoodsArr.length === 0) { - return { - canUse: false, - reason: "没有符合条件的商品" - }; - } - if (!isDikouAll && canUseGoodsArr.length === 0) { - return { - canUse: false, - reason: "没有符合条件的商品" - }; - } - } - //商品兑换券是否达到门槛金额 - if (coupon.type == 2 && goodsOrderPrice < coupon.fullAmount) { - return { - canUse: false, - reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元` - }; - } + // 买一送一券特殊验证 + if (coupon.type === 6) { + let canUse = false; + if (isDikouAll) { + canUse = canDikouGoodsArr.some((v) => v.num >= 2); + } else if (canCalcGoodsArr.length > 0) { + canUse = canCalcGoodsArr.some((v) => v.num >= 2); + } - // 买一送一券特殊验证 - if (coupon.type === 6) { - let canUse = false; - if (isDikouAll) { - canUse = canDikouGoodsArr.some((v) => v.num >= 2); - } else if (canCalcGoodsArr.length > 0) { - canUse = canCalcGoodsArr.some((v) => v.num >= 2); - } + if (!canUse) { + return { + canUse: false, + reason: "需要购买至少2件相同的商品才能使用", + }; + } + } - if (!canUse) { - return { - canUse: false, - reason: "需要购买至少2件相同的商品才能使用" - }; - } - } + // 第二件半价券特殊验证 + if (coupon.type === 4) { + let canUse = false; + if (isDikouAll) { + canUse = canDikouGoodsArr.some((v) => v.num >= 2); + } else if (canCalcGoodsArr.length > 0) { + canUse = canCalcGoodsArr.some((v) => v.num >= 2); + } - // 第二件半价券特殊验证 - if (coupon.type === 4) { - let canUse = false; - if (isDikouAll) { - canUse = canDikouGoodsArr.some((v) => v.num >= 2); - } else if (canCalcGoodsArr.length > 0) { - canUse = canCalcGoodsArr.some((v) => v.num >= 2); - } + if (!canUse) { + return { + canUse: false, + reason: "需要购买至少2件相同的商品才能使用", + }; + } + } - if (!canUse) { - return { - canUse: false, - reason: "需要购买至少2件相同的商品才能使用" - }; - } - } - - // 所有条件都满足 - return { - canUse: true, - reason: "" - }; + // 所有条件都满足 + return { + canUse: true, + reason: "", + }; } - /** * 计算抵扣商品金额 * @param discountGoodsArr 可抵扣商品列表 * @param discountNum 抵扣数量 * @param user 用户信息 - * @param {Object} shopInfo 店铺信息 + * @param {Object} shopInfo 店铺信息 */ export function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user, shopInfo) { - let hasCountNum = 0; - let discountPrice = 0; - let hasDiscountGoodsArr = []; - for (let i = 0; i < discountGoodsArr.length; i++) { - if (hasCountNum >= discountNum) { - break; - } - const goods = discountGoodsArr[i]; - const shengyuNum = discountNum - hasCountNum; - const num = Math.min(goods.num, shengyuNum); - discountPrice += returnGoodsPrice(goods, user, shopInfo) * num; - hasCountNum += num; - hasDiscountGoodsArr.push({ - ...goods, - num - }); - } - return { - discountPrice, - hasDiscountGoodsArr - }; + let hasCountNum = 0; + let discountPrice = 0; + let hasDiscountGoodsArr = []; + for (let i = 0; i < discountGoodsArr.length; i++) { + if (hasCountNum >= discountNum) { + break; + } + const goods = discountGoodsArr[i]; + const shengyuNum = discountNum - hasCountNum; + const num = Math.min(goods.num, shengyuNum); + discountPrice += returnGoodsPrice(goods, user, shopInfo) * num; + + hasCountNum += num; + hasDiscountGoodsArr.push({ + ...goods, + num, + }); + } + + return { + discountPrice, + hasDiscountGoodsArr, + }; } /** @@ -301,20 +298,20 @@ export function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user, s * @param shopInfo 店铺信息 */ export function returnCouponDiscount(arr, coupon, user, goodsOrderPrice, selCoupon, shopInfo) { - const canDikouGoodsArr = returnCanDikouGoodsArr(arr, selCoupon, user); - if (coupon.type == 2) { - return returnCouponProductDiscount(canDikouGoodsArr, coupon, user, shopInfo); - } - if (coupon.type == 6) { - const result = returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user, shopInfo); - return result - } - if (coupon.type == 4) { - return returnSecoendDiscount(canDikouGoodsArr, coupon, user, shopInfo); - } - if (coupon.type == 3) { - return returnCouponZhekouDiscount(canDikouGoodsArr, coupon, user, goodsOrderPrice, selCoupon); - } + const canDikouGoodsArr = returnCanDikouGoodsArr(arr, selCoupon, user); + if (coupon.type == 2) { + return returnCouponProductDiscount(canDikouGoodsArr, coupon, user, shopInfo); + } + if (coupon.type == 6) { + const result = returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user, shopInfo); + return result; + } + if (coupon.type == 4) { + return returnSecoendDiscount(canDikouGoodsArr, coupon, user, shopInfo); + } + if (coupon.type == 3) { + return returnCouponZhekouDiscount(canDikouGoodsArr, coupon, user, goodsOrderPrice, selCoupon); + } } /** @@ -327,45 +324,42 @@ export function returnCouponDiscount(arr, coupon, user, goodsOrderPrice, selCoup * */ export function returnCouponZhekouDiscount( - canDikouGoodsArr, - coupon, - user, - goodsOrderPrice, - selCoupon + canDikouGoodsArr, + coupon, + user, + goodsOrderPrice, + selCoupon ) { - const { - discountRate, - maxDiscountAmount - } = coupon; + const { discountRate, maxDiscountAmount } = coupon; - // 计算商品优惠券折扣总和,使用BigNumber避免精度问题 - const goodsCouponDiscount = selCoupon - .filter((v) => v.type == 2) - .reduce((prve, cur) => { - return new BigNumber(prve).plus(new BigNumber(cur.discount.discountPrice)); - }, new BigNumber(0)); + // 计算商品优惠券折扣总和,使用BigNumber避免精度问题 + const goodsCouponDiscount = selCoupon + .filter((v) => v.type == 2) + .reduce((prve, cur) => { + return new BigNumber(prve).plus(new BigNumber(cur.discount.discountPrice)); + }, new BigNumber(0)); - // 将商品订单价格转换为BigNumber并减去优惠券折扣 - const adjustedGoodsOrderPrice = new BigNumber(goodsOrderPrice).minus(goodsCouponDiscount); - console.log('adjustedGoodsOrderPrice', adjustedGoodsOrderPrice.toNumber()); + // 将商品订单价格转换为BigNumber并减去优惠券折扣 + const adjustedGoodsOrderPrice = new BigNumber(goodsOrderPrice).minus(goodsCouponDiscount); - // 计算优惠比例:(100 - 折扣率) / 100 - const discountAmountRatio = new BigNumber(100).minus(discountRate).dividedBy(100); + // 计算优惠比例:(100 - 折扣率) / 100 + const discountAmountRatio = new BigNumber(100).minus(discountRate).dividedBy(100); - // 计算折扣金额:调整后的商品订单金额 × 优惠比例 - let discountPrice = adjustedGoodsOrderPrice.times(discountAmountRatio) - .decimalPlaces(2, BigNumber.ROUND_FLOOR) - .toNumber(); + // 计算折扣金额:调整后的商品订单金额 × 优惠比例 + let discountPrice = adjustedGoodsOrderPrice + .times(discountAmountRatio) + .decimalPlaces(2, BigNumber.ROUND_FLOOR) + .toNumber(); - // 应用最大折扣金额限制 - if (maxDiscountAmount !== 0) { - discountPrice = discountPrice >= maxDiscountAmount ? maxDiscountAmount : discountPrice; - } + // 应用最大折扣金额限制 + if (maxDiscountAmount !== 0) { + discountPrice = discountPrice >= maxDiscountAmount ? maxDiscountAmount : discountPrice; + } - return { - discountPrice, // 折扣抵扣金额(即优惠的金额) - hasDiscountGoodsArr: [], - }; + return { + discountPrice, // 折扣抵扣金额(即优惠的金额) + hasDiscountGoodsArr: [], + }; } /** @@ -376,37 +370,29 @@ export function returnCouponZhekouDiscount( * @param shopInfo 店铺信息 */ export function returnCouponProductDiscount(canDikouGoodsArr, coupon, user, shopInfo) { - const { - useFoods, - discountNum, - useRule - } = coupon; - //抵扣商品数组 - let discountGoodsArr = []; - //抵扣全部商品 - if (useFoods.length === 0) { - if (useRule == "price_asc") { - discountGoodsArr = canDikouGoodsArr - .slice(canDikouGoodsArr.length - discountNum, canDikouGoodsArr.length) - .reverse(); - } else { - discountGoodsArr = canDikouGoodsArr.slice(0, discountNum); - } - } else { - //抵扣选中商品 - const discountSelGoodsArr = canDikouGoodsArr.filter((v) => - useFoods.find((food) => food.id == v.productId) - ); - if (useRule == "price_asc") { - discountGoodsArr = discountSelGoodsArr - .slice(discountSelGoodsArr.length - discountNum, discountSelGoodsArr.length) - .reverse(); - } else { - discountGoodsArr = discountSelGoodsArr.slice(0, discountNum); - } - } - const result = calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user, shopInfo); - return result; + const { useFoods, discountNum, useRule } = coupon; + //抵扣商品数组 + let discountGoodsArr = []; + //抵扣全部商品 + if (useFoods.length === 0) { + if (useRule == "price_asc") { + discountGoodsArr = canDikouGoodsArr.slice(discountNum * -1).reverse(); + } else { + discountGoodsArr = canDikouGoodsArr.slice(0, discountNum); + } + } else { + //抵扣选中商品 + const discountSelGoodsArr = canDikouGoodsArr.filter((v) => + useFoods.find((food) => food.id == v.productId) + ); + if (useRule == "price_asc") { + discountGoodsArr = discountSelGoodsArr.slice(discountNum * -1).reverse(); + } else { + discountGoodsArr = discountSelGoodsArr.slice(0, discountNum); + } + } + const result = calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user, shopInfo); + return result; } // 返回买一送一券抵扣详情 @@ -417,41 +403,38 @@ export function returnCouponProductDiscount(canDikouGoodsArr, coupon, user, shop * @param shopInfo 店铺信息 */ function returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user, shopInfo) { - const { - useFoods, - useRule - } = coupon; - //抵扣商品 - let discountGoods = undefined; - //符合买一送一条件的商品 - const canUseGoods = canDikouGoodsArr.filter((v) => v.num >= 2); - //抵扣全部商品 - if (useFoods.length === 0) { - if (useRule == "price_asc") { - discountGoods = canUseGoods[canUseGoods.length - 1] - } else { - discountGoods = canUseGoods[0]; - } - } else { - //符合抵扣条件的商品 - const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId)); - if (useRule == "price_asc") { - discountGoods = canUseGoods1[canUseGoods1.length - 1] - } else { - discountGoods = canUseGoods1[0] - } - } - let discountPrice = 0; - let hasDiscountGoodsArr = [] - console.log('returnCouponBuyOneGiveOneDiscount:discountGoods',discountGoods); - if (discountGoods) { - discountPrice = returnGoodsPrice(discountGoods, user, shopInfo); - hasDiscountGoodsArr = [discountGoods]; - } - return { - discountPrice:discountPrice<=0?0:discountPrice, - hasDiscountGoodsArr - }; + const { useFoods, useRule } = coupon; + //抵扣商品 + let discountGoods = undefined; + //符合买一送一条件的商品 + const canUseGoods = canDikouGoodsArr.filter((v) => v.num >= 2); + //抵扣全部商品 + if (useFoods.length === 0) { + if (useRule == "price_asc") { + discountGoods = canUseGoods[canUseGoods.length - 1]; + } else { + discountGoods = canUseGoods[0]; + } + } else { + //符合抵扣条件的商品 + const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId)); + if (useRule == "price_asc") { + discountGoods = canUseGoods1[canUseGoods1.length - 1]; + } else { + discountGoods = canUseGoods1[0]; + } + } + let discountPrice = 0; + let hasDiscountGoodsArr = []; + console.log("returnCouponBuyOneGiveOneDiscount:discountGoods", discountGoods); + if (discountGoods) { + discountPrice = returnGoodsPrice(discountGoods, user, shopInfo); + hasDiscountGoodsArr = [discountGoods]; + } + return { + discountPrice: discountPrice <= 0 ? 0 : discountPrice, + hasDiscountGoodsArr, + }; } /** @@ -462,41 +445,38 @@ function returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user, shopI * @param shopInfo 店铺信息 */ function returnSecoendDiscount(canDikouGoodsArr, coupon, user, shopInfo) { - const { - useFoods, - useRule - } = coupon; - //抵扣商品 - let discountGoods = undefined; - //符合条件的商品 - const canUseGoods = canDikouGoodsArr.filter((v) => v.num >= 2); - //抵扣全部商品 - if (useFoods.length === 0) { - if (useRule == "price_asc") { - discountGoods = canUseGoods[canUseGoods.length - 1] - } else { - discountGoods = canUseGoods[0] - } - } else { - //符合抵扣条件的商品 - const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId)); - if (useRule == "price_asc") { - discountGoods = canUseGoods1[canUseGoods1.length - 1] - } else { - discountGoods = canUseGoods1[0] - } - } - let discountPrice = 0; - let hasDiscountGoodsArr = [] - if (discountGoods) { - discountPrice = returnGoodsPrice(discountGoods, user, shopInfo); - hasDiscountGoodsArr = [discountGoods]; - } - //返回半价价格 - return { - discountPrice: discountPrice <= 0 ? 0 : new BigNumber(discountPrice).dividedBy(2).toNumber(), - hasDiscountGoodsArr, - }; + const { useFoods, useRule } = coupon; + //抵扣商品 + let discountGoods = undefined; + //符合条件的商品 + const canUseGoods = canDikouGoodsArr.filter((v) => v.num >= 2); + //抵扣全部商品 + if (useFoods.length === 0) { + if (useRule == "price_asc") { + discountGoods = canUseGoods[canUseGoods.length - 1]; + } else { + discountGoods = canUseGoods[0]; + } + } else { + //符合抵扣条件的商品 + const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId)); + if (useRule == "price_asc") { + discountGoods = canUseGoods1[canUseGoods1.length - 1]; + } else { + discountGoods = canUseGoods1[0]; + } + } + let discountPrice = 0; + let hasDiscountGoodsArr = []; + if (discountGoods) { + discountPrice = returnGoodsPrice(discountGoods, user, shopInfo); + hasDiscountGoodsArr = [discountGoods]; + } + //返回半价价格 + return { + discountPrice: discountPrice <= 0 ? 0 : new BigNumber(discountPrice).dividedBy(2).toNumber(), + hasDiscountGoodsArr, + }; } /** @@ -506,15 +486,15 @@ function returnSecoendDiscount(canDikouGoodsArr, coupon, user, shopInfo) { * @param shopInfo 店铺信息 */ export function returnCanDikouGoods(arr, user, shopInfo) { - const result = arr - .filter((v) => { - return v.is_temporary != 1 && v.is_gift != 1; - }) - .filter((v) => { - return v.num > 0; - }) - .sort((a, b) => { - return returnGoodsPrice(b, user, shopInfo) - returnGoodsPrice(a, user, shopInfo); - }); - return result; -} \ No newline at end of file + const result = arr + .filter((v) => { + return v.is_temporary != 1 && v.is_gift != 1; + }) + .filter((v) => { + return v.num > 0; + }) + .sort((a, b) => { + return returnGoodsPrice(b, user, shopInfo) - returnGoodsPrice(a, user, shopInfo); + }); + return result; +}