From e0c15b437e7158374006ee66aab0f104e0939abc Mon Sep 17 00:00:00 2001 From: lkddi Date: Sun, 1 Mar 2026 16:04:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=B1=82=E5=A9=9A?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E5=BC=82=E6=80=A7=E5=88=A4=E6=96=AD=20-=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=20sex=20=E5=AD=97=E6=AE=B5=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:sex 字段数据库存整数(0/1/2),但前后端判断混用了 字符串('男'/'女')导致比较永远错误。 修复三处: 1. UserController::show() - sex 返回统一转字符串(1→'男' 2→'女' 其他→'') 2. frame.blade.php - chatContext.userSex 注入时同样转字符串 3. MarriageService::propose() - 后端性别校验改用整数(1/2)比较 逻辑链路: - 未设置性别(sex=0) → '' → x-show && userInfo.sex 为'' falsy → 按钮隐藏 ✓ - 同性(如两个男) → '男'==='男' → !== 为false → 按钮隐藏 ✓ - 异性(男+女) → '男'!=='女' → 按钮显示 ✓ --- app/Http/Controllers/UserController.php | 4 +++- app/Services/MarriageService.php | 10 +++++----- resources/views/chat/frame.blade.php | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 808a260..9cb0687 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -44,7 +44,9 @@ class UserController extends Controller $activePosition = $targetUser->activePosition?->load('position.department')->position; $data = [ 'username' => $targetUser->username, - 'sex' => $targetUser->sex, + 'sex' => match ((int) $targetUser->sex) { + 1 => '男', 2 => '女', default => '' + }, 'headface' => $targetUser->headface, 'usersf' => $targetUser->usersf, 'user_level' => $targetUser->user_level, diff --git a/app/Services/MarriageService.php b/app/Services/MarriageService.php index 106580d..7c7b984 100644 --- a/app/Services/MarriageService.php +++ b/app/Services/MarriageService.php @@ -47,12 +47,12 @@ class MarriageService return ['ok' => false, 'message' => '不能向自己求婚!', 'marriage_id' => null]; } - // 只允许异性之间求婚 - $validSexes = ['男', '女']; + // 只允许异性之间求婚(sex 字段:1=男 2=女 0=未设置) + $validSexes = [1, 2]; if ( - ! in_array($proposer->sex, $validSexes, true) || - ! in_array($target->sex, $validSexes, true) || - $proposer->sex === $target->sex + ! in_array((int) $proposer->sex, $validSexes, true) || + ! in_array((int) $target->sex, $validSexes, true) || + (int) $proposer->sex === (int) $target->sex ) { return ['ok' => false, 'message' => '只有男女双方才能互相求婚,请确认双方性别设置。', 'marriage_id' => null]; } diff --git a/resources/views/chat/frame.blade.php b/resources/views/chat/frame.blade.php index 9c79676..9c718a7 100644 --- a/resources/views/chat/frame.blade.php +++ b/resources/views/chat/frame.blade.php @@ -31,7 +31,7 @@ roomId: {{ $room->id }}, userId: {{ $user->id }}, username: "{{ $user->username }}", - userSex: "{{ $user->sex }}", + userSex: "{{ match ((int) $user->sex) {1 => '男',2 => '女',default => ''} }}", userLevel: {{ $user->user_level }}, superLevel: {{ $superLevel }}, levelKick: {{ $levelKick }},