1
This commit is contained in:
@@ -122,7 +122,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { generatePromotionContent, replaceUrlsWithGiftCoupons } from '@/api/system/jdorder'
|
import { generatePromotionContent, createGiftCoupon, transferWithGift } from '@/api/system/jdorder'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'BatchGiftCoupon',
|
name: 'BatchGiftCoupon',
|
||||||
@@ -216,100 +216,179 @@ export default {
|
|||||||
this.result = null
|
this.result = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 使用第一个URL查询商品信息
|
// 为每个URL单独处理:查询商品 → 创建礼金 → 转链
|
||||||
const firstUrl = this.detectedUrls[0]
|
let replacedContent = this.content
|
||||||
this.progress = 10
|
const replacements = []
|
||||||
this.progressText = '正在查询商品信息...'
|
let successCount = 0
|
||||||
this.progressDetail = `正在查询商品: ${firstUrl.substring(0, 50)}...`
|
|
||||||
|
|
||||||
const queryRes = await generatePromotionContent({ promotionContent: firstUrl })
|
for (let i = 0; i < this.detectedUrls.length; i++) {
|
||||||
|
const originalUrl = this.detectedUrls[i]
|
||||||
|
this.progress = Math.floor(10 + (i / this.detectedUrls.length) * 80)
|
||||||
|
this.progressText = `正在处理第 ${i + 1}/${this.detectedUrls.length} 个商品...`
|
||||||
|
this.progressDetail = `URL: ${originalUrl.substring(0, 50)}...`
|
||||||
|
|
||||||
if (queryRes && queryRes.code === 200) {
|
try {
|
||||||
let parsed = null
|
// 1. 查询商品信息
|
||||||
let resultStr = queryRes.data || queryRes.msg
|
const queryRes = await generatePromotionContent({ promotionContent: originalUrl })
|
||||||
|
|
||||||
if (typeof resultStr === 'string') {
|
if (!queryRes || queryRes.code !== 200) {
|
||||||
parsed = JSON.parse(resultStr)
|
replacements.push({
|
||||||
} else {
|
originalUrl,
|
||||||
parsed = resultStr
|
newUrl: originalUrl,
|
||||||
}
|
success: false,
|
||||||
|
error: '查询商品信息失败'
|
||||||
// 提取第一个商品信息
|
})
|
||||||
let product = null
|
continue
|
||||||
if (Array.isArray(parsed) && parsed.length > 0) {
|
|
||||||
product = parsed[0]
|
|
||||||
} else if (parsed && typeof parsed === 'object') {
|
|
||||||
if (parsed.list && Array.isArray(parsed.list) && parsed.list.length > 0) {
|
|
||||||
product = parsed.list[0]
|
|
||||||
} else if (parsed.data && Array.isArray(parsed.data) && parsed.data.length > 0) {
|
|
||||||
product = parsed.data[0]
|
|
||||||
} else if (parsed.materialUrl || parsed.skuName) {
|
|
||||||
product = parsed
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!product) {
|
let parsed = null
|
||||||
this.$modal.msgError('无法从URL中提取商品信息,请检查链接是否正确')
|
let resultStr = queryRes.data || queryRes.msg
|
||||||
this.processing = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
this.progress = 30
|
if (typeof resultStr === 'string') {
|
||||||
this.progressText = '正在批量创建礼金券...'
|
parsed = JSON.parse(resultStr)
|
||||||
this.progressDetail = `准备创建 ${this.detectedUrls.length} 张礼金券,请耐心等待...`
|
} else {
|
||||||
|
parsed = resultStr
|
||||||
// 构建请求参数
|
|
||||||
const params = {
|
|
||||||
content: this.content,
|
|
||||||
amount: this.form.amount,
|
|
||||||
quantity: this.form.quantity,
|
|
||||||
owner: this.form.owner || 'g',
|
|
||||||
skuName: product.skuName || product.title || product.productName || product.cleanSkuName || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置skuId或materialUrl
|
|
||||||
const isPop = this.form.owner === 'pop'
|
|
||||||
if (isPop) {
|
|
||||||
if (product.materialUrl) {
|
|
||||||
params.materialUrl = product.materialUrl
|
|
||||||
} else if ((product.skuId || product.skuid) && /^\d+$/.test(String(product.skuId || product.skuid))) {
|
|
||||||
params.skuId = String(product.skuId || product.skuid)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
const skuIdValue = product.skuId || product.skuid
|
// 提取商品信息
|
||||||
if (skuIdValue && /^\d+$/.test(String(skuIdValue))) {
|
let product = null
|
||||||
params.skuId = String(skuIdValue)
|
if (Array.isArray(parsed) && parsed.length > 0) {
|
||||||
} else if (product.materialUrl) {
|
product = parsed[0]
|
||||||
params.materialUrl = product.materialUrl
|
} else if (parsed && typeof parsed === 'object') {
|
||||||
|
if (parsed.list && Array.isArray(parsed.list) && parsed.list.length > 0) {
|
||||||
|
product = parsed.list[0]
|
||||||
|
} else if (parsed.data && Array.isArray(parsed.data) && parsed.data.length > 0) {
|
||||||
|
product = parsed.data[0]
|
||||||
|
} else if (parsed.materialUrl || parsed.skuName) {
|
||||||
|
product = parsed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!product) {
|
||||||
|
replacements.push({
|
||||||
|
originalUrl,
|
||||||
|
newUrl: originalUrl,
|
||||||
|
success: false,
|
||||||
|
error: '无法提取商品信息'
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 创建礼金券
|
||||||
|
const createParams = {
|
||||||
|
amount: this.form.amount,
|
||||||
|
quantity: this.form.quantity,
|
||||||
|
owner: this.form.owner || 'g',
|
||||||
|
skuName: product.skuName || product.title || product.productName || product.cleanSkuName || ''
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置skuId或materialUrl
|
||||||
|
const isPop = this.form.owner === 'pop'
|
||||||
|
if (isPop) {
|
||||||
|
if (product.materialUrl) {
|
||||||
|
createParams.materialUrl = product.materialUrl
|
||||||
|
} else if ((product.skuId || product.skuid) && /^\d+$/.test(String(product.skuId || product.skuid))) {
|
||||||
|
createParams.skuId = String(product.skuId || product.skuid)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const skuIdValue = product.skuId || product.skuid
|
||||||
|
if (skuIdValue && /^\d+$/.test(String(skuIdValue))) {
|
||||||
|
createParams.skuId = String(skuIdValue)
|
||||||
|
} else if (product.materialUrl) {
|
||||||
|
createParams.materialUrl = product.materialUrl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const createRes = await createGiftCoupon(createParams)
|
||||||
|
|
||||||
|
if (!createRes || createRes.code !== 200 || !createRes.data) {
|
||||||
|
replacements.push({
|
||||||
|
originalUrl,
|
||||||
|
newUrl: originalUrl,
|
||||||
|
success: false,
|
||||||
|
error: '创建礼金失败'
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const giftCouponKey = typeof createRes.data === 'object' ? createRes.data.giftCouponKey : null
|
||||||
|
|
||||||
|
if (!giftCouponKey) {
|
||||||
|
replacements.push({
|
||||||
|
originalUrl,
|
||||||
|
newUrl: originalUrl,
|
||||||
|
success: false,
|
||||||
|
error: '礼金Key为空'
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 转链(带礼金)
|
||||||
|
const transferParams = {
|
||||||
|
materialUrl: originalUrl,
|
||||||
|
giftCouponKey: giftCouponKey
|
||||||
|
}
|
||||||
|
|
||||||
|
const transferRes = await transferWithGift(transferParams)
|
||||||
|
|
||||||
|
if (transferRes && transferRes.code === 200 && transferRes.data) {
|
||||||
|
const shortUrl = typeof transferRes.data === 'object' ? transferRes.data.shortURL : transferRes.data
|
||||||
|
|
||||||
|
if (shortUrl) {
|
||||||
|
// 替换文本中的URL
|
||||||
|
replacedContent = replacedContent.replace(originalUrl, shortUrl)
|
||||||
|
replacements.push({
|
||||||
|
originalUrl,
|
||||||
|
newUrl: shortUrl,
|
||||||
|
success: true,
|
||||||
|
giftCouponKey: giftCouponKey
|
||||||
|
})
|
||||||
|
successCount++
|
||||||
|
} else {
|
||||||
|
replacements.push({
|
||||||
|
originalUrl,
|
||||||
|
newUrl: originalUrl,
|
||||||
|
success: false,
|
||||||
|
error: '转链失败'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
replacements.push({
|
||||||
|
originalUrl,
|
||||||
|
newUrl: originalUrl,
|
||||||
|
success: false,
|
||||||
|
error: '转链接口调用失败'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`处理URL失败: ${originalUrl}`, e)
|
||||||
|
replacements.push({
|
||||||
|
originalUrl,
|
||||||
|
newUrl: originalUrl,
|
||||||
|
success: false,
|
||||||
|
error: e.message || '未知错误'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log('批量替换请求参数:', params)
|
// 构建结果
|
||||||
|
this.result = {
|
||||||
|
replacedContent: replacedContent,
|
||||||
|
originalContent: this.content,
|
||||||
|
replacements: replacements,
|
||||||
|
totalUrls: this.detectedUrls.length,
|
||||||
|
replacedCount: successCount
|
||||||
|
}
|
||||||
|
|
||||||
// 调用替换接口
|
this.progress = 100
|
||||||
this.progress = 40
|
this.progressStatus = successCount === this.detectedUrls.length ? 'success' : (successCount > 0 ? 'warning' : 'exception')
|
||||||
const res = await replaceUrlsWithGiftCoupons(params)
|
this.progressText = successCount === this.detectedUrls.length ? '完成!' : '部分成功'
|
||||||
|
this.progressDetail = `成功替换 ${successCount} / ${this.detectedUrls.length} 个URL`
|
||||||
|
|
||||||
this.progress = 80
|
if (successCount > 0) {
|
||||||
this.progressText = '正在处理结果...'
|
this.$modal.msgSuccess(`✅ 批量替换完成!成功 ${successCount} / ${this.detectedUrls.length} 个`)
|
||||||
|
|
||||||
if (res && res.code === 200 && res.data) {
|
|
||||||
this.result = res.data
|
|
||||||
this.progress = 100
|
|
||||||
this.progressStatus = 'success'
|
|
||||||
this.progressText = '完成!'
|
|
||||||
this.progressDetail = `成功替换 ${this.result.replacedCount || 0} / ${this.result.totalUrls || 0} 个URL`
|
|
||||||
|
|
||||||
this.$modal.msgSuccess(`✅ 批量替换完成!成功 ${this.result.replacedCount || 0} / ${this.result.totalUrls || 0} 个`)
|
|
||||||
} else {
|
|
||||||
this.progress = 100
|
|
||||||
this.progressStatus = 'exception'
|
|
||||||
this.progressText = '失败'
|
|
||||||
this.progressDetail = res.msg || '未知错误'
|
|
||||||
this.$modal.msgError('批量替换失败:' + (res.msg || '未知错误'))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.$modal.msgError('查询商品信息失败:' + (queryRes.msg || '未知错误'))
|
this.$modal.msgError('批量替换失败,所有URL处理均失败')
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('批量替换异常', e)
|
console.error('批量替换异常', e)
|
||||||
|
|||||||
Reference in New Issue
Block a user