fix: model auth

This commit is contained in:
archer
2023-04-08 11:57:13 +08:00
parent 33154a9c19
commit b7cd4dec89
2 changed files with 13 additions and 2 deletions

View File

@@ -1,7 +1,8 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, Chat } from '@/service/mongo';
import { connectToDatabase, Model, Chat } from '@/service/mongo';
import { authToken } from '@/service/utils/tools';
import type { ModelSchema } from '@/types/mongoSchema';
/* 获取我的模型 */
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
@@ -24,6 +25,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
await connectToDatabase();
// 校验是否为用户的模型
const model = await Model.findOne<ModelSchema>({
_id: modelId,
userId
});
if (!model) {
throw new Error('无权使用该模型');
}
// 创建 chat 数据
const response = await Chat.create({
userId,

View File

@@ -15,7 +15,7 @@ const ChatSchema = new Schema({
expiredTime: {
// 过期时间
type: Number,
default: Date.now()
default: () => new Date()
},
loadAmount: {
// 剩余加载次数