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 = { 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(config: AxiosOptions): Promise & R> { return new Promise((resolve, reject) => { this.axiosInstance .request & 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(config: AxiosOptions): Promise & R> { return this.request({ ...config, method: "get" }); } post(config: AxiosOptions): Promise & R> { return this.request({ ...config, method: "post" }); } put(config: AxiosOptions): Promise & R> { return this.request({ ...config, method: "put" }); } delete(config: AxiosOptions): Promise & R> { return this.request({ ...config, method: "delete" }); } }