'use strict'; /** * 微信授权表模型(wechat_oauth) * * 一条记录 = 某个用户在某个 appid 下的一次微信授权, * 因此一个用户可绑定多个微信(公众号 / 小程序 / 开放平台)。 * 关联:WechatOauth.belongsTo(User, { as: 'user' }), * 可通过 { include: [{ model: app.model.User, as: 'user' }] } 取出对应用户。 */ module.exports = (app) => { const { BIGINT, STRING, DATE } = app.Sequelize; const WechatOauth = app.model.define('wechatOauth', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, // 下划线映射由 config.sequelize.define.underscored 统一处理(userId -> user_id) userId: { 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), comment: '开放平台 unionid' }, nickname: { type: STRING(64), allowNull: false, defaultValue: '', comment: '授权时微信昵称' }, avatar: { type: STRING(255), allowNull: false, defaultValue: '', comment: '授权时微信头像' }, sessionKey: { type: STRING(128), comment: '小程序 session_key' }, accessToken: { type: STRING(512), comment: '网页授权 access_token' }, refreshToken: { type: STRING(512), comment: '刷新令牌' }, scope: { type: STRING(64), comment: '授权 scope' }, tokenExpiresAt: { type: DATE, comment: 'access_token 过期时间' }, lastAuthAt: { type: DATE, comment: '最近授权时间' }, }, { tableName: 'wechat_oauth', paranoid: true, comment: '微信授权表', }); WechatOauth.associate = function() { app.model.WechatOauth.belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' }); }; return WechatOauth; };