king
2022-12-30 6afb82b92c7de7a3d5551e721b4c8de39bd7de9d
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import CryptoJS from 'crypto-js'
import { Input, Modal, Form, notification } from 'antd'
import { EditOutlined, DeleteOutlined } from '@ant-design/icons'
 
import './index.scss'
 
const { TextArea } = Input
const { confirm } = Modal
 
class KeyInterface extends Component {
  static propTpyes = {
    type: PropTypes.string,
    onChange: PropTypes.func
  }
 
  state = {
    url: '',
    key: '',
    visible: false,
    setting: null
  }
 
  componentDidMount() {
    const { value, type } = this.props
 
    if (value) {
      let content = value
      try {
        content = JSON.parse(content) 
      } catch (e) {
        content = {}
      }
 
      this.setState({setting: content.message || null, url: content.url || ''})
    }
 
    if (type === 'develop') {
      this.setState({key: window.GLOB.appkey.slice(-16)})
    }
  }
 
  editKey = () => {
    let _setting = this.state.setting
 
    if (this.state.key && _setting && typeof(_setting) === 'string') {
      _setting = this.decrypt(this.state.key, _setting)
 
      if (!_setting) {
        notification.warning({
          top: 92,
          message: '信息解析失败!',
          duration: 5
        })
        this.props.onChange('')
      }
    }
 
    this.setState({visible: true, setting: _setting || {ssoInterface: 'http://sso.mk9h.cn/cloud/webapi/dostars'}})
  }
 
