import axios from 'axios'
import md5 from "js-md5"
// import envConfig from '@/config/env-config'
import envConfig from '../config/env-config'
// let BASE_API = "https://gd.chfatech.com/guangdian"
// let BASE_API = "/api/"
// let BASE_API = envConfig.appOyAPI
// let BASE_API = "http://111.26.165.55:8010/crminterface.ashx"
let BASE_API = "/api"

const zlog = console.log.bind(console)

// URL 编码与拼接
const createSign = (inPostData, inAppSecret) => {
    let appUser = inPostData.appUser
    let appCode = inPostData.appCode
    let ts = inPostData.ts
    let args = JSON.parse(JSON.stringify(inPostData.args));
    let appSecret = inAppSecret

    let res = "";

    // URL 编码
    for (let key in args) {
        res += encodeURIComponent(key) + "%3D" + encodeURIComponent(args[key]) + "%26";
    }
    res = res.slice(0, -3);

    // URL 拼接
    res += `&appUser=${appUser}&appCode=${appCode}&${ts}${appSecret}`

    return res;
};

// Basic Info
// 欧亚认证信息_V1
// appUser: "YBF001",
// appSecret: "t04yYm6gjsuHeehxOxojtmiwlYfXY8Zkdowf"
// 欧亚认证信息_V2
// appUser: "WZ001",
// appSecret: "mfdmTGAYU2M3F=JeC0mImYzU5Yjg1E2ZjZmO"

let basicInfo = {
    appUser: "WZ001",
    ver: "v2.17",
    appSecret: "mfdmTGAYU2M3F=JeC0mImYzU5Yjg1E2ZjZmO"
}

// 创建 axios 实例
// debugger

const service = axios.create({
    baseURL: BASE_API,
    timeout: 20000
})

// request 拦截器
service.interceptors.request.use(
    req => {
        // zlog('--->axios: req.params: start:', req.params)
        let apiMethod = req.method

        if (apiMethod === 'post') {
            // alert('--->PostToEA: bRequestEa.js: if: post: IN.')
            // zlog('--->axios: req.data:', req.data)
            let oldPostData = JSON.parse(JSON.stringify(req.data))
            let newPostData = {
                appUser: basicInfo.appUser,
                appCode: oldPostData.args.appCode,
                // ts: String(Date.parse(new Date())),
                // ts: '1563355520',
                ts: String(parseInt(new Date().getTime()/1000)),
                ver: basicInfo.ver,
                args: oldPostData.args
            }

            delete newPostData.args.appCode

            let postData = {
                appUser: newPostData.appUser,
                appCode: newPostData.appCode,
                ts: newPostData.ts,
                sign: md5(createSign(newPostData, basicInfo.appSecret)),
                ver: newPostData.ver,
                args: newPostData.args
            }

            // alert(`--->PostToEA: bRequestEa.js: if: end: req.data = ${JSON.stringify(postData)}`)
            req.data = postData
        }
        if (apiMethod === 'get') {
            // log('--->axios: req.data:', req.data)
            let oldPostData = JSON.parse(JSON.stringify(req.params))
            let newPostData = {
                appUser: basicInfo.appUser,
                appCode: oldPostData.args.appCode,
                ts: String(Date.parse(new Date())),
                ver: basicInfo.ver,
                args: oldPostData.args
            }

            delete newPostData.args.appCode

            let postData = {
                appUser: newPostData.appUser,
                appCode: newPostData.appCode,
                ts: newPostData.ts,
                sign: md5(createSign(newPostData, basicInfo.appSecret)),
                ver: newPostData.ver,
                args: newPostData.args
            }

            req.params = postData
        }

        // zlog('--->axios: req.params: end:', req.params)
        return req
    },
    error => {
        // alert(`--->PostToEA: bRequestEa.js: err = ${JSON.stringify(error)}`)
        Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
        const res = response.data
        return res
    },
    error => {
        return Promise.reject(error)
    }
)

export default service
