'use strict'; /** * 微信绑定表:记录「系统账号 ↔ 微信 openid」的绑定关系。 * - openid 唯一:一个微信身份只能绑定一个系统账号; * - user_id 唯一:一个系统账号只保留最近一次绑定的微信(重新绑定即覆盖); * - gateway_uid 便于按网关用户 ID 反查; * - app_key 记录所属网关应用,区分开发 / 生产环境。 */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable( 'user_wechat_binds', { id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true, }, user_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '系统用户ID' }, app_key: { type: Sequelize.STRING(64), allowNull: true, comment: '所属网关应用 appKey' }, gateway_uid: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '网关用户ID' }, openid: { type: Sequelize.STRING(64), allowNull: false, comment: '公众号 openid,全局唯一' }, unionid: { type: Sequelize.STRING(64), allowNull: true, comment: '开放平台 unionid' }, bind_scene: { type: Sequelize.STRING(16), allowNull: false, defaultValue: 'bind', comment: '绑定来源:bind / login' }, last_login_at: { type: Sequelize.DATE, allowNull: true, comment: '最近一次微信登录时间' }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), }, }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '微信绑定表' } ); await queryInterface.addIndex('user_wechat_binds', { fields: [ 'openid' ], unique: true, name: 'uk_wx_bind_openid' }); await queryInterface.addIndex('user_wechat_binds', { fields: [ 'user_id' ], unique: true, name: 'uk_wx_bind_user' }); await queryInterface.addIndex('user_wechat_binds', { fields: [ 'gateway_uid' ], name: 'idx_wx_bind_gateway_uid' }); }, async down(queryInterface) { await queryInterface.dropTable('user_wechat_binds'); }, };