This commit is contained in:
liushiwei
2023-11-10 10:41:19 +08:00
parent 0016f7e5bc
commit fe6fd6cfdc
4 changed files with 231 additions and 103 deletions

View File

@@ -35,13 +35,13 @@
</el-form>
<div class="bottom flex-center">
<el-button @click="$emit('onClose')"> </el-button>
<el-button type="primary" @click="addToDraft" :loading="isCopying">确定</el-button>
<el-button type="primary" @click="toAddToDraft" :loading="isCopying">确定</el-button>
</div>
</div>
</template>
<script>
import {sendChromeAPIMessage, sendChromeWebReqMessage} from '@/api/chromeApi'
import {sendChromeAPIMessage, sendTemuAPIMessage, sendChromeWebReqMessage} from '@/api/chromeApi'
import AiLazyCascader from "@/components/AiLazyCascader.vue"
import { Message } from 'element-ui'
@@ -109,7 +109,8 @@ export default {
goods: {},
sku: {},
productDetail: {},
isCopying: false
isCopying: false,
goodsId: ''
}
},
created () {
@@ -118,102 +119,159 @@ export default {
}
},
methods: {
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 + "}"
let goodsObj = JSON.parse(str)
this.goods = goodsObj.store.goods
this.sku = goodsObj.store.sku
this.productDetail = goodsObj.store.productDetail
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})
}
})
})
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
}
})
})
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') {
/*if (res.indexOf("runParams") == -1) {
Message.error("请检查地址是否正确,或者“速卖通”网站是否出现滑动条")
return
}
let str = res.substring(res.indexOf("runParams"))
str = str.substring(0, str.indexOf("<\/script>"))
str = str.substring(str.indexOf("{"))
str = str.substring(0, str.lastIndexOf("}"))
str = str.substring(str.indexOf('data'))
str = str.substring(5)
let obj = JSON.parse(str)
sendChromeWebReqMessage({
type: source,
url: obj.productDescComponent.descriptionUrl,
}).then((res1) => {
res1 = res1.substring(0, res1.indexOf("<script>"))
let str = res1.replace(/<img[^>]+src="([^">]+)"[^>]+>/g, '$1\n').replace(/<.*?>/g, '[||]')
let arr = str.split('[||]')
for (let i = 0; i < arr.length; i++) {
console.log(arr[i])
}
})*/
}
})
} else {
this.isCopying = false
}
})
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})
}
})
})
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 + "}"
let goodsObj = JSON.parse(str)
this.goods = goodsObj.store.goods
this.sku = goodsObj.store.sku
this.productDetail = goodsObj.store.productDetail
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})
}
})
})
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
}
})
})
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)
}
})
} else {
this.isCopying = false
}
})
}
})
},
createDraft(data) {
@@ -263,6 +321,22 @@ export default {
}
})
},
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)
}
})
},
getSpecId(data) {
return sendChromeAPIMessage({
url: 'bg-anniston-mms/sku/spec/byName/queryOrAdd',
@@ -294,6 +368,33 @@ export default {
this.$emit('onSuccess')
}
})
},
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('/')
};
}
}
}