This commit is contained in:
2025-10-31 15:47:31 +08:00
parent 60214c1acb
commit 06edf3d165

View File

@@ -456,21 +456,36 @@ export default {
}
// 调用创建礼金接口
console.log('创建礼金请求参数:', params)
const res = await createGiftCoupon(params)
console.log('创建礼金返回结果:', res)
if (res && res.code === 200) {
// 检查返回结果
if (!res) {
this.$modal.msgError('接口返回为空,请检查网络连接')
return
}
// 判断成功条件code === 200 或者直接返回了giftCouponKey
const isSuccess = res.code === 200 || (res.data && (res.data.giftCouponKey || res.data.giftKey))
if (isSuccess) {
// 解析返回的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
// 尝试多种可能的字段名
giftKey = res.data.giftCouponKey || res.data.giftKey || (res.data.data && (res.data.data.giftCouponKey || res.data.data.giftKey))
}
console.log('解析到的礼金Key', giftKey)
if (giftKey) {
// 可选自动生成短链参考JD项目的完整流程
try {
const transferParams = { materialUrl: materialUrl }
const transferParams = {}
// 优先使用SKU ID作为materialUrl
if (skuId && isUrl) {
transferParams.materialUrl = skuId
} else {
@@ -478,41 +493,72 @@ export default {
}
transferParams.giftCouponKey = giftKey
console.log('转链请求参数:', transferParams)
const tf = await transferWithGift(transferParams)
console.log('转链返回结果:', tf)
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('礼金创建成功礼金Key' + giftKey)
}
} else {
this.$modal.msgWarning('礼金创建完成但未返回礼金Key请检查')
} else if (tf.data && typeof tf.data === 'object') {
// 尝试其他可能的字段
shortUrl = tf.data.url || tf.data.link || ''
}
this.createDialogVisible = false
// 刷新列表和统计
this.getList()
this.loadStatistics()
if (shortUrl) {
this.$modal.msgSuccess('礼金创建成功!短链:' + shortUrl)
// 复制短链到剪贴板
this.copyToClipboard(shortUrl)
} else {
// 错误处理
const errorMsg = res?.msg || res?.data?.msg || '创建失败'
this.$modal.msgError(errorMsg)
this.$modal.msgSuccess('礼金创建成功礼金Key' + giftKey)
}
} else {
this.$modal.msgSuccess('礼金创建成功礼金Key' + giftKey)
}
} catch (e) {
console.error('创建礼金失败', e)
this.$modal.msgError('创建失败:' + (e.message || e.msg || '未知错误'))
console.error('生成短链失败', e)
this.$modal.msgSuccess('礼金创建成功礼金Key' + giftKey + '(短链生成失败)')
}
} else {
// 没有礼金Key但接口返回成功可能是其他格式
console.warn('未找到礼金Key完整返回', res)
this.$modal.msgWarning('礼金创建完成但未返回礼金Key。返回数据' + JSON.stringify(res.data).substring(0, 100))
}
// 关闭对话框并刷新
this.createDialogVisible = false
// 延迟刷新,确保用户能看到提示
setTimeout(() => {
this.getList()
this.loadStatistics()
}, 500)
} else {
// 错误处理 - 更详细的错误信息
console.error('创建礼金失败,返回:', res)
let errorMsg = '创建失败'
if (res.msg) {
errorMsg = res.msg
} else if (res.data && res.data.msg) {
errorMsg = res.data.msg
} else if (res.message) {
errorMsg = res.message
} else if (typeof res === 'string') {
errorMsg = res
}
this.$modal.msgError('创建失败:' + errorMsg)
}
} catch (e) {
console.error('创建礼金异常', e)
let errorMsg = '未知错误'
if (e.response && e.response.data) {
errorMsg = e.response.data.msg || e.response.data.message || JSON.stringify(e.response.data)
} else if (e.message) {
errorMsg = e.message
}
this.$modal.msgError('创建失败:' + errorMsg)
} finally {
this.createLoading = false
}