| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import axios from "axios";
- let baseURL: string;
- if (process.env.NODE_ENV === "production") {
- baseURL = "";
- } else {
- baseURL = process.env.NEXT_PUBLIC_DEV_BASE_URL as string;
- }
- // 拦截器
- axios.interceptors.response.use(
- (response) => {
- return response;
- },
- (error) => {
- return Promise.reject(error);
- }
- );
- axios.interceptors.request.use(
- (config) => {
- console.log(config);
- config.headers["Accept"] = "application/json";
- config.baseURL = baseURL;
- // config.timeout = 10000;
- return config;
- },
- (error) => {
- return Promise.reject(error);
- }
- );
- export function requests<T>(
- url: string,
- option: {
- method: "post" | "get" | "put" | "delete";
- params?: { [key: string]: any };
- data?: { [key: string]: any };
- headers?: { [key: string]: any };
- requestType?: "json" | "form";
- timeout?: number;
- }
- ) {
- if (!option.requestType) {
- option.requestType = "json";
- }
- return new Promise<T>((resolve, reject) => {
- const Content_Type =
- option.requestType == "json"
- ? "application/json;charset=UTF-8"
- : "application/x-www-form-urlencoded;charset=UTF-8";
- axios<T>({
- timeout: option.timeout,
- url: baseURL + url,
- method: option.method,
- params: option.params,
- data: option.data,
- headers: {
- "Content-Type": Content_Type,
- ...option.headers,
- },
- })
- .then((res) => {
- resolve(res.data);
- })
- .catch((err) => {
- reject(err);
- });
- });
- }
- export default requests;
|