diff --git a/middleware/device.js b/middleware/device.js index bf33a0f..89545f8 100644 --- a/middleware/device.js +++ b/middleware/device.js @@ -13,6 +13,27 @@ import { verifyDevicePassword } from "../utils/crypto.js"; const prisma = new PrismaClient(); +/** + * 为新设备创建默认的自动登录配置 + * @param {number} deviceId - 设备ID + */ +async function createDefaultAutoAuth(deviceId) { + try { + // 创建默认的自动授权配置:不需要密码、类型是classroom(一体机) + await prisma.autoAuth.create({ + data: { + deviceId: deviceId, + password: null, // 无密码 + deviceType: "classroom", // 一体机类型 + isReadOnly: false, // 非只读 + }, + }); + } catch (error) { + console.error('创建默认自动登录配置失败:', error); + // 这里不抛出错误,避免影响设备创建流程 + } +} + /** * 设备中间件 - 统一处理设备UUID * @@ -47,6 +68,9 @@ export const deviceMiddleware = errors.catchAsync(async (req, res, next) => { accountId: null, }, }); + + // 为新创建的设备添加默认的自动登录配置 + await createDefaultAutoAuth(device.id); } // 将设备信息存储到res.locals diff --git a/routes/device.js b/routes/device.js index c11d919..16147a3 100644 --- a/routes/device.js +++ b/routes/device.js @@ -9,6 +9,27 @@ import { getOnlineDevices } from "../utils/socket.js"; const prisma = new PrismaClient(); +/** + * 为新设备创建默认的自动登录配置 + * @param {number} deviceId - 设备ID + */ +async function createDefaultAutoAuth(deviceId) { + try { + // 创建默认的自动授权配置:不需要密码、类型是classroom(一体机) + await prisma.autoAuth.create({ + data: { + deviceId: deviceId, + password: null, // 无密码 + deviceType: "classroom", // 一体机类型 + isReadOnly: false, // 非只读 + }, + }); + } catch (error) { + console.error('创建默认自动登录配置失败:', error); + // 这里不抛出错误,避免影响设备创建流程 + } +} + /** * POST /devices * 注册新设备 @@ -56,6 +77,9 @@ router.post( }, }); + // 为新设备创建默认的自动登录配置 + await createDefaultAutoAuth(device.id); + return res.status(201).json({ success: true, device: {