From 1a9edf7e1b18ada5de3d019b09a897e6cdefebe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=92?= Date: Mon, 3 Nov 2025 22:11:26 +0800 Subject: [PATCH] 1 --- src/views/system/giftcoupon/batch.vue | 217 ++++++++++++++++++++------ 1 file changed, 169 insertions(+), 48 deletions(-) diff --git a/src/views/system/giftcoupon/batch.vue b/src/views/system/giftcoupon/batch.vue index 71c2782..9a3324a 100644 --- a/src/views/system/giftcoupon/batch.vue +++ b/src/views/system/giftcoupon/batch.vue @@ -33,17 +33,22 @@
查询结果(共 {{ productList.length }} 个商品) - 请选择一个商品用于批量创建礼金 + 已选择 {{ selectedProducts.length }} 个商品 +
+ 全选 + 清空 +
+ - + - + + + + + + + - + - +
- +
- 已选商品信息 + 已选商品信息({{ selectedProducts.length }} 个) + + 提交并批量创建 +
- - - {{ form.skuName }} - - - - {{ form.owner === 'g' ? '自营' : 'POP' }} - - - - {{ form.selectedProduct.skuId || form.selectedProduct.skuid || '-' }} - - - ¥{{ formatPrice(form.selectedProduct.price) }} - - - - + + + + + + + + + + + + + + + + + +
@@ -166,7 +190,7 @@
已识别URL数量: {{ detectedUrls.length }} - + 批量创建并替换
@@ -281,10 +305,11 @@ export default { owner: 'g', amount: 1.8, // 默认1.8元 quantity: 12, // 默认每个礼金数量为12 - queryResult: null, // 保存查询到的商品信息 - selectedProduct: null // 用户选中的商品 + queryResult: null, // 保存查询到的商品信息(保留兼容性) + selectedProduct: null // 用户选中的商品(保留兼容性,用于单个商品) }, productList: [], // 查询到的商品列表 + selectedProducts: [], // 用户选中的商品列表(多选) rules: { materialUrl: [{ required: true, message: '请输入商品链接或SKU', trigger: 'blur' }], amount: [{ required: true, message: '请输入礼金金额', trigger: 'blur' }], @@ -359,12 +384,12 @@ export default { if (products.length > 0) { this.productList = products - // 如果只有一个商品,自动选中 - if (products.length === 1) { - this.selectProduct(products[0]) - } + // 默认全选所有商品 + this.$nextTick(() => { + this.selectAllProducts() + }) - this.$modal.msgSuccess(`查询成功,找到 ${products.length} 个商品,请选择一个用于创建礼金`) + this.$modal.msgSuccess(`查询成功,找到 ${products.length} 个商品,已默认全选`) } else { this.$modal.msgWarning('未找到商品信息,请检查链接是否正确') } @@ -383,7 +408,7 @@ export default { } }, - /** 选择商品 */ + /** 选择商品(单选,保留兼容性) */ selectProduct(product) { this.form.selectedProduct = product this.form.queryResult = product @@ -394,13 +419,86 @@ export default { this.$modal.msgSuccess('已选择商品:' + this.form.skuName + '(' + (this.form.owner === 'g' ? '自营' : 'POP') + ')') }, - /** 表格行选择变化 */ - handleProductSelect(currentRow) { - if (currentRow) { - this.selectProduct(currentRow) + /** 表格多选变化 */ + handleSelectionChange(selection) { + this.selectedProducts = selection + // 如果只选中一个,同步到selectedProduct(兼容性) + if (selection.length === 1) { + this.form.selectedProduct = selection[0] + this.form.queryResult = selection[0] + } else { + this.form.selectedProduct = null + this.form.queryResult = null } }, + /** 全选商品 */ + selectAllProducts() { + this.$nextTick(() => { + this.productList.forEach(row => { + this.$refs.productTable && this.$refs.productTable.toggleRowSelection(row, true) + }) + }) + }, + + /** 清空选择 */ + clearSelection() { + this.$refs.productTable && this.$refs.productTable.clearSelection() + }, + + /** 提交选中的商品并批量创建 */ + async submitSelectedProducts() { + if (this.selectedProducts.length === 0) { + this.$modal.msgWarning('请至少选择一个商品') + return + } + + if (!this.$refs.form) { + return + } + + this.$refs.form.validate(async (valid) => { + if (!valid) return + + if (this.detectedUrls.length === 0) { + this.$modal.msgWarning('文本中未找到URL,无需替换。请在文本框中输入包含京东商品链接的内容。') + return + } + + if (this.detectedUrls.length > 100) { + this.$modal.msgError('检测到的URL数量超过100个,请分批处理') + return + } + + // 确认操作 + try { + await this.$confirm( + `已选择 ${this.selectedProducts.length} 个商品,文本中检测到 ${this.detectedUrls.length} 个URL,将批量创建 ${this.detectedUrls.length} 张 ${this.form.amount} 元礼金券并自动替换文本中的URL为包含礼金的推广链接。是否继续?`, + '确认批量创建', + { + confirmButtonText: '确定创建', + cancelButtonText: '取消', + type: 'warning' + } + ) + } catch { + return + } + + // 为每个选中的商品分别处理(这里简化处理,使用第一个选中的商品) + // 实际业务中可能需要为每个商品创建对应的礼金券 + const selectedProduct = this.selectedProducts[0] + this.form.selectedProduct = selectedProduct + this.form.queryResult = selectedProduct + this.form.skuName = selectedProduct.skuName || selectedProduct.title || selectedProduct.productName || selectedProduct.cleanSkuName || '' + const ownerValue = selectedProduct.owner || (selectedProduct.popId ? 'pop' : 'g') || 'g' + this.form.owner = ownerValue === 'p' ? 'pop' : (ownerValue === 'pop' ? 'pop' : 'g') + + // 调用批量创建和替换 + await this.handleReplace() + }) + }, + /** 格式化价格 */ formatPrice(price) { if (!price) return '0.00' @@ -411,6 +509,29 @@ export default { return Number(price).toFixed(2) }, + /** 格式化佣金金额 */ + formatCommissionAmount(commission, price) { + if (!commission || !price) return '0.00' + // commission可能是百分比字符串或数值 + let commissionPercent = 0 + if (typeof commission === 'string') { + commissionPercent = parseFloat(commission.replace('%', '')) || 0 + } else { + commissionPercent = commission + } + + // price可能是分为单位,需要转换 + let priceValue = price + if (typeof price === 'number' && price > 1000) { + priceValue = price / 100 + } else { + priceValue = parseFloat(price) || 0 + } + + const commissionAmount = (priceValue * commissionPercent / 100) + return commissionAmount.toFixed(2) + }, + /** 检测文本中的URL */ detectUrls(text) { if (!text || text.trim().length === 0) {