yunzer/app/admin/controller/UsersController.php
2025-05-19 21:51:21 +08:00

81 lines
2.4 KiB
PHP

<?php
/**
* 后台管理系统-用户管理
*/
namespace app\admin\controller;
use app\admin\controller\BaseController;
use think\facade\View;
use think\facade\Request;
use think\facade\Db;
use app\admin\controller\Log;
use think\App;
use app\admin\model\User\Users;
class UsersController extends BaseController
{
/**
* 统计用户数量
*/
public function counts()
{
try {
// 统计用户总数
$total = Users::where('delete_time', 0)
->where('status', 1)
->count();
// 获取今日新增用户数
$today = strtotime(date('Y-m-d'));
$todayNew = Users::where('delete_time', 0)
->where('status', 1)
->where('create_time', '>=', $today)
->count();
// 获取最近7天的用户数据
$dates = [];
$counts = [];
$totalCounts = []; // 存储每天的总用户数
for ($i = 6; $i >= 0; $i--) {
$date = date('Y-m-d', strtotime("-$i days"));
$start = strtotime($date);
$end = $start + 86400;
// 获取当天新增用户数
$count = Users::where('delete_time', 0)
->where('status', 1)
->where('create_time', '>=', $start)
->where('create_time', '<', $end)
->count();
// 获取截至当天的总用户数
$totalCount = Users::where('delete_time', 0)
->where('status', 1)
->where('create_time', '<', $end)
->count();
$dates[] = $date;
$counts[] = $count;
$totalCounts[] = $totalCount;
}
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'total' => $total,
'todayNew' => $todayNew,
'dates' => $dates,
'counts' => $counts,
'totalCounts' => $totalCounts
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
}