function.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="device-function">
  3. <el-empty description="暂无功能列表,请先在物模型的功能定义中添加" v-if="!loading && !list.length"></el-empty>
  4. <el-tabs tab-position="left">
  5. <el-tab-pane :label="item.name" v-for="item in list" :key="item.key">
  6. <div class="table-wrapper">
  7. <el-table :data="item.inputs || []" border>
  8. <el-table-column prop="name" label="参数名称" />
  9. <el-table-column prop="valueType.type" label="输入类型" />
  10. <el-table-column prop="name" label="值" min-width="200">
  11. <template #default="{ row }">
  12. <el-select v-model="row.value" clearable v-if="row.valueType.type === 'enum'" style="wdith: 100% !important;">
  13. <el-option v-for="item in row.valueType.elements" :key="item.value" :value="item.value" :label="item.text"></el-option>
  14. </el-select>
  15. <el-input v-model="row.value" clearable v-else>
  16. <template v-if="row.valueType.unit" #append>{{ row.valueType.unit }}</template>
  17. </el-input>
  18. </template>
  19. </el-table-column>
  20. </el-table>
  21. <el-input type="textarea" :value="item.result" class="result" read-only placeholder="执行结果:"> </el-input>
  22. </div>
  23. <div class="btn">
  24. <el-button type="primary" :loading="item.loading" @click="run(item)">执行</el-button>
  25. <el-button @click="clear(item)">清空</el-button>
  26. </div>
  27. </el-tab-pane>
  28. </el-tabs>
  29. </div>
  30. </template>
  31. <script setup lang="ts">
  32. import { ref } from 'vue'
  33. import { ElMessage } from 'element-plus'
  34. import api from '/@/api/device'
  35. const props = defineProps({
  36. productKey: String,
  37. deviceKey: String,
  38. })
  39. interface IListItem {
  40. key: string
  41. name: string
  42. inputs: any[]
  43. result: string
  44. loading: boolean
  45. }
  46. const list = ref<IListItem[]>([])
  47. const loading = ref(true)
  48. getData()
  49. function getData() {
  50. loading.value = true
  51. api.tabDeviceFucntion.getList({ key: props.productKey }).then((res: IListItem[]) => {
  52. if (!res) return
  53. res.forEach((item) => (item.result = ''))
  54. list.value = res
  55. }).finally(() => loading.value = false)
  56. }
  57. function run(row: IListItem) {
  58. row.result = ''
  59. const notValid = row.inputs.some((item) => !item.value)
  60. if (notValid) return ElMessage.info('请输入完整参数')
  61. row.loading = true
  62. const params: any = {}
  63. row.inputs.forEach(({ key, value }) => (params[key] = value))
  64. api.tabDeviceFucntion
  65. .do({
  66. deviceKey: props.deviceKey,
  67. funcKey: row.key,
  68. params,
  69. })
  70. .then((res: any) => {
  71. // console.log(res)
  72. row.result = JSON.stringify(res, null, 2)
  73. })
  74. .finally(() => (row.loading = false))
  75. }
  76. function clear(row: IListItem) {
  77. row.result = ''
  78. }
  79. </script>
  80. <style scoped lang="scss">
  81. .table-wrapper {
  82. width: 100%;
  83. padding: 10px;
  84. display: flex;
  85. align-items: stretch;
  86. justify-content: space-between;
  87. gap: 20px;
  88. .el-table {
  89. flex: 3;
  90. }
  91. .result {
  92. flex: 2;
  93. }
  94. ::v-deep(.el-textarea__inner) {
  95. height: 100%;
  96. }
  97. ::v-deep(.el-select) {
  98. width: 100%;
  99. }
  100. }
  101. </style>