功能:注册保存性别 + 聊天室个人设置弹窗

- 登录表单的性别选择(bSex)在注册时保存到数据库(男/女/保密)
- 新增 question/answer 密保字段迁移(hasColumn 安全检查)
- User 模型 fillable 增加 sign/question/answer
- UpdateProfileRequest 增加 email/question/answer 验证
- 聊天室工具栏新增设置按钮
- 设置弹窗包含:修改密码、性别、邮箱、密保问题
This commit is contained in:
2026-02-26 22:50:35 +08:00
parent 86732deaca
commit c38a53fa74
7 changed files with 225 additions and 1 deletions

View File

@@ -63,13 +63,17 @@ class AuthController extends Controller
// --- 核心:第一次登录即为注册 ---
// 映射性别1=男 2=女,默认保密
$sexMap = ['1' => '男', '2' => '女'];
$sex = $sexMap[$request->input('bSex', '')] ?? '保密';
$newUser = User::create([
'username' => $username,
'password' => Hash::make($password),
'first_ip' => $ip,
'last_ip' => $ip,
'user_level' => 1, // 默认普通用户等级
'sex' => 0, // 默认性别: 0保密 1男 2女
'sex' => $sex,
'usersf' => '1.GIF', // 默认头像
]);

View File

@@ -39,6 +39,7 @@ class LoginRequest extends FormRequest
'regex:/^[^<>\'"]+$/u',
],
'password' => ['required', 'string', 'min:1'],
'bSex' => ['nullable', 'in:1,2'],
'captcha' => ['required', 'captcha'],
];
}

View File

@@ -33,6 +33,9 @@ class UpdateProfileRequest extends FormRequest
'sex' => ['required', 'string', 'in:男,女,保密'],
'headface' => ['required', 'string', 'max:50'], // 比如存放 01.gif - 50.gif
'sign' => ['nullable', 'string', 'max:255'],
'email' => ['nullable', 'email', 'max:255'],
'question' => ['nullable', 'string', 'max:100'],
'answer' => ['nullable', 'string', 'max:100'],
];
}

View File

