From 94cb24041ad2f4e54cb4d7f1fadb7e76ed869d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=92?= Date: Fri, 31 Oct 2025 16:45:34 +0800 Subject: [PATCH] 2 --- src/views/system/giftcoupon/index.vue | 104 +++++++++++++++++++++----- 1 file changed, 84 insertions(+), 20 deletions(-) diff --git a/src/views/system/giftcoupon/index.vue b/src/views/system/giftcoupon/index.vue index cb5c4d3..73b8d1a 100644 --- a/src/views/system/giftcoupon/index.vue +++ b/src/views/system/giftcoupon/index.vue @@ -299,7 +299,8 @@ export default { skuName: '', owner: 'g', amount: 5, // 默认5元 - quantity: 10 // 默认10 + quantity: 10, // 默认10 + queryResult: null // 保存查询到的商品信息,包含materialUrl等 }, queryLoading: false, // 查询商品信息加载状态 createRules: { @@ -393,7 +394,8 @@ export default { skuName: '', owner: 'g', amount: 5, // 默认5元 - quantity: 10 // 默认10 + quantity: 10, // 默认10 + queryResult: null // 查询结果 } this.createDialogVisible = true }, @@ -450,16 +452,24 @@ export default { console.log('提取的商品信息:', productInfo) if (productInfo) { + // 保存完整的查询结果 + this.createForm.queryResult = productInfo + // 自动填充商品信息 this.createForm.skuName = productInfo.skuName || productInfo.title || productInfo.productName || productInfo.cleanSkuName || '' // owner字段:'p'表示POP,'g'表示自营,其他默认'g' const ownerValue = productInfo.owner || (productInfo.popId ? 'pop' : 'g') || 'g' this.createForm.owner = ownerValue === 'p' ? 'pop' : (ownerValue === 'pop' ? 'pop' : 'g') - // 如果有materialUrl,可以用于后续创建礼金 - if (productInfo.materialUrl && !this.createForm.materialUrl.includes('jingfen.jd.com')) { - // 如果用户输入的是普通链接,可以使用查询到的materialUrl(jingfen链接) - // 但为了不丢失用户输入,这里先不更新 + // 保存查询到的materialUrl或spuid,用于后续创建礼金 + // 优先使用jingfen链接的materialUrl,如果没有则使用spuid + if (productInfo.materialUrl) { + // 保存materialUrl到查询结果中,创建礼金时使用 + console.log('查询到的materialUrl:', productInfo.materialUrl) + } + if (productInfo.spuid) { + // 保存spuid(SKU ID),可以用于创建礼金 + console.log('查询到的spuid:', productInfo.spuid) } this.$modal.msgSuccess('商品信息查询成功:' + (this.createForm.owner === 'g' ? '自营' : 'POP')) @@ -538,15 +548,39 @@ export default { } // 根据提取结果设置skuId或materialUrl - if (isUrl && skuId) { - // 如果从URL中提取到了SKU ID,优先使用skuId - params.skuId = skuId - } else if (isUrl) { - // 如果提取不到SKU ID,使用原始URL - params.materialUrl = materialUrl + // 优先使用查询商品信息时获取的数据 + if (this.createForm.queryResult) { + // 如果查询过商品信息,优先使用查询结果中的materialUrl或spuid + const queryResult = this.createForm.queryResult + if (queryResult.materialUrl) { + // 优先使用jingfen链接 + params.materialUrl = queryResult.materialUrl + console.log('使用查询到的materialUrl:', queryResult.materialUrl) + } else if (queryResult.spuid && /^\d+$/.test(queryResult.spuid)) { + // 如果没有materialUrl,使用spuid(SKU ID) + params.skuId = queryResult.spuid + console.log('使用查询到的spuid:', queryResult.spuid) + } else if (isUrl && skuId && /^\d+$/.test(skuId)) { + // 从用户输入的URL中提取SKU ID + params.skuId = skuId + } else { + params.materialUrl = materialUrl + } } else { - // 纯数字,作为SKU ID - params.skuId = skuId + // 没有查询过商品信息,使用用户输入的 + if (isUrl && skuId && /^\d+$/.test(skuId)) { + // 如果从URL中提取到了纯数字SKU ID,优先使用skuId + params.skuId = skuId + } else if (isUrl) { + // 如果提取不到纯数字SKU ID,使用原始URL作为materialUrl + params.materialUrl = materialUrl + } else if (/^\d+$/.test(skuId)) { + // 纯数字,作为SKU ID + params.skuId = skuId + } else { + // 其他情况,作为materialUrl + params.materialUrl = materialUrl + } } // 调用创建礼金接口 @@ -566,14 +600,42 @@ export default { if (isSuccess) { // 解析返回的giftCouponKey(参考xbmessage中的处理方式) let giftKey = null + + // 先打印data的详细信息 + console.log('返回的data类型:', typeof res.data, 'data内容:', res.data) + if (typeof res.data === 'string') { - giftKey = res.data - } else if (res.data) { - // 尝试多种可能的字段名 - giftKey = res.data.giftCouponKey || res.data.giftKey || (res.data.data && (res.data.data.giftCouponKey || res.data.data.giftKey)) + // 如果是字符串,尝试解析 + try { + const parsed = JSON.parse(res.data) + console.log('data字符串解析后:', parsed) + giftKey = parsed.giftCouponKey || parsed.giftKey + } catch (e) { + // 如果解析失败,可能就是字符串本身 + giftKey = res.data + } + } else if (res.data && typeof res.data === 'object') { + // 尝试多种可能的字段名(优先顺序很重要) + // 1. 直接获取 giftCouponKey + if (res.data.giftCouponKey) { + giftKey = res.data.giftCouponKey + } + // 2. 获取 giftKey + else if (res.data.giftKey) { + giftKey = res.data.giftKey + } + // 3. 如果data里面还有data对象(嵌套情况) + else if (res.data.data && typeof res.data.data === 'object') { + giftKey = res.data.data.giftCouponKey || res.data.data.giftKey + } + // 4. 如果data是数组,取第一个 + else if (Array.isArray(res.data) && res.data.length > 0) { + const first = res.data[0] + giftKey = first.giftCouponKey || first.giftKey + } } - console.log('解析到的礼金Key:', giftKey) + console.log('解析到的礼金Key:', giftKey, '完整data结构:', JSON.stringify(res.data, null, 2)) if (giftKey) { // 可选:自动生成短链(参考JD项目的完整流程) @@ -619,7 +681,9 @@ export default { } else { // 没有礼金Key,但接口返回成功,可能是其他格式 console.warn('未找到礼金Key,完整返回:', res) - this.$modal.msgWarning('礼金创建完成,但未返回礼金Key。返回数据:' + JSON.stringify(res.data).substring(0, 100)) + console.warn('data的完整内容:', JSON.stringify(res.data, null, 2)) + // 即使没有Key,也提示成功,因为接口返回了code:200 + this.$modal.msgWarning('礼金创建完成,但未在返回数据中找到礼金Key。请查看控制台日志或联系技术支持。') } // 关闭对话框并刷新