{"size": 4892, "ext": "js", "max_stars_repo_path": "Front-end file/src/utils/http.js", "max_stars_repo_name": "michaelHookon/01-Hackathon-Dao-NFT-bank", "max_stars_repo_stars_event_min_datetime": "2022-03-23T01:05:51.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-23T01:05:51.000Z", "max_issues_repo_path": "Front-end file/src/utils/http.js", "max_issues_repo_name": "michaelHookon/01-Hackathon-Dao-NFT-bank", "max_issues_repo_issues_event_min_datetime": "2022-03-15T03:50:55.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-15T03:50:55.000Z", "max_forks_repo_path": "Front-end file/src/utils/http.js", "max_forks_repo_name": "michaelHookon/01-Hackathon-Dao-NFT-bank", "max_forks_repo_forks_event_min_datetime": "2022-03-14T10:04:08.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-28T04:01:28.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 5, "avg_line_length": 25.2164948454, "max_line_length": 82, "alphanum_fraction": 0.4801717089}
import axios from "axios";
const REQUEST_TIMEOUT = 10000;
const MSG_TIMEOUT = 3 * 1000;
const SERVER_ADDRESS = 'static.edu.sdanshu.com'
const SERVER_PORT = 80;

const STATIC_ADDRESS = 'https://static.edu.sdanshu.com'
const RESOURCE_PORT = 8386;
// const BASE_URL = `http://${SERVER_ADDRESS}:${SERVER_PORT}`
const BASE_URL = 'http://www.rainbowchain.io/'
const headers = {
    // "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Content-Type": "application/json",
};
export {
    STATIC_ADDRESS,
    BASE_URL
}
const axio = axios.create({
    baseURL: BASE_URL,
    headers
});

axio.defaults.timeout = REQUEST_TIMEOUT;

axio.interceptors.request.use(
    config => {
        // let token = localStorage.getItem('token')
        let token = sessionStorage.getItem("token");
        if (token) {
            config.headers.Authorization = "Bearer " + token;
        }
        if (config.url == "/login") {
            for (var k in config.headers) {
                if (k == "Authorization") {
                    delete config.headers[k];
                }
            }
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axio.interceptors.response.use(
    response => {
        if (response.headers && response.headers.authorization) {
            let token = response.headers.authorization;
            if (token != "" && token != null) {
                headers.Authorization = token;
            }
        }
        if (response.headers && response.headers.uid) {
            localStorage.setItem("uid", response.headers.uid);
        }
        if (response.data && response.data.code == 401) {
            console.log("401");
            localStorage.removeItem("token");
            sessionStorage.removeItem('token')
            if (location.href.indexOf("/login") === -1) {
                location.href = "#/login?errorMsg=Please log in before operation";
            }
        } else if (response.data && response.data.code == 100001) {
            if (location.href.indexOf("/login") === -1) {
                location.href = "#/login?errorMsg=Please log in before operation";
            }
        }
        return response;
    },
    error => {
        console.log("request timeout");
        return Promise.reject(error);
    }
);

/**
 * Encapsulate get method
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, data) {
    return new Promise((resolve, reject) => {
        axio({
            method: "get",
            url,
            params: data,
            responseType: "json"
        })
            .then(response => {
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            });
    });
}

/**
 * Encapsulate post request
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data) {
    return new Promise((resolve, reject) => {
        axio({
            method: "post",
            url,
            data,
            responseType: "json",
            headers: {
                "Content-Type": "application/json",
                Accept: "application/json"
            }
        })
            .then(response => {
                if (url == "/login") {
                    resolve(response);
                }
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            });
    });
}

/**
 * Encapsulate Petch requests
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data) {
    return new Promise((resolve, reject) => {
        axio({
            method: "patch",
            url,
            data,
            responseType: "json",
            headers: {
                "Content-Type": "application/json",
                Accept: "application/json"
            }
        })
            .then(response => {
                if (url == "/login") {
                    resolve(response);
                }
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            });
    });
}
/**
 * Encapsulate delete request
 * @param url
 * @param data
 * @returns {Promise}
 */

export function Delete(url, data) {
    return new Promise((resolve, reject) => {
        axio({
            method: "delete",
            url,
            data,
            responseType: "json",
            headers: {
                "Content-Type": "application/json",
                Accept: "application/json"
            }
        })
            .then(response => {
                if (url == "/login") {
                    resolve(response);
                }
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            });
    });
}
export {}