  decrypt = (token, value) => {
    let setting = null
    try {
      const key = CryptoJS.enc.Utf8.parse(token)
      const iv = CryptoJS.enc.Utf8.parse('mksoft')
  
      let encryptedHexStr = CryptoJS.enc.Hex.parse(value)
      let _srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
      let decrypt = CryptoJS.AES.decrypt(_srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
      let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
      setting = decryptedStr.toString()
  
      setting = JSON.parse(window.decodeURIComponent(window.atob(setting)))
    } catch (e) {
      setting = null
    }
 
    return setting
  }
 
  handleConfirm = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (err) return
 
      if (values.apptoken) {
        let key = values.apptoken.slice(-16)
        let _setting = this.state.setting
        if (_setting && typeof(_setting) === 'string') {
          _setting = this.decrypt(key, _setting)
 
          if (!_setting) {
            const that = this
            confirm({
              title: '信息解析失败!',
              content: '点击确定会清除配置信息,点击取消可重新输入appkey。',
              onOk() {
                that.setState({key: key, setting: {}, url: ''})
                that.props.onChange('')
              },
              onCancel() {}
            })
            return
          }
        }
        this.setState({key: key, setting: _setting})
        return
      }
 
      if (!/\/dostars$/.test(values.interface)) {
        notification.warning({
          top: 92,
          message: '接口地址请填写dostars接口!',
          duration: 5
        })
        return
      } else if (values.ssoInterface && !/\/dostars$/.test(values.ssoInterface)) {
        notification.warning({
          top: 92,
          message: 'sso地址请填写dostars接口!',
          duration: 5
        })
        return
      }
 
      let _values = window.btoa(window.encodeURIComponent(JSON.stringify(values)))
 
      const key = CryptoJS.enc.Utf8.parse(this.state.key)
      const iv = CryptoJS.enc.Utf8.parse('mksoft')
 
      let srcs = CryptoJS.enc.Utf8.parse(_values)
      let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
      let message = encrypted.ciphertext.toString()
      let content = JSON.stringify({message: message, url: values.interface})
 
      this.props.onChange(content)
 
      this.setState({setting: message, url: values.interface, visible: false})
 
      if (this.props.type !== 'develop') {
        this.setState({key: ''})
      }
    })
  }
 
  delKey = () => {
    const { url } = this.state
    const { type } = this.props
 
    if (!url) return
 
    if (type === 'develop') {
      this.setState({setting: null, url: ''})
    } else {
      this.setState({key: '', setting: null, url: ''})
    }
 
    this.props.onChange('')
  }
 
  cancel = () => {
    const { type } = this.props
 
    if (type !== 'develop') {
      this.setState({key: ''})
    }
    this.setState({visible: false})
  }
 
  render() {
    const { getFieldDecorator } = this.props.form
    const { url, visible, setting, key } = this.state
 
    return (
      <div className="mk-key-wrap">
        <TextArea value={url} rows={2} readOnly={true}/>
        <div className="mk-key-edit">
          <EditOutlined onClick={this.editKey}/>
          <DeleteOutlined className={!url ? 'disable' : ''} onClick={this.delKey}/>
        </div>
        <Modal
          wrapClassName='mk-key-modal'
          visible={visible}
          closable={false}
          maskClosable={false}
          width={650}
          onOk={this.handleConfirm}
          onCancel={this.cancel}
          destroyOnClose
        >
          {key && setting ?
            <Form>
              <Form.Item label="接口地址">
                {getFieldDecorator('interface', {
                  initialValue: setting.interface || 'http://******/webapi/dostars',
                  rules: [
                    {
                      required: true,
                      message: '请输入接口地址!'
                    },
                    {
                      pattern: /^[0-9a-zA-Z:_./]+$/,
                      message: '只可使用英文、数字以及:_./'
                    }
                  ]
                })(<TextArea placeholder="http://******/webapi/dostars" rows={2}/>)}
              </Form.Item>
              <Form.Item label="sso地址">
                {getFieldDecorator('ssoInterface', {
                  initialValue: setting.ssoInterface || '',
                  rules: [
                    {
                      pattern: /^[0-9a-zA-Z:_./]+$/,
                      message: '只可使用英文、数字以及:_./'
                    }
                  ]
                })(<TextArea placeholder="http://sso.mk9h.cn/cloud/webapi/dostars" rows={2}/>)}
              </Form.Item>
              <Form.Item label="appkey">
                {getFieldDecorator('appkey', {
                  initialValue: setting.appkey || '',
                  rules: [
                    {
                      required: true,
                      message: '请输入appkey!'
                    },
                    {
                      pattern: /^[0-9a-zA-Z]+$/,
                      message: '只可输入英文及数字。'
                    },
                    {
                      min: 16,
                      message: '不可小于16位!'
                    }
                  ]
                })(<Input placeholder="请输入目标系统appkey" autoComplete="off" />)}
              </Form.Item>
              <Form.Item label="用户名">
                {getFieldDecorator('username', {
                  initialValue: setting.username,
                  rules: [
                    {
                      required: true,
                      message: '请输入用户名!'
                    }
                  ]
                })(<Input placeholder="请输入用户名" autoComplete="off" />)}
              </Form.Item>
              <Form.Item label="密码">
                {getFieldDecorator('password', {
                  initialValue: setting.password,
                  rules: [
                    {
                      required: true,
                      message: '请输入密码!'
                    }
                  ]
                })(<Input.Password placeholder="请输入密码" />)}
              </Form.Item>
              <Form.Item label="公钥">
                {getFieldDecorator('publicKey', {
                  initialValue: setting.publicKey,
                  rules: [
                    {
                      required: true,
                      message: '请输入公钥!'
                    }
                  ]
                })(<Input.Password placeholder={''} autoComplete="off" />)}
              </Form.Item>
              <Form.Item label="私钥">
                {getFieldDecorator('privateKey', {
                  initialValue: setting.privateKey,
                  rules: [
                    {
                      required: true,
                      message: '请输入私钥!'
                    }
                  ]
                })(<Input.Password placeholder={''} autoComplete="off" />)}
              </Form.Item>
            </Form> : null}
          {!key ? <Form style={{marginTop: '20px', marginBottom: '50px'}}>
            <Form.Item label="appkey">
              {getFieldDecorator('apptoken', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: '请输入正式系统appkey!'
                  },
                  {
                    pattern: /^[0-9a-zA-Z]+$/,
                    message: '只可输入英文及数字。'
                  },
                  {
                    min: 16,
                    message: '不可小于16位!'
                  }
                ]
              })(<Input placeholder="请输入正式系统appkey" autoComplete="off" />)}
            </Form.Item>
          </Form> : null}
        </Modal>
      </div>
    )
  }
}
 
export default Form.create()(KeyInterface)