useInit.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 { getDisHome, getHomeData } from '../configurable.api';
  6. import { getFormattedText } from '../../../deviceManager/configurationTable/adapters';
  7. import { get } from 'lodash-es';
  8. // import mapComponent from './components/3Dmap/index.vue';
  9. // export function useInitConfig(deviceType: string) {
  10. // function fetchConfig() {
  11. // cfgList({
  12. // deviceType,
  13. // }).then(({ records }) => {
  14. // config.value = records[0];
  15. // });
  16. // }
  17. // const config = ref<Partial<Config>>({});
  18. // return {
  19. // fetchConfig,
  20. // config,
  21. // };
  22. // }
  23. /** 初始化配置以及根据配置获取数据 */
  24. export function useInitConfigs() {
  25. const configs = ref<Config[]>([]);
  26. const isOriginal = computed(() => {
  27. return configs.value.some((c) => {
  28. return c.showStyle.version === '原版';
  29. });
  30. });
  31. const isCommon = computed(() => {
  32. return configs.value.some((c) => {
  33. return c.showStyle.version === '普通版';
  34. });
  35. });
  36. const isBD = computed(() => {
  37. return configs.value.some((c) => {
  38. return c.showStyle.version === '保德';
  39. });
  40. });
  41. function fetchConfigs(pageType?: string) {
  42. return cfgList({ pageType }).then(({ records }) => {
  43. configs.value = records;
  44. return records;
  45. });
  46. }
  47. return {
  48. fetchConfigs,
  49. configs,
  50. isOriginal,
  51. isCommon,
  52. isBD,
  53. };
  54. }
  55. /** 初始化设备信息,包含了适配了 header config 的下拉框选项、已选择设备的各个详细子项等,如果模块不需要展示 header 那么会将全部信息提供给已选择设备以供消费 */
  56. export function useInitDevices(devicekind: string, config: Config['moduleData']) {
  57. const { header, mock } = config;
  58. const devices = ref<Record<string, any>[]>([]);
  59. const options = ref<{ label: string; value: string }[]>([]);
  60. const selectedDeviceID = ref<string>('');
  61. const selectedDevice = computed(() => {
  62. return (
  63. devices.value.find((e) => {
  64. return e.id === selectedDeviceID.value;
  65. }) || {}
  66. );
  67. });
  68. const selectedDeviceLabel = computed(() => {
  69. const res = options.value.find((e) => {
  70. return e.value === selectedDeviceID.value;
  71. });
  72. return res ? res.label : '';
  73. });
  74. const selectedDeviceSlot = computed(() => {
  75. return getFormattedText(selectedDevice.value, header.slot.value);
  76. });
  77. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  78. function fetchDevices({ init = false } = {}) {
  79. const { value } = header.selector;
  80. const promise = mock ? Promise.resolve(mock) : getHomeData({});
  81. return promise.then((result) => {
  82. if (header.show && header.showSelector) {
  83. // 如果配置里指明需要 header,检验后初始化设备信息
  84. const records: Record<string, any>[] = get(result, devicekind, []);
  85. devices.value = records.map((e, i) => {
  86. return {
  87. id: i,
  88. ...e,
  89. };
  90. });
  91. options.value = devices.value.map((e) => {
  92. return {
  93. label: getFormattedText(e, value),
  94. value: e.id,
  95. };
  96. });
  97. if (init) {
  98. selectedDeviceID.value = options.value[0]?.value;
  99. }
  100. } else {
  101. // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  102. const record = {
  103. ...result,
  104. id: '00000000',
  105. };
  106. devices.value = [record];
  107. selectedDeviceID.value = record.id;
  108. }
  109. });
  110. }
  111. return {
  112. fetchDevices,
  113. selectedDevice,
  114. selectedDeviceID,
  115. selectedDeviceSlot,
  116. selectedDeviceLabel,
  117. options,
  118. };
  119. }
  120. /** 保德专用-初始化设备信息,包含了适配了 header config 的下拉框选项、已选择设备的各个详细子项等,如果模块不需要展示 header 那么会将全部信息提供给已选择设备以供消费 */
  121. export function useInitDevicesBD(devicekind: string, config: Config['moduleData']) {
  122. const { header, mock } = config;
  123. const devices = ref<Record<string, any>[]>([]);
  124. const options = ref<{ label: string; value: string }[]>([]);
  125. const selectedDeviceID = ref<string>('');
  126. const selectedDevice = computed(() => {
  127. return (
  128. devices.value.find((e) => {
  129. return e.id === selectedDeviceID.value;
  130. }) || {}
  131. );
  132. });
  133. const selectedDeviceLabel = computed(() => {
  134. const res = options.value.find((e) => {
  135. return e.value === selectedDeviceID.value;
  136. });
  137. return res ? res.label : '';
  138. });
  139. const selectedDeviceSlot = computed(() => {
  140. return getFormattedText(selectedDevice.value, header.slot.value);
  141. });
  142. // 获取设备数据,赋值并以选项格式返回给 Header 消费
  143. function fetchDevices({ init = false } = {}) {
  144. const { value } = header.selector;
  145. const promise = mock ? Promise.resolve(mock) : getDisHome({ dataList: devicekind.split('.')[0] });
  146. return promise.then((result) => {
  147. if (header.show && header.showSelector) {
  148. // 如果配置里指明需要 header,检验后初始化设备信息
  149. const records: Record<string, any>[] = get(result, devicekind, []);
  150. devices.value = records.map((e, i) => {
  151. return {
  152. id: i,
  153. ...e,
  154. };
  155. });
  156. options.value = devices.value.map((e) => {
  157. return {
  158. label: getFormattedText(e, value),
  159. value: e.id,
  160. };
  161. });
  162. if (init) {
  163. selectedDeviceID.value = options.value[0]?.value;
  164. }
  165. } else {
  166. // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  167. const record = {
  168. ...result,
  169. id: '00000000',
  170. };
  171. devices.value = [record];
  172. selectedDeviceID.value = record.id;
  173. }
  174. });
  175. }
  176. return {
  177. fetchDevices,
  178. selectedDevice,
  179. selectedDeviceID,
  180. selectedDeviceSlot,
  181. selectedDeviceLabel,
  182. options,
  183. };
  184. }
  185. /**
  186. * 根据模块的配置初始化基准 Module 组件需要的数据,包括选择框、设备等信息
  187. * @param apidata api 返回的数据
  188. * @param config 本模块的配置
  189. */
  190. // export function useInitModule(moduleData: Config['moduleData'], apidata?: any) {
  191. // const { header, mock } = moduleData;
  192. // const data = mock || apidata;
  193. // const devices = ref<Record<string, any>[]>([]);
  194. // const options = ref<{ label: string; value: string }[]>([]);
  195. // const selectedDeviceID = ref<string>('');
  196. // const selectedDevice = computed(() => {
  197. // return (
  198. // devices.value.find((e) => {
  199. // return e.id === selectedDeviceID.value;
  200. // }) || {}
  201. // );
  202. // });
  203. // const selectedDeviceLabel = computed(() => {
  204. // const res = options.value.find((e) => {
  205. // return e.value === selectedDeviceID.value;
  206. // });
  207. // return res ? res.label : '';
  208. // });
  209. // const selectedDeviceSlot = computed(() => {
  210. // return getFormattedText(selectedDevice.value, header.slot.value);
  211. // });
  212. // }