Dataset.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <div class="dataset-component">
  3. <div class="form-item mt-8">
  4. <label>数据集名称</label>
  5. <t-input v-model="modelValue.name" placeholder="名称" />
  6. </div>
  7. <div class="form-item mt-8">
  8. <label>数据方式</label>
  9. <t-radio-group v-model="modelValue.mode">
  10. <t-radio value="api">HTTP请求</t-radio>
  11. <t-radio value="">自定义</t-radio>
  12. </t-radio-group>
  13. </div>
  14. <div v-if="modelValue.mode === 'api'" class="form-item mt-8">
  15. <label>URL地址</label>
  16. <t-input v-model="modelValue.url" @blur="getDatas" @enter="getDatas" />
  17. </div>
  18. <template v-else>
  19. <div class="form-item mt-8">
  20. <label>从Excel导入</label>
  21. <div class="flex middle w-full">
  22. <t-button
  23. class="shrink-0"
  24. style="width: 90px; height: 30px"
  25. @click="importDataset"
  26. >
  27. 导入Excel
  28. </t-button>
  29. <a href="/data.xlsx" download class="ml-12 mt-4 nowrap">
  30. 下载Excel示例
  31. </a>
  32. <span class="flex-grow"></span>
  33. <a class="ml-12 mt-4 nowrap" @click="showAddData()"> + 添加数据 </a>
  34. </div>
  35. </div>
  36. </template>
  37. <t-table
  38. class="mt-16"
  39. row-key="id"
  40. :data="modelValue.data"
  41. :columns="datasetColumns"
  42. size="small"
  43. >
  44. <template #type="{ row }">
  45. {{ row.type || 'string' }}
  46. </template>
  47. <template v-if="!modelValue.mode" #actions="{ row, rowIndex }">
  48. <t-icon name="edit" class="hover" @click="showAddData(row, rowIndex)" />
  49. <t-icon
  50. name="delete"
  51. class="ml-12 hover"
  52. @click="modelValue.data.splice(rowIndex, 1)"
  53. />
  54. </template>
  55. </t-table>
  56. <t-dialog
  57. v-if="addDataDialog.show"
  58. :visible="true"
  59. class="data-dialog"
  60. :header="addDataDialog.header"
  61. @close="addDataDialog.show = false"
  62. @confirm="onOkAddData"
  63. >
  64. <div class="form-item mt-16">
  65. <label>数据点名称</label>
  66. <t-input v-model="addDataDialog.data.label" placeholder="简短描述" />
  67. </div>
  68. <div class="form-item mt-16">
  69. <label>数据点ID</label>
  70. <t-input v-model="addDataDialog.data.id" placeholder="数据点ID" />
  71. </div>
  72. <div class="form-item mt-16">
  73. <label>类型</label>
  74. <t-select
  75. class="w-full"
  76. :options="typeOptions"
  77. v-model="addDataDialog.data.type"
  78. placeholder="字符串"
  79. @change="addDataDialog.data.value = null"
  80. />
  81. </div>
  82. <div class="form-item mt-16">
  83. <label>值范围</label>
  84. <div class="w-full">
  85. <t-input v-model="addDataDialog.data.mock" placeholder="值范围" />
  86. <h6 class="desc mt-8">值范围说明</h6>
  87. <ul class="desc ml-16">
  88. <li>
  89. <label class="inline" style="width: 80px">固定值:</label>
  90. 直接填写,例如:10
  91. </li>
  92. <li>
  93. <label class="inline" style="width: 80px">随机值:</label>
  94. {值1,值2,...}。例如:{1,2,3,4,5}
  95. </li>
  96. <li>
  97. <label class="inline" style="width: 80px">范围数字:</label>
  98. 最小值-最大值。例如:0-1.0 或 0-100
  99. </li>
  100. <li>
  101. <label class="inline" style="width: 80px">随机字符串:</label>
  102. [长度]。例如:[8]
  103. </li>
  104. </ul>
  105. </div>
  106. </div>
  107. </t-dialog>
  108. </div>
  109. </template>
  110. <script lang="ts" setup>
  111. import { onBeforeMount, reactive, ref } from 'vue';
  112. import axios from 'axios';
  113. import { MessagePlugin } from 'tdesign-vue-next';
  114. import { importExcel } from '@/services/excel';
  115. import { typeOptions } from '@/services/common';
  116. const { modelValue } = defineProps<{
  117. modelValue: any;
  118. }>();
  119. const emit = defineEmits(['update:modelValue', 'change']);
  120. const datasetColumns = ref([
  121. {
  122. colKey: 'label',
  123. title: '数据点名称',
  124. ellipsis: true,
  125. },
  126. {
  127. colKey: 'id',
  128. title: '数据点ID',
  129. ellipsis: true,
  130. },
  131. {
  132. colKey: 'type',
  133. title: '类型',
  134. ellipsis: true,
  135. },
  136. {
  137. colKey: 'mock',
  138. title: '值范围',
  139. ellipsis: true,
  140. },
  141. {
  142. colKey: 'actions',
  143. title: '操作',
  144. width: 100,
  145. },
  146. ]);
  147. const addDataDialog = reactive<any>({});
  148. onBeforeMount(() => {
  149. if (!modelValue.data) {
  150. modelValue.data = [];
  151. }
  152. getDatas();
  153. });
  154. const getDatas = async () => {
  155. if (!modelValue.url) {
  156. return;
  157. }
  158. const ret = await axios.get(modelValue.url);
  159. if (ret) {
  160. modelValue.data = ret;
  161. }
  162. };
  163. const importDataset = async () => {
  164. let columns: any = [
  165. {
  166. header: '数据点名称',
  167. key: 'label',
  168. },
  169. {
  170. header: '数据点ID',
  171. key: 'id',
  172. },
  173. {
  174. header: '类型',
  175. key: 'type',
  176. },
  177. {
  178. header: '值范围',
  179. key: 'mock',
  180. },
  181. ];
  182. const data: any = await importExcel(columns);
  183. modelValue.data = data;
  184. emit('update:modelValue', modelValue);
  185. emit('change', modelValue);
  186. };
  187. const showAddData = (row?: any, index?: number) => {
  188. if (row) {
  189. addDataDialog.header = '编辑数据';
  190. addDataDialog.data = JSON.parse(JSON.stringify(row));
  191. addDataDialog.index = index;
  192. } else {
  193. addDataDialog.header = '添加数据';
  194. addDataDialog.data = { type: 'string' };
  195. }
  196. addDataDialog.show = true;
  197. };
  198. const onOkAddData = () => {
  199. if (!addDataDialog.data.label) {
  200. MessagePlugin.error('请填写名称');
  201. return;
  202. }
  203. if (!addDataDialog.data.id) {
  204. MessagePlugin.error('请填写数据ID');
  205. return;
  206. }
  207. if (addDataDialog.header === '添加数据') {
  208. modelValue.data.push(addDataDialog.data);
  209. } else {
  210. modelValue.data[addDataDialog.index] = addDataDialog.data;
  211. }
  212. addDataDialog.show = false;
  213. };
  214. </script>
  215. <style lang="postcss" scoped>
  216. .dataset-component {
  217. padding-right: 16px;
  218. }
  219. </style>