批量更新

This commit is contained in:
李志强 2026-02-24 10:12:05 +08:00
parent 0ed654b5b8
commit 0bf9b77aed
4 changed files with 157 additions and 97 deletions

View File

@ -16,10 +16,32 @@ use app\model\AppsBabyhealthUsers;
class DashboradController extends BaseController class DashboradController extends BaseController
{ {
/** /**
* 统计宝贝数量 * dashborad总体输出
* @return Json
*/ */
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 { try {
// 总数 // 总数
@ -34,29 +56,29 @@ class DashboradController extends BaseController
// 未知性别 (sex = 0) // 未知性别 (sex = 0)
$unknownCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 0)->count(); $unknownCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 0)->count();
return json([ return [
'code' => 200, 'total' => $total,
'msg' => '统计成功', 'male' => $maleCount,
'data' => [ 'female' => $femaleCount,
'total' => $total, 'unknown' => $unknownCount
'male' => $maleCount, ];
'female' => $femaleCount,
'unknown' => $unknownCount
]
]);
} catch (DbException $e) { } catch (DbException $e) {
return json([ // 返回空数组而不是抛出异常,让外层处理
'code' => 500, error_log('统计宝贝数量失败: ' . $e->getMessage());
'msg' => '统计失败:' . $e->getMessage() return [
]); 'total' => 0,
'male' => 0,
'female' => 0,
'unknown' => 0
];
} }
} }
/** /**
* 统计用户数量 * 统计用户数量
* @return Json * @return array
*/ */
public function getUserCounts() private function getUserCounts(): array
{ {
try { try {
// 总数 // 总数
@ -65,27 +87,27 @@ class DashboradController extends BaseController
// 父亲 (sex = 1) // 父亲 (sex = 1)
$fatherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 1)->count(); $fatherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 1)->count();
// 母亲宝数 (sex = 2) // 母亲 (sex = 2)
$motherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 2)->count(); $motherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 2)->count();
// 未知性别 (sex = 0) // 未知性别 (sex = 0)
$unknownCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 0)->count(); $unknownCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 0)->count();
return json([ return [
'code' => 200, 'total' => $total,
'msg' => '统计成功', 'father' => $fatherCount,
'data' => [ 'mother' => $motherCount,
'total' => $total, 'unknown' => $unknownCount
'father' => $fatherCount, ];
'mother' => $motherCount,
'unknown' => $unknownCount
]
]);
} catch (DbException $e) { } catch (DbException $e) {
return json([ // 返回空数组而不是抛出异常,让外层处理
'code' => 500, error_log('统计用户数量失败: ' . $e->getMessage());
'msg' => '统计失败:' . $e->getMessage() return [
]); 'total' => 0,
'father' => 0,
'mother' => 0,
'unknown' => 0
];
} }
} }
} }

View File

@ -32,69 +32,105 @@ class LoginController extends BaseController
*/ */
public function login(): Json 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 { try {
$this->validate($data, [ $data = $this->request->param();
'account|账号' => 'require|length:3,32',
'password|密码' => 'require|length:6,32' if (isset($data['email'])) {
]); $data['account'] = $data['email'];
} catch (ValidateException $e) { } elseif (isset($data['phone'])) {
$this->logFail('登录管理', '登录', $e->getMessage()); $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([ return json([
'code' => 400, 'code' => 200,
'msg' => $e->getError() '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
]
]);
} }
/** /**

View File

@ -2,11 +2,11 @@
use think\facade\Route; use think\facade\Route;
// 宝贝管理路由 // 宝贝管理路由
Route::group('baby', function() { Route::group('babys', function() {
// 获取宝贝列表 // 获取宝贝列表
Route::get('list', 'app\admin\controller\BabyHealth\BabysController/getBabyList'); 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'); Route::post('create', 'app\admin\controller\BabyHealth\BabysController/createBaby');
// 更新宝贝 // 更新宝贝
@ -29,6 +29,6 @@ Route::group('babyhealthUser', function() {
}); });
// 统计路由 // 统计路由
Route::group('baby-dashboard', function() { Route::group('babyhealthDashborad', function() {
Route::get('counts', 'app\admin\controller\BabyHealth\DashboardController/getBabyCounts'); Route::get('dashborad', 'app\admin\controller\BabyHealth\DashboradController/getDashborad');
}); });

View File

@ -17,8 +17,10 @@ class AllowCrossDomain
$allowedDomains = [ $allowedDomains = [
'http://localhost:3000', 'http://localhost:3000',
'http://localhost:5000',
'http://localhost:8000', 'http://localhost:8000',
'http://127.0.0.1:3000', 'http://127.0.0.1:3000',
'http://127.0.0.1:5000',
'http://127.0.0.1:8000', 'http://127.0.0.1:8000',
'http://localhost:5173', 'http://localhost:5173',
'http://backapi.yunzer.cn', 'http://backapi.yunzer.cn',