From 0bf9b77aed56e5b1ad7549fa6e52a0ab78d3399a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=97=E5=BC=BA?= <357099073@qq.com> Date: Tue, 24 Feb 2026 10:12:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BabyHealth/DashboradController.php | 90 ++++++---- app/admin/controller/LoginController.php | 154 +++++++++++------- app/admin/route/routes/babyhealth.php | 8 +- app/common/middleware/AllowCrossDomain.php | 2 + 4 files changed, 157 insertions(+), 97 deletions(-) diff --git a/app/admin/controller/BabyHealth/DashboradController.php b/app/admin/controller/BabyHealth/DashboradController.php index df965f9..23998fc 100644 --- a/app/admin/controller/BabyHealth/DashboradController.php +++ b/app/admin/controller/BabyHealth/DashboradController.php @@ -16,10 +16,32 @@ use app\model\AppsBabyhealthUsers; class DashboradController extends BaseController { /** - * 统计宝贝数量 - * @return Json + * dashborad总体输出 */ - public function getBabyCounts() + public function getDashborad() + { + try { + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'userCounts' => $this->getUserCounts(), + 'babyCounts' => $this->getBabyCounts() + ] + ]); + } catch (DbException $e) { + return json([ + 'code' => 500, + 'msg' => '获取失败:' . $e->getMessage() + ]); + } + } + + /** + * 统计宝贝数量 + * @return array + */ + private function getBabyCounts(): array { try { // 总数 @@ -34,29 +56,29 @@ class DashboradController extends BaseController // 未知性别 (sex = 0) $unknownCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 0)->count(); - return json([ - 'code' => 200, - 'msg' => '统计成功', - 'data' => [ - 'total' => $total, - 'male' => $maleCount, - 'female' => $femaleCount, - 'unknown' => $unknownCount - ] - ]); + return [ + 'total' => $total, + 'male' => $maleCount, + 'female' => $femaleCount, + 'unknown' => $unknownCount + ]; } catch (DbException $e) { - return json([ - 'code' => 500, - 'msg' => '统计失败:' . $e->getMessage() - ]); + // 返回空数组而不是抛出异常,让外层处理 + error_log('统计宝贝数量失败: ' . $e->getMessage()); + return [ + 'total' => 0, + 'male' => 0, + 'female' => 0, + 'unknown' => 0 + ]; } } /** * 统计用户数量 - * @return Json + * @return array */ - public function getUserCounts() + private function getUserCounts(): array { try { // 总数 @@ -65,27 +87,27 @@ class DashboradController extends BaseController // 父亲 (sex = 1) $fatherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 1)->count(); - // 母亲宝数 (sex = 2) + // 母亲 (sex = 2) $motherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 2)->count(); // 未知性别 (sex = 0) $unknownCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 0)->count(); - return json([ - 'code' => 200, - 'msg' => '统计成功', - 'data' => [ - 'total' => $total, - 'father' => $fatherCount, - 'mother' => $motherCount, - 'unknown' => $unknownCount - ] - ]); + return [ + 'total' => $total, + 'father' => $fatherCount, + 'mother' => $motherCount, + 'unknown' => $unknownCount + ]; } catch (DbException $e) { - return json([ - 'code' => 500, - 'msg' => '统计失败:' . $e->getMessage() - ]); + // 返回空数组而不是抛出异常,让外层处理 + error_log('统计用户数量失败: ' . $e->getMessage()); + return [ + 'total' => 0, + 'father' => 0, + 'mother' => 0, + 'unknown' => 0 + ]; } } } diff --git a/app/admin/controller/LoginController.php b/app/admin/controller/LoginController.php index 455a00b..6a04180 100644 --- a/app/admin/controller/LoginController.php +++ b/app/admin/controller/LoginController.php @@ -32,69 +32,105 @@ class LoginController extends BaseController */ public function login(): Json { - $data = $this->request->param(); - - if (isset($data['email'])) { - $data['account'] = $data['email']; - } elseif (isset($data['phone'])) { - $data['account'] = $data['phone']; - } - try { - $this->validate($data, [ - 'account|账号' => 'require|length:3,32', - 'password|密码' => 'require|length:6,32' - ]); - } catch (ValidateException $e) { - $this->logFail('登录管理', '登录', $e->getMessage()); + $data = $this->request->param(); + + if (isset($data['email'])) { + $data['account'] = $data['email']; + } elseif (isset($data['phone'])) { + $data['account'] = $data['phone']; + } + + try { + $this->validate($data, [ + 'account|账号' => 'require|length:3,32', + 'password|密码' => 'require|length:6,32' + ]); + } catch (ValidateException $e) { + $this->logFail('登录管理', '登录', $e->getMessage()); + return json([ + 'code' => 400, + 'msg' => $e->getError() + ]); + } + + $user = AdminUser::where('account', $data['account']) + ->where('status', 1) + ->where('delete_time', null) + ->find(); + + if (!$user) { + return json([ + 'code' => 401, + 'msg' => '账号不存在或已禁用' + ]); + } + + if (md5($data['password']) !== $user['password']) { + return json([ + 'code' => 401, + 'msg' => '密码错误' + ]); + } + + // 更新登录次数和IP,确保 login_count 不为 null + try { + $loginCount = isset($user['login_count']) && $user['login_count'] !== null ? (int)$user['login_count'] : 0; + AdminUser::where('id', $user['id'])->update([ + 'login_count' => $loginCount + 1, + 'last_login_ip' => $this->request->ip() + ]); + } catch (\Exception $e) { + // 更新登录信息失败不影响登录流程 + error_log('更新登录信息失败: ' . $e->getMessage()); + } + + $userInfo = [ + 'id' => $user['id'], + 'account' => $user['account'], + 'name' => $user['name'], + 'group_id' => $user['group_id'] + ]; + + try { + $token = $this->generateToken($userInfo); + } catch (\Exception $e) { + $this->logFail('登录管理', '登录', 'Token生成失败: ' . $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '登录失败,请稍后重试' + ]); + } + + // 记录日志,但不影响登录流程 + try { + $this->logSuccess('登录管理', '登录', ['id' => $user['id']], $userInfo); + } catch (\Exception $e) { + // 日志记录失败不影响登录 + error_log('登录日志记录失败: ' . $e->getMessage()); + } + return json([ - 'code' => 400, - 'msg' => $e->getError() + 'code' => 200, + 'msg' => '登录成功', + 'data' => [ + 'token' => $token, + 'user' => $userInfo + ] + ]); + } catch (\Exception $e) { + // 捕获所有未预期的错误 + error_log('登录失败: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine()); + try { + $this->logFail('登录管理', '登录', $e->getMessage()); + } catch (\Exception $logError) { + error_log('记录登录失败日志也失败: ' . $logError->getMessage()); + } + return json([ + 'code' => 500, + 'msg' => '登录失败,请稍后重试' ]); } - - $user = AdminUser::where('account', $data['account']) - ->where('status', 1) - ->where('delete_time', null) - ->find(); - - if (!$user) { - return json([ - 'code' => 401, - 'msg' => '账号不存在或已禁用' - ]); - } - - if (md5($data['password']) !== $user['password']) { - return json([ - 'code' => 401, - 'msg' => '密码错误' - ]); - } - - AdminUser::where('id', $user['id'])->update([ - 'login_count' => $user['login_count'] + 1, - 'last_login_ip' => $this->request->ip() - ]); - - $userInfo = [ - 'id' => $user['id'], - 'account' => $user['account'], - 'name' => $user['name'], - 'group_id' => $user['group_id'] - ]; - - $token = $this->generateToken($userInfo); - - $this->logSuccess('登录管理', '登录', ['id' => $user['id']], $userInfo); - return json([ - 'code' => 200, - 'msg' => '登录成功', - 'data' => [ - 'token' => $token, - 'user' => $userInfo - ] - ]); } /** diff --git a/app/admin/route/routes/babyhealth.php b/app/admin/route/routes/babyhealth.php index c88fb5b..e249e67 100644 --- a/app/admin/route/routes/babyhealth.php +++ b/app/admin/route/routes/babyhealth.php @@ -2,11 +2,11 @@ use think\facade\Route; // 宝贝管理路由 -Route::group('baby', function() { +Route::group('babys', function() { // 获取宝贝列表 Route::get('list', 'app\admin\controller\BabyHealth\BabysController/getBabyList'); // 获取宝贝详情 - Route::get('detail/:id', 'app\admin\controller\BabyHealth\BabysController/getBabyDetail'); + Route::get(':id', 'app\admin\controller\BabyHealth\BabysController/getBabyDetail'); // 创建宝贝 Route::post('create', 'app\admin\controller\BabyHealth\BabysController/createBaby'); // 更新宝贝 @@ -29,6 +29,6 @@ Route::group('babyhealthUser', function() { }); // 统计路由 -Route::group('baby-dashboard', function() { - Route::get('counts', 'app\admin\controller\BabyHealth\DashboardController/getBabyCounts'); +Route::group('babyhealthDashborad', function() { + Route::get('dashborad', 'app\admin\controller\BabyHealth\DashboradController/getDashborad'); }); \ No newline at end of file diff --git a/app/common/middleware/AllowCrossDomain.php b/app/common/middleware/AllowCrossDomain.php index 4a48222..2b94dc7 100644 --- a/app/common/middleware/AllowCrossDomain.php +++ b/app/common/middleware/AllowCrossDomain.php @@ -17,8 +17,10 @@ class AllowCrossDomain $allowedDomains = [ 'http://localhost:3000', + 'http://localhost:5000', 'http://localhost:8000', 'http://127.0.0.1:3000', + 'http://127.0.0.1:5000', 'http://127.0.0.1:8000', 'http://localhost:5173', 'http://backapi.yunzer.cn',