'use strict'; /** * 微信登录网关绑定表(wechat_gateway_bindings) * * 一行 = 「某个网关应用下的一个 openid」 ↔ 「平台账号」的绑定关系。 * 登录页扫码登录、工作台扫码登入都靠它把微信身份换成平台账号; * 解绑直接删行(历史登录记录在 `workstation_login_records` 里,不依赖这张表)。 * * ⚠️ 一个 openid 只能绑一个平台账号(`uk_app_openid`), * 一个平台账号在同一应用下也只留一个微信(绑新的要先解绑,见 `service/wx_gateway.js`)。 */ module.exports = (app) => { const { BIGINT, STRING, DATE } = app.Sequelize; const WechatGatewayBinding = app.model.define('wechatGatewayBinding', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, userId: { type: BIGINT.UNSIGNED, allowNull: false, comment: '绑定的平台账号 users.id' }, appKey: { type: STRING(64), allowNull: false, comment: '网关应用标识 appKey' }, openid: { type: STRING(64), allowNull: false, comment: '网关返回的 openid' }, unionid: { type: STRING(64), comment: '开放平台 unionid(网关没给就为空)' }, bindScene: { type: STRING(16), allowNull: false, defaultValue: 'bind', comment: '绑定时用的场景:bind / login' }, lastLoginAt: { type: DATE, comment: '最近一次用这个微信登录的时间' }, }, { tableName: 'wechat_gateway_bindings', paranoid: true, comment: '微信登录网关绑定表', }); WechatGatewayBinding.associate = function() { app.model.WechatGatewayBinding.belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' }); }; return WechatGatewayBinding; };