You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.8 KiB

import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { generateFuturesOverview, generateFutureData, riskAlerts, aiMarketAnalysis } from '../utils/mockData';
// 模拟异步获取期货概览数据
export const fetchFuturesOverview = createAsyncThunk(
'futures/fetchOverview',
async () => {
// 模拟API请求延迟
await new Promise(resolve => setTimeout(resolve, 500));
return generateFuturesOverview();
}
);
// 模拟异步获取单个期货详情
export const fetchFutureDetail = createAsyncThunk(
'futures/fetchDetail',
async ({ code, name }) => {
// 模拟API请求延迟
await new Promise(resolve => setTimeout(resolve, 500));
return generateFutureData(code, name);
}
);
// 模拟异步获取风险预警
export const fetchRiskAlerts = createAsyncThunk(
'futures/fetchRiskAlerts',
async () => {
// 模拟API请求延迟
await new Promise(resolve => setTimeout(resolve, 300));
return riskAlerts;
}
);
// 模拟异步获取AI市场分析
export const fetchAIMarketAnalysis = createAsyncThunk(
'futures/fetchAIMarketAnalysis',
async () => {
// 模拟API请求延迟
await new Promise(resolve => setTimeout(resolve, 400));
return aiMarketAnalysis;
}
);
const futuresSlice = createSlice({
name: 'futures',
initialState: {
overview: [],
selectedFuture: null,
riskAlerts: [],
aiAnalysis: null,
loading: false,
error: null,
watchlists: [
{
id: 'default',
name: '默认自选',
codes: []
}
],
currentWatchlist: 'default'
},
reducers: {
selectFuture: (state, action) => {
state.selectedFuture = action.payload;
},
clearSelectedFuture: (state) => {
state.selectedFuture = null;
},
toggleWatchlist: (state, action) => {
const { code, watchlistId = state.currentWatchlist } = action.payload;
const targetWatchlist = state.watchlists.find(wl => wl.id === watchlistId);
if (targetWatchlist) {
const index = targetWatchlist.codes.indexOf(code);
if (index > -1) {
targetWatchlist.codes.splice(index, 1);
} else {
targetWatchlist.codes.push(code);
}
}
// 更新overview中每个合约的自选状态
state.overview = state.overview.map(item => ({
...item,
isInWatchlist: state.watchlists.some(wl => wl.codes.includes(item.code))
}));
// 更新selectedFuture的自选状态
if (state.selectedFuture && state.selectedFuture.code === code) {
state.selectedFuture.isInWatchlist = state.watchlists.some(wl => wl.codes.includes(code));
}
},
createWatchlist: (state, action) => {
const { name } = action.payload;
const newWatchlist = {
id: `wl-${Date.now()}`,
name,
codes: []
};
state.watchlists.push(newWatchlist);
state.currentWatchlist = newWatchlist.id;
},
selectWatchlist: (state, action) => {
state.currentWatchlist = action.payload;
},
deleteWatchlist: (state, action) => {
const watchlistId = action.payload;
if (watchlistId !== 'default') {
state.watchlists = state.watchlists.filter(wl => wl.id !== watchlistId);
if (state.currentWatchlist === watchlistId) {
state.currentWatchlist = 'default';
}
}
}
},
extraReducers: (builder) => {
builder
// 处理fetchFuturesOverview
.addCase(fetchFuturesOverview.pending, (state) => {
state.loading = true;
state.error = null;
})
.addCase(fetchFuturesOverview.fulfilled, (state, action) => {
state.loading = false;
// 更新overview数据并添加自选状态
state.overview = action.payload.map(item => ({
...item,
isInWatchlist: state.watchlists.some(wl => wl.codes.includes(item.code))
}));
})
.addCase(fetchFuturesOverview.rejected, (state, action) => {
state.loading = false;
state.error = action.error.message;
})
// 处理fetchFutureDetail
.addCase(fetchFutureDetail.pending, (state) => {
state.loading = true;
state.error = null;
})
.addCase(fetchFutureDetail.fulfilled, (state, action) => {
state.loading = false;
// 更新selectedFuture数据并添加自选状态
state.selectedFuture = {
...action.payload,
isInWatchlist: state.watchlists.some(wl => wl.codes.includes(action.payload.code))
};
console.log('fetchFutureDetail fulfilled with:', action.payload);
})
.addCase(fetchFutureDetail.rejected, (state, action) => {
state.loading = false;
state.error = action.error.message;
console.log('fetchFutureDetail rejected with error:', action.error.message);
})
// 处理fetchRiskAlerts
.addCase(fetchRiskAlerts.pending, (state) => {
state.loading = true;
state.error = null;
})
.addCase(fetchRiskAlerts.fulfilled, (state, action) => {
state.loading = false;
state.riskAlerts = action.payload;
})
.addCase(fetchRiskAlerts.rejected, (state, action) => {
state.loading = false;
state.error = action.error.message;
})
// 处理fetchAIMarketAnalysis
.addCase(fetchAIMarketAnalysis.pending, (state) => {
state.loading = true;
state.error = null;
})
.addCase(fetchAIMarketAnalysis.fulfilled, (state, action) => {
state.loading = false;
state.aiAnalysis = action.payload;
})
.addCase(fetchAIMarketAnalysis.rejected, (state, action) => {
state.loading = false;
state.error = action.error.message;
});
}
});
export const { selectFuture, clearSelectedFuture, toggleWatchlist, createWatchlist, selectWatchlist, deleteWatchlist } = futuresSlice.actions;
export default futuresSlice.reducer;