feat: add backend server

This commit is contained in:
2026-05-11 16:01:22 +08:00
parent 3c838d534f
commit e24a34deb4
11 changed files with 1709 additions and 0 deletions

79
server/model/stability.js Normal file
View File

@@ -0,0 +1,79 @@
import mongoose from 'mongoose'
import User from './user.js'
const CheckSchema = new mongoose.Schema(
{
// 检查日期
date: {
type: Date,
required: true,
},
// 是否已经检查
checked: {
type: Boolean,
required: true,
default: false,
},
// 检查人
checker: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
// 备注
description: {
type: String,
},
},
{ timestamps: true }
)
// 稳定性实验 模型
const StabilitySchema = new mongoose.Schema(
{
// 实验批次号
batch: {
type: String,
required: true,
},
// 实验类型
type: {
type: String,
enum: ['长期', '加速', '其它'],
},
// 检查点列表
checks: {
type: [CheckSchema],
default: [],
},
// 是否完成
ended: {
type: Boolean,
default: false,
},
// 创建人
creater: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
// 备注
description: {
type: String,
},
},
{ timestamps: true }
)
StabilitySchema.pre('save', async function () {
try {
this.checks = this.checks.sort((a, b) => {
return new Date(a.date) - new Date(b.date)
})
} catch (error) {
console.log(error)
}
})
const Stability = mongoose.model('Stability', StabilitySchema)
export default Stability

31
server/model/standard.js Normal file
View File

@@ -0,0 +1,31 @@
import mongoose from 'mongoose'
const StandardSchema = new mongoose.Schema(
{
batch: {
type: String,
require: true,
unique: true,
},
im: {
type: String,
},
ass: {
type: String,
},
calibration_date: {
type: Date,
},
expire_date: {
type: Date,
},
location: {
type: String,
},
},
{ timestamps: true }
)
const Standard = mongoose.model('Standard', StandardSchema)
export default Standard

47
server/model/user.js Normal file
View File

@@ -0,0 +1,47 @@
import mongoose from 'mongoose'
import bcrypt from 'bcrypt'
const UserSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
unique: true,
index: true,
},
nickname: {
type: String,
},
password: {
type: String,
required: true,
},
role: {
type: String,
enum: ['admin', 'user'],
default: 'user',
},
},
{ timestamps: true }
)
// 保存前自动对密码进行哈希加密
UserSchema.pre('save', async function () {
this.password = await bcrypt.hash(this.password, 10)
})
// 实例方法:校验密码
UserSchema.methods.comparePassword = async function (candidatePassword) {
return bcrypt.compare(candidatePassword, this.password)
}
// 输出时始终不返回密码
UserSchema.methods.toJSON = function () {
const obj = this.toObject()
delete obj.password
return obj
}
const User = mongoose.model('User', UserSchema)
export default User