import axios from 'axios'
import { Md5 } from 'ts-md5'
import qs from 'qs'
import {replaceAll} from "hardhat/internal/util/strings";

axios.defaults.baseURL = 'http://www.410eth.com:8088'

axios.interceptors.request.use(
  function(config) {
    // auth
    let timestamp = parseInt((Date.parse(new Date().toString()) / 1000) + '')
    let baseStr = '410410' + timestamp
    let splitStrList = baseStr.split("")
    let replacedStr = replaceAll(baseStr, splitStrList[splitStrList.length - 1], "")
    let authStr = Md5.hashStr(replacedStr)

    config.data.timestamp = timestamp
    config.data.auth = authStr

    // headers处理
    // @ts-ignore
    config.headers['content-type'] = 'application/x-www-form-urlencoded'
    config.data = qs.stringify(config.data)

    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
)

export default axios
