This commit is contained in:
2025-10-31 15:45:46 +08:00
parent 7026d1fe1d
commit 60214c1acb

View File

@@ -152,7 +152,8 @@
<el-dialog title="创建礼金" :visible.sync="createDialogVisible" width="500px" append-to-body>
<el-form :model="createForm" :rules="createRules" ref="createForm" label-width="120px">
<el-form-item label="商品链接/SKU" prop="materialUrl">
<el-input v-model="createForm.materialUrl" placeholder="请输入商品链接或SKU ID" />
<el-input v-model="createForm.materialUrl" placeholder="请输入商品链接https://item.jd.com/100012043978.html或SKU ID100012043978" />
<div style="color: #909399; font-size: 12px; margin-top: 5px;">支持京东商品链接或SKU ID纯数字</div>
</el-form-item>
<el-form-item label="商品名称" prop="skuName">
<el-input v-model="createForm.skuName" placeholder="商品名称(可选)" />
@@ -164,8 +165,8 @@
</el-radio-group>
</el-form-item>
<el-form-item label="礼金金额(元)" prop="amount">
<el-input-number v-model="createForm.amount" :min="0.01" :max="50" :precision="2" :step="0.01" style="width: 100%" />
<div style="color: #909399; font-size: 12px; margin-top: 5px;">范围1-50</div>
<el-input-number v-model="createForm.amount" :min="1" :max="50" :precision="2" :step="0.01" style="width: 100%" />
<div style="color: #909399; font-size: 12px; margin-top: 5px;">范围1-50参考JD项目要求</div>
</el-form-item>
<el-form-item label="礼金数量" prop="quantity">
<el-input-number v-model="createForm.quantity" :min="1" :max="100" style="width: 100%" />
@@ -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 })
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('礼金创建成功,短链已生成')
} catch (e) {
this.$modal.msgSuccess('礼金创建成功')
} else {
this.$modal.msgSuccess('礼金创建成功礼金Key' + giftKey)
}
} else {
this.$modal.msgSuccess('礼金创建成功')
this.$modal.msgSuccess('礼金创建成功礼金Key' + giftKey)
}
} catch (e) {
this.$modal.msgSuccess('礼金创建成功礼金Key' + giftKey)
}
} else {
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
}