批量更新
This commit is contained in:
parent
0ed654b5b8
commit
0bf9b77aed
@ -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
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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');
|
||||
});
|
||||
@ -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',
|
||||
|
||||
Loading…
Reference in New Issue
Block a user