align API response format with maqt.top real specs: weapon-categories, weapons, schemes, schemes_aob, favorites check, encryption key derivation

This commit is contained in:
2026-05-24 01:47:08 +08:00
parent ea4e0f6e07
commit 8bcb6c7e7a
13 changed files with 1861 additions and 366 deletions

View File

@@ -1,67 +1,98 @@
import { Router, Request, Response } from 'express';
import { z } from 'zod';
import { authMiddleware, optionalAuth } from '../middleware/auth';
import { encrypt } from '../utils/encryption';
import { encrypt, encryptResponse } from '../utils/encryption';
import { prisma } from '../utils/prisma';
const router = Router();
// ---- 响应 shape 对齐 maqt.top 原版 ----
function formatScheme(s: any) {
return {
id: parseInt(s.id, 36) || String(s.id).split('-')[0] || s.id,
user_id: s.userId,
description: s.description || '',
scheme_content: s.schemeContent || '',
category: s.category || '',
weapon_name: s.weaponName || '',
price: s.price ? `${s.price}W` : null,
tags: [],
likes: s.likesCount ?? null,
uses: s.downloadsCount || 0,
status: s.status === 'PUBLISHED' ? 'normal' : null,
comments: null,
shares: null,
created_at: s.createdAt?.toISOString?.() || s.createdAt,
updated_at: s.updatedAt?.toISOString?.() || s.updatedAt,
source: s.source ?? null,
total_historical_uses: s.downloadsCount || 0,
username: s.user?.username || s.username || '',
avatar: s.user?.avatar || s.avatar || 'https://tuku.maqt.top/i/2026/03/22/sv3fg9.png',
partner_type: null,
partner_level: null,
partner_badge: null,
partner_logo: null,
social_link: null,
};
}
// ============================================
// 获取方案列表
// ============================================
router.get('/', optionalAuth, async (req: Request, res: Response) => {
try {
const {
page = 1,
limit = 20,
weapon,
category,
sort = 'newest',
page = '1',
limit = '12',
weaponCategory,
weaponName,
minPrice,
maxPrice,
search,
sort = 'hot',
} = req.query;
const pageNum = Math.max(1, parseInt(String(page)) || 1);
const limitNum = Math.min(100, Math.max(1, parseInt(String(limit)) || 20));
const limitNum = Math.min(100, Math.max(1, parseInt(String(limit)) || 12));
const skip = (pageNum - 1) * limitNum;
const where: any = { status: 'PUBLISHED' };
if (weapon) where.weaponName = { contains: String(weapon) };
if (category) where.category = String(category);
let orderBy: any = { createdAt: 'desc' };
if (sort === 'popular') orderBy = { viewsCount: 'desc' };
if (sort === 'downloads') orderBy = { downloadsCount: 'desc' };
if (weaponCategory) where.category = String(weaponCategory);
if (weaponName) where.weaponName = { contains: String(weaponName) };
if (minPrice) where.price = { ...(where.price || {}), gte: parseInt(String(minPrice)) };
if (maxPrice) where.price = { ...(where.price || {}), lte: parseInt(String(maxPrice)) };
if (search) where.description = { contains: String(search) };
let orderBy: any = { downloadsCount: 'desc' };
if (sort === 'new') orderBy = { createdAt: 'desc' };
const schemes = await prisma.scheme.findMany({
where,
orderBy,
skip,
take: Number(limit),
select: {
id: true,
title: true,
description: true,
weaponName: true,
category: true,
price: true,
viewsCount: true,
downloadsCount: true,
likesCount: true,
isOfficial: true,
createdAt: true,
user: {
select: {
id: true,
username: true,
avatar: true,
},
},
take: limitNum + 1, // fetch one extra to detect hasMore
include: {
user: { select: { id: true, username: true, avatar: true } },
},
});
res.json({
const hasMore = schemes.length > limitNum;
const data = schemes.slice(0, limitNum).map(formatScheme);
const payload = {
success: true,
data: schemes,
});
data,
pagination: { page: pageNum, limit: limitNum, hasMore },
};
// 支持加密输出
const useEncryption = req.query.encrypted === '1';
if (useEncryption) {
res.json(encryptResponse(payload));
} else {
res.json(payload);
}
} catch (error) {
console.error('Get schemes error:', error);
res.status(500).json({ success: false, message: '获取失败' });
@@ -74,61 +105,40 @@ router.get('/', optionalAuth, async (req: Request, res: Response) => {
router.get('/:id', optionalAuth, async (req: Request, res: Response) => {
try {
const { id } = req.params;
const scheme = await prisma.scheme.findUnique({
where: { id },
include: {
user: {
select: {
id: true,
username: true,
avatar: true,
},
select: { id: true, username: true, avatar: true },
},
},
});
if (!scheme) {
return res.status(404).json({
success: false,
message: '方案不存在',
});
if (!scheme || (scheme.status !== 'PUBLISHED' && scheme.userId !== req.user?.userId)) {
return res.status(404).json({ success: false, message: '方案不存在' });
}
if (scheme.status !== 'PUBLISHED' && scheme.userId !== req.user?.userId) {
return res.status(404).json({
success: false,
message: '方案不存在',
});
}
// 增加浏览量
await prisma.scheme.update({
where: { id },
data: { viewsCount: { increment: 1 } },
});
// 检查是否收藏
let isFavorited = false;
if (req.user) {
const fav = await prisma.favorite.findFirst({
where: {
userId: req.user.userId,
targetType: 'SCHEME',
targetId: id,
},
where: { userId: req.user.userId, targetType: 'SCHEME', targetId: id },
});
isFavorited = !!fav;
}
// 加密方案内容
const encryptedContent = encrypt(scheme.schemeContent);
res.json({
success: true,
data: {
...scheme,
schemeContent: encryptedContent,
...formatScheme(scheme),
scheme_content: encryptedContent,
isFavorited,
},
});
@@ -142,51 +152,55 @@ router.get('/:id', optionalAuth, async (req: Request, res: Response) => {
// 创建方案
// ============================================
const createSchemeSchema = z.object({
title: z.string().min(1).max(100),
description: z.string().optional(),
weaponName: z.string().optional(),
description: z.string().min(1).max(50),
category: z.string().optional(),
schemeContent: z.string().min(1),
price: z.number().int().min(0).default(0),
gpuModel: z.string().optional(),
driverVersion: z.string().optional(),
appVersion: z.string().optional(),
weaponName: z.string().optional(),
scheme: z.string().min(1),
price: z.union([z.number(), z.string()]).optional(),
tags: z.array(z.string()).optional().default([]),
});
router.post('/', authMiddleware, async (req: Request, res: Response) => {
try {
const body = createSchemeSchema.parse(req.body);
// 支持 x-user-info header (Base64 JSON {id, username})
const userInfoHeader = req.headers['x-user-info'] as string | undefined;
let userId = req.user!.userId;
if (userInfoHeader) {
try {
const decoded = JSON.parse(Buffer.from(userInfoHeader, 'base64').toString('utf-8'));
if (decoded.id && decoded.username) {
// 用 header 中的 id 查找对应本地用户
const localUser = await prisma.user.findUnique({ where: { username: decoded.username } });
if (localUser) userId = localUser.id;
}
} catch { /* ignore */ }
}
const priceNum = typeof body.price === 'string' ? parseInt(body.price.replace(/[W万]/g, '')) : (body.price || 0);
const scheme = await prisma.scheme.create({
data: {
userId: req.user!.userId,
title: body.title,
userId,
description: body.description,
weaponName: body.weaponName,
category: body.category,
schemeContent: body.schemeContent,
price: body.price,
gpuModel: body.gpuModel,
driverVersion: body.driverVersion,
appVersion: body.appVersion,
schemeContent: body.scheme,
price: priceNum,
status: 'PUBLISHED',
},
});
// 更新用户方案数
await prisma.user.update({
where: { id: req.user!.userId },
where: { id: userId },
data: { schemesCount: { increment: 1 } },
});
res.json({
success: true,
message: '方案创建成功',
data: scheme,
});
res.json({ success: true, message: '方案发布成功', data: formatScheme(scheme) });
} catch (error) {
console.error('Create scheme error:', error);
res.status(500).json({ success: false, message: '创建失败' });
res.status(500).json({ success: false, message: '发布失败' });
}
});
@@ -196,89 +210,72 @@ router.post('/', authMiddleware, async (req: Request, res: Response) => {
router.delete('/:id', authMiddleware, async (req: Request, res: Response) => {
try {
const { id } = req.params;
const scheme = await prisma.scheme.findUnique({
where: { id },
select: { userId: true },
});
if (!scheme) {
return res.status(404).json({
success: false,
message: '方案不存在',
});
}
if (scheme.userId !== req.user!.userId) {
return res.status(403).json({
success: false,
message: '无权删除此方案',
});
}
await prisma.scheme.update({
where: { id },
data: { status: 'DELETED' },
});
await prisma.user.update({
where: { id: req.user!.userId },
data: { schemesCount: { decrement: 1 } },
});
res.json({
success: true,
message: '方案已删除',
});
const scheme = await prisma.scheme.findUnique({ where: { id }, select: { userId: true } });
if (!scheme) return res.status(404).json({ success: false, message: '方案不存在' });
if (scheme.userId !== req.user!.userId) return res.status(403).json({ success: false, message: '无权删除' });
await prisma.scheme.update({ where: { id }, data: { status: 'DELETED' } });
await prisma.user.update({ where: { id: req.user!.userId }, data: { schemesCount: { decrement: 1 } } });
res.json({ success: true, message: '方案已删除' });
} catch (error) {
console.error('Delete scheme error:', error);
res.status(500).json({ success: false, message: '删除失败' });
}
});
// 记录方案使用(增加下载计数)
router.post('/:id/use', authMiddleware, async (req: Request, res: Response) => {
// ============================================
// 记录使用 (POST /api/schemes/:id/use)
// ============================================
router.post('/:id/use', optionalAuth, async (req: Request, res: Response) => {
try {
const { id } = req.params;
const { source } = req.body;
const scheme = await prisma.scheme.findUnique({
where: { id },
select: { id: true, downloadsCount: true },
});
if (!scheme) {
return res.status(404).json({
success: false,
message: '方案不存在',
});
}
// 使用计数 +1增加 downloads 计数)
const scheme = await prisma.scheme.findUnique({ where: { id }, select: { id: true } });
if (!scheme) return res.status(404).json({ success: false, message: '方案不存在' });
const updated = await prisma.scheme.update({
where: { id },
data: { downloadsCount: { increment: 1 } },
select: { downloadsCount: true },
});
// 记录日志
await prisma.userLog.create({
data: {
userId: req.user!.userId,
action: 'SchemeUse',
targetType: 'Scheme',
targetId: id,
},
});
res.json({
success: true,
downloadsCount: updated.downloadsCount,
});
if (req.user) {
await prisma.userLog.create({
data: { userId: req.user.userId, action: 'SchemeUse', targetType: 'Scheme', targetId: id },
});
}
res.json({ success: true, message: '使用次数已记录', downloadsCount: updated.downloadsCount });
} catch (error) {
console.error('Scheme use error:', error);
res.status(500).json({ success: false, message: '记录失败' });
}
});
// ============================================
// 举报 (POST /api/schemes/:id/report)
// ============================================
router.post('/:id/report', authMiddleware, async (req: Request, res: Response) => {
try {
const { id } = req.params;
const { reason, description } = req.body;
const scheme = await prisma.scheme.findUnique({ where: { id }, select: { id: true } });
if (!scheme) return res.status(404).json({ success: false, message: '方案不存在' });
await prisma.userLog.create({
data: {
userId: req.user!.userId,
action: 'Report',
targetType: 'Scheme',
targetId: id,
},
});
res.json({ success: true, message: '举报成功' });
} catch (error) {
console.error('Report error:', error);
res.status(500).json({ success: false, message: '举报失败' });
}
});
export default router;