'use strict'; /** * 扫描器(设备)表模型(scanners) * * 设备身份只有一种:`GET /api/device/login` 扫码登录换来的长期 JWT * (建记录/绑定都发生在「手机确认登录」那一刻),库里只存当前令牌的 `jti`(重签即失效)。 * `user_id` 为空表示尚未绑定账号。 */ module.exports = (app) => { const { BIGINT, STRING, TINYINT, DATE } = app.Sequelize; const Scanner = app.model.define('scanner', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, subjectId: { type: BIGINT.UNSIGNED, comment: '所属主体 subjects.id' }, userId: { type: BIGINT.UNSIGNED, comment: '绑定的用户 id' }, sn: { type: STRING(64), allowNull: false, comment: '扫描器 SN(唯一标识)' }, name: { type: STRING(64), allowNull: false, defaultValue: '', comment: '名称 / 别名' }, vendor: { type: STRING(64), comment: '厂商' }, model: { type: STRING(64), comment: '型号' }, firmware: { type: STRING(32), comment: '固件 / 客户端版本' }, ip: { type: STRING(45), comment: '最近登录 IP' }, // ⚠️ 历史字段:早期「sn + secret 注册/登录」用的密钥哈希,现在已不再写入/校验(列保留) secretHash: { type: STRING(100), comment: '历史字段:扫描器密钥哈希(已废弃)' }, // 库里不存 JWT 本体,只存当前令牌的 jti:重签令牌会让旧令牌立即失效 tokenJti: { type: STRING(64), comment: '当前 JWT 的 jti' }, tokenExpiresAt: { type: DATE, comment: '令牌过期时间' }, // 刷新令牌:库里只存 sha256,不存原文;每次刷新轮换并顺延过期时间 refreshTokenHash: { type: STRING(128), comment: '刷新令牌 sha256' }, refreshExpiresAt: { type: DATE, comment: '刷新令牌过期时间' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0禁用' }, lastRegisterAt: { type: DATE, comment: '最近注册时间' }, lastLoginAt: { type: DATE, comment: '最近登录时间' }, lastSeenAt: { type: DATE, comment: '最近心跳时间' }, }, { tableName: 'scanners', paranoid: true, comment: '扫描器表', }); Scanner.associate = function() { app.model.Scanner.belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' }); }; return Scanner; };