useInit.ts 9.6 KB

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