新增分组的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

@@ -1,4 +1,4 @@
import React, { PureComponent as Component } from 'react';
import React, {PureComponent as Component} from 'react';
import GroupList from './GroupList/GroupList.js';
import ProjectList from './ProjectList/ProjectList.js';
import MemberList from './MemberList/MemberList.js';
@@ -16,6 +16,7 @@ import {
} from '../../reducer/modules/group';
import './Group.scss';
import axios from 'axios'
import plugin from "../../plugin";
@connect(
state => {
@@ -67,6 +68,11 @@ export default class Group extends Component {
// // }
// }
render() {
let panes = []
plugin.emitHook('add_group_tab', panes);
let filterPanes = panes.filter(it => it.roles.indexOf(this.props.curUserRoleInGroup) > -1 ||
this.props.curUserRole === 'admin');
if(this.state.groupId === -1)return <Spin />
const GroupContent = (
<Layout style={{ minHeight: 'calc(100vh - 100px)', marginLeft: '24px', marginTop: '24px' }}>
@@ -106,6 +112,14 @@ export default class Group extends Component {
<GroupSetting />
</TabPane>
) : null}
{ filterPanes.map(it =>{
let Com = it.component
return (
<TabPane tab={it.title} key={it.key}>
<Com groupId={this.props.curGroupId}/>
</TabPane>
)
}) }
</Tabs>
</Content>
</Layout>

View File

@@ -244,6 +244,11 @@ hooks = {
type: 'listener',
mulit: true,
listener: []
},
add_group_tab:{
type: 'listener',
mulit: true,
listener: []
}
};

View File

@@ -31,5 +31,14 @@ module.exports = function() {
name: 'Markdown',
component: Services
}
})
})
this.bindHook('add_group_tab', function (panes) {
panes.push({
key: 'Markdown',
title: 'Markdown设置',
component: mdTemplate,
roles: ['admin', 'owner']
})
})
};

View File

