suerprisePlus
2024-05-30 d4c810f9803aa13d806724a5f4f71fe85abac38e
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<template>
  <div style="padding: 6px">
    <!-- 添加或修改通用物模型对话框 -->
    <el-dialog title="编辑参数" :visible.sync="openEdit" width="900px" append-to-body>
      <div style="margin: -30px 0 30px; background-color: #ddd; height: 1px"></div>
      <el-row>
        <el-col :span="12" style="border: 1px solid #ddd; border-radius: 5px; padding: 10px; background-color: #eee">
          <el-form :model="queryParams" :inline="true" label-width="48px" size="small">
            <el-form-item label="" prop="templateName">
              <el-input v-model="queryParams.templateName" placeholder="请输入物模型名称" style="width: 160px" clearable size="mini" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="info" icon="el-icon-search" size="mini" @click="handleQuery" style="padding: 5px">搜索</el-button>
            </el-form-item>
            <el-form-item>
              <el-link :underline="false" icon="el-icon-info" type="primary" style="margin-left: 20px">单击应用模板</el-link>
            </el-form-item>
          </el-form>
 
          <el-table v-loading="loading" :data="templateList" size="mini" @row-click="rowClick" highlight-current-row :border="false" :show-header="false" :row-style="{ backgroundColor: '#eee' }">
            <el-table-column label="选择" width="30" align="center">
              <template slot-scope="scope">
                <input type="radio" :checked="scope.row.isSelect" :disabled="scope.row.datatype == 'array' || scope.row.datatype == 'object'" name="template" />
              </template>
            </el-table-column>
            <el-table-column label="名称" align="left" prop="templateName" />
            <el-table-column label="标识符" align="left" prop="identifier" />
            <el-table-column label="数据类型" align="center" prop="datatype" width="60">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.iot_data_type" :value="scope.row.datatype" />
              </template>
            </el-table-column>
          </el-table>
 
          <pagination
            v-show="total > 0"
            small
            style="margin: 0 0 10px; background-color: #eee"
            layout="prev, pager, next"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
 
        <el-col :span="11" :offset="1">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="参数名称" prop="name">
              <el-input v-model="form.name" placeholder="例如:温度" style="width: 270px" size="small" />
            </el-form-item>
            <el-form-item label="参数标识" prop="id">
              <el-input v-model="form.id" placeholder="例如:temperature" style="width: 270px" size="small"></el-input>
            </el-form-item>
            <el-form-item label="参数排序" prop="order">
              <el-input-number controls-position="right" v-model="form.order" placeholder="请输入排序" type="number" style="width: 270px" size="small" />
            </el-form-item>
 
            <el-form-item label="参数特性" prop="property">
              <el-checkbox name="isChart" label="图表展示" @change="isChartChange" v-model="form.isChart" :true-label="1" :false-label="0"></el-checkbox>
              <el-checkbox name="isMonitor" label="实时监测" @change="isMonitorChange" v-model="form.isMonitor" :true-label="1" :false-label="0"></el-checkbox>
              <el-checkbox name="isReadonly" label="只读数据" @change="isReadonlyChange" v-model="form.isReadonly" :true-label="1" :false-label="0"></el-checkbox>
              <el-checkbox name="isHistory" label="历史存储" v-model="form.isHistory" :true-label="1" :false-label="0"></el-checkbox>
              <el-checkbox name="isSharePerm" label="分享权限" v-model="form.isSharePerm" :true-label="1" :false-label="0"></el-checkbox>
            </el-form-item>
 
            <div style="margin-bottom: 20px; background-color: #ddd; height: 1px"></div>
            <el-form-item label="数据类型" prop="datatype">
              <el-select v-model="form.datatype" placeholder="请选择数据类型" style="width: 125px" size="small">
                <el-option key="integer" label="整数" value="integer"></el-option>
                <el-option key="decimal" label="小数" value="decimal"></el-option>
                <el-option key="bool" label="布尔" value="bool" :disabled="form.isChart == 1"></el-option>
                <el-option key="enum" label="枚举" value="enum" :disabled="form.isChart == 1"></el-option>
                <el-option key="string" label="字符串" value="string" :disabled="form.isChart == 1"></el-option>
              </el-select>
            </el-form-item>
            <div v-if="form.datatype == 'integer' || form.datatype == 'decimal'">
              <el-form-item label="取值范围">
                <el-row>
                  <el-col :span="10">
                    <el-input v-model="form.specs.min" placeholder="最小值" type="number" size="small" />
                  </el-col>
                  <el-col :span="4" align="center">到</el-col>
                  <el-col :span="10">
                    <el-input v-model="form.specs.max" placeholder="最大值" type="number" size="small" />
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item label="单位">
                <el-input v-model="form.specs.unit" placeholder="例如:℃" style="width: 308px" size="small" />
              </el-form-item>
              <el-form-item label="步长">
                <el-input-number controls-position="right" v-model="form.specs.step" placeholder="例如:1" type="number" style="width: 308px" size="small" />
              </el-form-item>
            </div>
            <div v-if="form.datatype == 'bool'">
              <el-form-item label="布尔值" prop="">
                <el-row style="margin-bottom: 10px">
                  <el-col :span="10">
                    <el-input v-model="form.specs.falseText" placeholder="例如:关闭" size="small" />
                  </el-col>
                  <el-col :span="10" :offset="1">(0 值对应文本)</el-col>
                </el-row>
                <el-row>
                  <el-col :span="10">
                    <el-input v-model="form.specs.trueText" placeholder="例如:打开" size="small" />
                  </el-col>
                  <el-col :span="10" :offset="1">(1 值对应文本)</el-col>
                </el-row>
              </el-form-item>
            </div>
            <div v-if="form.datatype == 'enum'">
              <el-form-item label="展示方式">
                <el-select v-model="form.specs.showWay" placeholder="请选择展示方式" style="width: 175px">
                  <el-option key="select" label="下拉框" value="select"></el-option>
                  <el-option key="button" label="按钮" value="button"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="枚举项" prop="">
                <el-row v-for="(item, index) in form.specs.enumList" :key="'enum' + index" style="margin-bottom: 10px">
                  <el-col :span="8">
                    <el-input v-model="item.value" placeholder="例如:0" size="small" />
                  </el-col>
                  <el-col :span="11" :offset="1">
                    <el-input v-model="item.text" placeholder="例如:中速挡位" size="small" />
                  </el-col>
                  <el-col :span="3" :offset="1" v-if="index != 0"><a style="color: #f56c6c" @click="removeEnumItem(index)">删除</a></el-col>
                </el-row>
                <div>
                  +
                  <a style="color: #409eff" @click="addEnumItem()">添加枚举项</a>
                </div>
              </el-form-item>
            </div>
            <div v-if="form.datatype == 'string'">
              <el-form-item label="最大长度" prop="">
                <el-row>
                  <el-col :span="10">
                    <el-input v-model="form.specs.maxLength" placeholder="例如:1024" type="number" size="small" />
                  </el-col>
                </el-row>
              </el-form-item>
            </div>
          </el-form>
        </el-col>
      </el-row>
 
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<style>
.specsColor {
  background-color: #fcfcfc;
}
</style>
 
