mock平台

swaggerAutoSync.js 6.5KB

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