61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
|
|
import { defineStore } from 'pinia';
|
|||
|
|
import { ref } from 'vue'; // 使用 ref 更简单直观
|
|||
|
|
import { getBaseDropdown, getEngInfoDropdown, getFpssDropdown } from '@/api/select';
|
|||
|
|
|
|||
|
|
export const useShuJuTianBaoStore = defineStore('shuJuTianBao', () => {
|
|||
|
|
// 1. 直接使用 ref 定义状态,确保响应式
|
|||
|
|
const fpssOption = ref<any[]>([]);
|
|||
|
|
const baseOption = ref<any[]>([]);
|
|||
|
|
const engOption = ref<any[]>([]);
|
|||
|
|
|
|||
|
|
// 2. 业务逻辑方法
|
|||
|
|
const getBaseOption = async () => {
|
|||
|
|
try {
|
|||
|
|
const res = await getBaseDropdown({});
|
|||
|
|
if (res.data && Array.isArray(res.data)) {
|
|||
|
|
const list = [...res.data];
|
|||
|
|
list.unshift({
|
|||
|
|
baseid: 'all',
|
|||
|
|
basename: '当前全部'
|
|||
|
|
});
|
|||
|
|
// 直接赋值给 ref,触发响应式更新
|
|||
|
|
baseOption.value = list;
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取水电基地列表失败:', error);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const getEngOption = async (baseId: string) => {
|
|||
|
|
try {
|
|||
|
|
const param = baseId === 'all' ? {} : { baseId };
|
|||
|
|
const res = await getEngInfoDropdown(param);
|
|||
|
|
if (res.data && Array.isArray(res.data)) {
|
|||
|
|
// 直接赋值给 ref
|
|||
|
|
engOption.value = res.data;
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取电站列表失败:', error);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
|
|||
|
|
const getFpssOption = async (baseId: string, rstcd: string) => {
|
|||
|
|
try {
|
|||
|
|
const res = await getFpssDropdown({ baseId, rstcd });
|
|||
|
|
fpssOption.value = res.data;
|
|||
|
|
} catch (error) {
|
|||
|
|
console.log(error);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
// 3. 直接返回 ref 和方法
|
|||
|
|
// 在组件中使用时:store.baseOption 会自动解包为数组
|
|||
|
|
return {
|
|||
|
|
fpssOption,
|
|||
|
|
baseOption,
|
|||
|
|
engOption,
|
|||
|
|
getBaseOption,
|
|||
|
|
getEngOption,
|
|||
|
|
getFpssOption,
|
|||
|
|
};
|
|||
|
|
});
|