143 lines
3.9 KiB
JavaScript
143 lines
3.9 KiB
JavaScript
import _ from "lodash";
|
|
import qs from "qs";
|
|
import { ConfigHelper } from "funjia-server";
|
|
// https://docs.strapi.io/cms/api/graphql#filters
|
|
const headers = {
|
|
'content-type': 'application/json'
|
|
}
|
|
|
|
const getQuestionAnswer = (router) => {
|
|
const fullUrl = ConfigHelper.config.server.url + "/api";
|
|
|
|
router.post('/api/questionAnswer/list', async (ctx) => {
|
|
// 设置头类型, 如果不设置,会直接下载该页面
|
|
ctx.type = 'application/json';
|
|
const params = ctx.request.body;
|
|
|
|
const url = buildUrl(`${fullUrl}/question-answers`, {
|
|
pagination: {
|
|
page: params.page,
|
|
pageSize: params.pageSize,
|
|
},
|
|
populate: 'tags',
|
|
sort: "id:desc",
|
|
filters: params.filters
|
|
});
|
|
|
|
const res = await fetch(url, {
|
|
method: 'GET',
|
|
})
|
|
|
|
const data = await res.json(),
|
|
ctxData = data.data.map(item => {
|
|
return { ...item, id: item.documentId }
|
|
});
|
|
ctx.body = {
|
|
code: ctxData.length > 0 ? 0 : 1, data: ctxData,
|
|
total: data.meta.pagination.total
|
|
}
|
|
});
|
|
|
|
router.get('/api/questionAnswer', async (ctx) => {
|
|
// 设置头类型, 如果不设置,会直接下载该页面
|
|
ctx.type = 'application/json';
|
|
const params = ctx.params,
|
|
tableName = params.tableName,
|
|
query = ctx.request.query,
|
|
id = query.id;
|
|
|
|
const url = buildUrl(`${fullUrl}/question-answers/${id}`, {
|
|
populate: 'tags'
|
|
});
|
|
const res = await fetch(url, {
|
|
// headers,
|
|
// body:JSON.parse({})
|
|
})
|
|
const data = await res.json();
|
|
const ctxData = data.data;
|
|
ctxData.id = ctxData?.documentId;
|
|
// console.log(ctxData.tags);
|
|
ctxData.tags = ctxData.tags.map(item => item.documentId);
|
|
ctx.body = {
|
|
code: 0,
|
|
data: ctxData
|
|
};
|
|
});
|
|
|
|
router.post('/api/questionAnswer/add', async (ctx) => {
|
|
ctx.type = 'application/json';
|
|
const body = ctx.request.body;
|
|
delete body.id;
|
|
delete body.documentId;
|
|
|
|
const res = await fetch(`${fullUrl}/question-answers`, {
|
|
method: "post",
|
|
headers,
|
|
body: JSON.stringify({ data: ctx.request.body })
|
|
});
|
|
|
|
const data = await res.json();
|
|
// console.log(data);
|
|
|
|
ctx.body = {
|
|
code: 0,
|
|
};
|
|
});
|
|
|
|
router.post('/api/questionAnswer/upd', async (ctx) => {
|
|
ctx.type = 'application/json';
|
|
// const tableName = ctx.params.tableName;
|
|
const body = ctx.request.body;
|
|
const documentId = body.id;// body.documentId;
|
|
delete body.id;
|
|
delete body.documentId;
|
|
// delete body.tags;
|
|
|
|
const putData = JSON.stringify({ data: body });
|
|
// console.log(putData, documentId);
|
|
const res = await fetch(`${fullUrl}/question-answers/` + documentId, {
|
|
method: "PUT",
|
|
headers,
|
|
body: putData
|
|
})
|
|
|
|
const data = await res.json();
|
|
// console.log(data);
|
|
ctx.body = {
|
|
code: 0,
|
|
};
|
|
});
|
|
|
|
router.post('/api/questionAnswer/delete', async (ctx) => {
|
|
ctx.type = 'application/json';
|
|
const tableName = 'questionAnswer';
|
|
const data = ctx.request.body;
|
|
const ids = data.ids;
|
|
|
|
for (const id of ids) {
|
|
await deleteData(tableName, id);
|
|
}
|
|
|
|
ids.forEach((id) => {
|
|
deleteData(tableName, id);
|
|
})
|
|
|
|
ctx.body = {
|
|
code: 0,
|
|
};
|
|
});
|
|
|
|
const deleteData = async (tableName, id) => {
|
|
return fetch(`${fullUrl}/${_.kebabCase(tableName)}s/${id}`, {
|
|
method: "delete"
|
|
})
|
|
}
|
|
}
|
|
|
|
export default getQuestionAnswer;
|
|
|
|
|
|
function buildUrl(baseUrl, params) {
|
|
return baseUrl + "?" + qs.stringify(params);
|
|
}
|