expressServer/models/userModel.js
LingandRX 8741389ef5 refactor: 优化项目结构和代码
- 更新 .gitignore 文件,排除 Idea 项目配置
- 移除 vuepress 缓存目录
- 优化 app.js 中的错误处理和状态码导入- 调整 CORS 配置,移除未使用的 allowdHeaders
- 更新 hashUtils.js 中的密码处理函数
- 优化 index.js 中的路由定义
- 更新 mongodbConfig.js 中的数据库连接逻辑
- 升级 express到 5.0.0- 更新 mongoose 到 8.7.1
2025-01-01 12:21:43 +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 },
register_date: { type: Date, default: Date.now },
last_login_date: { 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)