Files
temu-plugin/src/components/AiCopyFromTemu.vue

407 lines
14 KiB
Vue
Raw Normal View History

2023-10-15 22:09:33 +08:00
<template>
<div>
2023-10-16 01:23:01 +08:00
<el-alert
title="采集一个商品添加进草稿箱将消耗20金币"
type="success"
:closable="false">
</el-alert>
2023-10-15 22:09:33 +08:00
<el-form class="ai-form" :model="form" label-width="140px" ref="form">
<el-form-item label="来源:" style="width: 100%;" prop="type" :rules="[{ required: true, message: '请选择来源', trigger: 'blur' }]">
<el-radio-group v-model="form.type" size="medium">
<el-radio :label="1">TEMU</el-radio>
<!--<el-radio :label="2">速卖通</el-radio>-->
</el-radio-group>
</el-form-item>
<el-form-item label="商品地址:" style="width: 100%;" prop="url" :rules="[{ required: true, message: '请输入商品地址', trigger: 'blur' }]">
<el-input type="textarea" :rows="5" v-model="form.url"></el-input>
</el-form-item>
<el-form-item label="店铺:" style="width: 100%;" prop="targetMallId" :rules="[{ required: true, message: '请选择店铺', trigger: 'blur' }]">
<el-select style="width: 380px" v-model="form.targetMallId" placeholder="请选择">
<el-option
v-for="item in $store.state.mallList"
:key="item.mallId"
:label="item.mallName"
:value="item.mallId">
</el-option>
</el-select>
</el-form-item>
<el-form-item label="商品分类:" style="width: 100%;" prop="targetCatId" :rules="[{ required: true, message: '请选择商品分类', trigger: 'blur' }]">
2023-10-25 12:20:10 +08:00
<ai-lazy-cascader
style="width: 380px"
v-model="form.targetCatId"
filterable
:props="props"></ai-lazy-cascader>
2023-10-15 22:09:33 +08:00
</el-form-item>
</el-form>
<div class="bottom flex-center">
<el-button @click="$emit('onClose')"> </el-button>
2023-11-10 10:41:19 +08:00
<el-button type="primary" @click="toAddToDraft" :loading="isCopying">确定</el-button>
2023-10-15 22:09:33 +08:00
</div>
</div>
</template>
<script>
2023-11-10 10:41:19 +08:00
import {sendChromeAPIMessage, sendTemuAPIMessage, sendChromeWebReqMessage} from '@/api/chromeApi'
2023-10-25 12:20:10 +08:00
import AiLazyCascader from "@/components/AiLazyCascader.vue"
2023-10-15 22:09:33 +08:00
import { Message } from 'element-ui'
export default {
name: 'AiCopyFromTemu',
props: ['params'],
2023-10-25 12:20:10 +08:00
components: {AiLazyCascader},
2023-10-15 22:09:33 +08:00
data() {
return {
props: {
value: 'catId',
label: 'catName',
lazy: true,
2023-10-25 12:20:10 +08:00
lazyLoad (value, resolve) {
2023-10-15 22:09:33 +08:00
sendChromeAPIMessage({
url: 'bg-anniston-mms/category/children/list',
needMallId: true,
data: {
2023-10-25 12:20:10 +08:00
parentCatId: value || ''
2023-10-15 22:09:33 +08:00
}
}).then(res => {
if (res.errorCode === 1000000) {
resolve(res.result.categoryNodeVOS.map(v => {
return {
...v,
leaf: v.isLeaf
}
}))
}
})
2023-10-25 12:20:10 +08:00
},
lazySearch(queryString, resolve) {
sendChromeAPIMessage({
url: 'bg-anniston-mms/category/search',
needMallId: true,
data: {
searchText: queryString || ''
}
}).then(res => {
if (res.errorCode === 1000000) {
resolve(res.result.categoryPaths.map(v => {
let value = []
let label = []
for (let i = 1; i <= 10; i++ ) {
if (v['cat'+i+'NodeVO']) {
value.push(v['cat'+i+'NodeVO'].catId)
label.push(v['cat'+i+'NodeVO'].catName)
}
}
return {
catId: value,
catName: label
}
}))
}
})
2023-10-15 22:09:33 +08:00
}
},
form: {
url: '',
type: 1, // 默认从temu复制
targetMallId: '',
targetCatId: []
},
goods: {},
sku: {},
2023-10-28 12:28:12 +08:00
productDetail: {},
2023-11-10 10:41:19 +08:00
isCopying: false,
goodsId: ''
2023-10-15 22:09:33 +08:00
}
},
created () {
if (this.params?.url) {
this.form.url = this.params.url
}
},
methods: {
2023-11-10 10:41:19 +08:00
toAddToDraft() {
if (!this.form.url.startsWith("http")) {
this.goodsId = this.form.url
this.addToDraftNew()
} else {
let t = this.form.url
let urlParams = this.parseURL(t)
t = t.substring(0,t.indexOf(".html"))
if (t.lastIndexOf("-g-") > 0) {
t = t.substring(t.lastIndexOf("-g-"), t.length);
t = t.substring(3, t.length);
this.goodsId = t
this.addToDraftNew()
} else if(urlParams.params) {
let goodsId = urlParams.params.goods_id
if (!goodsId) {
this.addToDraft()
} else {
this.goodsId = goodsId
this.addToDraftNew()
}
} else {
this.addToDraft()
}
}
},
async addToDraftNew() {
this.$refs.form.validate((valid) => {
if (valid) {
this.isCopying = true
sendTemuAPIMessage({
url: 'api/oak/integration/render',
anti: true,
data: {
goods_id: this.goodsId
}}).then((res) => {
if (!res.goods) {
this.isCopying = false
Message.error("获取商品信息失败,采集失败")
return
}
this.goods = res.goods
this.sku = res.sku
this.productDetail = res.product_detail
let specIds = []
this.sku.forEach(item => {
item.specs.forEach(item1 => {
let flag = false
specIds.forEach(item2 => {
if (item2.spec_value == item1.spec_value) {
flag = true
}
})
if (!flag) {
specIds.push({spec_key_id: item1.spec_key_id, spec_value: item1.spec_value})
}
})
})
2023-10-15 22:09:33 +08:00
2023-11-10 10:41:19 +08:00
Promise.all(specIds.map(item => this.getSpecIdNew(item).then(res => {
this.sku.forEach(item1 => {
item1.specs.forEach(item2 => {
if (item2.spec_value == item.spec_value) {
item2.spec_value_id = res.result.specId
}
})
})
return 0
}))).then(() => {
this.$http.post('/api/copyProduct/translateNew',{type: 1, goods: this.goods, sku: this.sku, productDetail: this.productDetail}).then(res => {
if (res.code == 0) {
this.createDraft(res.data)
}
})
})
})
}
})
},
async addToDraft() {
this.$refs.form.validate((valid) => {
if (valid) {
this.isCopying = true
this.$http.post('/api/copyProduct/check',null, {params: {type: 0}}).then(res => {
if (res.code == 0) {
let source
if (this.form.type == '1') {
source = 'temu'
} else if (this.form.type == '2') {
source = 'aliexpress'
}
sendChromeWebReqMessage({
type: source,
url: this.form.url,
}).then((res) => {
if (this.form.type == '1') {
if (res.indexOf("window.rawData") == -1) {
this.isCopying = false
Message.error("请检查地址是否正确或者“TEMU”网站是否出现图形验证码")
return
}
let str = res.substring(res.indexOf("window.rawData"))
str = str.substring(0, str.indexOf("<\/script>"))
str = str.substring(str.indexOf("{"))
str = str.substring(0, str.lastIndexOf("}"))
str = str + "}"
2023-10-15 22:09:33 +08:00
2023-11-10 10:41:19 +08:00
let goodsObj = JSON.parse(str)
this.goods = goodsObj.store.goods
this.sku = goodsObj.store.sku
this.productDetail = goodsObj.store.productDetail
2023-10-15 22:09:33 +08:00
2023-11-10 10:41:19 +08:00
let specIds = []
this.sku.forEach(item => {
item.specs.forEach(item1 => {
let flag = false
specIds.forEach(item2 => {
if (item2.specValue == item1.specValue) {
flag = true
}
})
if (!flag) {
specIds.push({specKeyId: item1.specKeyId, specValue: item1.specValue})
}
})
})
2023-10-15 22:09:33 +08:00
2023-11-10 10:41:19 +08:00
Promise.all(specIds.map(item => this.getSpecId(item).then(res => {
this.sku.forEach(item1 => {
item1.specs.forEach(item2 => {
if (item2.specValue == item.specValue) {
item2.specValueId = res.result.specId
}
2023-10-15 22:09:33 +08:00
})
2023-11-10 10:41:19 +08:00
})
return 0
}))).then(() => {
this.$http.post('/api/copyProduct/translate',{type: 1, goods: this.goods, sku: this.sku, productDetail: this.productDetail}).then(res => {
if (res.code == 0) {
this.createDraft(res.data)
}
})
})
} else if (this.form.type == '2') {
console.log(1)
}
2023-10-15 22:09:33 +08:00
})
2023-11-10 10:41:19 +08:00
} else {
this.isCopying = false
}
})
}
2023-10-15 22:09:33 +08:00
})
},
createDraft(data) {
sendChromeAPIMessage({
url: 'bg-visage-mms/product/draft/add',
needMallId: true,
mallId: this.form.targetMallId,
data: {
catId: this.form.targetCatId[this.form.targetCatId.length - 1]
}}).then((res) => {
2023-10-28 12:28:12 +08:00
this.isCopying = false
2023-10-15 22:09:33 +08:00
if (res.errorCode == 1000000) {
let draftId = res.result.productDraftId
let content = data
let i = 0
for (; i < this.form.targetCatId.length; i++) {
content['cat' + (i+1) + 'Id'] = this.form.targetCatId[i]
}
for (; i < 10; i++) {
content['cat' + (i+1) + 'Id'] = ''
}
content.productDraftId = draftId
this.createProduct(content)
} else {
setTimeout(() => {
this.createDraft(data)
}, 500)
}
})
},
createProduct(content) {
sendChromeAPIMessage({
url: 'bg-visage-mms/product/draft/save',
needMallId: true,
mallId: this.form.targetMallId,
data: {
...content
}}).then((res) => {
if (res.errorCode == 1000000) {
Message.success("成功添加到草稿箱")
this.saveInfo()
} else {
setTimeout(() => {
this.createProduct(content)
}, 500)
}
})
},
2023-11-10 10:41:19 +08:00
getSpecIdNew(data) {
return sendChromeAPIMessage({
url: 'bg-anniston-mms/sku/spec/byName/queryOrAdd',
needMallId: true,
mallId: this.form.targetMallId,
data: {
parentSpecId: data.spec_key_id,
specName: data.spec_value
}}).then((res) => {
if (res.errorCode == 1000000) {
return res
} else {
this.getSpecIdNew(data)
}
})
},
2023-10-15 22:09:33 +08:00
getSpecId(data) {
return sendChromeAPIMessage({
url: 'bg-anniston-mms/sku/spec/byName/queryOrAdd',
needMallId: true,
mallId: this.form.targetMallId,
data: {
parentSpecId: data.specKeyId,
specName: data.specValue
}}).then((res) => {
if (res.errorCode == 1000000) {
return res
} else {
this.getSpecId(data)
}
})
},
saveInfo() {
let mallInfo = this.$store.state.mallList.filter(item => {
return item.mallId == this.form.targetMallId
})
this.$http.post('/api/copyProduct/add', {
mallId: mallInfo[0].mallId,
mallName: mallInfo[0].mallName,
url: this.form.url,
type: this.form.type
}).then(res1 => {
if (res1.code == 0) {
this.$store.dispatch('getUserInfo')
this.$emit('onSuccess')
}
})
2023-11-10 10:41:19 +08:00
},
parseURL(url) {
let a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};
2023-10-15 22:09:33 +08:00
}
}
}
</script>
<style scoped lang="scss">
.bottom {
justify-content: center;
}
</style>