useInit.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { computed, ref } from 'vue';
  2. import { list as cfgList } from '@/views/vent/deviceManager/configurationTable/configuration.api';
  3. // import { list } from '@/views/vent/deviceManager/deviceTable/device.api';
  4. import { Config } from '@/views/vent/deviceManager/configurationTable/types';
  5. import { getHomeData } from '../configurable.api';
  6. import { getFormattedText } from '../../../deviceManager/configurationTable/adapters';
  7. // import mapComponent from './components/3Dmap/index.vue';
  8. // export function useInitConfig(deviceType: string) {
  9. // function fetchConfig() {
  10. // cfgList({
  11. // deviceType,
  12. // }).then(({ records }) => {
  13. // config.value = records[0];
  14. // });
  15. // }
  16. // const config = ref<Partial<Config>>({});
  17. // return {
  18. // fetchConfig,
  19. // config,
  20. // };
  21. // }
  22. export function useInitConfigs() {
  23. const configs = ref<Config[]>([]);
  24. const isOriginal = computed(() => {
  25. return configs.value.some((c) => {
  26. return c.showStyle.version === '原版';
  27. });
  28. });
  29. const isCommon = computed(() => {
  30. return configs.value.some((c) => {
  31. return c.showStyle.version === '普通版';
  32. });
  33. });
  34. function fetchConfigs() {
  35. return cfgList({}).then(({ records }) => {
  36. configs.value = records;
  37. });
  38. }
  39. return {
  40. fetchConfigs,
  41. configs,
  42. isOriginal,
  43. isCommon,
  44. };
  45. }
  46. /** 初始化设备信息,包含了适配了 header config 的下拉框选项、已选择设备的各个详细子项等,如果模块不需要展示 header 那么会将全部信息提供给已选择设备以供消费 */
  47. export function useInitDevices(devicekind: string, config: Config['moduleData']['header']) {
  48. const devices = ref<Record<string, any>[]>([]);
  49. const options = ref<{ label: string; value: string }[]>([]);
  50. const selectedDeviceID = ref<string>('');
  51. const selectedDevice = computed(() => {
  52. return (
  53. devices.value.find((e) => {
  54. return e.deviceID === selectedDeviceID.value;
  55. }) || {}
  56. );
  57. });
  58. const selectedDeviceLabel = computed(() => {
  59. const res = options.value.find((e) => {
  60. return e.value === selectedDeviceID.value;
  61. });
  62. return res ? res.label : '';
  63. });
  64. const selectedDeviceSlot = computed(() => {
  65. return getFormattedText(selectedDevice.value, config.slot.prop, config.slot.formatter);
  66. });
  67. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  68. function fetchDevices() {
  69. const { formatter, prop } = config.selector;
  70. return getHomeData({}).then((result) => {
  71. if (!result[devicekind]) return;
  72. if (config.show && config.showSelector) {
  73. const records: { deviceID: string }[] = result[devicekind];
  74. devices.value = records;
  75. selectedDeviceID.value = records[0]?.deviceID;
  76. options.value = records.map((e) => {
  77. return {
  78. label: getFormattedText(e, prop, formatter),
  79. value: e.deviceID,
  80. };
  81. });
  82. } else {
  83. const record = result;
  84. devices.value = [record];
  85. selectedDeviceID.value = record.deviceID;
  86. }
  87. });
  88. }
  89. return {
  90. fetchDevices,
  91. selectedDevice,
  92. selectedDeviceID,
  93. selectedDeviceSlot,
  94. selectedDeviceLabel,
  95. options,
  96. };
  97. }
  98. export function useInitScene(scenekind: string) {
  99. const scene = ref<Record<string, any>[]>([]);
  100. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  101. function fetchScene() {
  102. return getHomeData({}).then((result) => {
  103. if (!result[scenekind]) return;
  104. // 如果数据只有一条,转为数据
  105. const records: { strinstallpos: string; deviceID: string }[] = result[scenekind];
  106. scene.value = records;
  107. });
  108. }
  109. return {
  110. fetchScene,
  111. scene,
  112. };
  113. }