useInit.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(pageType?: string) {
  40. return cfgList({ pageType }).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']) {
  54. const { header, mock } = config;
  55. const devices = ref<Record<string, any>[]>([]);
  56. const options = ref<{ label: string; value: string }[]>([]);
  57. const selectedDeviceID = ref<string>('');
  58. const selectedDevice = computed(() => {
  59. return (
  60. devices.value.find((e) => {
  61. return e.id === selectedDeviceID.value;
  62. }) || {}
  63. );
  64. });
  65. const selectedDeviceLabel = computed(() => {
  66. const res = options.value.find((e) => {
  67. return e.value === selectedDeviceID.value;
  68. });
  69. return res ? res.label : '';
  70. });
  71. const selectedDeviceSlot = computed(() => {
  72. return getFormattedText(selectedDevice.value, header.slot.value);
  73. });
  74. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  75. function fetchDevices() {
  76. const { value } = header.selector;
  77. const promise = mock ? Promise.resolve(mock) : getHomeData({});
  78. return promise.then((result) => {
  79. if (header.show && header.showSelector && result[devicekind]) {
  80. // 如果配置里指明需要 header,检验后初始化设备信息
  81. const records: Record<string, any>[] = result[devicekind];
  82. devices.value = records.map((e, i) => {
  83. return {
  84. id: i,
  85. ...e,
  86. };
  87. });
  88. options.value = devices.value.map((e) => {
  89. return {
  90. label: getFormattedText(e, value),
  91. value: e.id,
  92. };
  93. });
  94. selectedDeviceID.value = options.value[0]?.value;
  95. } else {
  96. // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  97. const record = {
  98. ...result,
  99. id: '00000000',
  100. };
  101. devices.value = [record];
  102. selectedDeviceID.value = record.id;
  103. }
  104. });
  105. }
  106. return {
  107. fetchDevices,
  108. selectedDevice,
  109. selectedDeviceID,
  110. selectedDeviceSlot,
  111. selectedDeviceLabel,
  112. options,
  113. };
  114. }
  115. export function useInitScene(scenekind: string) {
  116. const scene = ref<Record<string, any>[]>([]);
  117. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  118. function fetchScene() {
  119. return getHomeData({}).then((result) => {
  120. if (!result[scenekind]) return;
  121. // 如果数据只有一条,转为数据
  122. const records: { strinstallpos: string; id: string }[] = result[scenekind];
  123. scene.value = records;
  124. });
  125. }
  126. return {
  127. fetchScene,
  128. scene,
  129. };
  130. }