feat: 分类管理页开发
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
const cloud = require('wx-server-sdk');
|
||||
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
|
||||
|
||||
function validatePayload(event) {
|
||||
const id = String(event.id || '').trim();
|
||||
const name = String(event.name || '').trim();
|
||||
const icon = String(event.icon || '').trim();
|
||||
const color = String(event.color || '').trim();
|
||||
const sortOrder = Number(event.sortOrder || 0);
|
||||
const parentId =
|
||||
event.parentId === undefined || event.parentId === null
|
||||
? null
|
||||
: String(event.parentId).trim();
|
||||
|
||||
if (!id) throw new Error('分类 id 不能为空');
|
||||
if (!name) throw new Error('分类名称不能为空');
|
||||
if (!icon) throw new Error('分类图标不能为空');
|
||||
if (!color) throw new Error('分类颜色不能为空');
|
||||
if (!Number.isFinite(sortOrder)) throw new Error('分类排序不合法');
|
||||
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
icon,
|
||||
color,
|
||||
sortOrder,
|
||||
parentId,
|
||||
};
|
||||
}
|
||||
|
||||
exports.main = async (event) => {
|
||||
try {
|
||||
const payload = validatePayload(event);
|
||||
const db = cloud.database();
|
||||
const collection = db.collection('categories');
|
||||
|
||||
try {
|
||||
await collection.doc(payload.id).get();
|
||||
return {
|
||||
success: false,
|
||||
message: '分类已存在',
|
||||
};
|
||||
} catch (error) {
|
||||
// 文档不存在时继续创建
|
||||
}
|
||||
|
||||
await collection.doc(payload.id).set({
|
||||
data: {
|
||||
name: payload.name,
|
||||
icon: payload.icon,
|
||||
color: payload.color,
|
||||
sortOrder: payload.sortOrder,
|
||||
parentId: payload.parentId,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
id: payload.id,
|
||||
name: payload.name,
|
||||
icon: payload.icon,
|
||||
color: payload.color,
|
||||
sortOrder: payload.sortOrder,
|
||||
parentId: payload.parentId,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : '创建分类失败',
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "categories-create",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"wx-server-sdk": "^3.0.4"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user