WholeProcessPlatform/frontend/src/store/modules/shuJuTianBao.ts

116 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineStore } from 'pinia';
import { ref } from 'vue'; // 使用 ref 更简单直观
import {
getBaseDropdown,
getSelectForDropdown,
getEngInfoDropdown,
getFpssDropdown
} from '@/api/select';
export const useShuJuTianBaoStore = defineStore('shuJuTianBao', () => {
// 水电基地下拉列表
const baseLoading = ref(false);
const baseOption = ref<any[]>([]);
// 流域下拉列表
const lyLoading = ref(false);
const lyOption = ref<any[]>([]);
// 电站下拉列表
const engOption = ref<any[]>([]);
const engLoading = ref(false);
// 流域下拉列表
const fpssOption = ref<any[]>([]);
const fpssLoading = ref(false);
// 鱼类名称下拉列表
const fishOption = ref([]);
// 获取水电基地列表
const getBaseOption = async () => {
try {
baseLoading.value = true;
const res = await getBaseDropdown({});
if (res.data && Array.isArray(res.data)) {
const list = [...res.data];
// 直接赋值给 ref触发响应式更新
baseOption.value = list;
}
} catch (error) {
console.error('获取水电基地列表失败:', error);
} finally {
baseLoading.value = false;
}
};
// 获取流域列表
const getSelectForOption = async () => {
try {
lyLoading.value = true;
const res = await getSelectForDropdown({});
if (res.data && Array.isArray(res.data)) {
const list = [
{ rvcd: 'all', rvnm: '当前全部', baseid: '01' },
...res.data
];
// 直接赋值给 ref触发响应式更新
lyOption.value = list;
}
} catch (error) {
console.error('获取流域列表失败:', error);
} finally {
lyLoading.value = false;
}
};
// 获取电站列表
const getEngOption = async (rvcd: string) => {
try {
engLoading.value = true;
const param = rvcd === 'all' ? {} : { reachcd: rvcd };
const res = await getEngInfoDropdown(param);
if (res.data && Array.isArray(res.data)) {
// 直接赋值给 ref
engOption.value = res.data;
getFpssOption(rvcd, '');
}
} catch (error) {
console.error('获取电站列表失败:', error);
} finally {
engLoading.value = false;
}
};
// 获取过鱼设施列表
const getFpssOption = async (rvcd: string, rstcd: string) => {
try {
fpssLoading.value = true;
const param = rvcd === 'all' ? {} : { rvcd };
const res = await getFpssDropdown({ ...param, rstcd: rstcd });
fpssOption.value = res.data;
} catch (error) {
console.log(error);
} finally {
// fpssLoading.value = false;
}
};
const getFishOption = () => {
return fishOption.value;
};
const setFishOption = (data: any[]) => {
fishOption.value = data;
};
// 3. 直接返回 ref 和方法
// 在组件中使用时store.baseOption 会自动解包为数组
return {
fpssOption,
lyOption,
baseOption,
engOption,
fishOption,
fpssLoading,
baseLoading,
lyLoading,
engLoading,
getBaseOption,
getSelectForOption,
getEngOption,
getFpssOption,
getFishOption,
setFishOption
};
});