feat(/api/user): Compete registe-code

This commit is contained in:
漩葵 2024-06-20 22:56:45 +08:00
parent f91f2728d6
commit 1abe25849b
3 changed files with 67 additions and 9 deletions

View File

@ -16,6 +16,7 @@ model User {
username String?
password String
applications Application[]
loginlogs Loginlogs[]
}
model Adminer {
@ -40,4 +41,12 @@ model Register {
phone String
deadline DateTime
code String
}
model Loginlogs {
id Int @id @default(autoincrement())
phone String
time DateTime
ip String
loiner User @relation(fields: [userid], references: [id])
userid Int
}

View File

@ -2,15 +2,39 @@ import { PrismaClient } from "@prisma/client";
const db = new PrismaClient();
export default defineEventHandler(async (event) => {
const body = await readBody(event);
await db.user.create({
data: {
username: body.username,
const register = (await db.register.findFirst({
where: {
phone: body.phone,
password: (await import("crypto"))
.createHash("md5")
.update(body.password)
.digest("hex"),
},
});
return 1;
orderBy: {
deadline: "desc", // 'asc' 表示升序,'desc' 表示降序
},
})) || {
code: "",
deadline: new Date(),
};
const deadlineDate = new Date(register.deadline.getTime() + 7 * 60 * 1000);
const now = new Date();
if (now <= deadlineDate) {
if (register.code != body.code) {
return { code: 0, msg: "验证码错误请重新发送" };
} else {
await db.user.create({
data: {
username: body.username,
phone: body.phone,
password: (await import("crypto"))
.createHash("md5")
.update(body.password)
.digest("hex"),
},
});
return { code: 1, msg: "注册成功" };
}
} else {
return {
code: -1,
msg: "验证码超时",
};
}
});

25
server/api/user/login.ts Normal file
View File

@ -0,0 +1,25 @@
import { PrismaClient } from "@prisma/client";
const db = new PrismaClient();
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const loginer = await db.user.findFirst({
where: {
phone: body.phone,
},
});
if (loginer == null) {
return { code: -1, msg: "未注册" };
} else {
if (
loginer.password ==
(await import("crypto"))
.createHash("md5")
.update(body.password)
.digest("hex")
) {
return { code: 1, msg: "登录成功" };
} else {
return { code: 0, msg: "用户名、手机号或密码错误" };
}
}
});