mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-29 09:44:47 +00:00
perf: send code
This commit is contained in:
11
client/src/service/api/plugins.d.ts
vendored
Normal file
11
client/src/service/api/plugins.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import { UserAuthTypeEnum } from '../../constants/common';
|
||||
|
||||
export type SendCodeBody = {
|
||||
username: string;
|
||||
type: `${UserAuthTypeEnum}`;
|
||||
};
|
||||
export type AuthCodeBody = {
|
||||
username: string;
|
||||
type: `${UserAuthTypeEnum}`;
|
||||
code: string;
|
||||
};
|
5
client/src/service/api/plugins.ts
Normal file
5
client/src/service/api/plugins.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { POST } from './request';
|
||||
import type { SendCodeBody, AuthCodeBody } from './plugins.d';
|
||||
|
||||
export const sendCode = (data: SendCodeBody) => POST(global.systemPlugins.authCode?.sendUrl, data);
|
||||
export const authCode = (data: AuthCodeBody) => POST(global.systemPlugins.authCode?.authUrl, data);
|
129
client/src/service/api/request.ts
Normal file
129
client/src/service/api/request.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import axios, { Method, InternalAxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
import { clearToken, getToken } from '@/utils/user';
|
||||
import { TOKEN_ERROR_CODE } from '@/service/errorCode';
|
||||
|
||||
interface ConfigType {
|
||||
headers?: { [key: string]: string };
|
||||
hold?: boolean;
|
||||
timeout?: number;
|
||||
}
|
||||
interface ResponseDataType {
|
||||
code: number;
|
||||
message: string;
|
||||
data: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求开始
|
||||
*/
|
||||
function requestStart(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig {
|
||||
if (config.headers) {
|
||||
// config.headers.token = getToken();
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求成功,检查请求头
|
||||
*/
|
||||
function responseSuccess(response: AxiosResponse<ResponseDataType>) {
|
||||
return response;
|
||||
}
|
||||
/**
|
||||
* 响应数据检查
|
||||
*/
|
||||
function checkRes(data: ResponseDataType) {
|
||||
if (data === undefined) {
|
||||
console.log('error->', data, 'data is empty');
|
||||
return Promise.reject('服务器异常');
|
||||
} else if (data.code < 200 || data.code >= 400) {
|
||||
return Promise.reject(data);
|
||||
}
|
||||
return data.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应错误
|
||||
*/
|
||||
function responseError(err: any) {
|
||||
if (!err) {
|
||||
return Promise.reject({ message: '未知错误' });
|
||||
}
|
||||
if (typeof err === 'string') {
|
||||
return Promise.reject({ message: err });
|
||||
}
|
||||
// 有报错响应
|
||||
if (err?.code in TOKEN_ERROR_CODE) {
|
||||
clearToken();
|
||||
window.location.replace(
|
||||
`/login?lastRoute=${encodeURIComponent(location.pathname + location.search)}`
|
||||
);
|
||||
return Promise.reject({ message: 'token过期,重新登录' });
|
||||
}
|
||||
if (err?.response?.data) {
|
||||
return Promise.reject(err?.response?.data);
|
||||
}
|
||||
return Promise.reject(err);
|
||||
}
|
||||
|
||||
/* 创建请求实例 */
|
||||
const instance = axios.create({
|
||||
timeout: 60000, // 超时时间
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
/* 请求拦截 */
|
||||
instance.interceptors.request.use(requestStart, (err) => Promise.reject(err));
|
||||
/* 响应拦截 */
|
||||
instance.interceptors.response.use(responseSuccess, (err) => Promise.reject(err));
|
||||
|
||||
function request(url: string, data: any, config: ConfigType, method: Method): any {
|
||||
/* 去空 */
|
||||
for (const key in data) {
|
||||
if (data[key] === null || data[key] === undefined) {
|
||||
delete data[key];
|
||||
}
|
||||
}
|
||||
|
||||
return instance
|
||||
.request({
|
||||
baseURL: '/api',
|
||||
url,
|
||||
method,
|
||||
data: ['POST', 'PUT'].includes(method) ? data : null,
|
||||
params: !['POST', 'PUT'].includes(method) ? data : null,
|
||||
...config // 用户自定义配置,可以覆盖前面的配置
|
||||
})
|
||||
.then((res) => checkRes(res.data))
|
||||
.catch((err) => responseError(err));
|
||||
}
|
||||
|
||||
/**
|
||||
* api请求方式
|
||||
* @param {String} url
|
||||
* @param {Any} params
|
||||
* @param {Object} config
|
||||
* @returns
|
||||
*/
|
||||
export function GET<T>(url?: string, params = {}, config: ConfigType = {}): Promise<T> {
|
||||
if (!url) return Promise.reject('no url');
|
||||
return request(url, params, config, 'GET');
|
||||
}
|
||||
|
||||
export function POST<T>(url?: string, data = {}, config: ConfigType = {}): Promise<T> {
|
||||
if (!url) return Promise.reject('no url');
|
||||
return request(url, data, config, 'POST');
|
||||
}
|
||||
|
||||
export function PUT<T>(url?: string, data = {}, config: ConfigType = {}): Promise<T> {
|
||||
if (!url) return Promise.reject('no url');
|
||||
return request(url, data, config, 'PUT');
|
||||
}
|
||||
|
||||
export function DELETE<T>(url?: string, data = {}, config: ConfigType = {}): Promise<T> {
|
||||
if (!url) return Promise.reject('no url');
|
||||
return request(url, data, config, 'DELETE');
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
import { Schema, model, models, Model } from 'mongoose';
|
||||
import { AuthCodeSchema as AuthCodeType } from '@/types/mongoSchema';
|
||||
|
||||
const AuthCodeSchema = new Schema({
|
||||
username: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
code: {
|
||||
type: String,
|
||||
required: true,
|
||||
length: 6
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: ['register', 'findPassword'],
|
||||
required: true
|
||||
},
|
||||
expiredTime: {
|
||||
type: Number,
|
||||
default: () => Date.now() + 5 * 60 * 1000
|
||||
}
|
||||
});
|
||||
|
||||
export const AuthCode: Model<AuthCodeType> =
|
||||
models['auth_code'] || model('auth_code', AuthCodeSchema);
|
@@ -19,11 +19,6 @@ export async function connectToDatabase(): Promise<void> {
|
||||
// init global data
|
||||
global.qaQueueLen = 0;
|
||||
global.vectorQueueLen = 0;
|
||||
global.systemEnv = {
|
||||
vectorMaxProcess: 10,
|
||||
qaMaxProcess: 10,
|
||||
pgIvfflatProbe: 10
|
||||
};
|
||||
global.sendInformQueue = [];
|
||||
global.sendInformQueueLen = 0;
|
||||
// proxy obj
|
||||
@@ -116,7 +111,6 @@ async function initPg() {
|
||||
}
|
||||
}
|
||||
|
||||
export * from './models/authCode';
|
||||
export * from './models/chat';
|
||||
export * from './models/app';
|
||||
export * from './models/user';
|
||||
|
Reference in New Issue
Block a user