'use strict'; /** * 微信登录网关绑定表 * * 登录页扫码登录 / 工作台扫码登入都需要「微信 openid → 平台账号」的映射, * 由 `POST /api/wechat-gateway/notify`(网关回调)写入。 * * 与 `wechat_oauth`(自建公众号网页授权)、`wechat_mp_accounts`(小程序)分开: * 网关用的公众号 appid 我们并不掌握,只有 `appKey` + `openid`。 */ module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, DATE } = Sequelize; await queryInterface.createTable('wechat_gateway_bindings', { 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', }, app_key: { type: STRING(64), allowNull: false, comment: '网关应用标识 appKey' }, openid: { type: STRING(64), allowNull: false, comment: '网关返回的 openid' }, unionid: { type: STRING(64), allowNull: true, comment: '开放平台 unionid' }, bind_scene: { type: STRING(16), allowNull: false, defaultValue: 'bind', comment: '绑定场景:bind / login' }, last_login_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('wechat_gateway_bindings', [ 'app_key', 'openid' ], { name: 'uk_wechat_gateway_bindings_app_openid', unique: true, }); await queryInterface.addIndex('wechat_gateway_bindings', [ 'user_id' ], { name: 'idx_wechat_gateway_bindings_user', }); }, async down(queryInterface) { await queryInterface.dropTable('wechat_gateway_bindings'); }, };