'use strict'; /** * 用户模型,对应 users 表(密码使用 password + salt 加盐存储) * @param {Egg.Application} app egg application * @return {object} user model */ module.exports = app => { const { BIGINT, STRING, TINYINT, DATE } = app.Sequelize; const User = app.model.define('user', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, username: { type: STRING(50), allowNull: false, comment: '登录账号' }, password: { type: STRING(255), allowNull: false, comment: '加盐哈希后的密码' }, salt: { type: STRING(32), allowNull: false, comment: '密码盐值' }, nickname: { type: STRING(50), comment: '昵称' }, avatar: { type: STRING(255), comment: '头像' }, email: { type: STRING(100), comment: '邮箱' }, phone: { type: STRING(20), comment: '手机号' }, status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' }, last_login_at: { type: DATE, comment: '最后登录时间' }, created_at: { type: DATE, comment: '创建时间' }, updated_at: { type: DATE, comment: '更新时间' }, }); User.associate = () => { // 用户与主体:多对多,通过主体用户关联表 app.model.User.belongsToMany(app.model.Principal, { through: app.model.PrincipalUser, foreignKey: 'user_id', otherKey: 'principal_id', as: 'principals', }); }; return User; };