新增分组的hook模板配置

This commit is contained in:
2024-07-13 18:37:45 +08:00
parent e5a2af25e8
commit 34d765d1e2
11 changed files with 128 additions and 44 deletions

View File

@@ -50,6 +50,7 @@ export default class CodeGenTemplate extends Component {
form: PropTypes.object,
match: PropTypes.object,
projectId: PropTypes.number,
groupId: PropTypes.number,
projectMsg: PropTypes.object,
handleSwaggerUrlData: PropTypes.func
};
@@ -73,9 +74,10 @@ export default class CodeGenTemplate extends Component {
}
handleSubmit = async (data) => {
const {form, projectId} = this.props;
const {form, projectId, groupId} = this.props;
let params = {
project_id: projectId,
group_id: groupId,
tag: data.tag_t || data.tag,
tag_desc: data.tag_desc_t || data.tag_desc,
template_data: data.template_data,
@@ -99,7 +101,9 @@ export default class CodeGenTemplate extends Component {
});
};
componentDidMount() {
this.getSyncData();
}
UNSAFE_componentWillMount() {
//查询同步任务
@@ -107,12 +111,11 @@ export default class CodeGenTemplate extends Component {
config_data: {template_data: "", _id: null},
templateList: []
});
this.getSyncData();
}
async getSyncData() {
let projectId = this.props.projectMsg._id;
let result = await axios.get('/api/plugin/template/get?projectId=' + projectId);
const {projectId, groupId} = this.props;
let result = await axios.get('/api/plugin/template/get?' + (projectId ? `projectId=${projectId}` : `groupId=${groupId}`));
if (result.data.errcode === 0) {
if (result.data.data && result.data.data.length) {
let defaultTemplate = result.data.data.find(it => it.tag === "default")

View File

@@ -10,6 +10,15 @@ module.exports = function () {
})
this.bindHook('add_group_tab', function (panes) {
panes.push({
key: 'codeGen',
title: '代码生成模板配置',
component: CodeGenTemplate,
roles: ['admin', 'owner']
})
})
this.bindHook('sub_setting_nav', function (route) {
route.codeGen = {
name: '代码生成模板配置',

View File

@@ -26,16 +26,22 @@ class exportController extends baseController {
async genCode(ctx) {
let projectId = ctx.request.query.projectId;
let interfaceId = ctx.request.query.interfaceId;
try {
let curProject = await this.projectModel.get(projectId);
let interfaceData = await this.interModel.get(interfaceId);
if (!interfaceData) {
ctx.body = yapi.commons.resReturn(null, 502, '接口不存在');
return
}
let curProject = await this.projectModel.get(interfaceData.project_id);
let userData = await this.userModel.findById(this.getUid());
let errMsg = [];
let templateList = await this.genCodeModel.listByProjectId(projectId);
let templateList = await this.genCodeModel.listByProjectId(interfaceData.project_id);
let retData = {};
if (templateList == null || templateList.length === 0) {
templateList = await this.genCodeModel.listByGroupId(curProject.group_id);
}
console.log(templateList)
if (templateList == null || templateList.length === 0) {
ctx.body = yapi.commons.resReturn(errMsg, 502, '请先设置生成模板');
@@ -82,9 +88,10 @@ class exportController extends baseController {
async saveTemplate(ctx) {
let requestBody = ctx.request.body;
let projectId = requestBody.project_id;
if (!projectId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id'));
let projectId = requestBody.project_id || requestBody.projectId
let groupId = requestBody.group_id || requestBody.groupId
if (!projectId && !groupId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id/组id'));
}
if ((await this.checkAuth(projectId, 'project', 'edit')) !== true) {
@@ -96,7 +103,7 @@ class exportController extends baseController {
} else {
let existData = await this.genCodeModel.listByProjectId(projectId);
if (existData.find(templ => templ.tag === requestBody.tag)){
if (existData.find(templ => templ.tag === requestBody.tag)) {
return (ctx.body = yapi.commons.resReturn(null, 502, 'tag已存在'));
}
result = await this.genCodeModel.save(requestBody);
@@ -110,12 +117,18 @@ class exportController extends baseController {
* @param {*} ctx
*/
async getTemplate(ctx) {
let projectId = ctx.params.projectId;
if (!projectId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id'));
let projectId = ctx.params.projectId || ctx.params.project_id;
let groupId = ctx.params.groupId || ctx.params.group_id;
if (!projectId && !groupId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id/组id'));
}
console.log(this.genCodeModel)
let result = await this.genCodeModel.listByProjectId(projectId);
let result = null;
if (projectId) {
result = await this.genCodeModel.listByProjectId(projectId);
} else {
result = await this.genCodeModel.listByGroupId(groupId);
}
return (ctx.body = yapi.commons.resReturn(result));
}

View File

@@ -9,7 +9,8 @@ class genCodeModel extends baseModel {
getSchema() {
return {
uid: { type: Number},
project_id: { type: Number, required: true },
project_id: { type: Number, required: false },
group_id: { type: Number, required: false },
//是否开启自动同步
tag: { type: String, default: "default" },
tag_desc: { type: String, default: "默认" },
@@ -33,6 +34,13 @@ class genCodeModel extends baseModel {
})
}
listByGroupId(id) {
return this.model.find({
group_id: id
})
}
delByProjectId(project_id){
return this.model.remove({
project_id: project_id