HistoryTable.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <BasicTable ref="historyTable" @register="register" :data-source="data">
  3. <template #bodyCell="{ column, record }">
  4. <a-tag v-if="column.dataIndex === 'warnFlag'" :color="record.warnFlag == '0' ? 'green' : 'red'">
  5. {{ record.warnFlag == '0' ? '正常' : '报警' }}
  6. </a-tag>
  7. <a-tag v-if="column.dataIndex === 'netStatus'" :color="record.netStatus == '0' ? '#f00' : 'green'">
  8. {{ record.netStatus == '0' ? '断开' : '连接' }}
  9. </a-tag>
  10. </template>
  11. <template #form-submitBefore>
  12. <a-button type="primary" preIcon="ant-design:search-outlined" @click="search">查询</a-button>
  13. </template>
  14. </BasicTable>
  15. </template>
  16. <script lang="ts" setup>
  17. import { onMounted, ref, shallowRef } from 'vue';
  18. import { BasicColumn, PaginationProps, BasicTable } from '/@/components/Table';
  19. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  20. import { defaultFormProps, defaultPaginationProps, getDefaultSchemas, defaultTableProps } from './history.data';
  21. import { getDeviceList, list } from './history.api';
  22. import { useListPage } from '/@/hooks/system/useListPage';
  23. const props = withDefaults(
  24. defineProps<{
  25. /** 表格项配置,默认由deviceCode获取且联动dictCode,可以覆写,覆写后将不支持联动,参考BaiscTable */
  26. // columns?: BasicColumn[];
  27. /** 表格操作项配置,默认为空,可以覆写 */
  28. // actionColumns?: BasicColumn;
  29. /** 查询表单项配置,默认联动dictCode,可以覆写,覆写后将不支持联动,提供formProps时此项无效,参考BaiscTable */
  30. // schemas?: FormSchema[];
  31. /** 表格分页配置,可以覆写,参考BaiscTable */
  32. pagination?: PaginationProps;
  33. /** 设备编码,该编码用于从字段/点表配置中读出表头,示例:forcFan */
  34. deviceCode: string;
  35. /** 字典编码,该编码用于从字典配置中读出设备项,示例:forcFan_dict */
  36. dictCode: string;
  37. /** 表格配置,参考BaiscTable,该值会与默认的配置进行浅合并,这里提供的任何配置都是优先的 */
  38. // tableProps?: BasicTableProps;
  39. /** 查询表单配置,参考BaiscTable */
  40. // formProps?: FormProps;
  41. }>(),
  42. {
  43. deviceCode: '',
  44. dictCode: '',
  45. }
  46. );
  47. // 创建表格,此表格目前不具备常用功能,需要初始化后使用(props指定表格配置时除外)
  48. const { tableContext } = useListPage({ tableProps: defaultTableProps });
  49. const [register, { getForm, setLoading, getPaginationRef, setPagination, setProps, getColumns, setColumns }] = tableContext;
  50. /**
  51. * 初始化表格,该方法将根据参数设定新的表头、表单,如果提供了自定义的表头、表单配置则不作操作。
  52. *
  53. * 之所以将设备相关的信息作参数传入,是因为这样可以确认依赖关系,即需要有设备信息之后再初始化表格。
  54. *
  55. * @param deviceCodes 获取表头所用的编码,从左到右依次尝试,直到找到第一个有表头信息的为止
  56. * @param deviceOptions 设备下拉框对应的选项
  57. */
  58. function initTable(deviceCodes: string[], deviceOptions: any[]) {
  59. const defaultSchemas = getDefaultSchemas(props.dictCode, deviceOptions);
  60. let defaultColumns: BasicColumn[] = [];
  61. for (const code of deviceCodes) {
  62. const cols = getTableHeaderColumns(code);
  63. if (cols.length) {
  64. defaultColumns = cols;
  65. break;
  66. }
  67. }
  68. setProps({
  69. columns: defaultColumns,
  70. formConfig: {
  71. ...defaultFormProps,
  72. schemas: defaultSchemas,
  73. },
  74. pagination: props.pagination || defaultPaginationProps,
  75. });
  76. }
  77. /**
  78. * 更新表头,表头默认情况下需要和子设备联动
  79. * @param prefix 子设备的值,即为表头取数据时字段的前缀
  80. */
  81. function updateColumns(prefix: string) {
  82. const cols = getColumns();
  83. setColumns(
  84. cols.map((col) => {
  85. return {
  86. ...col,
  87. dataIndex: col.dataIndex ? `${prefix || ''}${col.dataIndex}` : undefined,
  88. };
  89. })
  90. );
  91. }
  92. // 表格数据相关的字段
  93. const data = shallowRef([]);
  94. /**
  95. * 获取列表的数据
  96. *
  97. * 这些参数确认了依赖关系,即需要有表单数据、设备信息之后再尝试获取表格数据。
  98. *
  99. * @param formData 表格上方的表单数据
  100. * @param deviceCode 设备编码
  101. * @param deviceInfo 设备信息
  102. */
  103. function fetchData(formData: Record<string, unknown>, deviceCode: string, deviceInfo: any) {
  104. setLoading(true);
  105. const pagination = getPaginationRef() as PaginationProps;
  106. return list(deviceCode, deviceInfo, formData, pagination)
  107. .then(({ records, total, current }) => {
  108. setPagination({
  109. current,
  110. total,
  111. });
  112. records.forEach((item) => {
  113. Object.assign(item, item.readData);
  114. });
  115. data.value = records;
  116. })
  117. .finally(() => {
  118. setLoading(false);
  119. });
  120. }
  121. // 设备信息相关的字段
  122. const deviceInfo = ref<Record<string, unknown>>({});
  123. const deviceOptions = ref<Record<string, unknown>[]>([]);
  124. /**
  125. * 获取设备信息列表,初始化设备信息及设备可选项
  126. */
  127. async function fetchDevice() {
  128. const results = await getDeviceList({ devicetype: props.deviceCode, pageSize: 10000 });
  129. const options = results.map((item) => {
  130. return {
  131. label: item.strinstallpos,
  132. value: item.id || item.deviceID,
  133. deviceType: item.strtype || item.deviceType,
  134. devicekind: item.devicekind,
  135. stationtype: item.stationtype,
  136. };
  137. });
  138. deviceOptions.value = options;
  139. deviceInfo.value = results[0];
  140. }
  141. /**
  142. * 搜索,核心方法
  143. *
  144. * 该方法获取表单、处理表单数据后尝试获取数据并设置表头
  145. */
  146. async function search() {
  147. const form = getForm();
  148. await form.validate();
  149. const formData = form.getFieldsValue();
  150. deviceInfo.value = deviceOptions.value.find((opt) => {
  151. return opt.value === formData.gdeviceid;
  152. }) as Record<string, unknown>;
  153. const code = (deviceInfo.value.deviceType || props.deviceCode.concat('*')) as string;
  154. await fetchData(formData, code, deviceInfo.value);
  155. updateColumns(formData.deviceNum);
  156. }
  157. onMounted(async () => {
  158. await fetchDevice();
  159. initTable([deviceInfo.value.deviceType as string, props.deviceCode.concat('_history')], deviceOptions.value);
  160. search();
  161. });
  162. </script>
  163. <style scoped lang="less">
  164. @import '/@/design/vent/color.less';
  165. </style>