1
This commit is contained in:
97
src/utils/publishHelper.js
Normal file
97
src/utils/publishHelper.js
Normal file
@@ -0,0 +1,97 @@
|
||||
import { addToFavorites, getBySkuid } from "@/api/system/favoriteProduct";
|
||||
|
||||
function parseMaybeJson(str) {
|
||||
if (!str || typeof str !== 'string') return null;
|
||||
try { return JSON.parse(str); } catch(e) { return null; }
|
||||
}
|
||||
|
||||
function getNested(obj, path) {
|
||||
try {
|
||||
if (!obj) return null;
|
||||
const segs = path.split('.');
|
||||
let cur = obj;
|
||||
for (const k of segs) {
|
||||
if (cur && typeof cur === 'object' && k in cur) cur = cur[k]; else return null;
|
||||
}
|
||||
return cur;
|
||||
} catch(e) { return null; }
|
||||
}
|
||||
|
||||
export function parsePublishResponse(res) {
|
||||
try {
|
||||
const d = res && res.data ? res.data : res;
|
||||
const productId = d?.product_id ?? d?.productId ?? d?.data?.product_id ?? d?.data?.productId ?? null;
|
||||
const productStatus = d?.product_status ?? d?.productStatus ?? d?.data?.product_status ?? d?.data?.productStatus ?? null;
|
||||
const outerId = d?.outer_id ?? d?.outerId ?? d?.data?.outer_id ?? d?.data?.outerId ?? null;
|
||||
return { productId, productStatus, outerId };
|
||||
} catch(e) { return { productId: null, productStatus: null, outerId: null }; }
|
||||
}
|
||||
|
||||
function buildFavoriteFromTransfer(product, pub) {
|
||||
const spuid = product?.spuid || product?.skuId || product?.skuid || '';
|
||||
return {
|
||||
skuid: spuid,
|
||||
productName: product?.skuName || product?.title || '',
|
||||
shopName: product?.shopName || '',
|
||||
productUrl: product?.url || '',
|
||||
productImage: Array.isArray(product?.images) && product.images.length ? product.images[0] : '',
|
||||
price: (product?.price != null ? String(product.price) : (product?.lowestCouponPrice != null ? String(product.lowestCouponPrice) : '')),
|
||||
commissionInfo: product?.commissionShare ? `${product.commissionShare}%` : (product?.commission != null ? String(product.commission) : ''),
|
||||
remark: `自动添加 - 发品时间: ${new Date().toLocaleString()}${pub.outerId ? `, 商家编码: ${pub.outerId}` : ''}`,
|
||||
productId: pub.productId,
|
||||
productStatus: pub.productStatus
|
||||
};
|
||||
}
|
||||
|
||||
function buildFavoriteFromXb(child, pub) {
|
||||
const jq = parseMaybeJson(child?.jsonQueryResult);
|
||||
const spuid = (jq && (jq.spuid || getNested(jq, 'spuid'))) || child?.spuid || child?.skuid || '';
|
||||
const shopName = getNested(jq, 'shopInfo.shopName') || child?.shopName || '';
|
||||
const shopId = getNested(jq, 'shopInfo.shopId') || child?.shopId || '';
|
||||
const productUrl = jq?.materialUrl || jq?.url || child?.materialUrl || child?.productUrl || '';
|
||||
const productImage = getNested(jq, 'imageInfo.mainImage') || child?.productImage || '';
|
||||
const price = child?.firstPrice || getNested(jq, 'priceInfo.lowestCouponPrice') || getNested(jq, 'priceInfo.price') || '';
|
||||
const commissionInfo = getNested(jq, 'commissionInfo.commissionShare') ? `${getNested(jq, 'commissionInfo.commissionShare')}%` : (getNested(jq, 'commissionInfo.commission') || '');
|
||||
return {
|
||||
skuid: spuid,
|
||||
productName: child?.skuName || child?.productName || '',
|
||||
shopName,
|
||||
shopId,
|
||||
productUrl,
|
||||
productImage,
|
||||
price,
|
||||
commissionInfo,
|
||||
productId: pub.productId,
|
||||
productStatus: pub.productStatus,
|
||||
remark: `自动添加 - 发品时间: ${new Date().toLocaleString()}${pub.outerId ? `, 商家编码: ${pub.outerId}` : ''}`
|
||||
};
|
||||
}
|
||||
|
||||
export async function addToFavoritesAfterPublishFromTransfer(product, res) {
|
||||
const pub = parsePublishResponse(res);
|
||||
const spuid = product?.spuid || product?.skuId || product?.skuid || '';
|
||||
if (!spuid) return { success: false, reason: 'no_spuid' };
|
||||
try {
|
||||
const exist = await getBySkuid(spuid);
|
||||
if (exist && exist.data) return { success: true, skipped: true };
|
||||
} catch(e) {}
|
||||
const payload = buildFavoriteFromTransfer(product, pub);
|
||||
const addRes = await addToFavorites(payload);
|
||||
return { success: addRes && addRes.code === 200 };
|
||||
}
|
||||
|
||||
export async function addToFavoritesAfterPublishFromXb(child, res) {
|
||||
const pub = parsePublishResponse(res);
|
||||
const jq = parseMaybeJson(child?.jsonQueryResult);
|
||||
const spuid = (jq && (jq.spuid || getNested(jq, 'spuid'))) || child?.spuid || child?.skuid || '';
|
||||
if (!spuid) return { success: false, reason: 'no_spuid' };
|
||||
try {
|
||||
const exist = await getBySkuid(spuid);
|
||||
if (exist && exist.data) return { success: true, skipped: true };
|
||||
} catch(e) {}
|
||||
const payload = buildFavoriteFromXb(child, pub);
|
||||
const addRes = await addToFavorites(payload);
|
||||
return { success: addRes && addRes.code === 200 };
|
||||
}
|
||||
|
||||
|
||||
@@ -336,32 +336,7 @@
|
||||
</div>
|
||||
</el-dialog>
|
||||
|
||||
<!-- 快速发品对话框 -->
|
||||
<el-dialog title="快速发品" :visible.sync="quickPublishVisible" width="600px" append-to-body>
|
||||
<el-form ref="quickPublishForm" :model="quickPublishForm" :rules="quickPublishRules" label-width="100px">
|
||||
<el-form-item label="ERP应用" prop="appid">
|
||||
<el-select v-model="quickPublishForm.appid" placeholder="请选择ERP应用" style="width: 100%">
|
||||
<el-option
|
||||
v-for="app in erpApps"
|
||||
:key="app.value"
|
||||
:label="app.label"
|
||||
:value="app.value"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="商品信息">
|
||||
<div style="padding: 10px; background: #f5f5f5; border-radius: 4px;">
|
||||
<div><strong>商品名称:</strong>{{ selectedProduct.productName }}</div>
|
||||
<div><strong>店铺名称:</strong>{{ selectedProduct.shopName }}</div>
|
||||
<div><strong>商品价格:</strong>¥{{ selectedProduct.price }}</div>
|
||||
</div>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<div slot="footer" class="dialog-footer">
|
||||
<el-button @click="quickPublishVisible = false">取 消</el-button>
|
||||
<el-button type="primary" @click="submitQuickPublish" :loading="quickPublishLoading">确 定</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
|
||||
|
||||
<!-- 通用发品对话框:从常用商品直接进入,不显示ERP应用选择 -->
|
||||
<PublishDialog :visible.sync="publishDialogVisible" :initial-data="publishInitialData" :hideAppid="true" />
|
||||
@@ -369,10 +344,11 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { listFavoriteProduct, getFavoriteProduct, delFavoriteProduct, addFavoriteProduct, updateFavoriteProduct, updateTopStatus, quickPublishFromFavorite } from "@/api/system/favoriteProduct";
|
||||
import { listFavoriteProduct, getFavoriteProduct, delFavoriteProduct, addFavoriteProduct, updateFavoriteProduct, updateTopStatus } from "@/api/system/favoriteProduct";
|
||||
import { generatePromotionContent } from "@/api/system/jdorder";
|
||||
import { mapGetters, mapActions } from 'vuex'
|
||||
import PublishDialog from '@/components/PublishDialog.vue'
|
||||
// 自动加入常用逻辑由 PublishDialog 内部触发(线报、转链页面),本页主要用于打开发品弹窗
|
||||
|
||||
export default {
|
||||
name: "FavoriteProduct",
|
||||
@@ -426,24 +402,7 @@ export default {
|
||||
// ERP商品对话框
|
||||
erpDialogVisible: false,
|
||||
erpProducts: [],
|
||||
// 快速发品对话框
|
||||
quickPublishVisible: false,
|
||||
quickPublishForm: {
|
||||
appid: ''
|
||||
},
|
||||
quickPublishRules: {
|
||||
appid: [
|
||||
{ required: true, message: "请选择ERP应用", trigger: "change" }
|
||||
]
|
||||
},
|
||||
selectedProduct: {},
|
||||
quickPublishLoading: false,
|
||||
// ERP应用列表
|
||||
erpApps: [
|
||||
{ label: '闲鱼', value: 'xianyu' },
|
||||
{ label: '淘宝', value: 'taobao' },
|
||||
{ label: '京东', value: 'jd' }
|
||||
],
|
||||
|
||||
// 通用发品弹窗
|
||||
publishDialogVisible: false,
|
||||
publishInitialData: {}
|
||||
@@ -618,8 +577,9 @@ export default {
|
||||
const p = res && res.data ? res.data : (row || {});
|
||||
try {
|
||||
let detail = null;
|
||||
if (p.productUrl) {
|
||||
const r = await generatePromotionContent({ promotionContent: p.productUrl });
|
||||
const inputContent = p.productUrl || p.productName || p.skuid || '';
|
||||
if (inputContent) {
|
||||
const r = await generatePromotionContent({ promotionContent: inputContent });
|
||||
const resultStr = (r && (r.msg || r.data)) || '';
|
||||
try { const arr = typeof resultStr === 'string' ? JSON.parse(resultStr) : resultStr; if (Array.isArray(arr) && arr.length) detail = arr[0]; } catch(e) {}
|
||||
}
|
||||
@@ -651,21 +611,7 @@ export default {
|
||||
this.publishDialogVisible = true;
|
||||
});
|
||||
},
|
||||
/** 提交快速发品 */
|
||||
submitQuickPublish() {
|
||||
this.$refs["quickPublishForm"].validate(valid => {
|
||||
if (valid) {
|
||||
this.quickPublishLoading = true;
|
||||
quickPublishFromFavorite(this.selectedProduct.id, this.quickPublishForm.appid).then(response => {
|
||||
this.$modal.msgSuccess("发品请求已提交");
|
||||
this.quickPublishVisible = false;
|
||||
this.quickPublishLoading = false;
|
||||
}).catch(() => {
|
||||
this.quickPublishLoading = false;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -152,6 +152,7 @@
|
||||
<script>
|
||||
import { generatePromotionContent } from "@/api/system/jdorder";
|
||||
import { addToFavorites, getBySkuid } from "@/api/system/favoriteProduct";
|
||||
import { addToFavoritesAfterPublishFromTransfer } from "@/utils/publishHelper";
|
||||
import PublishDialog from '@/components/PublishDialog.vue'
|
||||
|
||||
export default {
|
||||
@@ -437,44 +438,9 @@ export default {
|
||||
async handlePublishSuccess(res) {
|
||||
try {
|
||||
const p = this.currentPublishProduct || {};
|
||||
const data = res && res.data ? res.data : {};
|
||||
const productId = data.product_id || data.productId || (data.data && (data.data.product_id || data.data.productId));
|
||||
const productStatus = data.product_status || data.productStatus || (data.data && (data.data.product_status || data.data.productStatus));
|
||||
const outerId = data.outer_id || data.outerId || (data.data && (data.data.outer_id || data.data.outerId));
|
||||
|
||||
// 取spuid优先
|
||||
const spuid = p.spuid || p.skuId || p.skuid || '';
|
||||
if (!spuid) return;
|
||||
|
||||
// 查重,避免重复加入
|
||||
try {
|
||||
const exist = await getBySkuid(spuid);
|
||||
if (exist && exist.data) {
|
||||
// 已存在则不重复添加
|
||||
return;
|
||||
}
|
||||
} catch (e) { /* 忽略查重异常 */ }
|
||||
|
||||
const favoriteData = {
|
||||
skuid: spuid,
|
||||
productName: p.skuName || p.title || '',
|
||||
shopName: p.shopName || '',
|
||||
productUrl: p.url || '',
|
||||
productImage: Array.isArray(p.images) && p.images.length ? p.images[0] : '',
|
||||
price: (p.price != null ? String(p.price) : (p.lowestCouponPrice != null ? String(p.lowestCouponPrice) : '')),
|
||||
commissionInfo: p.commissionShare ? `${p.commissionShare}%` : (p.commission != null ? String(p.commission) : ''),
|
||||
remark: `自动添加 - 发品时间: ${new Date().toLocaleString()}${outerId ? `, 商家编码: ${outerId}` : ''}`,
|
||||
productId: productId,
|
||||
productStatus: productStatus
|
||||
};
|
||||
|
||||
const addRes = await addToFavorites(favoriteData);
|
||||
if (addRes && addRes.code === 200) {
|
||||
this.$store && this.$store.dispatch && this.$store.dispatch('app/triggerFavoriteProductRefresh');
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略自动加入失败,不影响发品流程
|
||||
}
|
||||
await addToFavoritesAfterPublishFromTransfer(p, res);
|
||||
this.$store && this.$store.dispatch && this.$store.dispatch('app/triggerFavoriteProductRefresh');
|
||||
} catch (e) { }
|
||||
},
|
||||
|
||||
onWenanChange(val) {
|
||||
|
||||
@@ -393,7 +393,8 @@
|
||||
import { listXbmessage, delXbmessage, getGroupNameOptions } from "@/api/system/xbmessage";
|
||||
import { parseTime } from "@/utils/ruoyi";
|
||||
import PublishDialog from '@/components/PublishDialog.vue'
|
||||
import { generatePromotionContent, createProductByPromotion, getProvinces, getCities, getAreas, getCategories, getUsernames, getERPAccounts, getProperties, createGiftCoupon, transferWithGift } from "@/api/system/jdorder";
|
||||
import { generatePromotionContent, createGiftCoupon, transferWithGift } from "@/api/system/jdorder";
|
||||
import { addToFavoritesAfterPublishFromXb } from "@/utils/publishHelper";
|
||||
import { addToFavorites, getBySkuid } from "@/api/system/favoriteProduct";
|
||||
|
||||
export default {
|
||||
@@ -449,47 +450,7 @@ export default {
|
||||
giftForm: { amount: null, quantity: 10 },
|
||||
giftLoading: false,
|
||||
giftContext: { materialUrl: '', owner: 'g', skuName: '' },
|
||||
// 发品相关
|
||||
quickLinkPublishDialog: {
|
||||
visible: false,
|
||||
loading: false,
|
||||
wenanOptions: [],
|
||||
productImages: [],
|
||||
form: {
|
||||
appid: '',
|
||||
wenanIndex: 0,
|
||||
title: '',
|
||||
content: '',
|
||||
extraImagesText: '',
|
||||
whiteImages: '',
|
||||
serviceSupport: ['NFR'],
|
||||
userName: '',
|
||||
province: 440000,
|
||||
city: 440400,
|
||||
district: 440402,
|
||||
price: null,
|
||||
originalPrice: null,
|
||||
expressFee: 0,
|
||||
stock: 999,
|
||||
outerId: '',
|
||||
itemBizType: 2,
|
||||
spBizType: 3,
|
||||
channelCatId: '',
|
||||
channelPvJson: '',
|
||||
stuffStatus: 100,
|
||||
},
|
||||
rules: {
|
||||
title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
|
||||
content: [{ required: true, message: '请输入文案内容', trigger: 'blur' }],
|
||||
userName: [{ required: true, message: '请输入闲鱼会员名', trigger: 'blur' }],
|
||||
price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
|
||||
expressFee: [{ required: true, message: '请输入运费', trigger: 'blur' }],
|
||||
stock: [{ required: true, message: '请输入库存', trigger: 'blur' }],
|
||||
itemBizType: [{ required: true, message: '请选择商品类型', trigger: 'change' }],
|
||||
spBizType: [{ required: true, message: '请选择行业类型', trigger: 'change' }],
|
||||
channelCatId: [{ required: true, message: '请选择类目', trigger: 'change' }]
|
||||
}
|
||||
},
|
||||
|
||||
// 地区选项
|
||||
regionOptions: {
|
||||
provinces: [],
|
||||
@@ -571,12 +532,11 @@ export default {
|
||||
// 记录当前线报项,便于发品成功后加入常用
|
||||
this.currentXbMessageItem = child;
|
||||
|
||||
// 先尝试生成转链文案,成功后带文案打开发品弹窗
|
||||
// 与转链一致:统一先调用转链接口补全字段
|
||||
const materialUrl = this.getJsonValue(child.jsonQueryResult, 'materialUrl');
|
||||
const skuName = child.skuName;
|
||||
const inputContent = materialUrl || skuName || '';
|
||||
|
||||
// 预先设置基本信息,等待文案生成后补充
|
||||
const baseInitialData = {
|
||||
title: this.decodeUnicode(child.skuName) || '',
|
||||
content: '',
|
||||
@@ -586,25 +546,18 @@ export default {
|
||||
};
|
||||
|
||||
if (!inputContent) {
|
||||
// 无法生成转链输入,直接打开(无文案)
|
||||
this.publishInitialData = baseInitialData;
|
||||
this.publishDialogVisible = true;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.$modal && this.$modal.loading('正在生成转链文案,请稍候…');
|
||||
} catch(e) { /* 忽略 */ }
|
||||
try { this.$modal && this.$modal.loading('正在生成转链文案,请稍候…'); } catch(e) {}
|
||||
|
||||
generatePromotionContent({ promotionContent: inputContent }).then(res => {
|
||||
let result = res && (res.msg || res.data);
|
||||
const result = res && (res.msg || res.data);
|
||||
let arr = [];
|
||||
try {
|
||||
if (typeof result === 'string') {
|
||||
arr = JSON.parse(result);
|
||||
} else if (Array.isArray(result)) {
|
||||
arr = result;
|
||||
}
|
||||
if (typeof result === 'string') arr = JSON.parse(result); else if (Array.isArray(result)) arr = result;
|
||||
} catch (e) { arr = []; }
|
||||
|
||||
let wenanOptions = [];
|
||||
@@ -612,20 +565,19 @@ export default {
|
||||
if (Array.isArray(arr) && arr.length > 0) {
|
||||
const first = arr[0] || {};
|
||||
const wenanArr = Array.isArray(first.wenan) ? first.wenan : [];
|
||||
wenanOptions = wenanArr.map((w, i) => ({ label: w.type || `版本${i+1}`, content: w.content || '' }));
|
||||
wenanOptions = wenanArr.map((w, i) => ({ label: w.type || `版本${i+1}` , content: w.content || '' }));
|
||||
detailImages = this.extractImageUrlsFromGenerateResult(first);
|
||||
}
|
||||
|
||||
this.publishInitialData = Object.assign({}, baseInitialData, {
|
||||
content: (wenanOptions[0] && wenanOptions[0].content) || '',
|
||||
wenanOptions,
|
||||
images: (detailImages && detailImages.length) ? detailImages : baseInitialData.images
|
||||
images: (detailImages && detailImages.length) ? detailImages : baseInitialData.images,
|
||||
wenanOptions
|
||||
});
|
||||
}).catch(() => {
|
||||
// 忽略错误,仍然打开弹窗(无文案)
|
||||
this.publishInitialData = baseInitialData;
|
||||
}).finally(() => {
|
||||
try { this.$modal && this.$modal.closeLoading(); } catch(e) { /* 忽略 */ }
|
||||
try { this.$modal && this.$modal.closeLoading(); } catch(e) {}
|
||||
this.publishDialogVisible = true;
|
||||
});
|
||||
},
|
||||
@@ -1960,14 +1912,10 @@ export default {
|
||||
/** 接收通用发品弹窗成功事件:等待接口返回后自动加常用,但不关闭页面 */
|
||||
onPublishDialogSuccess(res) {
|
||||
try {
|
||||
const data = res && res.data ? res.data : {};
|
||||
const productId = data.product_id || data.productId || (data.data && (data.data.product_id || data.data.productId));
|
||||
const productStatus = data.product_status || data.productStatus || (data.data && (data.data.product_status || data.data.productStatus));
|
||||
const outerId = data.outer_id || data.outerId || (data.data && (data.data.outer_id || data.data.outerId));
|
||||
if (productId) {
|
||||
this.addToFavoritesAfterPublish(productId, productStatus, outerId);
|
||||
// 触发全局常用商品列表刷新
|
||||
this.$store.dispatch('app/triggerFavoriteProductRefresh');
|
||||
if (this.currentXbMessageItem) {
|
||||
addToFavoritesAfterPublishFromXb(this.currentXbMessageItem, res).then(() => {
|
||||
this.$store.dispatch('app/triggerFavoriteProductRefresh');
|
||||
});
|
||||
}
|
||||
} catch (e) { /* 忽略解析异常 */ }
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user