@@ -2819,3 +2819,151 @@ window.addEventListener('click', function(event) {
28192819 closeLogsModal ( ) ;
28202820 }
28212821} ) ;
2822+ // ============= 用户设置功能 =============
2823+
2824+ // 打开用户设置模态框
2825+ async function openUserSettingsModal ( ) {
2826+ try {
2827+ // 获取当前用户信息
2828+ const response = await fetch ( `${ API_BASE } /auth/profile` , {
2829+ method : 'GET' ,
2830+ headers : {
2831+ 'Authorization' : `Bearer ${ localStorage . getItem ( 'token' ) } `
2832+ }
2833+ } ) ;
2834+
2835+ if ( response . ok ) {
2836+ const data = await response . json ( ) ;
2837+ const user = data . user ;
2838+
2839+ // 填充表单
2840+ document . getElementById ( 'settingsUsername' ) . value = user . username || '' ;
2841+ document . getElementById ( 'settingsEmail' ) . value = user . email || '' ;
2842+ document . getElementById ( 'settingsOldPassword' ) . value = '' ;
2843+ document . getElementById ( 'settingsNewPassword' ) . value = '' ;
2844+ document . getElementById ( 'settingsConfirmPassword' ) . value = '' ;
2845+
2846+ // 显示模态框
2847+ document . getElementById ( 'userSettingsModal' ) . style . display = 'block' ;
2848+ } else {
2849+ const error = await response . json ( ) ;
2850+ showNotification ( error . error || '获取用户信息失败' , 'error' ) ;
2851+ }
2852+ } catch ( error ) {
2853+ console . error ( '打开用户设置失败:' , error ) ;
2854+ showNotification ( '打开用户设置失败' , 'error' ) ;
2855+ }
2856+ }
2857+
2858+ // 关闭用户设置模态框
2859+ function closeUserSettingsModal ( ) {
2860+ document . getElementById ( 'userSettingsModal' ) . style . display = 'none' ;
2861+ // 清空密码字段
2862+ document . getElementById ( 'settingsOldPassword' ) . value = '' ;
2863+ document . getElementById ( 'settingsNewPassword' ) . value = '' ;
2864+ document . getElementById ( 'settingsConfirmPassword' ) . value = '' ;
2865+ }
2866+
2867+ // 处理用户设置提交
2868+ async function handleUserSettingsSubmit ( event ) {
2869+ event . preventDefault ( ) ;
2870+
2871+ const username = document . getElementById ( 'settingsUsername' ) . value . trim ( ) ;
2872+ const email = document . getElementById ( 'settingsEmail' ) . value . trim ( ) ;
2873+ const oldPassword = document . getElementById ( 'settingsOldPassword' ) . value ;
2874+ const newPassword = document . getElementById ( 'settingsNewPassword' ) . value ;
2875+ const confirmPassword = document . getElementById ( 'settingsConfirmPassword' ) . value ;
2876+
2877+ // 验证用户名和邮箱
2878+ if ( ! username ) {
2879+ showNotification ( '用户名不能为空' , 'error' ) ;
2880+ return ;
2881+ }
2882+
2883+ if ( ! email ) {
2884+ showNotification ( '邮箱不能为空' , 'error' ) ;
2885+ return ;
2886+ }
2887+
2888+ // 验证邮箱格式
2889+ if ( ! email . includes ( '@' ) ) {
2890+ showNotification ( '请输入有效的邮箱地址' , 'error' ) ;
2891+ return ;
2892+ }
2893+
2894+ // 构建更新数据
2895+ const updateData = {
2896+ username : username ,
2897+ email : email
2898+ } ;
2899+
2900+ // 如果填写了新密码,进行密码相关验证
2901+ if ( newPassword ) {
2902+ // 验证新密码长度
2903+ if ( newPassword . length < 8 ) {
2904+ showNotification ( '新密码长度至少8位' , 'error' ) ;
2905+ return ;
2906+ }
2907+
2908+ // 验证确认密码
2909+ if ( newPassword !== confirmPassword ) {
2910+ showNotification ( '两次输入的新密码不一致' , 'error' ) ;
2911+ return ;
2912+ }
2913+
2914+ // 验证是否填写旧密码
2915+ if ( ! oldPassword ) {
2916+ showNotification ( '修改密码需要输入旧密码' , 'error' ) ;
2917+ return ;
2918+ }
2919+
2920+ // 添加密码字段到更新数据
2921+ updateData . old_password = oldPassword ;
2922+ updateData . new_password = newPassword ;
2923+ }
2924+
2925+ try {
2926+ const response = await fetch ( `${ API_BASE } /auth/profile` , {
2927+ method : 'PUT' ,
2928+ headers : {
2929+ 'Authorization' : `Bearer ${ localStorage . getItem ( 'token' ) } ` ,
2930+ 'Content-Type' : 'application/json'
2931+ } ,
2932+ body : JSON . stringify ( updateData )
2933+ } ) ;
2934+
2935+ const result = await response . json ( ) ;
2936+
2937+ if ( response . ok ) {
2938+ showNotification ( result . message || '更新成功' , 'success' ) ;
2939+ closeUserSettingsModal ( ) ;
2940+
2941+ // 更新页面显示的用户名
2942+ if ( currentUser && updateData . username !== currentUser . username ) {
2943+ currentUser . username = updateData . username ;
2944+ document . getElementById ( 'currentUsername' ) . textContent = updateData . username ;
2945+ }
2946+
2947+ // 如果修改了密码,强制重新登录
2948+ if ( result . data && result . data . password_changed ) {
2949+ showNotification ( '密码已修改,请重新登录' , 'info' ) ;
2950+ setTimeout ( ( ) => {
2951+ logout ( ) ;
2952+ } , 1500 ) ;
2953+ }
2954+ } else {
2955+ showNotification ( result . error || '更新失败' , 'error' ) ;
2956+ }
2957+ } catch ( error ) {
2958+ console . error ( '更新用户设置失败:' , error ) ;
2959+ showNotification ( '更新用户设置失败' , 'error' ) ;
2960+ }
2961+ }
2962+
2963+ // 点击模态框外部关闭用户设置
2964+ window . addEventListener ( 'click' , function ( event ) {
2965+ const userSettingsModal = document . getElementById ( 'userSettingsModal' ) ;
2966+ if ( event . target === userSettingsModal ) {
2967+ closeUserSettingsModal ( ) ;
2968+ }
2969+ } ) ;
0 commit comments