king
2025-05-14 7c4e955aa35cf5aa2a0c86fe19d930b7fcbffd74
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Modal, Button, Popconfirm, message, notification } from 'antd'
import { StopTwoTone, DatabaseOutlined, CopyOutlined, CheckCircleTwoTone, DeleteOutlined, PlusOutlined, SwapOutlined } from '@ant-design/icons'
 
import Utils from '@/utils/utils.js'
import asyncComponent from '@/utils/asyncComponent'
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
const DataSource = asyncComponent(() => import('@/menu/datasource'))
const EditTable = asyncComponent(() => import('@/templates/zshare/editTable'))
 
class InterfaceController extends Component {
  static propTpyes = {
    config: PropTypes.object,       // 页面配置
    updateConfig: PropTypes.func    // 更新
  }
 
  state = {
    visible: false,
    interfaces: [],
    columns: [
      {
        title: '名称',
        dataIndex: 'name',
        width: '50%'
      },
      {
        title: '状态',
        dataIndex: 'status',
        width: '20%',
        render: (text, record) => record.status !== 'true' ?
          (
            <div>
              禁用
              <StopTwoTone style={{marginLeft: '5px'}} twoToneColor="#ff4d4f" />
            </div>
          ) :
          (
            <div>
              启用
              <CheckCircleTwoTone style={{marginLeft: '5px'}} twoToneColor="#52c41a" />
            </div>
          )
      },
      {
        title: '操作',
        align: 'center',
        width: '30%',
        dataIndex: 'operation',
        render: (text, record) =>
          (<div style={{textAlign: 'center'}}>
            <span onClick={() => this.handleStatus(record)} style={{color: '#8E44AD', cursor: 'pointer', fontSize: '16px', marginRight: '15px'}}><SwapOutlined /></span>
            <span onClick={() => this.copy(record)} style={{color: '#26C281', cursor: 'pointer', fontSize: '16px', marginRight: '15px'}}><CopyOutlined /></span>
            <Popconfirm
              overlayClassName="popover-confirm"
              title="确定删除?"
              onConfirm={() => this.deleteScript(record)
            }>
              <span style={{color: '#ff4d4f', cursor: 'pointer', fontSize: '16px', marginRight: '15px'}}><DeleteOutlined /></span>
            </Popconfirm>
            <DataSource config={record} updateConfig={this.update}/>
          </div>)
      }
    ]
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  copy = (item) => {
    let msg = { key: 'interface', type: 'line', data: item }
 
    try {
      msg = window.btoa(window.encodeURIComponent(JSON.stringify(msg)))
    } catch (e) {
      console.warn('Stringify Failure')
      msg = ''
    }
 
    if (msg) {
      let oInput = document.createElement('input')
      oInput.value = msg
      document.body.appendChild(oInput)
      oInput.select()
      document.execCommand('Copy')
      document.body.removeChild(oInput)
      message.success('复制成功。')
    }
  }
 
  trigger = () => {
    const { config } = this.props
    let interfaces = config.interfaces ? fromJS(config.interfaces).toJS() : []
 
    this.setState({
      visible: true,
      interfaces
    })
  }
 
  handleStatus = (record) => {
    const { config } = this.props
    
    if (record.status === 'false') {
      if (record.setting.interType === 'system' && record.setting.execute !== 'false' && !record.setting.dataresource) {
        notification.warning({
          top: 92,
          message: '未设置数据源,不可启用!',
          duration: 5
        })
        return
      } else if (!record.setting.primaryKey) {
        notification.warning({
          top: 92,
          message: '未设置主键,不可启用!',
          duration: 5
        })
        return
      } else if (record.columns.length === 0) {
        notification.warning({
          top: 92,
          message: '未添加字段集,不可启用!',
          duration: 5
        })
        return
      }
    }
 
    record = fromJS(record).toJS()
    record.status = record.status === 'false' ? 'true' : 'false'
 
    let interfaces = this.state.interfaces.map(item => {
      if (item.uuid !== record.uuid) {
        return item
      }
      return record
    })
 
    this.setState({ interfaces })
    this.props.updateConfig({...config, interfaces})
  }
 
  deleteScript = (record) => {
    const { config } = this.props
    let interfaces = this.state.interfaces.filter(item => item.uuid !== record.uuid)
 
    this.setState({ interfaces })
    this.props.updateConfig({...config, interfaces})
  }
  
  changeScripts = (interfaces) => {
    const { config } = this.props
 
    interfaces = interfaces.map(item => {
      item.$tables = this.getTables(item)
      return item
    })
 
    this.setState({ interfaces })
    this.props.updateConfig({...config, interfaces})
  }
 
  getTables = (record) => {
    let tables = []
    let cuts = []
    let cutreg = /(from|update|insert\s+into)\s+(@db@)?[a-z_]+/ig
    let trimreg = /(from|update|insert\s+into)\s+(@db@)?/ig
 
    if (record.setting.interType === 'system') {
      if (record.setting.execute !== 'false' && record.setting.dataresource) {
        let tbs = record.setting.dataresource.match(cutreg)
        tbs && cuts.push(...tbs)
      }
      record.scripts && record.scripts.forEach(script => {
        if (script.status === 'false') return
        let tbs = script.sql.match(cutreg)
        tbs && cuts.push(...tbs)
      })
    } else if (record.setting.tableName) {
      let tb = record.setting.tableName.replace(/@db@|\s+/ig, '')
      if (/[a-z_]+/ig.test(tb)) {
        tables.push(tb)
      }
    }
 
    cuts = cuts.map(item => item.replace(trimreg, ''))
    tables.push(...cuts)
    tables = tables.filter(Boolean)
    tables = Array.from(new Set(tables))
 
    return tables
  }
 
  update = (record) => {
    const { config } = this.props
 
    if (record.setting.primaryKey && record.columns.length > 0) {
      record.status = 'true'
    } else if (record.columns.length === 0) {
      record.status = 'false'
    }
    record.name = record.setting.name
    record.$tables = this.getTables(record)
 
    delete record.subColumns
 
    let interfaces = this.state.interfaces.map(item => {
      if (item.uuid !== record.uuid) {
        return item
      }
      return record
    })
 
    this.setState({ interfaces })
    this.props.updateConfig({...config, interfaces})
 
    MKEmitter.emit('editLineId', record.uuid)
    setTimeout(() => {
      MKEmitter.emit('mkUpdateInter', record, {delay: 0})
    }, 10)
  }
 
  addInterface = () => {
    const { config } = this.props
    let interfaces = fromJS(this.state.interfaces).toJS()
 
    interfaces.push({
      uuid: Utils.getuuid(),
      name: '数据源' + (interfaces.length + 1),
      status: 'false',
      format: 'array',
      type: 'interface',
      pageable: false,
      setting: { interType: 'system', name: '数据源' + (interfaces.length + 1), status: 'false' },
      columns: [],
      scripts: [],
    })
 
    this.setState({
      interfaces
    })
    this.props.updateConfig({...config, interfaces})
  }
 
  render() {
    const { visible, columns, interfaces } = this.state
 
    return (
      <>
        <Button className="mk-border-danger" onClick={this.trigger}><DatabaseOutlined /> 公共数据源</Button>
        <Modal
          title="公共数据源"
          wrapClassName="interface-controller-modal"
          visible={visible}
          width={800}
          maskClosable={false}
          onCancel={() => {this.setState({visible: false})}}
          footer={[
            <Button key="colse" onClick={() => {this.setState({visible: false})}}>
              关闭
            </Button>
          ]}
          destroyOnClose
        > 
          <PlusOutlined key="add-interface" onClick={this.addInterface}/>
          <EditTable key="manage-interface" actions={['copy']} type="interface" data={interfaces} columns={columns} onChange={this.changeScripts}/>
        </Modal>
      </>
    )
  }
}
 
export default InterfaceController