From 60214c1acb5423238427eedd6007f4a23167ecea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=92?= Date: Fri, 31 Oct 2025 15:45:46 +0800 Subject: [PATCH] 1 --- src/views/system/giftcoupon/index.vue | 116 ++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 16 deletions(-) diff --git a/src/views/system/giftcoupon/index.vue b/src/views/system/giftcoupon/index.vue index 55d943a..6e5d713 100644 --- a/src/views/system/giftcoupon/index.vue +++ b/src/views/system/giftcoupon/index.vue @@ -152,7 +152,8 @@ - + +
支持:京东商品链接或SKU ID(纯数字)
@@ -164,8 +165,8 @@ - -
范围:1-50元
+ +
范围:1-50元(参考JD项目要求)
@@ -386,49 +387,132 @@ export default { } this.createDialogVisible = true }, + /** 从链接中提取SKU ID */ + extractSkuIdFromUrl(url) { + if (!url) return null + // 如果是纯数字,直接返回 + if (/^\d+$/.test(url.trim())) { + return url.trim() + } + // 从URL中提取SKU ID + // 京东链接格式:https://item.jd.com/100012043978.html 或 jd.com/123456.html + const skuMatch = url.match(/(?:item\.jd\.com|jd\.com)\/(\d+)/i) + if (skuMatch && skuMatch[1]) { + return skuMatch[1] + } + // 如果包含skuId参数 + const paramMatch = url.match(/[?&]skuId=(\d+)/i) + if (paramMatch && paramMatch[1]) { + return paramMatch[1] + } + // 无法提取,返回原链接(让后端处理) + return url.trim() + }, /** 提交创建礼金 */ async submitCreate() { this.$refs.createForm.validate(async (valid) => { if (!valid) return - if (!this.createForm.amount || this.createForm.amount < 0.01 || this.createForm.amount > 50) { - this.$modal.msgError('礼金金额必须在0.01-50元之间') + + // 金额校验:1-50元(参考JD项目中的isValidAmount方法) + if (!this.createForm.amount || this.createForm.amount < 1 || this.createForm.amount > 50) { + this.$modal.msgError('礼金金额必须在1-50元之间') return } if (!this.createForm.quantity || this.createForm.quantity < 1 || this.createForm.quantity > 100) { this.$modal.msgError('礼金数量必须在1-100之间') return } + + // 提取SKU ID或使用materialUrl + const materialUrl = this.createForm.materialUrl.trim() + if (!materialUrl) { + this.$modal.msgError('请输入商品链接或SKU ID') + return + } + + const skuId = this.extractSkuIdFromUrl(materialUrl) + const isUrl = skuId !== materialUrl + this.createLoading = true try { + // 构建请求参数(参考JD项目的参数格式) const params = { - materialUrl: this.createForm.materialUrl, amount: this.createForm.amount, quantity: this.createForm.quantity, - owner: this.createForm.owner, - skuName: this.createForm.skuName + owner: this.createForm.owner || 'g', + skuName: this.createForm.skuName || '' } + + // 根据提取结果设置skuId或materialUrl + if (isUrl && skuId) { + // 如果从URL中提取到了SKU ID,优先使用skuId + params.skuId = skuId + } else if (isUrl) { + // 如果提取不到SKU ID,使用原始URL + params.materialUrl = materialUrl + } else { + // 纯数字,作为SKU ID + params.skuId = skuId + } + + // 调用创建礼金接口 const res = await createGiftCoupon(params) + if (res && res.code === 200) { - const giftKey = (res.data && (res.data.giftCouponKey || res.data.giftKey)) || (typeof res.data === 'string' ? res.data : null) + // 解析返回的giftCouponKey(参考xbmessage中的处理方式) + let giftKey = null + if (typeof res.data === 'string') { + giftKey = res.data + } else if (res.data) { + giftKey = res.data.giftCouponKey || res.data.giftKey || res.data.data?.giftCouponKey + } + if (giftKey) { - // 可选:自动生成短链 + // 可选:自动生成短链(参考JD项目的完整流程) try { - await transferWithGift({ materialUrl: this.createForm.materialUrl, giftCouponKey: giftKey }) - this.$modal.msgSuccess('礼金创建成功,短链已生成') + const transferParams = { materialUrl: materialUrl } + if (skuId && isUrl) { + transferParams.materialUrl = skuId + } else { + transferParams.materialUrl = materialUrl + } + transferParams.giftCouponKey = giftKey + + const tf = await transferWithGift(transferParams) + if (tf && tf.code === 200) { + let shortUrl = '' + if (typeof tf.data === 'string') { + shortUrl = tf.data + } else if (tf.data && tf.data.shortURL) { + shortUrl = tf.data.shortURL + } + if (shortUrl) { + this.$modal.msgSuccess('礼金创建成功,短链已生成') + } else { + this.$modal.msgSuccess('礼金创建成功,礼金Key:' + giftKey) + } + } else { + this.$modal.msgSuccess('礼金创建成功,礼金Key:' + giftKey) + } } catch (e) { - this.$modal.msgSuccess('礼金创建成功') + this.$modal.msgSuccess('礼金创建成功,礼金Key:' + giftKey) } } else { - this.$modal.msgSuccess('礼金创建成功') + this.$modal.msgWarning('礼金创建完成,但未返回礼金Key,请检查') } + this.createDialogVisible = false + // 刷新列表和统计 this.getList() this.loadStatistics() } else { - this.$modal.msgError(res.msg || '创建失败') + // 错误处理 + const errorMsg = res?.msg || res?.data?.msg || '创建失败' + this.$modal.msgError(errorMsg) } } catch (e) { - this.$modal.msgError('创建失败:' + (e.message || '未知错误')) + console.error('创建礼金失败', e) + this.$modal.msgError('创建失败:' + (e.message || e.msg || '未知错误')) } finally { this.createLoading = false }