From c1b5f6edeacfa0326931d06de6773b936dbabe27 Mon Sep 17 00:00:00 2001 From: maven <2163098428@qq.com> Date: 星期二, 26 八月 2025 15:18:44 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/dev_JLMY' into dev_JLMY --- src/views/production/productionReporting/components/useCoalData.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 96 insertions(+), 0 deletions(-) diff --git a/src/views/production/productionReporting/components/useCoalData.js b/src/views/production/productionReporting/components/useCoalData.js new file mode 100644 index 0000000..9a86b81 --- /dev/null +++ b/src/views/production/productionReporting/components/useCoalData.js @@ -0,0 +1,96 @@ +/** + * 鐓ょ鏁版嵁绠$悊缁勫悎寮忓嚱鏁� + * 鎻愪緵鐓ょ鏁版嵁鐨勮幏鍙栥�佺紦瀛樸�佽浆鎹㈢瓑鍔熻兘 + */ +import {ref, computed, watch} from 'vue'; +import {getCoalInfoList} from "@/api/production"; +import {ElMessage} from 'element-plus'; + +// 鍏ㄥ眬鐓ょ鏁版嵁缂撳瓨 +const coalData = ref([]); +const isLoading = ref(false); +const isLoaded = ref(false); + +export function useCoalData() { + + // 鑾峰彇鐓ょ鏁版嵁 + const getCoalData = async (forceRefresh = false) => { + if (isLoaded.value && !forceRefresh) { + return coalData.value; + } + + if (isLoading.value) { + // 濡傛灉姝e湪鍔犺浇锛岀瓑寰呭姞杞藉畬鎴� + return new Promise((resolve) => { + const unwatch = watch(isLoading, (loading) => { + if (!loading) { + unwatch(); + resolve(coalData.value); + } + }); + }); + } + + isLoading.value = true; + try { + const res = await getCoalInfoList(); + if (res.code === 200) { + coalData.value = res.data; + isLoaded.value = true; + return coalData.value; + } else { + ElMessage.error('鑾峰彇鐓ょ鏁版嵁澶辫触'); + return []; + } + } catch (error) { + ElMessage.error('鑾峰彇鐓ょ鏁版嵁澶辫触'); + console.error('鐓ょ鏁版嵁鑾峰彇閿欒:', error); + return []; + } finally { + isLoading.value = false; + } + }; + + // 鏍规嵁ID鑾峰彇鐓ょ鍚嶇О + const getCoalNameById = (id) => { + if (!id || coalData.value.length === 0) return id; + const coal = coalData.value.find(item => item.id == id); + return coal ? coal.coal : id; + }; + + // 鏍规嵁鍚嶇О鑾峰彇鐓ょID + const getCoalIdByName = (name) => { + if (!name || coalData.value.length === 0) return ''; + const coal = coalData.value.find(item => item.coal === name); + return coal ? coal.id : ''; + }; + + // 鐢熸垚涓嬫媺閫夐」 + const coalOptions = computed(() => { + return coalData.value.map(item => ({ + label: item.coal, + value: item.coal, + key: item.id + })); + }); + + // 鐢熸垚key-value鏄犲皠 + const coalMap = computed(() => { + const map = {}; + coalData.value.forEach(item => { + map[item.id] = item.coal; + }); + return map; + }); + + return { + coalData: computed(() => coalData.value), + coalOptions, + coalMap, + isLoading: computed(() => isLoading.value), + isLoaded: computed(() => isLoaded.value), + getCoalData, + getCoalNameById, + getCoalIdByName + }; +} -- Gitblit v1.9.3