packServer/backend/database/migrations/20261020230000-create-wechat-mp-accounts.js
liufumingren 4f637f1307 feat(web): 微信小程序 code2Session 登录与账号绑定
- POST /api/web/auth/wechat/login:code 换 openid,已绑定直接签发令牌,
  未绑定自动建平台账号(首登标记 firstTime)
- bind / bindings / unbind:登录态下绑定、查询、解绑小程序账号
- wechat_mp_accounts 表 + 迁移;未配置 WX_MP_APPID/SECRET 时本地与单测走 mock
- 新增 cors 中间件(小程序 H5 预览直连后端;鉴权只靠 header 令牌,不依赖 cookie)
- 补充 wechat-mp 接口单测

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 00:47:11 +08:00

49 lines
2.5 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` 分开:小程序登录是「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');
},
};