feat: new ui
This commit is contained in:
@@ -13,10 +13,8 @@ export const ERROR_CODE: { [key: number]: string } = {
|
||||
504: '网关超时'
|
||||
};
|
||||
|
||||
export const TOKEN_ERROR_CODE: { [key: number]: string } = {
|
||||
506: '请先登录',
|
||||
507: '请重新登录',
|
||||
508: '登录已过期'
|
||||
export const TOKEN_ERROR_CODE: Record<number, string> = {
|
||||
403: '登录状态无效,请重新登录'
|
||||
};
|
||||
|
||||
export const openaiError: Record<string, string> = {
|
||||
|
||||
@@ -31,6 +31,10 @@ const ChatSchema = new Schema({
|
||||
type: String,
|
||||
default: '历史记录'
|
||||
},
|
||||
latestChat: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
content: {
|
||||
type: [
|
||||
{
|
||||
|
||||
@@ -16,6 +16,10 @@ const UserSchema = new Schema({
|
||||
get: (val: string) => hashPassword(val),
|
||||
select: false
|
||||
},
|
||||
avatar: {
|
||||
type: String,
|
||||
default: '/icon/human.png'
|
||||
},
|
||||
balance: {
|
||||
// 平台余额,不可提现
|
||||
type: Number,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
import { openaiError, openaiError2, proxyError, ERROR_RESPONSE } from './errorCode';
|
||||
import { openaiError, openaiError2, proxyError, ERROR_RESPONSE, ERROR_ENUM } from './errorCode';
|
||||
|
||||
export interface ResponseType<T = any> {
|
||||
code: number;
|
||||
@@ -21,6 +21,11 @@ export const jsonRes = <T = any>(
|
||||
const errResponseKey = typeof error === 'string' ? error : error?.message;
|
||||
// Specified error
|
||||
if (ERROR_RESPONSE[errResponseKey]) {
|
||||
// login is expired
|
||||
if (errResponseKey === ERROR_ENUM.unAuthorization) {
|
||||
res.setHeader('Set-Cookie', 'token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT');
|
||||
}
|
||||
|
||||
return res.json(ERROR_RESPONSE[errResponseKey]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { NextApiRequest } from 'next';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import cookie from 'cookie';
|
||||
import { Chat, Model, OpenApi, User } from '../mongo';
|
||||
import type { ModelSchema } from '@/types/mongoSchema';
|
||||
import type { ChatItemSimpleType } from '@/types/chat';
|
||||
@@ -10,17 +11,21 @@ import { ERROR_ENUM } from '../errorCode';
|
||||
import { ChatModelType, OpenAiChatEnum } from '@/constants/model';
|
||||
|
||||
/* 校验 token */
|
||||
export const authToken = (token?: string): Promise<string> => {
|
||||
export const authToken = (req: NextApiRequest): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 获取 cookie
|
||||
const cookies = cookie.parse(req.headers.cookie || '');
|
||||
const token = cookies.token;
|
||||
|
||||
if (!token) {
|
||||
reject('缺少登录凭证');
|
||||
return;
|
||||
return reject(ERROR_ENUM.unAuthorization);
|
||||
}
|
||||
|
||||
const key = process.env.TOKEN_KEY as string;
|
||||
|
||||
jwt.verify(token, key, function (err, decoded: any) {
|
||||
if (err || !decoded?.userId) {
|
||||
reject('凭证无效');
|
||||
reject(ERROR_ENUM.unAuthorization);
|
||||
return;
|
||||
}
|
||||
resolve(decoded.userId);
|
||||
@@ -127,13 +132,13 @@ export const authModel = async ({
|
||||
export const authChat = async ({
|
||||
modelId,
|
||||
chatId,
|
||||
authorization
|
||||
req
|
||||
}: {
|
||||
modelId: string;
|
||||
chatId: '' | string;
|
||||
authorization?: string;
|
||||
req: NextApiRequest;
|
||||
}) => {
|
||||
const userId = await authToken(authorization);
|
||||
const userId = await authToken(req);
|
||||
|
||||
// 获取 model 数据
|
||||
const { model, showModelDetail } = await authModel({
|
||||
|
||||
Reference in New Issue
Block a user