<script>
import { listTemplate } from '@/api/iot/template';
export default {
  name: 'things_parameter',
  dicts: ['iot_things_type', 'iot_data_type', 'iot_yes_no'],
  props: {
    data: {
      type: Object,
      default: null,
    },
  },
  watch: {
    data: function (newVal, oldVal) {
      this.index = newVal.index;
      if (newVal && newVal.parameter.name && newVal.parameter.name != '') {
        this.form.name = newVal.parameter.name;
        this.form.id = newVal.parameter.id;
        this.form.order = newVal.parameter.order;
        this.form.isChart = newVal.parameter.isChart ? newVal.parameter.isChart : 0;
        this.form.isHistory = newVal.parameter.isHistory ? newVal.parameter.isHistory : 1;
        this.form.isSharePerm = newVal.parameter.isSharePerm ? newVal.parameter.isSharePerm : 0;
        this.form.isMonitor = newVal.parameter.isMonitor ? newVal.parameter.isMonitor : 0;
        this.form.isReadonly = newVal.parameter.isReadonly ? newVal.parameter.isReadonly : 0;
        this.form.specs = newVal.parameter.datatype;
        this.form.datatype = this.form.specs.type;
        if (!this.form.specs.enumList) {
          this.form.specs.enumList = [
            {
              value: '',
              text: '',
            },
          ];
        }
        if (!this.form.specs.arrayType) {
          this.form.specs.arrayType = 'int';
        }
      }
      this.openEdit = true;
      this.getList();
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 通用物模型表格数据
      templateList: [],
      // 是否显示弹出层
      openEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
      },
      // 参数索引
      index: -1,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            message: '参数名称不能为空',
            trigger: 'blur',
          },
        ],
        id: [
          {
            required: true,
            message: '参数标识符不能为空',
            trigger: 'blur',
          },
        ],
        order: [
          {
            required: true,
            message: '模型排序不能为空',
            trigger: 'blur',
          },
        ],
        datatype: [
          {
            required: true,
            message: '数据类型不能为空',
            trigger: 'change',
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.reset();
  },
  methods: {
    /** 查询通用物模型列表 */
    getList() {
      this.loading = true;
      listTemplate(this.queryParams).then((response) => {
        //模板列表初始化isSelect值,用于单选
        for (let i = 0; i < response.rows.length; i++) {
          response.rows[i].isSelect = false;
        }
        this.templateList = response.rows;
        this.total = response.total;
        this.setRadioSelected(this.productId);
        this.loading = false;
      });
    },
    /** 单选数据 */
    rowClick(item) {
      if (item != null && item.datatype != 'array' && item.datatype != 'object') {
        this.form.name = item.templateName;
        this.form.id = item.identifier;
        this.form.order = item.modelOrder;
        this.form.isChart = item.isChart ? item.isChart : 0;
        this.form.isHistory = item.isHistory ? item.isHistory : 1;
        this.form.isSharePerm = item.isSharePerm ? item.isSharePerm : 0;
        this.form.isReadonly = item.isReadonly ? item.isReadonly : 0;
        this.form.isMonitor = item.isMonitor ? item.isMonitor : 0;
        this.form.datatype = item.datatype;
        // Json转对象
        this.form.specs = JSON.parse(item.specs);
        if (!this.form.specs.enumList) {
          this.form.specs.enumList = [
            {
              value: '',
              text: '',
            },
          ];
        }
        if (!this.form.specs.arrayType) {
          this.form.specs.arrayType = 'int';
        }
        this.setRadioSelected(item.templateId);
      }
    },
    /** 设置单选按钮选中 */
    setRadioSelected(templateId) {
      for (let i = 0; i < this.templateList.length; i++) {
        if (this.templateList[i].templateId == templateId) {
          this.templateList[i].isSelect = true;
        } else {
          this.templateList[i].isSelect = false;
        }
      }
    },
    // 取消按钮
    cancel() {
      this.openEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.index = -1;
      this.form = {
        name: null,
        id: null,
        order: 0,
        datatype: 'integer',
        isChart: 0,
        isHistory: 1,
        isSharePerm: 0,
        isMonitor: 0,
        isReadonly: 0,
        specs: {
          enumList: [
            {
              value: '',
              text: '',
            },
          ],
          showWay: 'select', // 显示方式select=下拉选择框,button=按钮
        },
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 格式化datatype
          this.form.datatype = this.formatThingsSpecs();
          // 清空不需要存储数据
          delete this.form.specs;
          this.openEdit = false;
          // 返回参数对象
          let data = {
            parameter: JSON.parse(JSON.stringify(this.form)),
            index: this.index,
          };
          console.log('data', data);
          this.$emit('dataEvent', data);
          this.reset();
        }
      });
    },
    // 是否图表展示改变
    isChartChange() {
      if (this.form.isChart == 1) {
        this.form.isReadonly = 1;
      } else {
        this.form.isMonitor = 0;
      }
    },
    // 是否实时监测改变
    isMonitorChange() {
      if (this.form.isMonitor == 1) {
        this.form.isReadonly = 1;
        this.form.isChart = 1;
      }
    },
    // 是否只读数据改变
    isReadonlyChange() {
      if (this.form.isReadonly == 0) {
        this.form.isMonitor = 0;
        this.form.isChart = 0;
      }
    },
    // 格式化物模型
    formatThingsSpecs() {
      var data = {};
      data.type = this.form.datatype;
      if (this.form.datatype == 'integer' || this.form.datatype == 'decimal') {
        data.min = Number(this.form.specs.min ? this.form.specs.min : 0);
        data.max = Number(this.form.specs.max ? this.form.specs.max : 100);
        data.unit = this.form.specs.unit ? this.form.specs.unit : '';
        data.step = Number(this.form.specs.step ? this.form.specs.step : 1);
      } else if (this.form.datatype == 'string') {
        data.maxLength = Number(this.form.specs.maxLength ? this.form.specs.maxLength : 1024);
      } else if (this.form.datatype == 'bool') {
        data.falseText = this.form.specs.falseText ? this.form.specs.falseText : '关闭';
        data.trueText = this.form.specs.trueText ? this.form.specs.trueText : '打开';
      } else if (this.form.datatype == 'array') {
        data.arrayType = this.form.specs.arrayType;
      } else if (this.form.datatype == 'enum') {
        data.showWay = this.form.specs.showWay;
        if (this.form.specs.enumList && this.form.specs.enumList[0].text != '') {
          data.enumList = this.form.specs.enumList;
        } else {
          data.showWay = 'select';
          data.enumList = [
            {
              value: '0',
              text: '低',
            },
            {
              value: '1',
              text: '高',
            },
          ];
        }
      }
      return data;
    },
    /** 添加枚举项 */
    addEnumItem() {
      this.form.specs.enumList.push({
        value: '',
        text: '',
      });
    },
    /** 删除枚举项 */
    removeEnumItem(index) {
      this.form.specs.enumList.splice(index, 1);
    },
  },
};
</script>