来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
93 lines
3.7 KiB
TypeScript
93 lines
3.7 KiB
TypeScript
/**
|
|
* 通用数据网关(/api/data)的 HTTP 边界。
|
|
*
|
|
* 本文件只做三件事:入参形状校验、把错误映射成 HTTP 状态码、出响应。
|
|
* 策略判定、租户作用域、SQL 编排、事务、缓存与审计都在
|
|
* services/dataGatewayService.ts —— 别再把持久化逻辑写回这里。
|
|
*/
|
|
import { Router, Response } from 'express';
|
|
import type { QuerySpec } from '../services/queryExecutor';
|
|
import { readOperatingBranchCode, loadTenantContext } from '../services/tenantScope';
|
|
import { authMiddleware, AuthRequest } from '../middleware/auth';
|
|
import {
|
|
MAX_BATCH_OPERATIONS,
|
|
formatGatewayError,
|
|
runDataBatch,
|
|
runDataQuery,
|
|
} from '../services/dataGatewayService';
|
|
|
|
const router = Router();
|
|
|
|
// GET 请求兜底:若因网络层(Nginx/CDN)将 POST 误转为 GET,返回结构化错误而非默认 404 文本
|
|
router.get('/query', authMiddleware, (_req: AuthRequest, res: Response) => {
|
|
return res.status(405).json({
|
|
data: null,
|
|
error: { message: '数据接口仅支持 POST 请求,当前请求被转为 GET,请检查网络或联系管理员', code: 'METHOD_NOT_ALLOWED' },
|
|
count: null,
|
|
});
|
|
});
|
|
|
|
router.post('/query', authMiddleware, async (req: AuthRequest, res: Response) => {
|
|
const table = (req.body as QuerySpec | undefined)?.table;
|
|
try {
|
|
if (req.tokenScope === 'mobile') {
|
|
throw Object.assign(new Error('移动端 token 无权访问通用数据接口'), { code: 'forbidden' });
|
|
}
|
|
const ctx = await loadTenantContext(req.userId);
|
|
const operatingBranch = readOperatingBranchCode(req);
|
|
const result = await runDataQuery(req.body as QuerySpec, { ctx, operatingBranch });
|
|
return res.json(result);
|
|
} catch (error: any) {
|
|
console.error('Query error:', table, error);
|
|
if (error?.code === 'forbidden') {
|
|
return res.status(403).json(formatGatewayError(error, table));
|
|
}
|
|
if (error?.code === 'bad_request') {
|
|
return res.status(400).json(formatGatewayError(error, table));
|
|
}
|
|
// 与 Supabase SDK 一致:业务/校验错误走 200 + error 字段,避免控制台 HTTP 400 噪音
|
|
return res.status(200).json(formatGatewayError(error, table));
|
|
}
|
|
});
|
|
|
|
/** 事务性批量写:所有操作在单个数据库事务中执行,任一失败全部回滚 */
|
|
router.post('/batch', authMiddleware, async (req: AuthRequest, res: Response) => {
|
|
if (req.tokenScope === 'mobile') {
|
|
return res.status(403).json({ data: null, error: { message: '移动端 token 无权访问通用数据接口' } });
|
|
}
|
|
const operations = req.body?.operations;
|
|
if (!Array.isArray(operations) || operations.length === 0) {
|
|
return res.status(400).json({ data: null, error: { message: '缺少操作列表' } });
|
|
}
|
|
if (operations.length > MAX_BATCH_OPERATIONS) {
|
|
return res.status(400).json({ data: null, error: { message: `批量操作不能超过 ${MAX_BATCH_OPERATIONS} 条` } });
|
|
}
|
|
|
|
try {
|
|
const ctx = await loadTenantContext(req.userId);
|
|
const operatingBranch = readOperatingBranchCode(req);
|
|
const results = await runDataBatch(operations, { ctx, operatingBranch });
|
|
return res.json({ data: results, error: null });
|
|
} catch (error: any) {
|
|
console.error('Batch error:', error);
|
|
if (error?.code === 'bad_request') {
|
|
return res.status(400).json({
|
|
data: null,
|
|
error: { message: error?.message || '批量操作失败', code: 'bad_request' },
|
|
});
|
|
}
|
|
if (error?.code === 'forbidden') {
|
|
return res.status(403).json({
|
|
data: null,
|
|
error: { message: error?.message || '批量操作失败', code: 'forbidden' },
|
|
});
|
|
}
|
|
return res.status(200).json({
|
|
data: null,
|
|
error: { message: error?.message || '批量操作失败', code: 'PGRST000' },
|
|
});
|
|
}
|
|
});
|
|
|
|
export default router;
|