const adminService = require('../service/admin'); const projectService = require('../service/projects.service'); const userService = require('../service/project.user.service'); const roleService = require('../service/roles.service'); const pagesRoleService = require('../service/pagesRole.service'); const util = require('../utils/util'); // 项目配置 async function getProjectConfig(ctx) { const { projectId } = ctx.request.query; if (!util.isNumber(projectId)) { return ctx.throw(400, '项目ID不能为空'); } const { userId } = util.decodeToken(ctx); const info = await adminService.getProjectConfig(projectId); if (!info?.length) { return util.fail(ctx, '当前项目不存在', 404); } if (info[0].isTemplate === 1) { return util.success(ctx, info?.[0] || {}); } // 私有项目且当前登录人不是创建人,则需要进一步判断当前用户是否为开发者 if (info[0].isPublic === 2 && info[0].userId !== userId) { // 创建用户相当于系统超级管理员 const user = await userService.getUserRole(userId, projectId); if (!user) { return util.fail(ctx, '您当前暂无访问权限', '10001'); } } util.success(ctx, info?.[0] || {}); } // 页面详情 async function getPageDetail(ctx) { const { projectId } = ctx.request.query; const { id, env } = ctx.request.params; if (!util.isNumber(id)) { return ctx.throw(400, '页面ID不能为空'); } if (!util.isNumber(projectId)) { return ctx.throw(400, '项目ID不能为空'); } if (!util.checkEnv(env)) { return ctx.throw(400, '环境参数不能为空'); } // 单独访问页面时,需要判断权限:公开页面或拥有开发者和体验者权限 let [pageInfo] = await adminService.getPageDetailById(id); if (!pageInfo) { return util.fail(ctx, '当前页面不存在', 404); } console.log('info', pageInfo); // 如果项目ID为空,则判断改页面是否公开或私有,私有页面需要登录才可访问,公开页面无需登录 if (pageInfo.isTemplate == 0 && !projectId && pageInfo.isPublic === 2) { const { userId } = util.decodeToken(ctx); // 需要判断用户是否是开发者或体验者; if (pageInfo.userId !== userId) { // 查询页面开发者 const list = await pagesRoleService.getPagesRoleList([id, pageInfo.projectId].join(',')); // 如果不是创建者,又不是开发者,无法访问 if (list.filter((item) => item.userId === userId).length === 0) { return util.fail(ctx, '您当前暂无查看权限', 403); } } } if (pageInfo) { const lastPublishId = pageInfo[`${env}PublishId`]; if (lastPublishId > 0) { const pageId = pageInfo.id; const [result] = await adminService.getLastPublishInfo(pageId, lastPublishId); util.success(ctx, { ...result, projectId: pageInfo.projectId }); } else { util.fail(ctx, '当前页面未发布', 500); } } } async function getProjectList(ctx) { const { pageNum, pageSize } = ctx.request.query; const { userId } = util.decodeToken(ctx); const { total } = await projectService.getCategoryCount('', userId); if (total === 0) { util.success(ctx, { list: [], total: 0, pageSize: +pageSize, pageNum: +pageNum, }); } const list = await projectService.getCategoryList(pageNum || 1, pageSize || 12, '', userId); util.success(ctx, { list, total, pageSize: +pageSize, pageNum: +pageNum, }); } // 菜单列表 async function getMenuList(ctx) { const { id } = ctx.request.params; if (!util.isNumber(id)) { return ctx.throw(400, '项目ID不能为空'); } const { userId } = util.decodeToken(ctx); const project = await adminService.getProjectConfig(id); // 判断项目是否存在 if (project.length > 0) { // 判断项目是否公开或者模板 if (project[0].isPublic === 1 || project[0].isTemplate === 1) { const menuList = await adminService.getAllMenuList(id); util.success(ctx, { list: menuList }); } else { // 创建用户相当于系统超级管理员 if (project[0].userId === userId) { const menuList = await adminService.getAllMenuList(id); util.success(ctx, { list: menuList }); } else { const user = await userService.getUserRole(userId, id); if (!user) { return util.fail(ctx, '您当前暂无访问权限', '10001'); } // 管理员返回全部菜单 if (user.systemRole === 1) { const menuList = await adminService.getAllMenuList(id); util.success(ctx, { list: menuList }); } else { // 根据用户角色查询对应菜单列表 const { id, checked = '', halfChecked = '' } = await roleService.getRoleInfo(user.roleId); let menuIds = []; if (checked) { menuIds = menuIds.concat(checked.split(',')); } if (halfChecked) { menuIds = menuIds.concat(halfChecked.split(',')); } if (menuIds.length === 0) { util.success(ctx, { list: [] }); } else { const menuList = await adminService.getMenuList(menuIds.join(', '), id); util.success(ctx, { list: menuList }); } } } } } else { util.success(ctx, { list: [] }); } } module.exports = { getProjectConfig, getPageDetail, getProjectList, getMenuList, };