123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import { Response } from "@/types";
- import { Toast } from "antd-mobile";
- import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from "axios";
- import type { AxiosOptions } from "./type";
- import { setupConfig } from "@/api/setup";
- import { defaultLocale } from "@/i18n/routing";
- import { getCookies, getToken } from "@/utils/Cookies";
- interface CustomRequestConfig {
- toast?: boolean;
- }
- const getInitBaseUrl = () => {
- const str = sessionStorage.getItem("config");
- if (str) return JSON.parse(str);
- return fetch("https://static.tiktokjakjkl.icu/test/index.json")
- .then((res) => res.json())
- .then((config) => {
- sessionStorage.setItem("config", JSON.stringify(config));
- return config;
- });
- };
- const errorHandle = (status: number): void => {
- const msgMap: Record<number, string> = {
- 1022: "Restrição anormal de conta",
- 6002: "Nenhum bônus não pode ser reclamado no modo bônus ativado",
- 6001: "As condições do bônus de esvaziamento não são atendidas",
- };
- const msg = msgMap[status];
- if (msg) {
- Toast.show({
- icon: "fail",
- content: msg,
- });
- }
- };
- export default class Request {
- private axiosInstance: AxiosInstance;
- private readonly options: AxiosOptions;
- constructor(options: AxiosOptions) {
- this.axiosInstance = axios.create(options);
- this.options = options;
- this.setupInterceptors();
- }
- setupInterceptors() {
- // 定义拦截器
- const {
- options: { transform },
- axiosInstance,
- } = this;
- // 如果没有自定义拦截器则返回
- if (!transform) {
- return;
- }
- // 解包拦截器
- const {
- requestInterceptor,
- requestInterceptorCatch,
- responseInterceptor,
- responseInterceptorCatch,
- } = transform;
- /**
- * @description 全局请求拦截
- */
- axiosInstance.interceptors.request.use(
- async (config: InternalAxiosRequestConfig & CustomRequestConfig) => {
- const result = await setupConfig();
- config.baseURL = result.NEXT_PUBLIC_BASE_URL;
- if (requestInterceptor) {
- config = requestInterceptor(config);
- // header请求头添加token
- config.headers["Token"] = getToken() || "";
- config.headers["language"] = getCookies("language") || defaultLocale;
- if (config && config?.toast) {
- Toast.show({
- icon: "loading",
- maskClickable: false,
- });
- }
- }
- return config;
- },
- (error) => {
- if (requestInterceptorCatch) {
- requestInterceptorCatch(error);
- }
- }
- );
- /**
- * @description 全局响应拦截
- */
- axiosInstance.interceptors.response.use(
- (res) => {
- // Toast.clear();
- if (responseInterceptor) {
- res = responseInterceptor(res);
- }
- return res;
- },
- (error) => {
- if (responseInterceptorCatch) {
- responseInterceptorCatch(error);
- }
- Toast.clear();
- }
- );
- }
- /**
- * @description 单个实列请求
- */
- request<T = unknown, R = unknown>(config: AxiosOptions): Promise<Response<T> & R> {
- return new Promise((resolve, reject) => {
- this.axiosInstance
- .request<unknown, AxiosResponse<Response<T> & R>>(config)
- .then(async (res) => {
- const { transform } = config;
- if (transform && transform.responseInterceptor) {
- res = transform.responseInterceptor(res);
- }
- if (res && res.data && res.data.code === 200) {
- resolve(res.data);
- } else {
- errorHandle(res.data.code);
- reject(res);
- }
- })
- .catch((error) => {
- reject(error);
- });
- });
- }
- get<T, R = unknown>(config: AxiosOptions): Promise<Response<T> & R> {
- return this.request({ ...config, method: "get" });
- }
- post<T, R = unknown>(config: AxiosOptions): Promise<Response<T> & R> {
- return this.request({ ...config, method: "post" });
- }
- put<T, R = unknown>(config: AxiosOptions): Promise<Response<T> & R> {
- return this.request({ ...config, method: "put" });
- }
- delete<T, R = unknown>(config: AxiosOptions): Promise<Response<T> & R> {
- return this.request({ ...config, method: "delete" });
- }
- }
|