diff --git a/src/views/system/giftcoupon/batch.vue b/src/views/system/giftcoupon/batch.vue index 5de93cd..71c2782 100644 --- a/src/views/system/giftcoupon/batch.vue +++ b/src/views/system/giftcoupon/batch.vue @@ -10,38 +10,113 @@ - + - - 查询 - + +
+ 查询商品 +
- 重要:请先点击"查询"按钮获取商品信息(特别是SKU ID),然后再创建礼金。 + 重要:请输入商品链接或SKU ID,支持多行输入多个商品,点击查询后会显示商品列表供选择。
- - - - -
+ + + +
+ 查询结果(共 {{ productList.length }} 个商品) + 请选择一个商品用于批量创建礼金 +
+ + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ 已选商品信息 +
+ + + {{ form.skuName }} + + + + {{ form.owner === 'g' ? '自营' : 'POP' }} + + + + {{ form.selectedProduct.skuId || form.selectedProduct.skuid || '-' }} + + + ¥{{ formatPrice(form.selectedProduct.price) }} + - + + +
+ - - - - - - - @@ -91,7 +166,7 @@
已识别URL数量: {{ detectedUrls.length }} - + 批量创建并替换
@@ -204,10 +279,12 @@ export default { materialUrl: '', skuName: '', owner: 'g', - amount: 1.8, // 默认1元 - quantity: 12, // 默认每个礼金数量为1 - queryResult: null // 保存查询到的商品信息 + amount: 1.8, // 默认1.8元 + quantity: 12, // 默认每个礼金数量为12 + queryResult: null, // 保存查询到的商品信息 + selectedProduct: null // 用户选中的商品 }, + productList: [], // 查询到的商品列表 rules: { materialUrl: [{ required: true, message: '请输入商品链接或SKU', trigger: 'blur' }], amount: [{ required: true, message: '请输入礼金金额', trigger: 'blur' }], @@ -238,11 +315,14 @@ export default { } this.queryLoading = true + this.productList = [] + this.form.selectedProduct = null + this.form.queryResult = null + try { const res = await generatePromotionContent({ promotionContent: materialUrl }) if (res && res.code === 200) { - let productInfo = null try { let resultStr = res.data || res.msg let parsed = null @@ -252,41 +332,85 @@ export default { parsed = resultStr } + console.log('解析后的数据:', parsed) + + // 解析商品数组 + let products = [] if (Array.isArray(parsed) && parsed.length > 0) { - productInfo = parsed[0] + // 格式1:直接是数组 + products = parsed } else if (parsed && typeof parsed === 'object') { + // 格式2:对象中包含list数组 if (parsed.list && Array.isArray(parsed.list) && parsed.list.length > 0) { - productInfo = parsed.list[0] - } else if (parsed.data && Array.isArray(parsed.data) && parsed.data.length > 0) { - productInfo = parsed.data[0] - } else if (parsed.materialUrl || parsed.owner || parsed.skuName) { - productInfo = parsed + products = parsed.list + } + // 格式3:对象中包含data数组 + else if (parsed.data && Array.isArray(parsed.data) && parsed.data.length > 0) { + products = parsed.data + } + // 格式4:对象本身包含商品信息(单个商品) + else if (parsed.materialUrl || parsed.owner || parsed.skuName) { + products = [parsed] } } - if (productInfo) { - this.form.queryResult = productInfo - this.form.skuName = productInfo.skuName || productInfo.title || productInfo.productName || productInfo.cleanSkuName || '' - const ownerValue = productInfo.owner || (productInfo.popId ? 'pop' : 'g') || 'g' - this.form.owner = ownerValue === 'p' ? 'pop' : (ownerValue === 'pop' ? 'pop' : 'g') + console.log('解析到的商品列表:', products) + + if (products.length > 0) { + this.productList = products - this.$modal.msgSuccess('商品信息查询成功:' + (this.form.owner === 'g' ? '自营' : 'POP')) + // 如果只有一个商品,自动选中 + if (products.length === 1) { + this.selectProduct(products[0]) + } + + this.$modal.msgSuccess(`查询成功,找到 ${products.length} 个商品,请选择一个用于创建礼金`) } else { this.$modal.msgWarning('未找到商品信息,请检查链接是否正确') } } catch (e) { + console.error('解析商品信息失败', e) this.$modal.msgWarning('返回数据格式异常:' + e.message) } } else { this.$modal.msgError('查询商品信息失败:' + (res.msg || '未知错误')) } } catch (e) { + console.error('查询商品信息异常', e) this.$modal.msgError('查询失败:' + (e.message || '未知错误')) } finally { this.queryLoading = false } }, + /** 选择商品 */ + selectProduct(product) { + this.form.selectedProduct = product + this.form.queryResult = product + this.form.skuName = product.skuName || product.title || product.productName || product.cleanSkuName || '' + const ownerValue = product.owner || (product.popId ? 'pop' : 'g') || 'g' + this.form.owner = ownerValue === 'p' ? 'pop' : (ownerValue === 'pop' ? 'pop' : 'g') + + this.$modal.msgSuccess('已选择商品:' + this.form.skuName + '(' + (this.form.owner === 'g' ? '自营' : 'POP') + ')') + }, + + /** 表格行选择变化 */ + handleProductSelect(currentRow) { + if (currentRow) { + this.selectProduct(currentRow) + } + }, + + /** 格式化价格 */ + formatPrice(price) { + if (!price) return '0.00' + // 如果价格是分为单位,转换为元 + if (typeof price === 'number' && price > 1000) { + return (price / 100).toFixed(2) + } + return Number(price).toFixed(2) + }, + /** 检测文本中的URL */ detectUrls(text) { if (!text || text.trim().length === 0) { @@ -317,8 +441,8 @@ export default { this.$refs.form.validate(async (valid) => { if (!valid) return - if (!this.form.queryResult) { - this.$modal.msgWarning('请先查询商品信息') + if (!this.form.selectedProduct || !this.form.queryResult) { + this.$modal.msgWarning('请先查询商品信息并选择一个商品') return }