useSystemSelect.ts 3.7 KB

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