packServer/backend/database/migrations/20261020220000-create-agreement-tables.js
liufumingren 7b11f7052d feat(subject): 主体到期暂停设备上报,新增续期订单与支付回调
- 主体状态缓存(Redis 优先、进程内兜底):设备心跳与上报判断不再查库,续期/停用后主动失效
- 上报写接口按缓存校验期限:过期 403 subject_expired、停用 subject_disabled、主体缺失 subject_missing
- 心跳与 /api/scanner/me 返回 subject 状态,桌面端据此暂停推送并在续期后自动补传积压
- 新增续期订单(subject_orders)与接口,支付回调 POST /api/pay/notify 幂等、支持 HMAC 签名,
  另提供内部令牌模拟支付用于联调
- 顺带提交工作区中已完成的「登录协议」后端改动(agreement):与本次改动共用 router.js / config.default.js

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-20 18:55:57 +08:00

109 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 协议 / 协议版本 / 用户协议同意记录
*
* 协议内容没有管理界面:新增版本直接走 SQL(见 backend/README「用户协议」一节),
* 这里只负责三张表的结构与索引。
*/
module.exports = {
async up(queryInterface, Sequelize) {
const { BIGINT, STRING, TINYINT, INTEGER, TEXT, DATE } = Sequelize;
await queryInterface.createTable('agreements', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false },
code: { type: STRING(64), allowNull: false, unique: true, comment: '协议标识(user_agreement 等)' },
name: { type: STRING(128), allowNull: false, comment: '协议名称(展示用)' },
description: { type: STRING(255), allowNull: true, comment: '简介' },
required: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '登录是否必须同意:1是 0否' },
status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1启用 0停用' },
sort_no: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '展示排序' },
// 不建外键:agreements ↔ agreement_versions 互相引用,建表顺序会互相依赖
current_version_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '当前生效版本 agreement_versions.id' },
created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
deleted_at: { type: DATE, allowNull: true },
}, { comment: '协议定义表' });
await queryInterface.addIndex('agreements', [ 'status' ], { name: 'idx_agreements_status' });
await queryInterface.createTable('agreement_versions', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false },
agreement_id: {
type: BIGINT.UNSIGNED,
allowNull: false,
references: { model: 'agreements', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'RESTRICT',
comment: 'agreements.id',
},
version: { type: STRING(32), allowNull: false, comment: '版本号(1.0.0)' },
title: { type: STRING(128), allowNull: false, comment: '版本标题' },
summary: { type: STRING(255), allowNull: true, comment: '变更摘要' },
content: { type: TEXT('long'), allowNull: false, comment: '协议正文' },
content_type: { type: STRING(16), allowNull: false, defaultValue: 'text', comment: '正文类型:text / html' },
status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:0草稿 1已发布 2已归档' },
published_at: { type: DATE, allowNull: true, comment: '发布时间' },
effective_at: { type: DATE, allowNull: true, comment: '生效时间' },
created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
deleted_at: { type: DATE, allowNull: true },
}, { comment: '协议版本表' });
await queryInterface.addIndex('agreement_versions', [ 'agreement_id', 'version' ], {
name: 'uk_agreement_versions_agreement_version',
unique: true,
});
await queryInterface.addIndex('agreement_versions', [ 'status' ], { name: 'idx_agreement_versions_status' });
await queryInterface.createTable('user_agreements', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false },
user_id: {
type: BIGINT.UNSIGNED,
allowNull: false,
references: { model: 'users', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
comment: 'users.id',
},
agreement_id: {
type: BIGINT.UNSIGNED,
allowNull: false,
references: { model: 'agreements', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'RESTRICT',
comment: 'agreements.id',
},
version_id: {
type: BIGINT.UNSIGNED,
allowNull: false,
references: { model: 'agreement_versions', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'RESTRICT',
comment: 'agreement_versions.id',
},
version: { type: STRING(32), allowNull: false, comment: '同意时的版本号快照' },
source: { type: STRING(32), allowNull: true, comment: '来源:mini_program / web / h5' },
ip: { type: STRING(64), allowNull: true, comment: '同意时的客户端 IP' },
user_agent: { type: STRING(255), allowNull: true, comment: '同意时的 User-Agent' },
agreed_at: { type: DATE, allowNull: false, comment: '同意时间' },
created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
deleted_at: { type: DATE, allowNull: true },
}, { comment: '用户协议同意记录表' });
await queryInterface.addIndex('user_agreements', [ 'user_id', 'agreement_id', 'version_id' ], {
name: 'uk_user_agreements_user_agreement_version',
unique: true,
});
await queryInterface.addIndex('user_agreements', [ 'user_id' ], { name: 'idx_user_agreements_user' });
await queryInterface.addIndex('user_agreements', [ 'agreement_id' ], { name: 'idx_user_agreements_agreement' });
},
async down(queryInterface) {
await queryInterface.dropTable('user_agreements');
await queryInterface.dropTable('agreement_versions');
await queryInterface.dropTable('agreements');
},
};