@@ -32,6 +32,7 @@ class User extends Authenticatable
'password',
'email',
'sex',
'sign',
'user_level',
'room_id',
'first_ip',
@@ -39,6 +40,8 @@ class User extends Authenticatable
'usersf',
'vip_level_id',
'hy_time',
'question',
'answer',
];
/**

View File

@@ -0,0 +1,49 @@
<?php
/**
* 文件功能:为用户表添加密码保护问题和答案字段
*
* 用于用户找回密码的密保问题功能。
* 使用 hasColumn 检查避免重复添加。
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 添加密保问题和答案字段(已存在则跳过)
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
if (! Schema::hasColumn('users', 'question')) {
$table->string('question', 100)->nullable()->after('email')->comment('密保问题');
}
if (! Schema::hasColumn('users', 'answer')) {
$table->string('answer', 100)->nullable()->after('question')->comment('密保答案');
}
});
}
/**
* 回滚:删除密保字段
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$columns = [];
if (Schema::hasColumn('users', 'question')) {
$columns[] = 'question';
}
if (Schema::hasColumn('users', 'answer')) {
$columns[] = 'answer';
}
if ($columns) {
$table->dropColumn($columns);
}
});
}
};

View File

@@ -404,6 +404,168 @@
</div>
</div>
{{-- ═══════════ 个人设置弹窗 ═══════════ --}}
<div id="settings-modal"
style="display:none; position:fixed; top:0; left:0; right:0; bottom:0;
background:rgba(0,0,0,0.5); z-index:9999; justify-content:center; align-items:center;">
<div
style="background:#fff; border-radius:8px; width:380px; max-height:90vh; overflow-y:auto;
box-shadow:0 8px 32px rgba(0,0,0,0.3);">
{{-- 标题栏 --}}
<div
style="background:linear-gradient(135deg,#336699,#5a8fc0); color:#fff;
padding:12px 16px; border-radius:8px 8px 0 0; display:flex; justify-content:space-between; align-items:center;">
<span style="font-size:14px; font-weight:bold;">⚙️ 个人设置</span>
<span onclick="document.getElementById('settings-modal').style.display='none'"
style="cursor:pointer; font-size:18px; opacity:0.8;">&times;</span>
</div>
<div style="padding:16px;">
{{-- 修改密码 --}}
<div style="margin-bottom:16px; border:1px solid #e0e0e0; border-radius:6px; padding:12px;">
<div style="font-size:12px; font-weight:bold; color:#336699; margin-bottom:8px;">🔒 修改密码</div>
<div style="display:flex; flex-direction:column; gap:6px;">
<input id="set-old-pwd" type="password" placeholder="当前旧密码"
style="padding:6px 8px; border:1px solid #ccc; border-radius:4px; font-size:12px;">
<input id="set-new-pwd" type="password" placeholder="新密码至少6位"
style="padding:6px 8px; border:1px solid #ccc; border-radius:4px; font-size:12px;">
<input id="set-new-pwd2" type="password" placeholder="确认新密码"
style="padding:6px 8px; border:1px solid #ccc; border-radius:4px; font-size:12px;">
<button onclick="savePassword()"
style="padding:6px; background:#336699; color:#fff; border:none; border-radius:4px;
font-size:12px; cursor:pointer;">确定修改密码</button>
</div>
</div>
{{-- 个人资料 --}}
<div style="margin-bottom:16px; border:1px solid #e0e0e0; border-radius:6px; padding:12px;">
<div style="font-size:12px; font-weight:bold; color:#336699; margin-bottom:8px;">👤 个人资料</div>
<div style="display:flex; flex-direction:column; gap:6px;">
<div style="display:flex; align-items:center; gap:8px;">
<label style="font-size:12px; width:50px; text-align:right;">性别:</label>
<select id="set-sex"
style="flex:1; padding:5px; border:1px solid #ccc; border-radius:4px; font-size:12px;">
<option value="" {{ Auth::user()->sex === '男' ? 'selected' : '' }}></option>
<option value="" {{ Auth::user()->sex === '女' ? 'selected' : '' }}></option>
<option value="保密" {{ Auth::user()->sex === '保密' ? 'selected' : '' }}>保密</option>
</select>
</div>
<div style="display:flex; align-items:center; gap:8px;">
<label style="font-size:12px; width:50px; text-align:right;">邮箱:</label>
<input id="set-email" type="email" value="{{ Auth::user()->email ?? '' }}"
placeholder="选填"
style="flex:1; padding:5px; border:1px solid #ccc; border-radius:4px; font-size:12px;">
</div>
</div>
</div>
{{-- 密保设置 --}}
<div style="margin-bottom:16px; border:1px solid #e0e0e0; border-radius:6px; padding:12px;">
<div style="font-size:12px; font-weight:bold; color:#336699; margin-bottom:8px;">🛡️ 密码保护</div>
<div style="display:flex; flex-direction:column; gap:6px;">
<input id="set-question" type="text" value="{{ Auth::user()->question ?? '' }}"
placeholder="密保问题(如:我的小学名字?)"
style="padding:6px 8px; border:1px solid #ccc; border-radius:4px; font-size:12px;">
<input id="set-answer" type="text" value="{{ Auth::user()->answer ?? '' }}"
placeholder="密保答案"
style="padding:6px 8px; border:1px solid #ccc; border-radius:4px; font-size:12px;">
</div>
</div>
{{-- 保存按钮 --}}
<button onclick="saveSettings()"
style="width:100%; padding:8px; background:linear-gradient(135deg,#336699,#5a8fc0);
color:#fff; border:none; border-radius:4px; font-size:13px; font-weight:bold; cursor:pointer;">
💾 保存资料设置
</button>
</div>
</div>
</div>
<script>
/**
* 保存密码(调用修改密码 API
*/
async function savePassword() {
const oldPwd = document.getElementById('set-old-pwd').value;
const newPwd = document.getElementById('set-new-pwd').value;
const newPwd2 = document.getElementById('set-new-pwd2').value;
if (!oldPwd || !newPwd) {
alert('请填写旧密码和新密码');
return;
}
if (newPwd.length < 6) {
alert('新密码最少6位');
return;
}
if (newPwd !== newPwd2) {
alert('两次输入的新密码不一致!');
return;
}
try {
const res = await fetch('{{ route('user.update_password') }}', {
method: 'PUT',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
old_password: oldPwd,
new_password: newPwd,
new_password_confirmation: newPwd2
})
});
const data = await res.json();
if (res.ok && data.status === 'success') {
alert('密码修改成功!');
document.getElementById('set-old-pwd').value = '';
document.getElementById('set-new-pwd').value = '';
document.getElementById('set-new-pwd2').value = '';
} else {
alert('修改失败: ' + (data.message || '请输入正确的旧密码'));
}
} catch (e) {
alert('网络异常');
}
}
/**
* 保存个人资料和密保设置
*/
async function saveSettings() {
const profileData = {
sex: document.getElementById('set-sex').value,
email: document.getElementById('set-email').value,
question: document.getElementById('set-question').value,
answer: document.getElementById('set-answer').value,
headface: '{{ Auth::user()->usersf ?: '1.GIF' }}',
sign: '{{ Auth::user()->sign ?? '' }}'
};
try {
const res = await fetch('{{ route('user.update_profile') }}', {
method: 'PUT',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(profileData)
});
const data = await res.json();
if (res.ok && data.status === 'success') {
alert('设置保存成功!');
} else {
alert('保存失败: ' + (data.message || '输入有误'));
}
} catch (e) {
alert('网络异常');
}
}
</script>
</body>
</html>

View File

@@ -22,6 +22,8 @@
<div class="tool-btn" onclick="switchTab('users')" title="呼叫在线用户">呼叫</div>
<div class="tool-btn" onclick="openAvatarPicker()" title="修改头像">头像
</div>
<div class="tool-btn" onclick="document.getElementById('settings-modal').style.display='flex'" title="个人设置">设置
</div>
<div class="tool-btn" onclick="window.open('{{ route('guestbook.index') }}', '_blank')" title="提议/建议">
提议</div>
<div class="tool-btn" onclick="window.open('{{ route('guestbook.index') }}', '_blank')" title="留言板/私信">