2024-06-18 15:54:20 +00:00
|
|
|
import { PrismaClient } from "@prisma/client";
|
|
|
|
const db = new PrismaClient();
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const body = await readBody(event);
|
2024-06-20 14:56:45 +00:00
|
|
|
const register = (await db.register.findFirst({
|
|
|
|
where: {
|
2024-06-18 15:54:20 +00:00
|
|
|
phone: body.phone,
|
|
|
|
},
|
2024-06-20 14:56:45 +00:00
|
|
|
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: "验证码超时",
|
|
|
|
};
|
|
|
|
}
|
2024-06-18 15:54:20 +00:00
|
|
|
});
|