|
@@ -1,597 +0,0 @@
|
|
|
-import { OpenAPIHono } from '@hono/zod-openapi';
|
|
|
|
|
-import { DataSource, In } from 'typeorm';
|
|
|
|
|
-import { AuthContext } from '@d8d/shared-types';
|
|
|
|
|
-import { authMiddleware } from '@d8d/auth-module-mt';
|
|
|
|
|
-import { PrinterService } from '../services/printer.service';
|
|
|
|
|
-import { PrintTaskService } from '../services/print-task.service';
|
|
|
|
|
-import { DelaySchedulerService } from '../services/delay-scheduler.service';
|
|
|
|
|
-import { ConfigService } from '../services/config.service';
|
|
|
|
|
-import { FeieApiConfig } from '../types/feie.types';
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * 获取飞鹅API配置
|
|
|
|
|
- */
|
|
|
|
|
-async function getFeieApiConfig(tenantId: number, dataSource: DataSource): Promise<FeieApiConfig | null> {
|
|
|
|
|
- try {
|
|
|
|
|
- console.debug(`开始获取租户 ${tenantId} 的飞鹅API配置`);
|
|
|
|
|
-
|
|
|
|
|
- // 从 system_config_mt 表获取飞鹅API配置
|
|
|
|
|
- const configKeys = [
|
|
|
|
|
- 'feie.api.user', // 飞鹅API用户
|
|
|
|
|
- 'feie.api.ukey', // 飞鹅API密钥
|
|
|
|
|
- 'feie.api.base_url', // 飞鹅API基础URL
|
|
|
|
|
- 'feie.api.timeout', // API超时时间
|
|
|
|
|
- 'feie.api.max_retries' // 最大重试次数
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
- // 直接查询数据库
|
|
|
|
|
- const configRepository = dataSource.getRepository('system_config_mt');
|
|
|
|
|
- console.debug(`查询数据库,租户ID: ${tenantId}, 配置键: ${configKeys.join(', ')}`);
|
|
|
|
|
-
|
|
|
|
|
- const configs = await configRepository.find({
|
|
|
|
|
- where: {
|
|
|
|
|
- tenantId,
|
|
|
|
|
- configKey: In(configKeys)
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- console.debug(`找到 ${configs.length} 条配置记录`);
|
|
|
|
|
-
|
|
|
|
|
- // 转换为键值对
|
|
|
|
|
- const configMap: Record<string, string> = {};
|
|
|
|
|
- configs.forEach((config: any) => {
|
|
|
|
|
- configMap[config.configKey] = config.configValue;
|
|
|
|
|
- console.debug(`配置键: ${config.configKey}, 值: ${config.configValue}`);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 检查必要的配置项
|
|
|
|
|
- const user = configMap['feie.api.user'];
|
|
|
|
|
- const ukey = configMap['feie.api.ukey'];
|
|
|
|
|
-
|
|
|
|
|
- if (!user || !ukey) {
|
|
|
|
|
- console.warn(`[租户${tenantId}] 飞鹅API配置不完整,缺少user或ukey`);
|
|
|
|
|
- console.debug(`当前配置映射:`, JSON.stringify(configMap, null, 2));
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 返回配置结构
|
|
|
|
|
- const feieConfig = {
|
|
|
|
|
- user,
|
|
|
|
|
- ukey,
|
|
|
|
|
- baseUrl: configMap['feie.api.base_url'] || 'https://api.feieyun.cn/Api/Open/',
|
|
|
|
|
- timeout: parseInt(configMap['feie.api.timeout'] || '10000', 10),
|
|
|
|
|
- maxRetries: parseInt(configMap['feie.api.max_retries'] || '3', 10)
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- console.debug(`租户 ${tenantId} 的飞鹅API配置获取成功:`, JSON.stringify(feieConfig, null, 2));
|
|
|
|
|
- return feieConfig;
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.warn(`[租户${tenantId}] 获取飞鹅API配置失败:`, error);
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-export function createFeieRoutes(dataSource: DataSource) {
|
|
|
|
|
- const app = new OpenAPIHono<AuthContext>();
|
|
|
|
|
-
|
|
|
|
|
- // 添加鉴权中间件
|
|
|
|
|
- app.use('*', authMiddleware);
|
|
|
|
|
-
|
|
|
|
|
- // 初始化服务(使用空配置,实际配置在路由处理中动态获取)
|
|
|
|
|
- const printTaskService = new PrintTaskService(dataSource, {
|
|
|
|
|
- baseUrl: 'https://api.feieyun.cn/Api/Open/',
|
|
|
|
|
- user: '',
|
|
|
|
|
- ukey: '',
|
|
|
|
|
- timeout: 10000,
|
|
|
|
|
- maxRetries: 3
|
|
|
|
|
- });
|
|
|
|
|
- const configService = new ConfigService(dataSource);
|
|
|
|
|
-
|
|
|
|
|
- // 打印机管理路由
|
|
|
|
|
- app.get('/printers', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- console.debug('获取打印机列表 - 用户对象:', JSON.stringify(user, null, 2));
|
|
|
|
|
- const tenantId = user?.tenantId || 1; // 从认证中间件获取
|
|
|
|
|
- console.debug(`获取打印机列表 - 租户ID: ${tenantId}`);
|
|
|
|
|
-
|
|
|
|
|
- // 获取查询参数
|
|
|
|
|
- const query = c.req.query();
|
|
|
|
|
- const page = query.page ? parseInt(query.page, 10) : 1;
|
|
|
|
|
- const pageSize = query.pageSize ? parseInt(query.pageSize, 10) : 10;
|
|
|
|
|
- const search = query.search;
|
|
|
|
|
- const status = query.status as any;
|
|
|
|
|
- const printerType = query.printerType as any;
|
|
|
|
|
- const isDefault = query.isDefault === 'true';
|
|
|
|
|
-
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- console.error(`租户 ${tenantId} 的飞鹅API配置未找到或配置不完整`);
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- console.debug(`租户 ${tenantId} 的飞鹅API配置获取成功`);
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的服务实例
|
|
|
|
|
- const tenantPrinterService = new PrinterService(dataSource, feieConfig);
|
|
|
|
|
- const printers = await tenantPrinterService.getPrinters(tenantId);
|
|
|
|
|
-
|
|
|
|
|
- // 应用筛选逻辑
|
|
|
|
|
- let filteredPrinters = printers;
|
|
|
|
|
-
|
|
|
|
|
- // 搜索筛选
|
|
|
|
|
- if (search) {
|
|
|
|
|
- filteredPrinters = filteredPrinters.filter(p =>
|
|
|
|
|
- (p.printerName && p.printerName.includes(search)) ||
|
|
|
|
|
- p.printerSn.includes(search)
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 状态筛选
|
|
|
|
|
- if (status && status !== 'ALL') {
|
|
|
|
|
- filteredPrinters = filteredPrinters.filter(p => p.printerStatus === status);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 类型筛选
|
|
|
|
|
- if (printerType && printerType !== 'ALL') {
|
|
|
|
|
- filteredPrinters = filteredPrinters.filter(p => p.printerType === printerType);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 默认打印机筛选
|
|
|
|
|
- if (isDefault) {
|
|
|
|
|
- filteredPrinters = filteredPrinters.filter(p => p.isDefault === 1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 分页逻辑
|
|
|
|
|
- const total = filteredPrinters.length;
|
|
|
|
|
- const startIndex = (page - 1) * pageSize;
|
|
|
|
|
- const endIndex = Math.min(startIndex + pageSize, total);
|
|
|
|
|
- const paginatedPrinters = filteredPrinters.slice(startIndex, endIndex);
|
|
|
|
|
-
|
|
|
|
|
- console.debug(`返回打印机列表,总数: ${total}, 分页: ${page}/${Math.ceil(total/pageSize)}, 数量: ${paginatedPrinters.length}`);
|
|
|
|
|
-
|
|
|
|
|
- return c.json({
|
|
|
|
|
- success: true,
|
|
|
|
|
- data: {
|
|
|
|
|
- data: paginatedPrinters,
|
|
|
|
|
- total,
|
|
|
|
|
- page,
|
|
|
|
|
- pageSize
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.post('/printers', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- const body = await c.req.json();
|
|
|
|
|
-
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的服务实例
|
|
|
|
|
- const tenantPrinterService = new PrinterService(dataSource, feieConfig);
|
|
|
|
|
- const printer = await tenantPrinterService.addPrinter(tenantId, body);
|
|
|
|
|
- return c.json({ success: true, data: printer });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 添加打印机失败:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '添加打印机失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.get('/printers/:printerSn', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const printerSn = c.req.param('printerSn');
|
|
|
|
|
-
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的服务实例
|
|
|
|
|
- const tenantPrinterService = new PrinterService(dataSource, feieConfig);
|
|
|
|
|
- const printer = await tenantPrinterService.repository.findOne({
|
|
|
|
|
- where: { tenantId, printerSn } as any
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- if (!printer) {
|
|
|
|
|
- return c.json({ success: false, message: '打印机不存在' }, 404);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return c.json({ success: true, data: printer });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.put('/printers/:printerSn', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const printerSn = c.req.param('printerSn');
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- const body = await c.req.json();
|
|
|
|
|
-
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的服务实例
|
|
|
|
|
- const tenantPrinterService = new PrinterService(dataSource, feieConfig);
|
|
|
|
|
- const printer = await tenantPrinterService.updatePrinter(tenantId, printerSn, body);
|
|
|
|
|
- return c.json({ success: true, data: printer });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 更新打印机失败,打印机SN: ${printerSn}:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '更新打印机失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.delete('/printers/:printerSn', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const printerSn = c.req.param('printerSn');
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的服务实例
|
|
|
|
|
- const tenantPrinterService = new PrinterService(dataSource, feieConfig);
|
|
|
|
|
- await tenantPrinterService.deletePrinter(tenantId, printerSn);
|
|
|
|
|
- return c.json({ success: true, message: '打印机删除成功' });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 删除打印机失败,打印机SN: ${printerSn}:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '删除打印机失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.get('/printers/:printerSn/status', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const printerSn = c.req.param('printerSn');
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的服务实例
|
|
|
|
|
- const tenantPrinterService = new PrinterService(dataSource, feieConfig);
|
|
|
|
|
- const status = await tenantPrinterService.getPrinterStatus(tenantId, printerSn);
|
|
|
|
|
- return c.json({ success: true, data: status });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 查询打印机状态失败,打印机SN: ${printerSn}:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '查询打印机状态失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.post('/printers/:printerSn/set-default', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const printerSn = c.req.param('printerSn');
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的服务实例
|
|
|
|
|
- const tenantPrinterService = new PrinterService(dataSource, feieConfig);
|
|
|
|
|
- const printer = await tenantPrinterService.setDefaultPrinter(tenantId, printerSn);
|
|
|
|
|
- return c.json({ success: true, data: printer });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 设置默认打印机失败,打印机SN: ${printerSn}:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '设置默认打印机失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 打印任务管理路由
|
|
|
|
|
- app.post('/tasks', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- const body = await c.req.json();
|
|
|
|
|
-
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的打印任务服务实例
|
|
|
|
|
- const tenantPrintTaskService = new PrintTaskService(dataSource, feieConfig);
|
|
|
|
|
- const task = await tenantPrintTaskService.createPrintTask(tenantId, body);
|
|
|
|
|
- return c.json({ success: true, data: task });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 创建打印任务失败:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '创建打印任务失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.get('/tasks', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- const query = c.req.query();
|
|
|
|
|
- const filters = {
|
|
|
|
|
- orderId: query.orderId ? parseInt(query.orderId, 10) : undefined,
|
|
|
|
|
- printerSn: query.printerSn,
|
|
|
|
|
- printType: query.printType as any,
|
|
|
|
|
- printStatus: query.status as any, // 前端传递的是status参数
|
|
|
|
|
- startDate: query.startDate ? new Date(query.startDate) : undefined,
|
|
|
|
|
- endDate: query.endDate ? new Date(query.endDate) : undefined,
|
|
|
|
|
- search: query.search
|
|
|
|
|
- };
|
|
|
|
|
- const page = query.page ? parseInt(query.page, 10) : 1;
|
|
|
|
|
- const limit = query.pageSize ? parseInt(query.pageSize, 10) : 20; // 前端传递的是pageSize参数
|
|
|
|
|
-
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的打印任务服务实例
|
|
|
|
|
- const tenantPrintTaskService = new PrintTaskService(dataSource, feieConfig);
|
|
|
|
|
- const result = await tenantPrintTaskService.getPrintTasks(tenantId, filters, page, limit);
|
|
|
|
|
- return c.json({
|
|
|
|
|
- success: true,
|
|
|
|
|
- data: {
|
|
|
|
|
- data: result.tasks,
|
|
|
|
|
- total: result.total,
|
|
|
|
|
- page,
|
|
|
|
|
- pageSize: limit
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 获取打印任务列表失败:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '获取打印任务列表失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.get('/tasks/:taskId', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const taskId = c.req.param('taskId');
|
|
|
|
|
- const task = await printTaskService.repository.findOne({
|
|
|
|
|
- where: { tenantId, taskId } as any
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- if (!task) {
|
|
|
|
|
- return c.json({ success: false, message: '打印任务不存在' }, 404);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return c.json({ success: true, data: task });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.get('/tasks/:taskId/status', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const taskId = c.req.param('taskId');
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的打印任务服务实例
|
|
|
|
|
- const tenantPrintTaskService = new PrintTaskService(dataSource, feieConfig);
|
|
|
|
|
- const status = await tenantPrintTaskService.getPrintTaskStatus(tenantId, taskId);
|
|
|
|
|
- return c.json({ success: true, data: status });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 获取打印任务状态失败,任务ID: ${taskId}:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '获取打印任务状态失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.post('/tasks/:taskId/cancel', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const taskId = c.req.param('taskId');
|
|
|
|
|
- const body = await c.req.json();
|
|
|
|
|
- const task = await printTaskService.cancelPrintTask(tenantId, taskId, body.reason);
|
|
|
|
|
- return c.json({ success: true, data: task });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.post('/tasks/:taskId/retry', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const taskId = c.req.param('taskId');
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- // 获取飞鹅API配置
|
|
|
|
|
- const feieConfig = await getFeieApiConfig(tenantId, dataSource);
|
|
|
|
|
- if (!feieConfig) {
|
|
|
|
|
- return c.json({ success: false, message: '飞鹅API配置未找到或配置不完整' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建带配置的打印任务服务实例
|
|
|
|
|
- const tenantPrintTaskService = new PrintTaskService(dataSource, feieConfig);
|
|
|
|
|
- const task = await tenantPrintTaskService.retryPrintTask(tenantId, taskId);
|
|
|
|
|
- return c.json({ success: true, data: task });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 重试打印任务失败,任务ID: ${taskId}:`, error);
|
|
|
|
|
- const errorMessage = error instanceof Error ? error.message : '重试打印任务失败';
|
|
|
|
|
- return c.json({ success: false, message: errorMessage }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 调度器管理路由
|
|
|
|
|
- app.get('/scheduler/status', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- console.debug('获取调度器状态 - 用户对象:', JSON.stringify(user, null, 2));
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- // 创建租户特定的调度器实例
|
|
|
|
|
- const delaySchedulerService = new DelaySchedulerService(dataSource, {
|
|
|
|
|
- baseUrl: 'https://api.feieyun.cn/Api/Open/',
|
|
|
|
|
- user: '',
|
|
|
|
|
- ukey: '',
|
|
|
|
|
- timeout: 10000,
|
|
|
|
|
- maxRetries: 3
|
|
|
|
|
- }, tenantId);
|
|
|
|
|
-
|
|
|
|
|
- const status = delaySchedulerService.getStatus();
|
|
|
|
|
- return c.json({ success: true, data: status });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.post('/scheduler/start', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- console.debug('启动调度器 - 用户对象:', JSON.stringify(user, null, 2));
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- // 创建租户特定的调度器实例
|
|
|
|
|
- const delaySchedulerService = new DelaySchedulerService(dataSource, {
|
|
|
|
|
- baseUrl: 'https://api.feieyun.cn/Api/Open/',
|
|
|
|
|
- user: '',
|
|
|
|
|
- ukey: '',
|
|
|
|
|
- timeout: 10000,
|
|
|
|
|
- maxRetries: 3
|
|
|
|
|
- }, tenantId);
|
|
|
|
|
-
|
|
|
|
|
- await delaySchedulerService.start();
|
|
|
|
|
- return c.json({ success: true, message: '调度器已启动' });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.post('/scheduler/stop', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- console.debug('停止调度器 - 用户对象:', JSON.stringify(user, null, 2));
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- // 创建租户特定的调度器实例
|
|
|
|
|
- const delaySchedulerService = new DelaySchedulerService(dataSource, {
|
|
|
|
|
- baseUrl: 'https://api.feieyun.cn/Api/Open/',
|
|
|
|
|
- user: '',
|
|
|
|
|
- ukey: '',
|
|
|
|
|
- timeout: 10000,
|
|
|
|
|
- maxRetries: 3
|
|
|
|
|
- }, tenantId);
|
|
|
|
|
-
|
|
|
|
|
- await delaySchedulerService.stop();
|
|
|
|
|
- return c.json({ success: true, message: '调度器已停止' });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.get('/scheduler/health', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- console.debug('调度器健康检查 - 用户对象:', JSON.stringify(user, null, 2));
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- // 创建租户特定的调度器实例
|
|
|
|
|
- const delaySchedulerService = new DelaySchedulerService(dataSource, {
|
|
|
|
|
- baseUrl: 'https://api.feieyun.cn/Api/Open/',
|
|
|
|
|
- user: '',
|
|
|
|
|
- ukey: '',
|
|
|
|
|
- timeout: 10000,
|
|
|
|
|
- maxRetries: 3
|
|
|
|
|
- }, tenantId);
|
|
|
|
|
-
|
|
|
|
|
- const health = await delaySchedulerService.healthCheck();
|
|
|
|
|
- return c.json({ success: true, data: health });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.post('/scheduler/trigger', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- console.debug('手动触发调度器处理 - 用户对象:', JSON.stringify(user, null, 2));
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- // 创建租户特定的调度器实例
|
|
|
|
|
- const delaySchedulerService = new DelaySchedulerService(dataSource, {
|
|
|
|
|
- baseUrl: 'https://api.feieyun.cn/Api/Open/',
|
|
|
|
|
- user: '',
|
|
|
|
|
- ukey: '',
|
|
|
|
|
- timeout: 10000,
|
|
|
|
|
- maxRetries: 3
|
|
|
|
|
- }, tenantId);
|
|
|
|
|
-
|
|
|
|
|
- const result = await delaySchedulerService.triggerManualProcess();
|
|
|
|
|
- return c.json({ success: result.success, data: result });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 打印配置管理路由
|
|
|
|
|
- app.get('/config', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- const configs = await configService.getPrintConfigs(tenantId);
|
|
|
|
|
-
|
|
|
|
|
- // 将 FeieConfigMt 转换为前端需要的 FeieConfig 格式
|
|
|
|
|
- const transformedConfigs = configs.map(config => ({
|
|
|
|
|
- id: config.id,
|
|
|
|
|
- tenantId: config.tenantId,
|
|
|
|
|
- configKey: config.configKey as any, // 字符串转换为 ConfigKey 枚举
|
|
|
|
|
- configValue: config.configValue || '', // 处理null值
|
|
|
|
|
- configType: config.configType as any, // 字符串转换为 ConfigType 枚举
|
|
|
|
|
- description: config.description || undefined,
|
|
|
|
|
- createdAt: config.createdAt.toISOString(),
|
|
|
|
|
- updatedAt: config.updatedAt.toISOString()
|
|
|
|
|
- }));
|
|
|
|
|
-
|
|
|
|
|
- return c.json({ success: true, data: { data: transformedConfigs } });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 获取打印配置失败:`, error);
|
|
|
|
|
- return c.json({ success: false, message: '获取打印配置失败' }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- app.put('/config/:configKey', async (c) => {
|
|
|
|
|
- const user = c.get('user');
|
|
|
|
|
- const tenantId = user?.tenantId || 1;
|
|
|
|
|
- const configKey = c.req.param('configKey');
|
|
|
|
|
- const body = await c.req.json();
|
|
|
|
|
-
|
|
|
|
|
- if (!body.configValue) {
|
|
|
|
|
- return c.json({ success: false, message: '配置值不能为空' }, 400);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- const config = await configService.updatePrintConfig(tenantId, configKey, body.configValue);
|
|
|
|
|
-
|
|
|
|
|
- // 将 FeieConfigMt 转换为前端需要的 FeieConfig 格式
|
|
|
|
|
- const transformedConfig = {
|
|
|
|
|
- id: config.id,
|
|
|
|
|
- tenantId: config.tenantId,
|
|
|
|
|
- configKey: config.configKey as any, // 字符串转换为 ConfigKey 枚举
|
|
|
|
|
- configValue: config.configValue || '', // 处理null值
|
|
|
|
|
- configType: config.configType as any, // 字符串转换为 ConfigType 枚举
|
|
|
|
|
- description: config.description || undefined,
|
|
|
|
|
- createdAt: config.createdAt.toISOString(),
|
|
|
|
|
- updatedAt: config.updatedAt.toISOString()
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- return c.json({ success: true, data: transformedConfig });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(`[租户${tenantId}] 更新配置失败,key: ${configKey}:`, error);
|
|
|
|
|
- return c.json({ success: false, message: '更新配置失败' }, 500);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- return app;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-export default createFeieRoutes;
|
|
|