-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
swaggerAutoSync.js
executable file
·239 lines (224 loc) · 6.84 KB
/
swaggerAutoSync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { formatTime } from 'client/common.js';
import { Form, Switch, Button, Icon, Tooltip, message, Input, Select } from 'antd';
import {handleSwaggerUrlData} from 'client/reducer/modules/project';
const FormItem = Form.Item;
const Option = Select.Option;
import axios from 'axios';
// layout
const formItemLayout = {
labelCol: {
lg: { span: 5 },
xs: { span: 24 },
sm: { span: 10 }
},
wrapperCol: {
lg: { span: 16 },
xs: { span: 24 },
sm: { span: 12 }
},
className: 'form-item'
};
const tailFormItemLayout = {
wrapperCol: {
sm: {
span: 16,
offset: 11
}
}
};
@connect(
state => {
return {
projectMsg: state.project.currProject
};
},
{
handleSwaggerUrlData
}
)
@Form.create()
export default class ProjectInterfaceSync extends Component {
static propTypes = {
form: PropTypes.object,
match: PropTypes.object,
projectId: PropTypes.number,
projectMsg: PropTypes.object,
handleSwaggerUrlData: PropTypes.func
};
constructor(props) {
super(props);
this.state = {
sync_data: { is_sync_open: false }
};
}
handleSubmit = async () => {
const { form, projectId } = this.props;
let params = {
project_id: projectId,
is_sync_open: this.state.sync_data.is_sync_open,
uid: this.props.projectMsg.uid
};
if (this.state.sync_data._id) {
params.id = this.state.sync_data._id;
}
form.validateFields(async (err, values) => {
if (!err) {
let assignValue = Object.assign(params, values);
await axios.post('/api/plugin/autoSync/save', assignValue).then(res => {
if (res.data.errcode === 0) {
message.success('保存成功');
} else {
message.error(res.data.errmsg);
}
});
}
});
};
validSwaggerUrl = async (rule, value, callback) => {
if(!value)return;
try{
await this.props.handleSwaggerUrlData(value);
} catch(e) {
callback('swagger地址不正确');
}
callback()
}
componentWillMount() {
//查询同步任务
this.setState({
sync_data: {}
});
//默认每份钟同步一次,取一个随机数
this.setState({
random_corn: '*/2 * * * *'
});
this.getSyncData();
}
async getSyncData() {
let projectId = this.props.projectMsg._id;
let result = await axios.get('/api/plugin/autoSync/get?project_id=' + projectId);
if (result.data.errcode === 0) {
if (result.data.data) {
this.setState({
sync_data: result.data.data
});
}
}
}
// 是否开启
onChange = v => {
let sync_data = this.state.sync_data;
sync_data.is_sync_open = v;
this.setState({
sync_data: sync_data
});
};
sync_cronCheck(rule, value, callback){
if(!value)return;
value = value.trim();
if(value.split(/ +/).length > 5){
callback('不支持秒级别的设置,建议使用 "*/10 * * * *" ,每隔10分钟更新')
}
callback()
}
render() {
const { getFieldDecorator } = this.props.form;
return (
<div className="m-panel">
<Form>
<FormItem
label="是否开启自动同步"
{...formItemLayout}
>
<Switch
checked={this.state.sync_data.is_sync_open}
onChange={this.onChange}
checkedChildren="开"
unCheckedChildren="关"
/>
{this.state.sync_data.last_sync_time != null ? (<div>上次更新时间:<span className="logtime">{formatTime(this.state.sync_data.last_sync_time)}</span></div>) : null}
</FormItem>
<div>
<FormItem {...formItemLayout} label={
<span className="label">
数据同步
<Tooltip
title={
<div>
<h3 style={{ color: 'white' }}>普通模式</h3>
<p>不导入已存在的接口</p>
<br />
<h3 style={{ color: 'white' }}>智能合并</h3>
<p>
已存在的接口,将合并返回数据的 response,适用于导入了 swagger
数据,保留对数据结构的改动
</p>
<br />
<h3 style={{ color: 'white' }}>完全覆盖</h3>
<p>不保留旧数据,完全使用新数据,适用于接口定义完全交给后端定义</p>
</div>
}
>
<Icon type="question-circle-o" />
</Tooltip>{' '}
</span>
}>
{getFieldDecorator('sync_mode', {
initialValue: this.state.sync_data.sync_mode,
rules: [
{
required: true,
message: '请选择同步方式!'
}
]
})(
<Select>
<Option value="normal">普通模式</Option>
<Option value="good">智能合并</Option>
<Option value="merge">完全覆盖</Option>
</Select>
)}
</FormItem>
<FormItem {...formItemLayout} label="项目的swagger json地址">
{getFieldDecorator('sync_json_url', {
rules: [
{
required: true,
message: '输入swagger地址'
},
{
validator: this.validSwaggerUrl
}
],
validateTrigger: 'onBlur',
initialValue: this.state.sync_data.sync_json_url
})(<Input />)}
</FormItem>
<FormItem {...formItemLayout} label={<span>类cron风格表达式(默认10分钟更新一次) <a href="https://blog.csdn.net/shouldnotappearcalm/article/details/89469047">参考</a></span>}>
{getFieldDecorator('sync_cron', {
rules: [
{
required: true,
message: '输入node-schedule的类cron表达式!'
},
{
validator: this.sync_cronCheck
}
],
initialValue: this.state.sync_data.sync_cron ? this.state.sync_data.sync_cron : this.state.random_corn
})(<Input />)}
</FormItem>
</div>
<FormItem {...tailFormItemLayout}>
<Button type="primary" htmlType="submit" icon="save" size="large" onClick={this.handleSubmit}>
保存
</Button>
</FormItem>
</Form>
</div>
);
}
}