useInit.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.deviceID === 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: { deviceID: string }[] = result[devicekind];
  82. devices.value = records;
  83. selectedDeviceID.value = records[0]?.deviceID;
  84. options.value = records.map((e) => {
  85. return {
  86. label: getFormattedText(e, value),
  87. value: e.deviceID,
  88. };
  89. });
  90. } else {
  91. // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  92. const record = {
  93. ...result,
  94. deviceID: 'isthisthereallife',
  95. };
  96. devices.value = [record];
  97. selectedDeviceID.value = record.deviceID;
  98. }
  99. });
  100. }
  101. return {
  102. fetchDevices,
  103. selectedDevice,
  104. selectedDeviceID,
  105. selectedDeviceSlot,
  106. selectedDeviceLabel,
  107. options,
  108. };
  109. }
  110. export function useInitScene(scenekind: string) {
  111. const scene = ref<Record<string, any>[]>([]);
  112. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  113. function fetchScene() {
  114. return getHomeData({}).then((result) => {
  115. if (!result[scenekind]) return;
  116. // 如果数据只有一条,转为数据
  117. const records: { strinstallpos: string; deviceID: string }[] = result[scenekind];
  118. scene.value = records;
  119. });
  120. }
  121. return {
  122. fetchScene,
  123. scene,
  124. };
  125. }