feat: 添加日志

This commit is contained in:
zdl
2025-10-18 08:08:58 +08:00
parent 36558e0715
commit b46ee4a18e
5 changed files with 74 additions and 45 deletions

View File

@@ -57,6 +57,7 @@ import {
} from '@chakra-ui/icons';
import { FaWeixin, FaMobile, FaEnvelope } from 'react-icons/fa';
import { useAuth } from '../../contexts/AuthContext';
import { logger } from '../../utils/logger';
export default function SettingsPage() {
const { user, updateUser, logout } = useAuth();
@@ -117,24 +118,28 @@ export default function SettingsPage() {
// 获取密码状态
const fetchPasswordStatus = async () => {
try {
const API_BASE_URL = process.env.NODE_ENV === 'production'
? ""
const API_BASE_URL = process.env.NODE_ENV === 'production'
? ""
: process.env.REACT_APP_API_URL || "http://49.232.185.254:5001";
logger.api.request('GET', '/api/account/password-status', null);
const response = await fetch(`${API_BASE_URL}/api/account/password-status`, {
method: 'GET',
credentials: 'include'
});
if (response.ok) {
const data = await response.json();
logger.api.response('GET', '/api/account/password-status', response.status, data);
if (data.success) {
console.log('密码状态数据:', data.data); // 调试信息
logger.debug('SettingsPage', '密码状态获取成功', data.data);
setPasswordStatus(data.data);
}
}
} catch (error) {
console.error('获取密码状态失败:', error);
logger.error('SettingsPage', 'fetchPasswordStatus', error);
} finally {
setPasswordStatusLoading(false);
}
@@ -249,34 +254,41 @@ export default function SettingsPage() {
setIsLoading(true);
try {
if (type === 'phone') {
const res = await fetch((process.env.NODE_ENV === 'production' ? '' : process.env.REACT_APP_API_URL || 'http://49.232.185.254:5001') + '/api/account/phone/send-code', {
const url = '/api/account/phone/send-code';
logger.api.request('POST', url, { phone: phoneForm.phone.substring(0, 3) + '****' });
const res = await fetch((process.env.NODE_ENV === 'production' ? '' : process.env.REACT_APP_API_URL || 'http://49.232.185.254:5001') + url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ phone: phoneForm.phone })
});
const data = await res.json();
logger.api.response('POST', url, res.status, data);
if (!res.ok) throw new Error(data.error || '发送失败');
} else {
const url = '/api/account/email/send-bind-code';
logger.api.request('POST', url, { email: emailForm.email.substring(0, 3) + '***@***' });
// 使用绑定邮箱的验证码API
const res = await fetch((process.env.NODE_ENV === 'production' ? '' : process.env.REACT_APP_API_URL || 'http://49.232.185.254:5001') + '/api/account/email/send-bind-code', {
const res = await fetch((process.env.NODE_ENV === 'production' ? '' : process.env.REACT_APP_API_URL || 'http://49.232.185.254:5001') + url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ email: emailForm.email })
});
const data = await res.json();
logger.api.response('POST', url, res.status, data);
if (!res.ok) throw new Error(data.error || '发送失败');
}
toast({
title: "验证码已发送",
description: `请查收${type === 'phone' ? '短信' : '邮件'}`,
status: "success",
duration: 3000,
isClosable: true,
});
// ❌ 移除验证码发送成功toast
logger.info('SettingsPage', `${type === 'phone' ? '短信' : '邮件'}验证码已发送`);
} catch (error) {
logger.error('SettingsPage', 'sendVerificationCode', error, { type });
toast({
title: "发送失败",
description: error.message,
@@ -381,18 +393,18 @@ export default function SettingsPage() {
const saveNotificationSettings = async () => {
setIsLoading(true);
try {
logger.debug('SettingsPage', '保存通知设置', notifications);
// 这里应该调用后端API保存设置
await new Promise(resolve => setTimeout(resolve, 1000));
updateUser(notifications);
toast({
title: "通知设置已保存",
status: "success",
duration: 3000,
isClosable: true,
});
// ❌ 移除设置保存成功toast
logger.info('SettingsPage', '通知设置已保存');
} catch (error) {
logger.error('SettingsPage', 'saveNotificationSettings', error);
toast({
title: "保存失败",
description: error.message,
@@ -409,6 +421,8 @@ export default function SettingsPage() {
const savePrivacySettings = async () => {
setIsLoading(true);
try {
logger.debug('SettingsPage', '保存隐私设置', { privacy, blockedKeywords });
// 这里应该调用后端API保存设置
await new Promise(resolve => setTimeout(resolve, 1000));
@@ -417,13 +431,11 @@ export default function SettingsPage() {
blocked_keywords: blockedKeywords
});
toast({
title: "隐私设置已保存",
status: "success",
duration: 3000,
isClosable: true,
});
// ❌ 移除设置保存成功toast
logger.info('SettingsPage', '隐私设置已保存');
} catch (error) {
logger.error('SettingsPage', 'savePrivacySettings', error);
toast({
title: "保存失败",
description: error.message,