expressServer/models/userModel.js
LingandRX e6adea5f52 refactor(user): 重构用户模块代码
- 将 userController、userModel、userRepository 和 userService 文件中的代码进行重新组织和优化
- 使用 import 语句替代 require 引入模块
- 将函数定义从 es5 改为 es6 语法
- 优化了部分函数的实现,提高了代码可读性和维护性
2025-01-05 00:13:10 +08:00

33 lines
1.1 KiB
JavaScript

import { Schema as _Schema, model } from 'mongoose'
import { hashPassword } from '../utils/hashUtils'
const Schema = _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)
}
})
export default model('User', UserSchema)