packServer/backend/app/model/wechat_mp_account.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

36 lines
1.8 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_mp_accounts)
*
* 一行 = 某个小程序 appid 下的一个 openid,以及它绑定的平台账号(`user_id`)。
* 解绑后 `user_id` 置空,记录保留(重新绑定 / 审计用)。
*/
module.exports = (app) => {
const { BIGINT, STRING, TINYINT, DATE } = app.Sequelize;
const WechatMpAccount = app.model.define('wechat_mp_account', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
userId: { type: BIGINT.UNSIGNED, allowNull: true, 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: '微信头像(有授权时)' },
sessionKey: { type: STRING(128), allowNull: true, comment: 'jscode2session 返回的 session_key' },
lastLoginAt: { type: DATE, allowNull: true, comment: '最近一次微信登录时间' },
loginCount: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '微信登录次数' },
status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0停用' },
}, {
tableName: 'wechat_mp_accounts',
paranoid: true,
comment: '微信小程序授权账号表',
});
WechatMpAccount.associate = function() {
app.model.WechatMpAccount.belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' });
};
return WechatMpAccount;
};