packServer/backend/database/migrations/20261029100000-drop-legacy-wechat-tables.js
liufumingren 08d9ea103b chore(微信): 移除自建微信授权表 wechat_oauth / wechat_mp_accounts
微信侧接入已统一到登录网关,自建的两套都下线:

- 删除表:wechat_oauth(自建公众号网页授权 + 运维手工维护的绑定关系)、
  wechat_mp_accounts(小程序 code2Session 一键登录);迁移 20261029100000(up 删表、down 按原结构重建)
- 删除代码:service/{wechat,wechat_mp}.js、controller/web/wechat_mp.js、model/{wechat_oauth,wechat_mp_account}.js、
  service/user.js#findByOpenid、user 模型的 hasMany WechatOauth、公众号 OAuth 回调与 /api/web/auth/wechat/*
- 删除环境变量:WECHAT_APPID/SECRET/REDIRECT_URI/API_BASE、WX_MP_*(config.wechat 只剩设备扫码登录用到的串码/二维码参数)
- 小程序:去掉「微信一键登录」tab,只留账号密码登录(删 utils/wechat.ts 与 wechatLogin 调用)
- sql/schema.sql 同步去掉 wechat_oauth 建表

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-29 17:20:28 +08:00

87 lines
5.1 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';
/**
* 移除自建的微信授权表(`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' });
}
},
};