cuikaidong
2025-06-12 64931370717723655d4ecec4802dcdc54ec015f5
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
<template>
  <j-modal
    :fullscreen='true'
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    :switchFullscreen='false'
    :title='title'
    :visible='visible'
    :width='900'
    cancelText='关闭'
    @cancel='handleCancel'
    @ok='handleOk'
  >
    <a-row :gutter='0' type='flex'>
      <a-col :md='4' :sm='0'>
        <tree-left @insertTextAtCursor='insertTextAtCursor' />
      </a-col>
      <a-col :md='20' :sm='0'>
        <a-row :gutter='0'>
          <a-col :span='24'>
            <div>
              <a-row>
                <a-col :span='24'>
                  <j-code-editor
                    ref='editorRef'
                    v-model='code'
                    :fullScreen='true'
                    :height='100'
                    background-color='#f0f0f0'
                    language='C#'
                  />
                </a-col>
              </a-row>
              <a-row>
                <a-col :span='24' style='border: 1px solid #e8e8e8;'>
                  <span
                    style='background-color: rgb(247, 247, 247); text-align: center; line-height: 31px;'
                  >
                    &nbsp;&nbsp;
                    <a-icon style='color: #1890FF;' theme='filled' type='read' />
                    编译信息
                  </span>
                  <p>{{ compileInformation }}</p>
                </a-col>
              </a-row>
            </div>
          </a-col>
        </a-row>
      </a-col>
    </a-row>
    <!-- 自定义标题栏 -->
    <template #title>
      <div class='custom-title'>
        <span>脚本编辑器</span>
        <div class='title-buttons'>
          <a-button
            :style="{ marginRight: '25px', height: '26px' }"
            icon='redo'
            type='primary'
            @click='handleEmpty'
          >
            清空
          </a-button>
          <a-button
            :style="{ marginRight: '25px', height: '26px' }"
            icon='cloud-sync'
            type='primary'
            @click='handleCompile'
          >
            编译
          </a-button>
          <a-button
            :style="{ marginRight: '25px', height: '26px' }"
            icon='table'
            type='primary'
            @click='handleVariable'
          >
            变量
          </a-button>
          <a-button
            :style="{ marginRight: '25px', height: '26px' }"
            icon='gold'
            type='primary'
            @click='handleFunction'
          >
            函数
          </a-button>
        </div>
      </div>
    </template>
    <!-- 实时数据 -->
    <parameter-address ref='modalAddress' :server-id='serverId' @ok='selectAttribute' />
    <script-function ref='modalScriptFunction' :server-id='serverId' @ok='scriptFunction' />
  </j-modal>
</template>
 
<script>
import TreeLeft from './TreeLeft'
import JCodeEditor from '@/components/jeecg/JCodeEditor'
import { httpAction } from '@/api/manage'
import ParameterAddress from './variable/ParameterAddress'
import ScriptFunction from './variable/ScriptFunction'
 
export default {
  components: {
    JCodeEditor,
    TreeLeft,
    ParameterAddress,
    ScriptFunction
  },
  name: 'ScriptModel',
  data() {
    return {
      compileInformation: '',
      serverId: '',
      currentTheme: 'default',
      editor: null,
      code: '',
      indexStyle: false,
      systemDataType: '',
      readWriteType: '只读',
      title: '脚本编辑器',
      visible: false,
      model: {},
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        preserveScript: '/empty/parameter/preserveScript',
        handleCompile: '/empty/parameter/handleCompile',
        edit: '/empty/parameter/edit',
        systemDataType: '/equipment/querySystemDataType'
      },
      disableSubmit: false
    }
  },
  methods: {
    setTheme(theme) {
      this.currentTheme = theme
    },
    // 插入内容
    insertTextAtCursor(value) {
      this.$nextTick(() => {
        const editorRef = this.$refs.editorRef
        if (editorRef) {
          const editor = editorRef.coder
          if (editor) {
            const cursor = editor.getCursor()
            editor.replaceRange(value, cursor)
          }
        }
      })
    },
    onSearch() {
      // this.$refs.modalAddress.visible1 = true
    },
    add() {
      this.edit({})
    },
    modalFormOk(record) {
      // let model = Object.assign({}, record)
      // this.$nextTick(() => {
      //   this.form.setFieldsValue({
      //     parameterDescribe: model.describe,
      //     address: model.default1
      //   })
      // })
      // this.systemDataType = model.dataType
    },
    selectAttribute(record) {
      // 1.赋值
      this.insertTextAtCursor(record.code)
    },
    scriptFunction(record) {
      // 1.赋值
      this.insertTextAtCursor(record)
    },
    edit(record) {
      this.visible = true
 
      // 查询系统类型
      this.form.resetFields()
      this.model = Object.assign({}, record)
      if (!record.scriptContent) {
        this.handleEmpty()
      } else {
        this.code = this.model.scriptContent
        const editor = this.$refs.editorRef
        if (editor) {
          // 调用 setValue 方法将内容设置为空字符串
          editor.coder.setValue(this.code)
        }
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      this.compileInformation = ''
      const that = this
      that.confirmLoading = true
      let formData = Object.assign(this.model)
      formData.script = this.code
      httpAction(this.url.preserveScript, formData, 'post').then((res) => {
        if (res.code !== 500) {
          that.$message.success('保存成功')
          this.$emit('ok', res.result)
          that.close()
        } else {
          that.$message.warning('保存失败')
          this.compileInformation = res.message
        }
      }).finally(() => {
        that.confirmLoading = false
      })
    },
    // 清空脚本
    handleEmpty() {
      this.compileInformation = ''
      // 通过 ref 获取 JCodeEditor 实例
      const editor = this.$refs.editorRef
      if (editor) {
        // 调用 setValue 方法将内容设置为空字符串
        editor.coder.setValue('')
      }
    },
    // 编译脚本
    handleCompile() {
      this.compileInformation = ''
      const that = this
      that.confirmLoading = true
      let formData = Object.assign(this.model)
      formData.script = this.code
      httpAction(this.url.handleCompile, formData, 'post').then((res) => {
        if (res.code !== 500) {
          that.$message.success('编译成功')
        } else {
          that.$message.warning('编译失败')
          this.compileInformation = res.message
        }
      }).finally(() => {
        that.confirmLoading = false
      })
    },
    // 变量
    handleVariable() {
      let formData = Object.assign(this.model)
      this.serverId = formData.serveId
      this.$refs.modalAddress.openMqtt()
    },
    // 函数
    handleFunction() {
      let formData = Object.assign(this.model)
      this.serverId = formData.serveId
      this.$refs.modalScriptFunction.openMqtt()
    },
    handleCancel() {
      this.close()
    }
  },
}
</script>
 
<style>
/* 标题栏按钮样式 */
.custom-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}
 
.title-buttons {
  margin-right: -80px; /* 与关闭按钮对齐 */
}
 
.full-screen-parent.auto-height .full-screen-child {
  min-height: 500px;
  height: unset;
  overflow: hidden;
}
 
.j-code-editor {
  background-color: #f0f0f0;
}
 
.light {
  background-color: #f0f0f0;
  color: #333;
}
</style>