'use strict'; /** * 微信小程序授权账号表 * * 与公众号/网页授权的 `wechat_oauth` 分开:小程序登录是「openid → 平台账号」的一对一绑定关系, * 解绑时只把 `user_id` 置空,记录保留(重新绑定、审计都能用)。 */ module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, TINYINT, DATE } = Sequelize; await queryInterface.createTable('wechat_mp_accounts', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, user_id: { type: BIGINT.UNSIGNED, allowNull: true, references: { model: 'users', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL', comment: '绑定的平台账号 users.id(解绑后为空)', }, appid: { type: STRING(64), allowNull: false, comment: '小程序 appid' }, openid: { type: STRING(64), allowNull: false, comment: '用户在该 appid 下的 openid' }, unionid: { type: STRING(64), allowNull: true, comment: '开放平台 unionid' }, nickname: { type: STRING(64), allowNull: false, defaultValue: '', comment: '微信昵称(有授权时)' }, avatar: { type: STRING(255), allowNull: false, defaultValue: '', comment: '微信头像(有授权时)' }, session_key: { type: STRING(128), allowNull: true, comment: 'jscode2session 返回的 session_key' }, last_login_at: { type: DATE, allowNull: true, comment: '最近一次微信登录时间' }, login_count: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '微信登录次数' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0停用' }, 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_mp_accounts', [ 'appid', 'openid' ], { name: 'uk_wechat_mp_accounts_appid_openid', unique: true, }); await queryInterface.addIndex('wechat_mp_accounts', [ 'user_id' ], { name: 'idx_wechat_mp_accounts_user' }); await queryInterface.addIndex('wechat_mp_accounts', [ 'unionid' ], { name: 'idx_wechat_mp_accounts_unionid' }); }, async down(queryInterface) { await queryInterface.dropTable('wechat_mp_accounts'); }, };