69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { auth } from '@/lib/auth'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
// GET /api/wx-users - Get all wx users with optional search and pagination
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await auth.api.getSession({
|
|
headers: request.headers,
|
|
})
|
|
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const search = searchParams.get('search') || ''
|
|
const page = parseInt(searchParams.get('page') || '1', 10)
|
|
const limit = parseInt(searchParams.get('limit') || '20', 10)
|
|
const skip = (page - 1) * limit
|
|
|
|
const where = search
|
|
? {
|
|
OR: [
|
|
{ nickname: { contains: search } },
|
|
{ openid: { contains: search } },
|
|
],
|
|
}
|
|
: {}
|
|
|
|
const [users, total] = await Promise.all([
|
|
prisma.wxUser.findMany({
|
|
where,
|
|
skip,
|
|
take: limit,
|
|
orderBy: { createdAt: 'desc' },
|
|
select: {
|
|
id: true,
|
|
openid: true,
|
|
nickname: true,
|
|
avatarUrl: true,
|
|
points: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
},
|
|
}),
|
|
prisma.wxUser.count({ where }),
|
|
])
|
|
|
|
return NextResponse.json({
|
|
users,
|
|
meta: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit),
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('Error fetching wx users:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch wx users' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|