useInit.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 { getData, getFormattedText } from './helper';
  6. import { get, isNil } from 'lodash-es';
  7. interface EnhancedConfig extends Config {
  8. visible: boolean;
  9. }
  10. // import mapComponent from './components/3Dmap/index.vue';
  11. // export function useInitConfig(deviceType: string) {
  12. // function fetchConfig() {
  13. // cfgList({
  14. // deviceType,
  15. // }).then(({ records }) => {
  16. // config.value = records[0];
  17. // });
  18. // }
  19. // const config = ref<Partial<Config>>({});
  20. // return {
  21. // fetchConfig,
  22. // config,
  23. // };
  24. // }
  25. /** 初始化配置以及根据配置获取数据 */
  26. export function useInitConfigs() {
  27. const configs = ref<Config[]>([]);
  28. const isOriginal = computed(() => {
  29. return configs.value.some((c) => {
  30. return c.showStyle.version === '原版';
  31. });
  32. });
  33. const isCommon = computed(() => {
  34. return configs.value.some((c) => {
  35. return c.showStyle.version === '普通版';
  36. });
  37. });
  38. const isBD = computed(() => {
  39. return configs.value.some((c) => {
  40. return c.showStyle.version === '保德';
  41. });
  42. });
  43. const devicesTypes = computed(() => {
  44. return configs.value
  45. .map((e) => e.deviceType)
  46. .filter((e) => {
  47. // 过滤不为空且 string 有内容的 deviceType
  48. return !isNil(e) && !!e.length;
  49. });
  50. });
  51. function fetchConfigs(pageType?: string) {
  52. return cfgList({ pageType }).then(({ records }) => {
  53. configs.value = records;
  54. return records;
  55. });
  56. }
  57. return {
  58. fetchConfigs,
  59. configs,
  60. devicesTypes,
  61. isOriginal,
  62. isCommon,
  63. isBD,
  64. };
  65. }
  66. /** 初始化设备信息,包含了适配了 header config 的下拉框选项、已选择设备的各个详细子项等,如果模块不需要展示 header 那么会将全部信息提供给已选择设备以供消费 */
  67. // export function useInitDevices(devicekind: string, config: Config['moduleData']) {
  68. // const { header, mock } = config;
  69. // const devices = ref<Record<string, any>[]>([]);
  70. // const options = ref<{ label: string; value: string }[]>([]);
  71. // const selectedDeviceID = ref<string>('');
  72. // const selectedDevice = computed(() => {
  73. // return (
  74. // devices.value.find((e) => {
  75. // return e.id === selectedDeviceID.value;
  76. // }) || {}
  77. // );
  78. // });
  79. // const selectedDeviceLabel = computed(() => {
  80. // const res = options.value.find((e) => {
  81. // return e.value === selectedDeviceID.value;
  82. // });
  83. // return res ? res.label : '';
  84. // });
  85. // const selectedDeviceSlot = computed(() => {
  86. // return getFormattedText(selectedDevice.value, header.slot.value);
  87. // });
  88. // // 获取设备数据,赋值并以选项格式返回给 Header 消费
  89. // function fetchDevices({ init = false } = {}) {
  90. // const { value } = header.selector;
  91. // const promise = mock ? Promise.resolve(mock) : getHomeData({});
  92. // return promise.then((result) => {
  93. // if (header.show && header.showSelector) {
  94. // // 如果配置里指明需要 header,检验后初始化设备信息
  95. // const records: Record<string, any>[] = get(result, devicekind, []);
  96. // devices.value = records.map((e, i) => {
  97. // return {
  98. // id: i,
  99. // ...e,
  100. // };
  101. // });
  102. // options.value = devices.value.map((e) => {
  103. // return {
  104. // label: getFormattedText(e, value),
  105. // value: e.id,
  106. // };
  107. // });
  108. // if (init) {
  109. // selectedDeviceID.value = options.value[0]?.value;
  110. // }
  111. // } else {
  112. // // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  113. // const record = {
  114. // ...result,
  115. // id: '00000000',
  116. // };
  117. // devices.value = [record];
  118. // selectedDeviceID.value = record.id;
  119. // }
  120. // });
  121. // }
  122. // return {
  123. // fetchDevices,
  124. // selectedDevice,
  125. // selectedDeviceID,
  126. // selectedDeviceSlot,
  127. // selectedDeviceLabel,
  128. // options,
  129. // };
  130. // }
  131. /** 保德专用-初始化设备信息,包含了适配了 header config 的下拉框选项、已选择设备的各个详细子项等,如果模块不需要展示 header 那么会将全部信息提供给已选择设备以供消费 */
  132. // export function useInitDevicesBD(devicekind: string, config: Config['moduleData']) {
  133. // const { header, mock } = config;
  134. // const devices = ref<Record<string, any>[]>([]);
  135. // const options = ref<{ label: string; value: string }[]>([]);
  136. // const selectedDeviceID = ref<string>('');
  137. // const selectedDevice = computed(() => {
  138. // return (
  139. // devices.value.find((e) => {
  140. // return e.id === selectedDeviceID.value;
  141. // }) || {}
  142. // );
  143. // });
  144. // const selectedDeviceLabel = computed(() => {
  145. // const res = options.value.find((e) => {
  146. // return e.value === selectedDeviceID.value;
  147. // });
  148. // return res ? res.label : '';
  149. // });
  150. // const selectedDeviceSlot = computed(() => {
  151. // return getFormattedText(selectedDevice.value, header.slot.value);
  152. // });
  153. // // 获取设备数据,赋值并以选项格式返回给 Header 消费
  154. // function fetchDevices({ init = false } = {}) {
  155. // const { value } = header.selector;
  156. // const promise = mock ? Promise.resolve(mock) : getDisHome({ dataList: devicekind.split('.')[0] });
  157. // return promise.then((result) => {
  158. // if (header.show && header.showSelector) {
  159. // // 如果配置里指明需要 header,检验后初始化设备信息
  160. // const records: Record<string, any>[] = get(result, devicekind, []);
  161. // devices.value = records.map((e, i) => {
  162. // return {
  163. // id: i,
  164. // ...e,
  165. // };
  166. // });
  167. // options.value = devices.value.map((e) => {
  168. // return {
  169. // label: getFormattedText(e, value),
  170. // value: e.id,
  171. // };
  172. // });
  173. // if (init) {
  174. // selectedDeviceID.value = options.value[0]?.value;
  175. // }
  176. // } else {
  177. // // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  178. // const record = {
  179. // ...result,
  180. // id: '00000000',
  181. // };
  182. // devices.value = [record];
  183. // selectedDeviceID.value = record.id;
  184. // }
  185. // });
  186. // }
  187. // return {
  188. // fetchDevices,
  189. // selectedDevice,
  190. // selectedDeviceID,
  191. // selectedDeviceSlot,
  192. // selectedDeviceLabel,
  193. // options,
  194. // };
  195. // }
  196. /**
  197. * 根据模块的配置初始化基准 Module 组件需要的数据,包括选择框、设备等信息
  198. * @param deviceType 本模块的设备类型
  199. * @param moduleData 本模块的配置项
  200. */
  201. export function useInitModule(deviceType: Config['deviceType'], moduleData: Config['moduleData']) {
  202. const { header, mock } = moduleData;
  203. const devices = ref<Record<string, any>[]>([]);
  204. const options = ref<{ label: string; value: string }[]>([]);
  205. const selectedDeviceID = ref<string>('');
  206. const selectedDevice = computed(() => {
  207. return (
  208. devices.value.find((e) => {
  209. return e.id === selectedDeviceID.value;
  210. }) || {}
  211. );
  212. });
  213. const selectedDeviceLabel = computed(() => {
  214. const res = options.value.find((e) => {
  215. return e.value === selectedDeviceID.value;
  216. });
  217. return res ? res.label : '';
  218. });
  219. const selectedDeviceSlot = computed(() => {
  220. return getFormattedText(selectedDevice.value, header.slot.value, header.slot.trans);
  221. });
  222. function init(data) {
  223. const result = mock || data;
  224. if (header.show && header.selector.show) {
  225. // 如果配置里指明需要 header,检验后初始化设备信息
  226. const records: Record<string, any>[] = getData(get(result, deviceType, []), header.readFrom) || [];
  227. devices.value = records.map((e, i) => {
  228. return {
  229. id: i,
  230. ...e,
  231. };
  232. });
  233. options.value = devices.value.map((e) => {
  234. return {
  235. label: getFormattedText(e, header.selector.value),
  236. value: e.id,
  237. };
  238. });
  239. } else {
  240. // 没有的话按默认的,将返回结果直接作为一整个设备信息供模块使用
  241. const record = {
  242. id: '00000000',
  243. ...result,
  244. };
  245. devices.value = [record];
  246. options.value = [
  247. {
  248. value: record.id,
  249. label: '--',
  250. },
  251. ];
  252. }
  253. }
  254. return {
  255. init,
  256. selectedDevice,
  257. selectedDeviceID,
  258. selectedDeviceSlot,
  259. selectedDeviceLabel,
  260. options,
  261. };
  262. }
  263. // 初始化各个基准首页的辅助字段
  264. export function useInitPage(title: string) {
  265. const mainTitle = ref(title);
  266. const enhancedConfigs = ref<EnhancedConfig[]>([]);
  267. const data = ref<any>({});
  268. const hiddenList = computed(() => {
  269. return enhancedConfigs.value.filter((e) => e.visible === false);
  270. });
  271. function updateEnhancedConfigs(configs: Config[]) {
  272. enhancedConfigs.value = configs.map((c) => {
  273. return {
  274. visible: true,
  275. ...c,
  276. };
  277. });
  278. }
  279. function updateData(d: any) {
  280. data.value = d;
  281. }
  282. return {
  283. mainTitle,
  284. data,
  285. enhancedConfigs,
  286. hiddenList,
  287. updateEnhancedConfigs,
  288. updateData,
  289. };
  290. }