doc gpt V0.2

This commit is contained in:
archer
2023-02-19 14:35:25 +08:00
parent cc5cf99e7a
commit 0ecf576e4e
124 changed files with 11780 additions and 573 deletions

View File

@@ -0,0 +1,47 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase } from '@/service/mongo';
import { User } from '@/service/models/user';
import { generateToken } from '@/service/utils/tools';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { email, password } = req.body;
if (!email || !password) {
throw new Error('缺少参数');
}
await connectToDatabase();
// 检测邮箱是否存在
const authEmail = await User.findOne({
email
});
if (!authEmail) {
throw new Error('邮箱未注册');
}
const user = await User.findOne({
email,
password
});
if (!user) {
throw new Error('密码错误');
}
jsonRes(res, {
data: {
token: generateToken(user._id),
user
}
});
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}

View File

@@ -0,0 +1,61 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { User } from '@/service/models/user';
import { AuthCode } from '@/service/models/authCode';
import { connectToDatabase } from '@/service/mongo';
import { generateToken } from '@/service/utils/tools';
import { EmailTypeEnum } from '@/constants/common';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { email, code, password } = req.body;
if (!email || !code || !password) {
throw new Error('缺少参数');
}
await connectToDatabase();
// 验证码校验
const authCode = await AuthCode.findOne({
email,
code,
type: EmailTypeEnum.register,
expiredTime: { $gte: Date.now() }
});
if (!authCode) {
throw new Error('验证码错误');
}
// 重名校验
const authRepeat = await User.findOne({
email
});
if (authRepeat) {
throw new Error('邮箱已被注册');
}
const response = await User.create({
email,
password
});
// 根据 id 获取用户信息
const user = await User.findById(response._id);
jsonRes(res, {
data: {
token: generateToken(user._id),
user
}
});
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}

View File

@@ -0,0 +1,54 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { AuthCode } from '@/service/models/authCode';
import { connectToDatabase } from '@/service/mongo';
import { sendCode } from '@/service/utils/sendEmail';
import { EmailTypeEnum } from '@/constants/common';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { email, type } = req.query;
if (!email || !type) {
throw new Error('缺少参数');
}
await connectToDatabase();
let code = '';
for (let i = 0; i < 6; i++) {
code += Math.floor(Math.random() * 10);
}
// 判断 1 分钟内是否有重复数据
const authCode = await AuthCode.findOne({
email,
type,
expiredTime: { $gte: Date.now() + 4 * 60 * 1000 } // 如果有一个记录的过期时间,大于当前+4分钟说明距离上次发送还没到1分钟。因为默认创建时过期时间是未来5分钟
});
if (authCode) {
throw new Error('请勿频繁获取验证码');
}
// 创建 auth 记录
await AuthCode.create({
email,
type,
code
});
// 发送验证码
await sendCode(email as string, code, type as `${EmailTypeEnum}`);
jsonRes(res, {
message: '发送验证码成功'
});
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}

View File

@@ -0,0 +1,36 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase } from '@/service/mongo';
import { User } from '@/service/models/user';
import { authToken } from '@/service/utils/tools';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { authorization } = req.headers;
if (!authorization) {
throw new Error('缺少参数');
}
const userId = await authToken(authorization);
await connectToDatabase();
// 根据 id 获取用户信息
const user = await User.findById(userId);
if (!user) {
throw new Error('账号异常');
}
jsonRes(res, {
data: user
});
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}

View File

@@ -0,0 +1,41 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { User } from '@/service/models/user';
import { connectToDatabase } from '@/service/mongo';
import { authToken } from '@/service/utils/tools';
import { UserUpdateParams } from '@/types/user';
/* 更新一些基本信息 */
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { accounts } = req.body as UserUpdateParams;
const { authorization } = req.headers;
if (!authorization) {
throw new Error('缺少参数');
}
const userId = await authToken(authorization);
await connectToDatabase();
// 更新对应的记录
await User.updateOne(
{
_id: userId
},
{
// 限定字段
...(accounts ? { accounts } : {})
}
);
jsonRes(res);
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}

View File

@@ -0,0 +1,59 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { User } from '@/service/models/user';
import { AuthCode } from '@/service/models/authCode';
import { connectToDatabase } from '@/service/mongo';
import { generateToken } from '@/service/utils/tools';
import { EmailTypeEnum } from '@/constants/common';
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { email, code, password } = req.body;
if (!email || !code || !password) {
throw new Error('缺少参数');
}
await connectToDatabase();
// 验证码校验
const authCode = await AuthCode.findOne({
email,
code,
type: EmailTypeEnum.findPassword,
expiredTime: { $gte: Date.now() }
});
if (!authCode) {
throw new Error('验证码错误');
}
// 更新对应的记录
await User.updateOne(
{
email
},
{
password
}
);
// 根据 email 获取用户信息
const user = await User.findOne({
email
});
jsonRes(res, {
data: {
token: generateToken(user._id),
user
}
});
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}