優化管理者用戶列表、統計功能
This commit is contained in:
@@ -13,17 +13,54 @@ export async function GET(request: NextRequest) {
|
||||
const page = Math.max(1, parseInt(searchParams.get('page') || '1', 10));
|
||||
const limit = Math.max(1, Math.min(100, parseInt(searchParams.get('limit') || '20', 10)));
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
// 查詢用戶總數
|
||||
const countResult = await db.queryOne<{ total: number }>('SELECT COUNT(*) as total FROM users');
|
||||
const total = countResult?.total || 0;
|
||||
// 查詢用戶列表
|
||||
const users = await db.query(
|
||||
`SELECT id, name, email, avatar, department, role, join_date, total_likes, total_views, created_at, updated_at FROM users ORDER BY created_at DESC LIMIT ${limit} OFFSET ${offset}`
|
||||
);
|
||||
|
||||
// 查詢用戶列表,包含應用和評價統計
|
||||
const users = await db.query(`
|
||||
SELECT
|
||||
u.id,
|
||||
u.name,
|
||||
u.email,
|
||||
u.avatar,
|
||||
u.department,
|
||||
u.role,
|
||||
u.join_date,
|
||||
u.total_likes,
|
||||
u.total_views,
|
||||
u.created_at,
|
||||
u.updated_at,
|
||||
COUNT(DISTINCT a.id) as total_apps,
|
||||
COUNT(DISTINCT js.id) as total_reviews
|
||||
FROM users u
|
||||
LEFT JOIN apps a ON u.id = a.creator_id
|
||||
LEFT JOIN judge_scores js ON u.id = js.judge_id
|
||||
GROUP BY u.id
|
||||
ORDER BY u.created_at DESC
|
||||
LIMIT ${limit} OFFSET ${offset}
|
||||
`);
|
||||
|
||||
// 分頁資訊
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
const hasNext = page < totalPages;
|
||||
const hasPrev = page > 1;
|
||||
|
||||
// 格式化日期函數
|
||||
const formatDate = (dateString: string | null) => {
|
||||
if (!dateString) return "-";
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleString('zh-TW', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false
|
||||
}).replace(/\//g, '/');
|
||||
};
|
||||
|
||||
return NextResponse.json({
|
||||
users: users.map(user => ({
|
||||
id: user.id,
|
||||
@@ -32,11 +69,14 @@ export async function GET(request: NextRequest) {
|
||||
avatar: user.avatar,
|
||||
department: user.department,
|
||||
role: user.role,
|
||||
joinDate: user.join_date,
|
||||
totalLikes: user.total_likes,
|
||||
totalViews: user.total_views,
|
||||
createdAt: user.created_at,
|
||||
updatedAt: user.updated_at
|
||||
status: "active", // 預設狀態為活躍
|
||||
joinDate: formatDate(user.join_date),
|
||||
lastLogin: formatDate(user.updated_at), // 使用 updated_at 作為最後登入時間
|
||||
totalApps: user.total_apps || 0,
|
||||
totalReviews: user.total_reviews || 0,
|
||||
totalLikes: user.total_likes || 0,
|
||||
createdAt: formatDate(user.created_at),
|
||||
updatedAt: formatDate(user.updated_at)
|
||||
})),
|
||||
pagination: { page, limit, total, totalPages, hasNext, hasPrev }
|
||||
});
|
||||
|
@@ -5,17 +5,26 @@ import { db } from '@/lib/database';
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
await requireAdmin(request);
|
||||
|
||||
// 基本用戶統計
|
||||
const total = await db.queryOne<{ count: number }>('SELECT COUNT(*) as count FROM users');
|
||||
const admin = await db.queryOne<{ count: number }>("SELECT COUNT(*) as count FROM users WHERE role = 'admin'");
|
||||
const developer = await db.queryOne<{ count: number }>("SELECT COUNT(*) as count FROM users WHERE role = 'developer'");
|
||||
const user = await db.queryOne<{ count: number }>("SELECT COUNT(*) as count FROM users WHERE role = 'user'");
|
||||
const today = await db.queryOne<{ count: number }>("SELECT COUNT(*) as count FROM users WHERE join_date = CURDATE()");
|
||||
|
||||
// 應用和評價統計
|
||||
const totalApps = await db.queryOne<{ count: number }>('SELECT COUNT(*) as count FROM apps');
|
||||
const totalReviews = await db.queryOne<{ count: number }>('SELECT COUNT(*) as count FROM judge_scores');
|
||||
|
||||
return NextResponse.json({
|
||||
total: total?.count || 0,
|
||||
admin: admin?.count || 0,
|
||||
developer: developer?.count || 0,
|
||||
user: user?.count || 0,
|
||||
today: today?.count || 0
|
||||
today: today?.count || 0,
|
||||
totalApps: totalApps?.count || 0,
|
||||
totalReviews: totalReviews?.count || 0
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: '內部伺服器錯誤', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 });
|
||||
|
Reference in New Issue
Block a user