172 lines
6.7 KiB
JavaScript
172 lines
6.7 KiB
JavaScript
|
|
const common = require('../../common.js');
|
||
|
|
let db = null;
|
||
|
|
|
||
|
|
exports.main = async (context, mongodb) => {
|
||
|
|
db = mongodb;
|
||
|
|
switch (context.type) {
|
||
|
|
case "addArticleCate":
|
||
|
|
return await addArticleCate(context);
|
||
|
|
case "updateArticleCate":
|
||
|
|
return await updateArticleCate(context);
|
||
|
|
case "deleteArticleCate":
|
||
|
|
return await deleteArticleCate(context);
|
||
|
|
case "getArticleCateList":
|
||
|
|
return await getArticleCateList(context);
|
||
|
|
case "initCorpArticleCate":
|
||
|
|
return await initCorpArticleCate(context);
|
||
|
|
case "sortArticleCate":
|
||
|
|
return await sortArticleCate(context);
|
||
|
|
case "ifCateExist":
|
||
|
|
return await ifCateExist(context);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
async function addArticleCate(context) {
|
||
|
|
let { corpId, label, parentId } = context;
|
||
|
|
if (!corpId) return { success: false, message: "机构id不能为空" };
|
||
|
|
if (typeof label !== "string" || label.trim() === "")
|
||
|
|
return { success: false, message: "分类名称无效" };
|
||
|
|
if (label.trim().length > 10)
|
||
|
|
return { success: false, message: "分类名称不能超过10个字" };
|
||
|
|
try {
|
||
|
|
if (parentId) {
|
||
|
|
const parent = await db.collection("article-cate").findOne({ _id: parentId, corpId });
|
||
|
|
if (!parent) return { success: false, message: "父级分类不存在" };
|
||
|
|
if (![1, 2].includes(parent.level))
|
||
|
|
return { success: false, message: "父级分类层级错误" };
|
||
|
|
const res = await db.collection("article-cate").insertOne({
|
||
|
|
_id: common.generateRandomString(24),
|
||
|
|
corpId,
|
||
|
|
label,
|
||
|
|
level: parent.level + 1,
|
||
|
|
parentId,
|
||
|
|
createTime: Date.now(),
|
||
|
|
});
|
||
|
|
return { success: true, data: res, message: "添加分类成功" };
|
||
|
|
}
|
||
|
|
const res = await db.collection("article-cate").insertOne({
|
||
|
|
_id: common.generateRandomString(24),
|
||
|
|
corpId,
|
||
|
|
label,
|
||
|
|
level: 1,
|
||
|
|
createTime: Date.now()
|
||
|
|
});
|
||
|
|
return { success: true, data: res, message: "添加分类成功" };
|
||
|
|
} catch (e) {
|
||
|
|
return { success: false, message: e.message };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function updateArticleCate(context) {
|
||
|
|
let { corpId, id: _id, label } = context;
|
||
|
|
if (!corpId) return { success: false, message: "机构id不能为空" };
|
||
|
|
if (!_id) return { success: false, message: "分类id不能为空" };
|
||
|
|
if (typeof label !== "string" || label.trim() === "")
|
||
|
|
return { success: false, message: "分类名称无效" };
|
||
|
|
if (label.trim().length > 10)
|
||
|
|
return { success: false, message: "分类名称不能超过10个字" };
|
||
|
|
try {
|
||
|
|
const res = await db.collection("article-cate").updateOne(
|
||
|
|
{ _id, corpId },
|
||
|
|
{ $set: { label: label.trim() } }
|
||
|
|
);
|
||
|
|
return { success: true, data: res, message: "更新分类成功" };
|
||
|
|
} catch (e) {
|
||
|
|
return { success: false, message: e.message };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function deleteArticleCate(context) {
|
||
|
|
let { corpId, id: _id } = context;
|
||
|
|
if (!corpId) return { success: false, message: "机构id不能为空" };
|
||
|
|
if (!_id) return { success: false, message: "分类id不能为空" };
|
||
|
|
try {
|
||
|
|
const cate = await db.collection("article-cate").findOne({ _id, corpId });
|
||
|
|
if (!cate) return { success: false, message: "分类不存在" };
|
||
|
|
const ids = [_id];
|
||
|
|
if (cate.level < 3) {
|
||
|
|
const children = await db.collection("article-cate").find({ corpId, parentId: _id }).toArray();
|
||
|
|
const childrenIds = children.map((item) => item._id);
|
||
|
|
if (cate.level === 1 && childrenIds.length) {
|
||
|
|
const grandchildren = await db.collection("article-cate").find({ corpId, parentId: { $in: childrenIds } }).toArray();
|
||
|
|
ids.push(...grandchildren.map((item) => item._id));
|
||
|
|
}
|
||
|
|
ids.push(...childrenIds);
|
||
|
|
}
|
||
|
|
const res = await db.collection("article-cate").deleteMany({ corpId, _id: { $in: ids } });
|
||
|
|
const res2 = await db.collection("article").deleteMany({ corpId, cateId: { $in: ids } });
|
||
|
|
return { success: true, data: [res, res2], message: "删除分类成功" };
|
||
|
|
} catch (e) {
|
||
|
|
return { success: false, message: e.message };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function getArticleCateList(context) {
|
||
|
|
let { corpId } = context;
|
||
|
|
if (!corpId) return { success: false, message: "机构id不能为空" };
|
||
|
|
try {
|
||
|
|
const list = await db.collection("article-cate").find({ corpId, init: { $exists: false } }).sort({ createTime: 1 }).limit(10000).toArray();
|
||
|
|
if (list.length) return { success: true, list, message: "获取分类成功" };
|
||
|
|
const initRecord = await db.collection("article-cate").findOne({ corpId, init: true });
|
||
|
|
if (initRecord) return { success: true, list: [], message: "获取分类成功" };
|
||
|
|
await db.collection("article-cate").insertOne({ _id: common.generateRandomString(24), corpId, init: true });
|
||
|
|
await db.collection("article-cate").insertOne({ _id: common.generateRandomString(24), corpId, label: "默认", level: 1, createTime: Date.now() });
|
||
|
|
const newlist = await db.collection("article-cate").find({ corpId, init: { $exists: false } }).sort({ createTime: 1 }).limit(10000).toArray();
|
||
|
|
return { success: true, list: newlist, message: "获取分类成功" };
|
||
|
|
} catch (e) {
|
||
|
|
return { success: false, message: e.message };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function sortArticleCate(context) {
|
||
|
|
const { corpId, sortData } = context;
|
||
|
|
if (!corpId) return { success: false, message: "机构id不能为空" };
|
||
|
|
const arr = Array.isArray(sortData)
|
||
|
|
? sortData.filter(
|
||
|
|
(i) =>
|
||
|
|
i._id && typeof i.sort === "number" && i.sort % 1 === 0 && i.sort >= 0
|
||
|
|
)
|
||
|
|
: [];
|
||
|
|
if (arr.length === 0) return { success: false, message: "参数错误" };
|
||
|
|
|
||
|
|
const bulkOps = arr.map((item) => ({
|
||
|
|
updateOne: {
|
||
|
|
filter: { _id: item._id, corpId, parentId: { $exists: false } },
|
||
|
|
update: { $set: { sort: item.sort } }
|
||
|
|
}
|
||
|
|
}));
|
||
|
|
|
||
|
|
try {
|
||
|
|
const res = await db.collection("article-cate").bulkWrite(bulkOps);
|
||
|
|
return { success: true, data: res, message: "操作成功" };
|
||
|
|
} catch (e) {
|
||
|
|
return { success: false, message: e.message };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function initCorpArticleCate(context) {
|
||
|
|
try {
|
||
|
|
const list = await db.collection("article-cate").find({ init: { $exists: false }, level: { $exists: false } }).toArray();
|
||
|
|
for (let item of list) {
|
||
|
|
await db.collection("article-cate").updateOne(
|
||
|
|
{ _id: item._id },
|
||
|
|
{ $set: { level: 1, label: item.name } }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return { success: true, message: "操作成功" };
|
||
|
|
} catch (e) {
|
||
|
|
return { success: false, message: e.message };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function ifCateExist(context) {
|
||
|
|
const { corpId, cateId } = context;
|
||
|
|
if (!corpId) return { success: false, message: "机构id不能为空" };
|
||
|
|
try {
|
||
|
|
const cate = await db.collection("article-cate").findOne({ corpId, _id: cateId });
|
||
|
|
return cate ? { success: true } : { success: false, message: "分类不存在" };
|
||
|
|
} catch (e) {
|
||
|
|
return { success: false, message: e.message };
|
||
|
|
}
|
||
|
|
}
|