useSystemSelect.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { ref, nextTick } from 'vue';
  2. import { defHttp } from '/@/utils/http/axios';
  3. type DeviceType = { deviceType: string; deviceName: string; datalist: any[] };
  4. type OptionType = { id: string; strsystype: string; systemname: string };
  5. const getTableList = (params) => defHttp.get({ url: '/safety/ventanalyManageSystem/list', params });
  6. const systemList = (params) => defHttp.post({ url: '/monitor/device', params });
  7. /**
  8. * 用于setup关于系统设备下拉框相关的内容
  9. * @param sysType 对应的设备/类型码,可在设备管理 -> 设备类型管理里查阅
  10. * @param changeModalType 即 optionValue(选择了的设备)变化后的回调,参数为该 option
  11. */
  12. export function useSystemSelect(sysType: string, changeModalType?: (param) => {}) {
  13. const options = ref<OptionType[]>([]);
  14. const optionValue = ref('');
  15. const deviceList = ref<DeviceType[]>([]);
  16. const deviceActive = ref('');
  17. const deviceType = ref('');
  18. const deviceValue = ref({});
  19. const isRefresh = ref(true);
  20. async function getDeviceList() {
  21. const res = await systemList({ devicetype: 'sys', systemID: optionValue.value });
  22. const result = res.msgTxt;
  23. const deviceArr = <DeviceType[]>[];
  24. result.forEach((item) => {
  25. const data = item['datalist'].filter((data: any) => {
  26. const readData = data.readData;
  27. return Object.assign(data, readData);
  28. });
  29. if (item.type != 'sys') {
  30. deviceArr.unshift({
  31. deviceType: item.type,
  32. deviceName: item['typeName'] ? item['typeName'] : item['datalist'][0]['typeName'],
  33. datalist: data,
  34. });
  35. }
  36. });
  37. deviceList.value = deviceArr;
  38. deviceActive.value = deviceArr[0].deviceType;
  39. deviceChange(0);
  40. }
  41. async function getSysDataSource() {
  42. const res = await getTableList({ strtype: sysType, pagetype: 'normal' });
  43. if (options.value.length == 0) {
  44. // 初始时选择第一条数据
  45. options.value = res.records || [];
  46. if (!optionValue.value) {
  47. optionValue.value = options.value[0]['id'];
  48. getDeviceList();
  49. if (changeModalType) changeModalType(options.value[0]);
  50. }
  51. }
  52. }
  53. function deviceChange(index) {
  54. deviceValue.value = deviceList.value[index];
  55. deviceActive.value = deviceType.value = deviceValue.value['deviceType'];
  56. isRefresh.value = false;
  57. nextTick(() => {
  58. isRefresh.value = true;
  59. });
  60. }
  61. async function getSelectRow(deviceID) {
  62. const currentData = options.value.find((item: any) => {
  63. return item.id == deviceID;
  64. });
  65. optionValue.value = deviceID;
  66. if (changeModalType) changeModalType(currentData);
  67. getDeviceList();
  68. }
  69. return {
  70. /** 下拉框选项 */
  71. options,
  72. /** 下拉框目前选择的值 */
  73. optionValue,
  74. /** 设备列表 */
  75. deviceList,
  76. deviceActive,
  77. deviceType,
  78. isRefresh,
  79. /** 选中了的具体设备 */
  80. deviceValue,
  81. getSysDataSource,
  82. getSelectRow,
  83. deviceChange,
  84. };
  85. }