HistoryTable.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, BasicTableProps, PaginationProps, FormProps, FormSchema, 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 defaultSchemas = getDefaultSchemas(props.dictCode);
  49. const defaultColumns = getTableHeaderColumns(props.deviceCode.concat('_history'));
  50. const { tableContext } = useListPage({
  51. tableProps: {
  52. ...defaultTableProps,
  53. columns: props.columns || defaultColumns,
  54. actionColumn: props.actionColumns,
  55. showActionColumn: Boolean(props.actionColumns),
  56. formConfig: props.formProps || {
  57. ...defaultFormProps,
  58. schemas: props.schemas || defaultSchemas,
  59. },
  60. pagination: props.pagination || defaultPaginationProps,
  61. ...props.tableProps,
  62. },
  63. });
  64. const [register, { getForm, setLoading, setColumns, getPaginationRef, setPagination }] = tableContext;
  65. // 表格数据相关
  66. const data = shallowRef([]);
  67. // 搜索
  68. async function search() {
  69. const form = getForm();
  70. await form.validate();
  71. await fetchData(form.getFieldsValue());
  72. }
  73. // 分站类型,不同的分站类型对应不同的方式获取历史数据
  74. const deviceInfo = ref({});
  75. // 获取设备列表信息,初始化一些信息
  76. async function fetchDevice() {
  77. const results = await getDeviceList({ devicetype: props.deviceCode, pageSize: 10000 });
  78. const id = results[0].id || results[0].deviceID;
  79. deviceInfo.value = results[0];
  80. await getForm().setFieldsValue({ gdeviceid: id });
  81. }
  82. // 核心,获取表格的数据
  83. function fetchData(formData: Record<string, unknown>) {
  84. setLoading(true);
  85. const pagination = getPaginationRef() as PaginationProps;
  86. list(
  87. props.deviceCode,
  88. deviceInfo,
  89. {
  90. ...formData,
  91. pageNo: pagination.current,
  92. pageSize: pagination.pageSize,
  93. column: 'createTime',
  94. strtype: props.deviceCode + '*',
  95. isEmployee: props.deviceCode.startsWith('vehicle') ? false : true,
  96. },
  97. pagination
  98. )
  99. .then(({ records, total, current }) => {
  100. // 表格的列需要默认情况下需要和设备列表联动
  101. defaultColumns.forEach((col) => {
  102. col.dataIndex = `${formData.deviceid}${col.dataIndex}`;
  103. });
  104. setColumns(defaultColumns);
  105. setPagination({
  106. current,
  107. total,
  108. });
  109. data.value = records;
  110. })
  111. .finally(() => {
  112. setLoading(false);
  113. });
  114. }
  115. onMounted(() => {
  116. fetchDevice();
  117. search();
  118. });
  119. </script>
  120. <style scoped lang="less">
  121. @import '/@/design/vent/color.less';
  122. </style>