useInit.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. const isBD = computed(() => {
  35. return configs.value.some((c) => {
  36. return c.showStyle.version === '保德';
  37. });
  38. });
  39. function fetchConfigs() {
  40. return cfgList({}).then(({ records }) => {
  41. configs.value = records;
  42. });
  43. }
  44. return {
  45. fetchConfigs,
  46. configs,
  47. isOriginal,
  48. isCommon,
  49. isBD,
  50. };
  51. }
  52. /** 初始化设备信息,包含了适配了 header config 的下拉框选项、已选择设备的各个详细子项等,如果模块不需要展示 header 那么会将全部信息提供给已选择设备以供消费 */
  53. export function useInitDevices(devicekind: string, config: Config['moduleData']['header']) {
  54. const devices = ref<Record<string, any>[]>([]);
  55. const options = ref<{ label: string; value: string }[]>([]);
  56. const selectedDeviceID = ref<string>('');
  57. const selectedDevice = computed(() => {
  58. return (
  59. devices.value.find((e) => {
  60. return e.deviceID === selectedDeviceID.value;
  61. }) || {}
  62. );
  63. });
  64. const selectedDeviceLabel = computed(() => {
  65. const res = options.value.find((e) => {
  66. return e.value === selectedDeviceID.value;
  67. });
  68. return res ? res.label : '';
  69. });
  70. const selectedDeviceSlot = computed(() => {
  71. return getFormattedText(selectedDevice.value, config.slot.value);
  72. });
  73. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  74. function fetchDevices() {
  75. const { value } = config.selector;
  76. return getHomeData({}).then((result) => {
  77. if (config.show && config.showSelector && result[devicekind]) {
  78. // 如果配置里指明需要 header,检验后初始化设备信息
  79. const records: { deviceID: string }[] = result[devicekind];
  80. devices.value = records;
  81. selectedDeviceID.value = records[0]?.deviceID;
  82. options.value = records.map((e) => {
  83. return {
  84. label: getFormattedText(e, value),
  85. value: e.deviceID,
  86. };
  87. });
  88. } else {
  89. // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  90. const record = {
  91. ...result,
  92. deviceID: 'isthisthereallife',
  93. };
  94. devices.value = [record];
  95. selectedDeviceID.value = record.deviceID;
  96. }
  97. });
  98. }
  99. return {
  100. fetchDevices,
  101. selectedDevice,
  102. selectedDeviceID,
  103. selectedDeviceSlot,
  104. selectedDeviceLabel,
  105. options,
  106. };
  107. }
  108. export function useInitScene(scenekind: string) {
  109. const scene = ref<Record<string, any>[]>([]);
  110. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  111. function fetchScene() {
  112. return getHomeData({}).then((result) => {
  113. if (!result[scenekind]) return;
  114. // 如果数据只有一条,转为数据
  115. const records: { strinstallpos: string; deviceID: string }[] = result[scenekind];
  116. scene.value = records;
  117. });
  118. }
  119. return {
  120. fetchScene,
  121. scene,
  122. };
  123. }