51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
(async function () {
|
|
const res = await fetch("http://all.funjia.top/api/QA/list", {
|
|
method: "post",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({ "page": 1, "pageSize": 100000, "userId": "cl3fqx6zb000034ny90rd333s" })
|
|
});
|
|
|
|
const importDataObj = await res.json(),
|
|
tagDataObj = await getTag(),
|
|
importData = importDataObj.data,
|
|
tagData = tagDataObj.data;
|
|
// console.log(tagData);
|
|
// return;
|
|
for (const item of importData) {
|
|
console.log(item.tag);
|
|
const tag = tagData.filter(tDItem => (Array.isArray(item.tag) ? item.tag : [item.tag]).some((child) => child == tDItem.name));
|
|
console.log(tag);
|
|
await saveData({ question: item.question, answer: item.answer, tags: tag.map((child) => child.documentId) });
|
|
await sleep(200);
|
|
}
|
|
})()
|
|
|
|
async function sleep(timeout) {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve(true);
|
|
}, timeout);
|
|
});
|
|
}
|
|
|
|
async function getTag() {
|
|
const res = await fetch("http://127.0.0.1:1337/api/tags", {
|
|
method: "get",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
}
|
|
});
|
|
return res.json();
|
|
}
|
|
|
|
function saveData(importData) {
|
|
return fetch("http://127.0.0.1:1337/api/question-answers", {
|
|
method: "post",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({ data: importData })
|
|
})
|
|
} |