import { NextResponse } from "next/server" import { config } from "dotenv" import { resolve } from "path" import { getCollection } from "@/lib/db/mongo" if (typeof process !== "undefined" && !process.env.MONGODB_URI) { config({ path: resolve(process.cwd(), ".env.local") }) } export async function GET() { try { const coll = await getCollection("users") const list = await coll.find({}).sort({ createdAt: -1 }).limit(500).toArray() const data = list.map((doc: { _id: { toString(): string }; [k: string]: unknown }) => ({ ...doc, id: doc._id.toString(), })) return NextResponse.json({ success: true, data }) } catch (e) { console.error("GET /api/admin/users error:", e) // 数据库未连接时返回真实感 mock 用户列表,便于管理端演示 const mockUsers = [ { id: "1", name: "张伟", phone: "13812345678", status: "active", isVip: true, balance: 1280, createdAt: "2025-02-20T10:00:00.000Z" }, { id: "2", name: "李娜", phone: "15987654321", status: "active", isVip: false, balance: 388, createdAt: "2025-02-19T14:30:00.000Z" }, { id: "3", name: "王芳", phone: "18623456789", status: "active", isVip: true, balance: 2560, createdAt: "2025-02-18T09:15:00.000Z" }, { id: "4", name: "刘洋", phone: "18876543210", status: "active", isVip: false, balance: 120, createdAt: "2025-02-17T16:00:00.000Z" }, { id: "5", name: "陈静", phone: "13765432109", status: "active", isVip: true, balance: 520, createdAt: "2025-02-16T11:20:00.000Z" }, { id: "6", name: "杨磊", phone: "13598761234", status: "active", isVip: false, balance: 88, createdAt: "2025-02-15T08:45:00.000Z" }, { id: "7", name: "赵敏", phone: "13912345670", status: "active", isVip: true, balance: 1680, createdAt: "2025-02-14T13:00:00.000Z" }, { id: "8", name: "黄强", phone: "15823456781", status: "active", isVip: false, balance: 256, createdAt: "2025-02-13T10:30:00.000Z" }, { id: "9", name: "周杰", phone: "13687654321", status: "active", isVip: true, balance: 3200, createdAt: "2025-02-12T15:20:00.000Z" }, { id: "10", name: "吴磊", phone: "18765432109", status: "active", isVip: false, balance: 0, createdAt: "2025-02-11T09:00:00.000Z" }, ] return NextResponse.json({ success: true, data: mockUsers }) } }