Compare commits
2 Commits
7a2c73f3ca
...
dabedc1c0b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dabedc1c0b | ||
|
|
7b4c4be7bf |
@@ -322,16 +322,6 @@ export default function AuthFormContent() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (response.ok && data.success) {
|
if (response.ok && data.success) {
|
||||||
// ⚡ Mock 模式:先在前端侧写入 localStorage,确保时序正确
|
|
||||||
if (process.env.REACT_APP_ENABLE_MOCK === 'true' && data.user) {
|
|
||||||
setCurrentUser(data.user);
|
|
||||||
logger.debug('AuthFormContent', '前端侧设置当前用户(Mock模式)', {
|
|
||||||
userId: data.user?.id,
|
|
||||||
phone: data.user?.phone,
|
|
||||||
mockMode: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新session
|
// 更新session
|
||||||
await checkSession();
|
await checkSession();
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
resetToFree,
|
resetToFree,
|
||||||
selectSubscriptionInfo,
|
selectSubscriptionInfo,
|
||||||
selectSubscriptionLoading,
|
selectSubscriptionLoading,
|
||||||
|
selectSubscriptionLoaded,
|
||||||
selectSubscriptionError,
|
selectSubscriptionError,
|
||||||
selectSubscriptionModalOpen
|
selectSubscriptionModalOpen
|
||||||
} from '../store/slices/subscriptionSlice';
|
} from '../store/slices/subscriptionSlice';
|
||||||
@@ -66,21 +67,24 @@ export const useSubscription = () => {
|
|||||||
// Redux 状态
|
// Redux 状态
|
||||||
const subscriptionInfo = useSelector(selectSubscriptionInfo);
|
const subscriptionInfo = useSelector(selectSubscriptionInfo);
|
||||||
const loading = useSelector(selectSubscriptionLoading);
|
const loading = useSelector(selectSubscriptionLoading);
|
||||||
|
const loaded = useSelector(selectSubscriptionLoaded);
|
||||||
const error = useSelector(selectSubscriptionError);
|
const error = useSelector(selectSubscriptionError);
|
||||||
const isSubscriptionModalOpen = useSelector(selectSubscriptionModalOpen);
|
const isSubscriptionModalOpen = useSelector(selectSubscriptionModalOpen);
|
||||||
|
|
||||||
// 自动加载订阅信息
|
// 自动加载订阅信息(带防重复逻辑)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isAuthenticated && user) {
|
if (isAuthenticated && user) {
|
||||||
// 用户已登录,加载订阅信息
|
// 只在未加载且未在加载中时才请求,避免多个组件重复调用
|
||||||
|
if (!loaded && !loading) {
|
||||||
dispatch(fetchSubscriptionInfo());
|
dispatch(fetchSubscriptionInfo());
|
||||||
logger.debug('useSubscription', '加载订阅信息', { userId: user.id });
|
logger.debug('useSubscription', '加载订阅信息', { userId: user.id });
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 用户未登录,重置为免费版
|
// 用户未登录,重置为免费版
|
||||||
dispatch(resetToFree());
|
dispatch(resetToFree());
|
||||||
logger.debug('useSubscription', '用户未登录,重置为免费版');
|
|
||||||
}
|
}
|
||||||
}, [isAuthenticated, user, dispatch]);
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [isAuthenticated, user?.id, dispatch, loaded, loading]);
|
||||||
|
|
||||||
// 获取订阅级别数值
|
// 获取订阅级别数值
|
||||||
const getSubscriptionLevel = (type = null) => {
|
const getSubscriptionLevel = (type = null) => {
|
||||||
|
|||||||
@@ -102,7 +102,6 @@ export function setCurrentUser(user) {
|
|||||||
subscription_days_left: user.subscription_days_left || 0
|
subscription_days_left: user.subscription_days_left || 0
|
||||||
};
|
};
|
||||||
localStorage.setItem('mock_current_user', JSON.stringify(normalizedUser));
|
localStorage.setItem('mock_current_user', JSON.stringify(normalizedUser));
|
||||||
console.log('[Mock State] 设置当前登录用户:', normalizedUser);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -613,14 +613,6 @@ export const accountHandlers = [
|
|||||||
end_date: currentUser.subscription_end_date || null
|
end_date: currentUser.subscription_end_date || null
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('[Mock API] 获取当前订阅详情:', {
|
|
||||||
user_id: currentUser.id,
|
|
||||||
phone: currentUser.phone,
|
|
||||||
subscription_type: userSubscriptionType,
|
|
||||||
subscription_status: subscriptionDetails.status,
|
|
||||||
days_left: subscriptionDetails.days_left
|
|
||||||
});
|
|
||||||
|
|
||||||
return HttpResponse.json({
|
return HttpResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: subscriptionDetails
|
data: subscriptionDetails
|
||||||
|
|||||||
@@ -127,8 +127,6 @@ export const authHandlers = [
|
|||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const { credential, verification_code, login_type } = body;
|
const { credential, verification_code, login_type } = body;
|
||||||
|
|
||||||
console.log('[Mock] 验证码登录:', { credential, verification_code, login_type });
|
|
||||||
|
|
||||||
// 验证验证码
|
// 验证验证码
|
||||||
const storedCode = mockVerificationCodes.get(credential);
|
const storedCode = mockVerificationCodes.get(credential);
|
||||||
if (!storedCode) {
|
if (!storedCode) {
|
||||||
@@ -180,11 +178,8 @@ export const authHandlers = [
|
|||||||
subscription_days_left: 0
|
subscription_days_left: 0
|
||||||
};
|
};
|
||||||
mockUsers[credential] = user;
|
mockUsers[credential] = user;
|
||||||
console.log('[Mock] 创建新用户:', user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[Mock] 登录成功:', user);
|
|
||||||
|
|
||||||
// 设置当前登录用户
|
// 设置当前登录用户
|
||||||
setCurrentUser(user);
|
setCurrentUser(user);
|
||||||
|
|
||||||
@@ -362,8 +357,6 @@ export const authHandlers = [
|
|||||||
// 获取当前登录用户
|
// 获取当前登录用户
|
||||||
const currentUser = getCurrentUser();
|
const currentUser = getCurrentUser();
|
||||||
|
|
||||||
console.log('[Mock] 检查 Session:', currentUser);
|
|
||||||
|
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
return HttpResponse.json({
|
return HttpResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -386,8 +379,6 @@ export const authHandlers = [
|
|||||||
// 获取当前登录用户
|
// 获取当前登录用户
|
||||||
const currentUser = getCurrentUser();
|
const currentUser = getCurrentUser();
|
||||||
|
|
||||||
console.log('[Mock] 检查 Session (旧端点):', currentUser);
|
|
||||||
|
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
return HttpResponse.json({
|
return HttpResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ const subscriptionSlice = createSlice({
|
|||||||
},
|
},
|
||||||
// 加载状态
|
// 加载状态
|
||||||
loading: false,
|
loading: false,
|
||||||
|
loaded: false, // 是否已加载过(用于防止重复请求)
|
||||||
error: null,
|
error: null,
|
||||||
// 订阅 Modal 状态
|
// 订阅 Modal 状态
|
||||||
isModalOpen: false,
|
isModalOpen: false,
|
||||||
@@ -104,8 +105,8 @@ const subscriptionSlice = createSlice({
|
|||||||
end_date: null
|
end_date: null
|
||||||
};
|
};
|
||||||
state.loading = false;
|
state.loading = false;
|
||||||
|
state.loaded = false; // 重置已加载标记,下次登录时重新获取
|
||||||
state.error = null;
|
state.error = null;
|
||||||
logger.debug('subscriptionSlice', '重置订阅信息为免费版');
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
@@ -118,6 +119,7 @@ const subscriptionSlice = createSlice({
|
|||||||
// fetchSubscriptionInfo - fulfilled
|
// fetchSubscriptionInfo - fulfilled
|
||||||
.addCase(fetchSubscriptionInfo.fulfilled, (state, action) => {
|
.addCase(fetchSubscriptionInfo.fulfilled, (state, action) => {
|
||||||
state.loading = false;
|
state.loading = false;
|
||||||
|
state.loaded = true; // 标记已加载
|
||||||
state.info = action.payload;
|
state.info = action.payload;
|
||||||
state.error = null;
|
state.error = null;
|
||||||
})
|
})
|
||||||
@@ -136,6 +138,7 @@ export const { openModal, closeModal, resetToFree } = subscriptionSlice.actions;
|
|||||||
// 导出 selectors
|
// 导出 selectors
|
||||||
export const selectSubscriptionInfo = (state) => state.subscription.info;
|
export const selectSubscriptionInfo = (state) => state.subscription.info;
|
||||||
export const selectSubscriptionLoading = (state) => state.subscription.loading;
|
export const selectSubscriptionLoading = (state) => state.subscription.loading;
|
||||||
|
export const selectSubscriptionLoaded = (state) => state.subscription.loaded;
|
||||||
export const selectSubscriptionError = (state) => state.subscription.error;
|
export const selectSubscriptionError = (state) => state.subscription.error;
|
||||||
export const selectSubscriptionModalOpen = (state) => state.subscription.isModalOpen;
|
export const selectSubscriptionModalOpen = (state) => state.subscription.isModalOpen;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user