feat: 添加mock数据,给导航添加选中标识
This commit is contained in:
248
src/mocks/handlers/account.js
Normal file
248
src/mocks/handlers/account.js
Normal file
@@ -0,0 +1,248 @@
|
||||
// src/mocks/handlers/account.js
|
||||
import { http, HttpResponse, delay } from 'msw';
|
||||
import { getCurrentUser } from '../data/users';
|
||||
|
||||
// 模拟网络延迟(毫秒)
|
||||
const NETWORK_DELAY = 300;
|
||||
|
||||
export const accountHandlers = [
|
||||
// ==================== 用户资料管理 ====================
|
||||
|
||||
// 1. 获取资料完整度
|
||||
http.get('/api/account/profile-completeness', async () => {
|
||||
await delay(NETWORK_DELAY);
|
||||
|
||||
// 获取当前登录用户
|
||||
const currentUser = getCurrentUser();
|
||||
|
||||
// 如果没有登录,返回 401
|
||||
if (!currentUser) {
|
||||
return HttpResponse.json({
|
||||
success: false,
|
||||
error: '用户未登录'
|
||||
}, { status: 401 });
|
||||
}
|
||||
|
||||
console.log('[Mock] 获取资料完整度:', currentUser);
|
||||
|
||||
// 检查用户是否是微信用户
|
||||
const isWechatUser = currentUser.has_wechat || !!currentUser.wechat_openid;
|
||||
|
||||
// 检查各项信息
|
||||
const completeness = {
|
||||
hasPassword: !!currentUser.password_hash || !isWechatUser, // 非微信用户默认有密码
|
||||
hasPhone: !!currentUser.phone,
|
||||
hasEmail: !!currentUser.email && currentUser.email.includes('@') && !currentUser.email.endsWith('@valuefrontier.temp'),
|
||||
isWechatUser: isWechatUser
|
||||
};
|
||||
|
||||
// 计算完整度
|
||||
const totalItems = 3;
|
||||
const completedItems = [completeness.hasPassword, completeness.hasPhone, completeness.hasEmail].filter(Boolean).length;
|
||||
const completenessPercentage = Math.round((completedItems / totalItems) * 100);
|
||||
|
||||
// 智能判断是否需要提醒
|
||||
let needsAttention = false;
|
||||
const missingItems = [];
|
||||
|
||||
// Mock 模式下,对微信用户进行简化判断
|
||||
if (isWechatUser && completenessPercentage < 100) {
|
||||
needsAttention = true;
|
||||
if (!completeness.hasPassword) {
|
||||
missingItems.push('登录密码');
|
||||
}
|
||||
if (!completeness.hasPhone) {
|
||||
missingItems.push('手机号');
|
||||
}
|
||||
if (!completeness.hasEmail) {
|
||||
missingItems.push('邮箱');
|
||||
}
|
||||
}
|
||||
|
||||
const result = {
|
||||
success: true,
|
||||
data: {
|
||||
completeness,
|
||||
completenessPercentage,
|
||||
needsAttention,
|
||||
missingItems,
|
||||
isComplete: completedItems === totalItems,
|
||||
showReminder: needsAttention
|
||||
}
|
||||
};
|
||||
|
||||
console.log('[Mock] 资料完整度结果:', result.data);
|
||||
|
||||
return HttpResponse.json(result);
|
||||
}),
|
||||
|
||||
// 2. 更新用户资料
|
||||
http.put('/api/account/profile', async ({ request }) => {
|
||||
await delay(NETWORK_DELAY);
|
||||
|
||||
// 获取当前登录用户
|
||||
const currentUser = getCurrentUser();
|
||||
|
||||
if (!currentUser) {
|
||||
return HttpResponse.json({
|
||||
success: false,
|
||||
error: '用户未登录'
|
||||
}, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
|
||||
console.log('[Mock] 更新用户资料:', body);
|
||||
|
||||
// 在 Mock 模式下,我们直接更新当前用户对象(实际应该调用 setCurrentUser)
|
||||
Object.assign(currentUser, body);
|
||||
|
||||
return HttpResponse.json({
|
||||
success: true,
|
||||
message: '资料更新成功',
|
||||
data: currentUser
|
||||
});
|
||||
}),
|
||||
|
||||
// 3. 获取用户资料
|
||||
http.get('/api/account/profile', async () => {
|
||||
await delay(NETWORK_DELAY);
|
||||
|
||||
const currentUser = getCurrentUser();
|
||||
|
||||
if (!currentUser) {
|
||||
return HttpResponse.json({
|
||||
success: false,
|
||||
error: '用户未登录'
|
||||
}, { status: 401 });
|
||||
}
|
||||
|
||||
console.log('[Mock] 获取用户资料:', currentUser);
|
||||
|
||||
return HttpResponse.json({
|
||||
success: true,
|
||||
data: currentUser
|
||||
});
|
||||
}),
|
||||
|
||||
// ==================== 订阅管理 ====================
|
||||
|
||||
// 4. 获取订阅信息
|
||||
http.get('/api/subscription/info', async () => {
|
||||
await delay(NETWORK_DELAY);
|
||||
|
||||
const currentUser = getCurrentUser();
|
||||
|
||||
// 未登录时返回免费用户信息
|
||||
if (!currentUser) {
|
||||
return HttpResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
type: 'free',
|
||||
status: 'active',
|
||||
is_active: true,
|
||||
days_left: 0,
|
||||
end_date: null
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[Mock] 获取订阅信息:', currentUser);
|
||||
|
||||
// 从当前用户对象中获取订阅信息
|
||||
const subscriptionInfo = {
|
||||
type: currentUser.subscription_type || 'free',
|
||||
status: currentUser.subscription_status || 'active',
|
||||
is_active: currentUser.is_subscription_active !== false,
|
||||
days_left: currentUser.subscription_days_left || 0,
|
||||
end_date: currentUser.subscription_end_date || null
|
||||
};
|
||||
|
||||
console.log('[Mock] 订阅信息结果:', subscriptionInfo);
|
||||
|
||||
return HttpResponse.json({
|
||||
success: true,
|
||||
data: subscriptionInfo
|
||||
});
|
||||
}),
|
||||
|
||||
// 5. 获取订阅权限
|
||||
http.get('/api/subscription/permissions', async () => {
|
||||
await delay(NETWORK_DELAY);
|
||||
|
||||
const currentUser = getCurrentUser();
|
||||
|
||||
// 未登录时返回免费权限
|
||||
if (!currentUser) {
|
||||
return HttpResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
permissions: {
|
||||
'related_stocks': false,
|
||||
'related_concepts': false,
|
||||
'transmission_chain': false,
|
||||
'historical_events': 'limited',
|
||||
'concept_html_detail': false,
|
||||
'concept_stats_panel': false,
|
||||
'concept_related_stocks': false,
|
||||
'concept_timeline': false,
|
||||
'hot_stocks': false
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const subscriptionType = (currentUser.subscription_type || 'free').toLowerCase();
|
||||
|
||||
// 根据订阅类型返回对应权限
|
||||
let permissions = {};
|
||||
|
||||
if (subscriptionType === 'free') {
|
||||
permissions = {
|
||||
'related_stocks': false,
|
||||
'related_concepts': false,
|
||||
'transmission_chain': false,
|
||||
'historical_events': 'limited',
|
||||
'concept_html_detail': false,
|
||||
'concept_stats_panel': false,
|
||||
'concept_related_stocks': false,
|
||||
'concept_timeline': false,
|
||||
'hot_stocks': false
|
||||
};
|
||||
} else if (subscriptionType === 'pro') {
|
||||
permissions = {
|
||||
'related_stocks': true,
|
||||
'related_concepts': true,
|
||||
'transmission_chain': false,
|
||||
'historical_events': 'full',
|
||||
'concept_html_detail': true,
|
||||
'concept_stats_panel': true,
|
||||
'concept_related_stocks': true,
|
||||
'concept_timeline': false,
|
||||
'hot_stocks': true
|
||||
};
|
||||
} else if (subscriptionType === 'max') {
|
||||
permissions = {
|
||||
'related_stocks': true,
|
||||
'related_concepts': true,
|
||||
'transmission_chain': true,
|
||||
'historical_events': 'full',
|
||||
'concept_html_detail': true,
|
||||
'concept_stats_panel': true,
|
||||
'concept_related_stocks': true,
|
||||
'concept_timeline': true,
|
||||
'hot_stocks': true
|
||||
};
|
||||
}
|
||||
|
||||
console.log('[Mock] 订阅权限:', { subscriptionType, permissions });
|
||||
|
||||
return HttpResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
subscription_type: subscriptionType,
|
||||
permissions
|
||||
}
|
||||
});
|
||||
})
|
||||
];
|
||||
Reference in New Issue
Block a user