@@ -10,13 +10,14 @@ class configModel 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 },
//是否开启自动同步
is_export_by_interface: { type: Boolean, default: false },
// 模板
template_data: String,
add_time: Number,
up_time: Number,
up_time: Number
};
}
@@ -26,6 +27,11 @@ class configModel extends baseModel {
})
}
getByGroupId(id) {
return this.model.findOne({
group_id: id
})
}
delByProjectId(project_id){
return this.model.remove({
project_id: project_id

View File

@@ -3,6 +3,7 @@ const interfaceModel = require('models/interface.js');
const projectModel = require('models/project.js');
const interfaceCatModel = require('models/interfaceCat.js');
const userModel = require("models/user.js");
const groupModel = require("models/group.js");
const yapi = require('yapi.js');
const uuid = require('uuid');
const configModel = require("./configModel");
@@ -17,6 +18,7 @@ class exportMarkdownController extends baseController {
this.projectModel = yapi.getInst(projectModel);
this.configModel = yapi.getInst(configModel);
this.userModel = yapi.getInst(userModel);
this.groupModel = yapi.getInst(groupModel);
}
/*
@@ -82,16 +84,19 @@ class exportMarkdownController extends baseController {
}
let templateData = await this.configModel.getByProjectId(pid);
let curProject = await this.projectModel.get(pid);
if (!templateData){
templateData = await this.configModel.getByGroupId(curProject.group_id);
}
// console.log(result);
if (!templateData.is_export_by_interface) {
if (!templateData || !templateData.is_export_by_interface) {
console.log("重定向")
ctx.status = 302;
ctx.redirect(`/api/plugin/export?type=markdown&pid=${pid}`);
return;
}
let curProject;
try {
curProject = await this.projectModel.get(pid);
// ctx.set('Content-Type', 'application/json');
// ctx.set('Content-Type', 'application/octet-stream');
const list = await this.handleListClass(pid, status);
@@ -153,8 +158,9 @@ class exportMarkdownController extends baseController {
async upConfig(ctx) {
let requestBody = ctx.request.body;
let projectId = requestBody.project_id;
if (!projectId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id'));
let groupId = requestBody.group_id;
if (!projectId && !groupId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id/组id'));
}
if ((await this.checkAuth(projectId, 'project', 'edit')) !== true) {
@@ -176,11 +182,17 @@ class exportMarkdownController extends baseController {
* @param {*} ctx
*/
async getConfig(ctx) {
let projectId = ctx.query.project_id;
if (!projectId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id'));
let projectId = ctx.query.project_id || ctx.query.projectId;
let groupId = ctx.query.group_id || ctx.query.groupId;
if (!projectId && !groupId) {
return (ctx.body = yapi.commons.resReturn(null, 408, '缺少项目Id/组id'));
}
let result = null;
if (projectId){
result = await this.configModel.getByProjectId(projectId);
}else {
result= await this.configModel.getByGroupId(groupId);
}
let result = await this.configModel.getByProjectId(projectId);
return (ctx.body = yapi.commons.resReturn(result));
}
@@ -196,12 +208,15 @@ class exportMarkdownController extends baseController {
if (!interfaceData) {
return (ctx.body = yapi.commons.resReturn(null, 200, ''));
}
let project = await this.projectModel.get(interfaceData.project_id);
let templateData = await this.configModel.getByProjectId(interfaceData.project_id);
if (!templateData || !templateData.template_data) {
templateData = await this.configModel.getByGroupId(project.group_id);
}
if (!templateData || !templateData.template_data) {
return (ctx.body = yapi.commons.resReturn(null, 200, ''));
}
let interfaceCat = await this.catModel.get(interfaceData.catid);
let project = await this.projectModel.get(interfaceData.project_id);
let {result, errMsg} = await this.executeJsRender(templateData.template_data, project, interfaceData, interfaceCat, userData);
if (errMsg){
ctx.body = yapi.commons.resReturn(errMsg, 502, '渲染出错');

View File

@@ -69,10 +69,10 @@ class MDTemplateServices extends Component {
const extraContent = (
<Fragment>
<Button size={"small"} type={"link"} icon="copy"
onClick={(e) => this.preCopy(this.state.render_data.data)}>拷贝原文</Button>
onClick={() => this.preCopy(this.state.render_data.data)}>拷贝原文</Button>
<Button size={"small"} type={"link"} icon="table"
onClick={(e) => {
onClick={() => {
this.setState({
isHtml: !this.state.isHtml
})

View File

@@ -44,6 +44,7 @@ export default class ProjectInterfaceSync extends Component {
static propTypes = {
form: PropTypes.object,
match: PropTypes.object,
groupId: PropTypes.number,
projectId: PropTypes.number,
projectMsg: PropTypes.object,
handleSwaggerUrlData: PropTypes.func
@@ -55,11 +56,14 @@ export default class ProjectInterfaceSync extends Component {
config_data: { is_export_by_interface: false, template_data:"",_id:null }
};
}
componentDidMount() {
this.getSyncData();
}
handleSubmit = async () => {
const { form, projectId } = this.props;
const { form, projectId, groupId } = this.props;
let params = {
project_id: projectId,
group_id: groupId,
is_export_by_interface: this.state.config_data.is_export_by_interface,
template_data: this.state.config_data.template_data,
uid: this.props.projectMsg.uid
@@ -97,12 +101,10 @@ export default class ProjectInterfaceSync extends Component {
this.setState({
config_data: {}
});
this.getSyncData();
}
async getSyncData() {
let projectId = this.props.projectMsg._id;
let result = await axios.get('/api/plugin/mdConfig/get?project_id=' + projectId);
let result = await axios.get('/api/plugin/mdConfig/get' + (this.props.projectId ? `?project_id=${this.props.projectId}` : `?groupId=${this.props.groupId}`));
if (result.data.errcode === 0) {
if (result.data.data) {
this.setState({

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