1
This commit is contained in:
@@ -152,7 +152,8 @@
|
|||||||
<el-dialog title="创建礼金" :visible.sync="createDialogVisible" width="500px" append-to-body>
|
<el-dialog title="创建礼金" :visible.sync="createDialogVisible" width="500px" append-to-body>
|
||||||
<el-form :model="createForm" :rules="createRules" ref="createForm" label-width="120px">
|
<el-form :model="createForm" :rules="createRules" ref="createForm" label-width="120px">
|
||||||
<el-form-item label="商品链接/SKU" prop="materialUrl">
|
<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 ID(如:100012043978)" />
|
||||||
|
<div style="color: #909399; font-size: 12px; margin-top: 5px;">支持:京东商品链接或SKU ID(纯数字)</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="商品名称" prop="skuName">
|
<el-form-item label="商品名称" prop="skuName">
|
||||||
<el-input v-model="createForm.skuName" placeholder="商品名称(可选)" />
|
<el-input v-model="createForm.skuName" placeholder="商品名称(可选)" />
|
||||||
@@ -164,8 +165,8 @@
|
|||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="礼金金额(元)" prop="amount">
|
<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%" />
|
<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元</div>
|
<div style="color: #909399; font-size: 12px; margin-top: 5px;">范围:1-50元(参考JD项目要求)</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="礼金数量" prop="quantity">
|
<el-form-item label="礼金数量" prop="quantity">
|
||||||
<el-input-number v-model="createForm.quantity" :min="1" :max="100" style="width: 100%" />
|
<el-input-number v-model="createForm.quantity" :min="1" :max="100" style="width: 100%" />
|
||||||
@@ -386,49 +387,132 @@ export default {
|
|||||||
}
|
}
|
||||||
this.createDialogVisible = true
|
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() {
|
async submitCreate() {
|
||||||
this.$refs.createForm.validate(async (valid) => {
|
this.$refs.createForm.validate(async (valid) => {
|
||||||
if (!valid) return
|
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
|
return
|
||||||
}
|
}
|
||||||
if (!this.createForm.quantity || this.createForm.quantity < 1 || this.createForm.quantity > 100) {
|
if (!this.createForm.quantity || this.createForm.quantity < 1 || this.createForm.quantity > 100) {
|
||||||
this.$modal.msgError('礼金数量必须在1-100之间')
|
this.$modal.msgError('礼金数量必须在1-100之间')
|
||||||
return
|
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
|
this.createLoading = true
|
||||||
try {
|
try {
|
||||||
|
// 构建请求参数(参考JD项目的参数格式)
|
||||||
const params = {
|
const params = {
|
||||||
materialUrl: this.createForm.materialUrl,
|
|
||||||
amount: this.createForm.amount,
|
amount: this.createForm.amount,
|
||||||
quantity: this.createForm.quantity,
|
quantity: this.createForm.quantity,
|
||||||
owner: this.createForm.owner,
|
owner: this.createForm.owner || 'g',
|
||||||
skuName: this.createForm.skuName
|
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)
|
const res = await createGiftCoupon(params)
|
||||||
|
|
||||||
if (res && res.code === 200) {
|
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) {
|
if (giftKey) {
|
||||||
// 可选:自动生成短链
|
// 可选:自动生成短链(参考JD项目的完整流程)
|
||||||
try {
|
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('礼金创建成功,短链已生成')
|
this.$modal.msgSuccess('礼金创建成功,短链已生成')
|
||||||
} catch (e) {
|
} else {
|
||||||
this.$modal.msgSuccess('礼金创建成功')
|
this.$modal.msgSuccess('礼金创建成功,礼金Key:' + giftKey)
|
||||||
}
|
}
|
||||||
} else {
|
} 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.createDialogVisible = false
|
||||||
|
// 刷新列表和统计
|
||||||
this.getList()
|
this.getList()
|
||||||
this.loadStatistics()
|
this.loadStatistics()
|
||||||
} else {
|
} else {
|
||||||
this.$modal.msgError(res.msg || '创建失败')
|
// 错误处理
|
||||||
|
const errorMsg = res?.msg || res?.data?.msg || '创建失败'
|
||||||
|
this.$modal.msgError(errorMsg)
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.$modal.msgError('创建失败:' + (e.message || '未知错误'))
|
console.error('创建礼金失败', e)
|
||||||
|
this.$modal.msgError('创建失败:' + (e.message || e.msg || '未知错误'))
|
||||||
} finally {
|
} finally {
|
||||||
this.createLoading = false
|
this.createLoading = false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user