優化管理者用戶列表、統計功能
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 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 limit = Math.max(1, Math.min(100, parseInt(searchParams.get('limit') || '20', 10)));
|
||||||
const offset = (page - 1) * limit;
|
const offset = (page - 1) * limit;
|
||||||
|
|
||||||
// 查詢用戶總數
|
// 查詢用戶總數
|
||||||
const countResult = await db.queryOne<{ total: number }>('SELECT COUNT(*) as total FROM users');
|
const countResult = await db.queryOne<{ total: number }>('SELECT COUNT(*) as total FROM users');
|
||||||
const total = countResult?.total || 0;
|
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 totalPages = Math.ceil(total / limit);
|
||||||
const hasNext = page < totalPages;
|
const hasNext = page < totalPages;
|
||||||
const hasPrev = page > 1;
|
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({
|
return NextResponse.json({
|
||||||
users: users.map(user => ({
|
users: users.map(user => ({
|
||||||
id: user.id,
|
id: user.id,
|
||||||
@@ -32,11 +69,14 @@ export async function GET(request: NextRequest) {
|
|||||||
avatar: user.avatar,
|
avatar: user.avatar,
|
||||||
department: user.department,
|
department: user.department,
|
||||||
role: user.role,
|
role: user.role,
|
||||||
joinDate: user.join_date,
|
status: "active", // 預設狀態為活躍
|
||||||
totalLikes: user.total_likes,
|
joinDate: formatDate(user.join_date),
|
||||||
totalViews: user.total_views,
|
lastLogin: formatDate(user.updated_at), // 使用 updated_at 作為最後登入時間
|
||||||
createdAt: user.created_at,
|
totalApps: user.total_apps || 0,
|
||||||
updatedAt: user.updated_at
|
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 }
|
pagination: { page, limit, total, totalPages, hasNext, hasPrev }
|
||||||
});
|
});
|
||||||
|
@@ -5,17 +5,26 @@ import { db } from '@/lib/database';
|
|||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
await requireAdmin(request);
|
await requireAdmin(request);
|
||||||
|
|
||||||
|
// 基本用戶統計
|
||||||
const total = await db.queryOne<{ count: number }>('SELECT COUNT(*) as count FROM users');
|
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 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 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 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 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({
|
return NextResponse.json({
|
||||||
total: total?.count || 0,
|
total: total?.count || 0,
|
||||||
admin: admin?.count || 0,
|
admin: admin?.count || 0,
|
||||||
developer: developer?.count || 0,
|
developer: developer?.count || 0,
|
||||||
user: user?.count || 0,
|
user: user?.count || 0,
|
||||||
today: today?.count || 0
|
today: today?.count || 0,
|
||||||
|
totalApps: totalApps?.count || 0,
|
||||||
|
totalReviews: totalReviews?.count || 0
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return NextResponse.json({ error: '內部伺服器錯誤', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 });
|
return NextResponse.json({ error: '內部伺服器錯誤', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 });
|
||||||
|
@@ -63,7 +63,9 @@ export function UserManagement() {
|
|||||||
admin: 0,
|
admin: 0,
|
||||||
developer: 0,
|
developer: 0,
|
||||||
user: 0,
|
user: 0,
|
||||||
today: 0
|
today: 0,
|
||||||
|
totalApps: 0,
|
||||||
|
totalReviews: 0
|
||||||
})
|
})
|
||||||
|
|
||||||
// 載入用戶資料
|
// 載入用戶資料
|
||||||
@@ -440,7 +442,7 @@ export function UserManagement() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Stats Cards */}
|
{/* Stats Cards */}
|
||||||
<div className="grid grid-cols-1 md:grid-cols-6 gap-4">
|
<div className="grid grid-cols-1 md:grid-cols-8 gap-4">
|
||||||
<Card>
|
<Card>
|
||||||
<CardContent className="p-4">
|
<CardContent className="p-4">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
@@ -524,6 +526,34 @@ export function UserManagement() {
|
|||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<CardContent className="p-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-600">應用</p>
|
||||||
|
<p className="text-2xl font-bold">{stats.totalApps}</p>
|
||||||
|
</div>
|
||||||
|
<div className="w-8 h-8 bg-indigo-100 rounded-full flex items-center justify-center">
|
||||||
|
<Code className="w-4 h-4 text-indigo-600" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<CardContent className="p-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-600">評價</p>
|
||||||
|
<p className="text-2xl font-bold">{stats.totalReviews}</p>
|
||||||
|
</div>
|
||||||
|
<div className="w-8 h-8 bg-pink-100 rounded-full flex items-center justify-center">
|
||||||
|
<Activity className="w-4 h-4 text-pink-600" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Filters */}
|
{/* Filters */}
|
||||||
|
Reference in New Issue
Block a user