perf: plus api

This commit is contained in:
archer
2023-08-22 17:45:18 +08:00
parent 7a231c6501
commit a5f8fae3f2
25 changed files with 136 additions and 499 deletions

View File

@@ -1,11 +0,0 @@
import { UserAuthTypeEnum } from '../../constants/common';
export type SendCodeBody = {
username: string;
type: `${UserAuthTypeEnum}`;
};
export type AuthCodeBody = {
username: string;
type: `${UserAuthTypeEnum}`;
code: string;
};

View File

@@ -1,21 +0,0 @@
import { GET, 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);
export const textCensor = (data: { text: string }) => {
if (!global.systemPlugins.censor?.textUrl) return;
return POST(global.systemPlugins.censor?.textUrl, data);
};
export const getWxPayQRUrl = (amount: number) =>
POST<{
code_url: string;
orderId: string;
}>(global.systemPlugins.pay?.getWxQRUrl, { amount });
export const getWxPayQRResult = (orderId: string) =>
POST<{
trade_state: string;
trade_state_desc: string;
}>(global.systemPlugins.pay?.getWxQRResult, { orderId });

View File

@@ -1,128 +0,0 @@
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 && (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({
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('The Plugin is not installed');
return request(url, params, config, 'GET');
}
export function POST<T>(url?: string, data = {}, config: ConfigType = {}): Promise<T> {
if (!url) return Promise.reject('The Plugin is not installed');
return request(url, data, config, 'POST');
}
export function PUT<T>(url?: string, data = {}, config: ConfigType = {}): Promise<T> {
if (!url) return Promise.reject('The Plugin is not installed');
return request(url, data, config, 'PUT');
}
export function DELETE<T>(url?: string, data = {}, config: ConfigType = {}): Promise<T> {
if (!url) return Promise.reject('The Plugin is not installed');
return request(url, data, config, 'DELETE');
}

View File

@@ -19,7 +19,7 @@ const PromotionRecordSchema = new Schema({
type: {
type: String,
required: true,
enum: ['invite', 'register']
enum: ['pay', 'register']
},
amount: {
type: Number,

View File

@@ -15,7 +15,7 @@ import { getChatModel } from '@/service/utils/data';
import { countModelPrice } from '@/service/events/pushBill';
import { ChatModelItemType } from '@/types/model';
import { UserModelSchema } from '@/types/mongoSchema';
import { textCensor } from '@/service/api/plugins';
import { textCensor } from '@/api/service/plugins';
import { ChatCompletionRequestMessageRoleEnum } from 'openai';
import { AppModuleItemType } from '@/types/app';

View File

@@ -54,16 +54,16 @@ export const jsonRes = <T = any>(
}
addLog.error(msg, {
message: error.message,
stack: error.stack,
...(error.config && {
message: msg,
stack: error?.stack,
...(error?.config && {
config: {
headers: error.config.headers,
url: error.config.url,
data: error.config.data
}
}),
...(error.response && {
...(error?.response && {
response: {
status: error.response.status,
statusText: error.response.statusText
@@ -111,16 +111,16 @@ export const sseErrRes = (res: NextApiResponse, error: any) => {
}
addLog.error(`sse error: ${msg}`, {
message: error.message,
stack: error.stack,
...(error.config && {
message: msg,
stack: error?.stack,
...(error?.config && {
config: {
headers: error.config.headers,
url: error.config.url,
data: error.config.data
}
}),
...(error.response && {
...(error?.response && {
response: {
status: error.response.status,
statusText: error.response.statusText

View File

@@ -1,44 +0,0 @@
// @ts-ignore
import Payment from 'wxpay-v3';
export const getPayment = () => {
try {
return new Payment({
appid: process.env.WX_APPID,
mchid: process.env.WX_MCHID,
private_key: process.env.WX_PRIVATE_KEY?.replace(/\\n/g, '\n'),
serial_no: process.env.WX_SERIAL_NO,
apiv3_private_key: process.env.WX_V3_CODE,
notify_url: process.env.WX_NOTIFY_URL
});
} catch (error) {
return Promise.reject(error);
}
};
export const nativePay = async (amount: number, payId: string) => {
try {
const res = await getPayment().native({
description: 'Fast GPT 余额充值',
out_trade_no: payId,
amount: {
total: amount
}
});
return JSON.parse(res.data).code_url as string;
} catch (error) {
return Promise.reject(error);
}
};
export const getPayResult = async (payId: string) => {
try {
const res = await getPayment().getTransactionsByOutTradeNo({
out_trade_no: payId
});
return JSON.parse(res.data);
} catch (error) {
return Promise.reject(error);
}
};