修改器

优质
小牛编辑
139浏览
2023-12-01

set() 新增数据时触发

'use strict';
const moment =  require('moment')
const bcrypt = require('bcryptjs')
module.exports = app => {
    const { STRING, INTEGER, DATE } = app.Sequelize;

    const User = app.model.define('user', {
        id: { type: INTEGER(20).UNSIGNED, primaryKey: true, autoIncrement: true },
        nickname: { type: STRING},
        password: {
            type: STRING,
            // 密码自动加盐
            set(val) {
                const salt = bcrypt.genSaltSync(10)
                const pwd = bcrypt.hashSync(val, salt)
                // 传入2个参数,第一个是字段名称,第二个是加密字符串
                this.setDataValue('password',pwd)
            }
        },
        email: { type: STRING},
        openid: { type: STRING}
    });

    return User;
};