import { computed, ref } from 'vue'; import { list as cfgList } from '@/views/vent/deviceManager/configurationTable/configuration.api'; // import { list } from '@/views/vent/deviceManager/deviceTable/device.api'; import { Config } from '@/views/vent/deviceManager/configurationTable/types'; import { getHomeData } from '../configurable.api'; import { getFormattedText } from '../../../deviceManager/configurationTable/adapters'; // import mapComponent from './components/3Dmap/index.vue'; // export function useInitConfig(deviceType: string) { // function fetchConfig() { // cfgList({ // deviceType, // }).then(({ records }) => { // config.value = records[0]; // }); // } // const config = ref>({}); // return { // fetchConfig, // config, // }; // } export function useInitConfigs() { const configs = ref([]); const isOriginal = computed(() => { return configs.value.some((c) => { return c.showStyle.version === '原版'; }); }); const isCommon = computed(() => { return configs.value.some((c) => { return c.showStyle.version === '普通版'; }); }); const isBD = computed(() => { return configs.value.some((c) => { return c.showStyle.version === '保德'; }); }); function fetchConfigs(pageType?: string) { return cfgList({ pageType }).then(({ records }) => { configs.value = records; }); } return { fetchConfigs, configs, isOriginal, isCommon, isBD, }; } /** 初始化设备信息,包含了适配了 header config 的下拉框选项、已选择设备的各个详细子项等,如果模块不需要展示 header 那么会将全部信息提供给已选择设备以供消费 */ export function useInitDevices(devicekind: string, config: Config['moduleData']) { const { header, mock } = config; const devices = ref[]>([]); const options = ref<{ label: string; value: string }[]>([]); const selectedDeviceID = ref(''); const selectedDevice = computed(() => { return ( devices.value.find((e) => { return e.id === selectedDeviceID.value; }) || {} ); }); const selectedDeviceLabel = computed(() => { const res = options.value.find((e) => { return e.value === selectedDeviceID.value; }); return res ? res.label : ''; }); const selectedDeviceSlot = computed(() => { return getFormattedText(selectedDevice.value, header.slot.value); }); // 获取设备数据,赋值并以选项格式返回给 Header 消费 function fetchDevices() { const { value } = header.selector; const promise = mock ? Promise.resolve(mock) : getHomeData({}); return promise.then((result) => { if (header.show && header.showSelector && result[devicekind]) { // 如果配置里指明需要 header,检验后初始化设备信息 const records: Record[] = result[devicekind]; devices.value = records.map((e, i) => { return { id: i, ...e, }; }); options.value = devices.value.map((e) => { return { label: getFormattedText(e, value), value: e.id, }; }); selectedDeviceID.value = options.value[0]?.value; } else { // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用 const record = { ...result, id: '00000000', }; devices.value = [record]; selectedDeviceID.value = record.id; } }); } return { fetchDevices, selectedDevice, selectedDeviceID, selectedDeviceSlot, selectedDeviceLabel, options, }; } export function useInitScene(scenekind: string) { const scene = ref[]>([]); // 获取设备数据,赋值并以选项格式返回给 Header 消费 function fetchScene() { return getHomeData({}).then((result) => { if (!result[scenekind]) return; // 如果数据只有一条,转为数据 const records: { strinstallpos: string; id: string }[] = result[scenekind]; scene.value = records; }); } return { fetchScene, scene, }; }