123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <template>
- <div class="dataset-component">
- <div class="form-item mt-8">
- <label>数据模型名称</label>
- <t-input v-model="modelValue.name" placeholder="名称" />
- </div>
- <div class="form-item mt-8">
- <label>数据方式</label>
- <t-radio-group v-model="modelValue.mode">
- <t-radio value="api">HTTP请求</t-radio>
- <t-radio value="">自定义</t-radio>
- </t-radio-group>
- </div>
- <div v-if="modelValue.mode === 'api'" class="form-item mt-8">
- <label>URL地址</label>
- <t-input v-model="modelValue.url" @blur="getDatas" @enter="getDatas" />
- </div>
- <template v-else>
- <div class="form-item mt-8">
- <label>从Excel导入</label>
- <div class="flex middle w-full">
- <t-button
- class="shrink-0"
- style="width: 90px; height: 30px"
- @click="importDataset"
- >
- 导入Excel
- </t-button>
- <a
- :href="cdn ? cdn + '/v/data.xlsx' : '/data.xlsx'"
- download
- class="ml-12 mt-4 nowrap"
- >
- 下载Excel示例
- </a>
- <span class="flex-grow"></span>
- <a class="ml-12 mt-4 nowrap" @click="showAddData()"> + 添加数据 </a>
- </div>
- </div>
- </template>
- <t-table
- class="mt-16"
- row-key="id"
- :data="modelValue.data"
- :columns="datasetColumns"
- size="small"
- >
- <template #type="{ row }">
- {{ row.type || 'string' }}
- </template>
- <template v-if="!modelValue.mode" #actions="{ row, rowIndex }">
- <t-icon name="edit" class="hover" @click="showAddData(row, rowIndex)" />
- <t-icon
- name="delete"
- class="ml-12 hover"
- @click="modelValue.data.splice(rowIndex, 1)"
- />
- </template>
- </t-table>
- <t-dialog
- v-if="addDataDialog.show"
- :visible="true"
- class="data-dialog"
- :header="addDataDialog.header"
- @close="addDataDialog.show = false"
- @confirm="onOkAddData"
- >
- <div class="form-item mt-16">
- <label>设备</label>
- <t-input v-model="addDataDialog.data.device" placeholder="设备名称" />
- </div>
- <div class="form-item mt-16">
- <label>数据点名称</label>
- <t-input
- v-model="addDataDialog.data.label"
- placeholder="数据点简短描述"
- />
- </div>
- <div class="form-item mt-16">
- <label>数据点ID</label>
- <t-input v-model="addDataDialog.data.id" placeholder="数据点ID" />
- </div>
- <div class="form-item mt-16">
- <label>类型</label>
- <t-select
- class="w-full"
- :options="typeOptions"
- v-model="addDataDialog.data.type"
- placeholder="字符串"
- @change="addDataDialog.data.value = null"
- />
- </div>
- <div class="form-item mt-16">
- <label>值范围</label>
- <div class="w-full">
- <t-input v-model="addDataDialog.data.mock" placeholder="值范围" />
- <h6 class="desc mt-8">值范围说明</h6>
- <ul class="desc ml-16">
- <li>
- <label class="inline" style="width: 80px">固定值:</label>
- 直接填写,例如:10
- </li>
- <li>
- <label class="inline" style="width: 80px">随机值:</label>
- {值1,值2,...}。例如:{1,2,3,4,5}
- </li>
- <li>
- <label class="inline" style="width: 80px">范围数字:</label>
- 最小值-最大值。例如:0-1.0 或 0-100
- </li>
- <li>
- <label class="inline" style="width: 80px">随机字符串:</label>
- [长度]。例如:[8]
- </li>
- </ul>
- </div>
- </div>
- </t-dialog>
- </div>
- </template>
- <script lang="ts" setup>
- import { onBeforeMount, reactive, ref } from 'vue';
- import axios from 'axios';
- import { MessagePlugin } from 'tdesign-vue-next';
- import { importExcel } from '@/services/excel';
- import { typeOptions } from '@/services/common';
- import { cdn } from '@/services/api';
- const { modelValue } = defineProps<{
- modelValue: any;
- }>();
- const emit = defineEmits(['update:modelValue', 'change']);
- const datasetColumns = ref([
- {
- colKey: 'device',
- title: '设备',
- ellipsis: true,
- },
- {
- colKey: 'label',
- title: '数据点名称',
- ellipsis: true,
- },
- {
- colKey: 'id',
- title: '数据点ID',
- ellipsis: true,
- },
- {
- colKey: 'type',
- title: '类型',
- ellipsis: true,
- },
- {
- colKey: 'mock',
- title: '值范围',
- ellipsis: true,
- },
- {
- colKey: 'actions',
- title: '操作',
- width: 100,
- },
- ]);
- const addDataDialog = reactive<any>({});
- onBeforeMount(() => {
- if (!modelValue.data) {
- modelValue.data = [];
- }
- getDatas();
- });
- const getDatas = async () => {
- if (!modelValue.url) {
- return;
- }
- const ret = await axios.get(modelValue.url);
- if (ret) {
- modelValue.data = ret;
- }
- };
- const importDataset = async () => {
- let columns: any = [
- {
- header: '设备',
- key: 'device',
- },
- {
- header: '数据点名称',
- key: 'label',
- },
- {
- header: '数据点ID',
- key: 'id',
- },
- {
- header: '类型',
- key: 'type',
- },
- {
- header: '值范围',
- key: 'mock',
- },
- ];
- const data: any = await importExcel(columns);
- modelValue.data = data;
- emit('update:modelValue', modelValue);
- emit('change', modelValue);
- };
- const showAddData = (row?: any, index?: number) => {
- if (row) {
- addDataDialog.header = '编辑数据';
- addDataDialog.data = JSON.parse(JSON.stringify(row));
- addDataDialog.index = index;
- } else {
- addDataDialog.header = '添加数据';
- addDataDialog.data = { type: 'string' };
- }
- addDataDialog.show = true;
- };
- const onOkAddData = () => {
- if (!addDataDialog.data.label) {
- MessagePlugin.error('请填写名称');
- return;
- }
- if (!addDataDialog.data.id) {
- MessagePlugin.error('请填写数据ID');
- return;
- }
- if (addDataDialog.header === '添加数据') {
- modelValue.data.push(addDataDialog.data);
- } else {
- modelValue.data[addDataDialog.index] = addDataDialog.data;
- }
- addDataDialog.show = false;
- };
- </script>
- <style lang="postcss" scoped>
- .dataset-component {
- padding-right: 16px;
- }
- </style>
|