You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.1 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. const { Dept } = require("../models/dept");
  2. const { validatedRequest } = require("../utils/middleware/validatedRequest");
  3. const {
  4. strictMultiUserRoleValid,
  5. ROLES
  6. } = require("../utils/middleware/multiUserProtected");
  7. const { reqBody } = require("../utils/http");
  8. function deptEndpoints(app) {
  9. if (!app) return;
  10. app.get(
  11. "/dept/list",
  12. [validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
  13. async (_request, response) => {
  14. try {
  15. const depts = await Dept.where({ delFlag: 0 });
  16. response.status(200).json({ depts });
  17. } catch (e) {
  18. console.error(e);
  19. response.sendStatus(500).end();
  20. }
  21. }
  22. );
  23. // 获取组织机构树状结构
  24. app.get(
  25. "/dept/tree",
  26. [validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
  27. async (_request, response) => {
  28. try {
  29. const deptTree = await Dept.getDeptTree();
  30. response.status(200).json({ deptTree });
  31. } catch (e) {
  32. console.error(e);
  33. response.sendStatus(500).end();
  34. }
  35. }
  36. );
  37. // 懒加载子组织机构列表
  38. app.get(
  39. "/dept/children",
  40. [validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
  41. async (request, response) => {
  42. try {
  43. const parentId = request.query.parentId
  44. ? parseInt(request.query.parentId)
  45. : null;
  46. const children = await Dept.getChildrenByParentId(parentId);
  47. response.status(200).json({ children });
  48. } catch (e) {
  49. console.error(e);
  50. response.sendStatus(500).end();
  51. }
  52. }
  53. );
  54. app.post("/dept/add",
  55. [validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
  56. async (request, response) => {
  57. try {
  58. const dept = reqBody(request); // 获取请求体中的组织机构数据
  59. console.log("dept 类型:", typeof dept);
  60. // 检查组织机构名称是否唯一
  61. const isUnique = await Dept.checkDeptNameUnique(dept);
  62. if (!isUnique) {
  63. return response.status(400).json({
  64. success: false,
  65. message: `新增组织机构 '${dept.deptName}' 失败,组织机构名称已存在`,
  66. });
  67. };
  68. // 按照deptId查询父组织机构
  69. const parentDept = await Dept.get({ deptId: dept.parentId });
  70. dept.ancestors = parentDept.dept.ancestors + ',' + dept.parentId;
  71. // 插入组织机构数据
  72. const insertedDept = await Dept.insertDept(dept);
  73. // 返回成功响应
  74. response.status(200).json({
  75. success: true,
  76. data: insertedDept,
  77. });
  78. } catch (error) {
  79. // 处理错误
  80. console.error("添加组织机构失败:", error);
  81. response.status(500).json({
  82. success: false,
  83. message: "添加组织机构失败,服务器内部错误",
  84. });
  85. }
  86. });
  87. app.post("/dept/edit/:deptId",
  88. [validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
  89. async (request, response) => {
  90. try {
  91. const deptId = parseInt(request.params.deptId);
  92. const dept = reqBody(request); // 获取请求体中的组织机构数据
  93. // 检查组织机构名称是否唯一
  94. const isUnique = await Dept.checkDeptNameUnique(dept);
  95. console.log("isUnique:", isUnique);
  96. if (!isUnique) {
  97. return response.status(400).json({
  98. success: false,
  99. message: `修改组织机构 '${dept.deptName}' 失败,组织机构名称已存在`,
  100. });
  101. }
  102. // 检查上级组织机构是否是自己
  103. if (dept.parentId === deptId) {
  104. return response.status(400).json({
  105. success: false,
  106. message: `修改组织机构 '${dept.deptName}' 失败,上级组织机构不能是自己`,
  107. });
  108. }
  109. // 检查组织机构是否包含未停用的子组织机构
  110. if (dept.status === 1) {
  111. const normalChildrenCount = await Dept.selectNormalChildrenDeptById(deptId);
  112. if (normalChildrenCount > 0) {
  113. return response.status(400).json({
  114. success: false,
  115. message: "该组织机构包含未停用的子组织机构!",
  116. });
  117. }
  118. }
  119. // 更新组织机构数据
  120. const updatedDept = await Dept.update(deptId, dept);
  121. // 返回成功响应
  122. response.status(200).json({
  123. success: true,
  124. data: updatedDept,
  125. });
  126. } catch (error) {
  127. // 处理错误
  128. console.error("修改组织机构失败:", error);
  129. response.status(500).json({
  130. success: false,
  131. message: "修改组织机构失败,服务器内部错误",
  132. });
  133. }
  134. });
  135. // 删除组织机构的接口
  136. app.delete("/dept/:deptId",
  137. [validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
  138. async (request, response) => {
  139. try {
  140. const deptId = parseInt(request.params.deptId); // 获取组织机构 ID
  141. // 检查组织机构是否有子组织机构
  142. const hasChild = await Dept.hasChildByDeptId(deptId);
  143. if (hasChild) {
  144. return response.status(400).json({
  145. success: false,
  146. message: "存在下级组织机构,不允许删除",
  147. });
  148. }
  149. // 检查组织机构是否存在用户
  150. const hasUser = await Dept.checkDeptExistUser(deptId);
  151. if (hasUser) {
  152. return response.status(400).json({
  153. success: false,
  154. message: "组织机构存在用户,不允许删除",
  155. });
  156. }
  157. // // 检查组织机构数据权限
  158. // const hasDataScope = await Dept.checkDeptDataScope(deptId);
  159. // if (!hasDataScope) {
  160. // return response.status(403).json({
  161. // success: false,
  162. // message: "无权限删除该组织机构",
  163. // });
  164. // }
  165. // 删除组织机构
  166. const deletedDept = await Dept.softDelete(deptId);
  167. // 返回成功响应
  168. response.status(200).json({
  169. success: true,
  170. data: deletedDept,
  171. });
  172. } catch (error) {
  173. // 处理错误
  174. console.error("删除组织机构失败:", error);
  175. response.status(500).json({
  176. success: false,
  177. message: "删除组织机构失败,服务器内部错误",
  178. });
  179. }
  180. });
  181. }
  182. module.exports = { deptEndpoints };