oms/backend/database/migrations/20261101000000-create-user-wechat-binds.js
liufumingren 1847e17ba7 feat(auth): 接入微信扫码登录与微信绑定
- 新增网关客户端与通知验签(sha256(appSecret) 派生 HMAC-SHA256,5 分钟防重放),
  扫码会话与一次性结果存 Redis,openid 仅服务端保存
- 新增 user_wechat_binds 表与迁移:openid 唯一(一个微信只绑一个账号)、
  user_id 唯一(重新绑定覆盖旧微信)
- 新增 /api/auth/wechat/* 接口与 /api/wx/notify 网关回调;登录二维码按 IP 限流,
  未配置网关时相关接口返回 400 提示,不影响账号密码 / 邮箱验证码登录
- 前端:「微信登录」tab(二维码 + 倒计时 + 轮询)与「我的」页微信绑定 / 解绑
- 开发与生产各出一份 WX_GATEWAY_* 配置样板,生产权威副本走 shared-config 覆盖
- 同步补充 swagger 契约与请求日志脱敏字段(bind_code / sign)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-01 23:17:11 +08:00

51 lines
2.3 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';
/**
* 微信绑定表:记录「系统账号 ↔ 微信 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');
},
};