From 091c372d9bbf1539c6b05cc5d7c9b91571273eb8 Mon Sep 17 00:00:00 2001 From: ma-zhongxu Date: Thu, 27 Feb 2025 19:51:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E6=96=B0=E5=A2=9E,=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/models/admin.js | 12 ++ frontend/src/pages/Admin/Section/index.jsx | 112 +++++++++++++++++- server/endpoints/dept.js | 16 ++- server/models/dept.js | 40 +++++-- .../cebc49a6-9df4-46cc-8ba5-672b5d72de0e.txt | 1 + .../f51243b3-13e6-4374-8b64-a7fa4ba38ed9.txt | 20 ++++ 6 files changed, 179 insertions(+), 22 deletions(-) create mode 100644 server/storage/localFile/cebc49a6-9df4-46cc-8ba5-672b5d72de0e.txt create mode 100644 server/storage/localFile/f51243b3-13e6-4374-8b64-a7fa4ba38ed9.txt diff --git a/frontend/src/models/admin.js b/frontend/src/models/admin.js index 31794c2..3a6d93d 100644 --- a/frontend/src/models/admin.js +++ b/frontend/src/models/admin.js @@ -270,6 +270,18 @@ const Admin = { return { depts: null, error: e.message }; }); }, + updateDept: async (deptId,data) => { + return await fetch(`${API_BASE}/dept/edit/${deptId}`, { + method: "POST", + headers: baseHeaders(), + body: JSON.stringify(data), + }) + .then((res) => res.json()) + .catch((e) => { + console.error(e); + return { depts: null, error: e.message }; + }); + }, }; export default Admin; diff --git a/frontend/src/pages/Admin/Section/index.jsx b/frontend/src/pages/Admin/Section/index.jsx index 3557ca1..ef212ba 100644 --- a/frontend/src/pages/Admin/Section/index.jsx +++ b/frontend/src/pages/Admin/Section/index.jsx @@ -112,13 +112,24 @@ function DepartmentsContainer() { ); } + function DepartmentRow({ dept }) { const [expanded, setExpanded] = useState(false); + const { isOpen, openModal, closeModal } = useModal(); const toggleExpand = () => { setExpanded(!expanded); }; + const handleEdit = () => { + openModal(); + }; + + const handleSuccess = () => { + // 刷新部门数据 + window.location.reload(); // 或者调用父组件的刷新方法 + }; + return ( <> @@ -155,10 +166,16 @@ function DepartmentRow({ dept }) {
- -
@@ -169,10 +186,16 @@ function DepartmentRow({ dept }) { dept.children.map((child) => ( ))} + + + ); } - function NewDepartmentModal({ closeModal }) { const [formData, setFormData] = useState({ deptName: "", @@ -275,6 +298,89 @@ function NewDepartmentModal({ closeModal }) { ); } +function EditDepartmentModal({ dept, closeModal, onSuccess }) { + const [formData, setFormData] = useState({ + deptName: dept.deptName, + parentId: dept.parentId, + orderNum: dept.orderNum, + status: dept.status, + }); + + const [departments, setDepartments] = useState([]); + const [treeData, setTreeData] = useState([]); + + useEffect(() => { + async function fetchDepartments() { + const _departments = await Admin.depts(); + setDepartments(_departments); + setTreeData(buildTree(_departments)); + } + fetchDepartments(); + }, []); + + const handleSubmit = async () => { + await Admin.updateDept(dept.deptId,formData); // 调用更新 API + onSuccess(); // 通知父组件刷新数据 + closeModal(); + }; + + function buildTree(depts, parentId = 0) { + return depts + .filter((dept) => dept.parentId === parentId) + .map((dept) => ({ + title: dept.deptName, // 确保 title 是部门名称 + value: dept.deptId, // 部门 ID + children: buildTree(depts, dept.deptId), // 递归处理子部门 + })); + } + + return ( +
+

+ 编辑部门 +

+
+
+ + setFormData({ ...formData, parentId: value })} + placeholder="请选择上级部门" + className="w-full" + dropdownStyle={{ maxHeight: 400, overflow: "auto" }} + /> +
+ setFormData({ ...formData, deptName: e.target.value })} + className="border-none bg-theme-settings-input-bg text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5" + /> + setFormData({ ...formData, orderNum: Number(e.target.value) })} + className="border-none bg-theme-settings-input-bg text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5" + /> + + 保存 +
+
+ ); +} + // 将部门列表转换为树状结构 function buildTree(departments, parentId = 0) { return departments diff --git a/server/endpoints/dept.js b/server/endpoints/dept.js index 29eadc9..15d0a45 100644 --- a/server/endpoints/dept.js +++ b/server/endpoints/dept.js @@ -4,6 +4,7 @@ const { strictMultiUserRoleValid, ROLES } = require("../utils/middleware/multiUserProtected"); +const { reqBody } = require("../utils/http"); function deptEndpoints(app) { if (!app) return; @@ -57,7 +58,8 @@ function deptEndpoints(app) { [validatedRequest, strictMultiUserRoleValid([ROLES.admin])], async (request, response) => { try { - const dept = request.body; // 获取请求体中的部门数据 + const dept = reqBody(request); // 获取请求体中的部门数据 + console.log("dept 类型:", typeof dept); // 检查部门名称是否唯一 const isUnique = await Dept.checkDeptNameUnique(dept); if (!isUnique) { @@ -86,14 +88,16 @@ function deptEndpoints(app) { } }); - app.post("/dept/edit", + app.post("/dept/edit/:deptId", [validatedRequest, strictMultiUserRoleValid([ROLES.admin])], async (request, response) => { try { - const dept = request.body; // 获取请求体中的部门数据 + const deptId = parseInt(request.params.deptId); + const dept = reqBody(request); // 获取请求体中的部门数据 // 检查部门名称是否唯一 const isUnique = await Dept.checkDeptNameUnique(dept); + console.log("isUnique:", isUnique); if (!isUnique) { return response.status(400).json({ success: false, @@ -102,7 +106,7 @@ function deptEndpoints(app) { } // 检查上级部门是否是自己 - if (dept.parentId === dept.deptId) { + if (dept.parentId === deptId) { return response.status(400).json({ success: false, message: `修改部门 '${dept.deptName}' 失败,上级部门不能是自己`, @@ -111,7 +115,7 @@ function deptEndpoints(app) { // 检查部门是否包含未停用的子部门 if (dept.status === 1) { - const normalChildrenCount = await Dept.selectNormalChildrenDeptById(dept.deptId); + const normalChildrenCount = await Dept.selectNormalChildrenDeptById(deptId); if (normalChildrenCount > 0) { return response.status(400).json({ success: false, @@ -120,7 +124,7 @@ function deptEndpoints(app) { } } // 更新部门数据 - const updatedDept = await Dept.update(dept); + const updatedDept = await Dept.update(deptId, dept); // 返回成功响应 response.status(200).json({ success: true, diff --git a/server/models/dept.js b/server/models/dept.js index a820f29..52e8d24 100644 --- a/server/models/dept.js +++ b/server/models/dept.js @@ -103,26 +103,39 @@ const Dept = { } }, update: async function (deptId, updates = {}) { + console.log("更新部门数据:", updates); try { - if (!deptId) throw new Error('No dept id provided for update'); + // 检查 deptId 是否存在 + if (!deptId) throw new Error('没有提供用于查询的deptId'); - const currentDept = await prisma.dept.findUnique({ where: { deptId } }); - if (!currentDept) throw new Error('Dept not found'); + // 检查 updates 是否为空 + if (Object.keys(updates).length === 0) throw new Error('没有提供更新字段'); - Object.entries(updates).forEach(([key, value]) => { - if (this.writable.includes(key)) { - if (Object.prototype.hasOwnProperty.call(this.validations, key)) { - updates[key] = this.validations[key](this.castColumnValue(key, value)); - } else { - updates[key] = this.castColumnValue(key, value); - } - } - }); + // 查询当前部门 + const currentDept = await prisma.dept.findUnique({ where: { deptId } }); + if (!currentDept) throw new Error('不存在该部门'); + // 更新 lastUpdatedAt 字段 updates.lastUpdatedAt = new Date(); - const updatedDept = await prisma.dept.update({ where: { deptId }, data: updates }); + // 如果 parentId 发生变化,更新 ancestors 字段 + if (updates.parentId !== undefined && updates.parentId !== currentDept.parentId) { + const parentId = updates.parentId; + const parentDept = await prisma.dept.findUnique({ where: { deptId: parentId } }); + updates.ancestors = parentDept ? `${parentDept.ancestors},${parentId}` : `${parentId}`; + } + + console.log("更新前部门数据:", updates); + + // 执行更新操作 + const updatedDept = await prisma.dept.update({ + where: { deptId }, // 使用 deptId 作为查询条件 + data: updates, // 更新的字段 + }); + + console.log("更新后部门数据:", updatedDept); + // 返回成功结果 return { success: true, error: null, dept: updatedDept }; } catch (error) { console.error(error.message); @@ -174,6 +187,7 @@ const Dept = { }); // 如果查询到记录,说明部门名称已存在 + console.log("existingDept:", existingDept); return !existingDept; } catch (error) { console.error('检查部门名称唯一性失败:', error); diff --git a/server/storage/localFile/cebc49a6-9df4-46cc-8ba5-672b5d72de0e.txt b/server/storage/localFile/cebc49a6-9df4-46cc-8ba5-672b5d72de0e.txt new file mode 100644 index 0000000..f3ca610 --- /dev/null +++ b/server/storage/localFile/cebc49a6-9df4-46cc-8ba5-672b5d72de0e.txt @@ -0,0 +1 @@ +你好 \ No newline at end of file diff --git a/server/storage/localFile/f51243b3-13e6-4374-8b64-a7fa4ba38ed9.txt b/server/storage/localFile/f51243b3-13e6-4374-8b64-a7fa4ba38ed9.txt new file mode 100644 index 0000000..43a1c3f --- /dev/null +++ b/server/storage/localFile/f51243b3-13e6-4374-8b64-a7fa4ba38ed9.txt @@ -0,0 +1,20 @@ +一、数字驼乡 + +1、政策类型修改增删改查接口的逻辑 +2、服务事项主题分类增删改查逻辑修改,信息发布修改数据库字段类型 +3、政策内容id改成UUID, 行业资讯id修改为UUID,政策解读ID修改为UUID,用户注册校验用户名是否重复, 商家信息新增和更新时校验商家名称和统一社会信用代码 +4、商家信息添加字段:其他证照,商家信息添加字段:审核状态,审核时间,审核人,审核反馈; 商家资质添加字段:审核人 +5、添加商家信息图标 +7、政企服务建表,页面和路由生成,后端新开一个微服务 +8、配合赵洋做商家信息相关的问题 +9、 添加5个图标供参考 +10、生成提问和反馈的页面和路由 +11、商家信息修改时校验审核状态 +12、提问反馈的页面字段错误问题和提问反馈的关联用户ID问题 +13、修复商家信息和行业类型,行政区划实体里面的对应混乱问题 +二、盟林草-森林保险管理系统 + +1、修改上次测出问题,部署 +三、社会治理联动指挥平台(一期) + +1、修复导航权限问题,部署 \ No newline at end of file