'use strict'; /** * 移除自建的微信授权表(`wechat_oauth`)与小程序绑定表(`wechat_mp_accounts`) * * 微信侧的接入已经统一到**微信登录网关**(`wechat_gateway_bindings` + 网关自己的 openid): * - `wechat_oauth`:自建公众号网页授权(设备扫码登录手机页静默授权)+ 运维手工维护的绑定关系,已下线; * - `wechat_mp_accounts`:小程序 `code2Session` 一键登录,已下线(小程序改用账号密码登录)。 * * 两张表在升级前都是空的,直接删除;`down` 会按原结构重建(不带数据)。 */ module.exports = { async up(queryInterface) { const tables = await queryInterface.showAllTables(); for (const table of [ 'wechat_oauth', 'wechat_mp_accounts' ]) { if (tables.includes(table)) await queryInterface.dropTable(table); } }, async down(queryInterface, Sequelize) { const { BIGINT, STRING, TINYINT, DATE } = Sequelize; const tables = await queryInterface.showAllTables(); if (!tables.includes('wechat_oauth')) { await queryInterface.createTable('wechat_oauth', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, user_id: { type: BIGINT.UNSIGNED, allowNull: false, 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: '小程序 session_key' }, access_token: { type: STRING(512), allowNull: true, comment: '网页授权 access_token' }, refresh_token: { type: STRING(512), allowNull: true, comment: '刷新令牌' }, scope: { type: STRING(64), allowNull: true, comment: '授权 scope' }, token_expires_at: { type: DATE, allowNull: true, comment: 'access_token 过期时间' }, last_auth_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_oauth', [ 'appid', 'openid' ], { name: 'uk_appid_openid', unique: true, }); await queryInterface.addIndex('wechat_oauth', [ 'user_id' ], { name: 'idx_user_id' }); await queryInterface.addIndex('wechat_oauth', [ 'unionid' ], { name: 'idx_unionid' }); } if (!tables.includes('wechat_mp_accounts')) { 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' }); } }, };