'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; };