expressServer/models/userModel.js
LingandRX 04885717c2 refactor(user): 重构用户相关代码并统一错误信息格式
- 修改用户控制器、模型和服务中的方法命名,使用驼峰命名法
- 更新错误信息常量,使用驼峰命名法
- 优化部分代码结构,提高可读性
2025-01-04 23:31:15 +08:00

33 lines
1.1 KiB
JavaScript

const mongoose = require('mongoose')
const { hashPassword } = require('../utils/hashUtils')
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: { type: String, required: true, maxlength: 100 },
gender: { type: String, enum: ['male', 'female', 'other'], maxlength: 20 },
birth: { type: Date },
avatar: { type: String, maxlength: 100 },
account: { type: String, required: true, unique: true, maxlength: 100, index: true },
password: { type: String, required: true, maxlength: 100 },
email: { type: String, maxlength: 255, index: true },
phone: { type: String, maxlength: 11 },
registerDate: { type: Date, default: Date.now },
lastLoginDate: { type: Date, default: Date.now },
status: { type: String, enum: ['active', 'inactive', 'pending'], default: 'pending' }
})
UserSchema.pre('save', async function (next) {
const user = this
if (!user.isModified('password')) return next()
try {
console.log(user.password)
user.password = await hashPassword(user.password)
next()
} catch (error) {
next(error)
}
})
module.exports = mongoose.model('User', UserSchema)