bRequestEa.js 3.85 KB
Newer Older
1 2
import axios from 'axios'
import md5 from "js-md5"
xulili's avatar
xulili committed
3 4
// import envConfig from '@/config/env-config'
import envConfig from '../config/env-config'
Z's avatar
Z committed
5
// let BASE_API = "https://gd.chfatech.com/guangdian"
6
// let BASE_API = "/api/"
7
// let BASE_API = envConfig.appOyAPI
8 9
// let BASE_API = "http://111.26.165.55:8010/crminterface.ashx"
let BASE_API = "/api"
Z's avatar
Z committed
10

11
const zlog = console.log.bind(console)
12 13 14 15 16 17 18 19

// 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
xulili's avatar
xulili committed
20

21
    let res = "";
xulili's avatar
xulili committed
22

23 24
    // URL 编码
    for (let key in args) {
25
        res += encodeURIComponent(key) + "%3D" + encodeURIComponent(args[key]) + "%26";
26 27
    }
    res = res.slice(0, -3);
xulili's avatar
xulili committed
28

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

32
    return res;
33
};
34 35

// Basic Info
Z's avatar
Z committed
36 37 38 39 40 41 42
// 欧亚认证信息_V1
// appUser: "YBF001",
// appSecret: "t04yYm6gjsuHeehxOxojtmiwlYfXY8Zkdowf"
// 欧亚认证信息_V2
// appUser: "WZ001",
// appSecret: "mfdmTGAYU2M3F=JeC0mImYzU5Yjg1E2ZjZmO"

43
let basicInfo = {
Z's avatar
Z committed
44
    appUser: "WZ001",
Z's avatar
Z committed
45
    ver: "v2.17",
Z's avatar
Z committed
46
    appSecret: "mfdmTGAYU2M3F=JeC0mImYzU5Yjg1E2ZjZmO"
47 48 49
}

// 创建 axios 实例
xulili's avatar
xulili committed
50 51
debugger

52 53 54 55 56 57 58
const service = axios.create({
    baseURL: BASE_API,
    timeout: 20000
})

// request 拦截器
service.interceptors.request.use(
Z's avatar
Z committed
59
    req => {
60
        // zlog('--->axios: req.params: start:', req.params)
61 62 63
        let apiMethod = req.method

        if (apiMethod === 'post') {
Z's avatar
Z committed
64
            // alert('--->PostToEA: bRequestEa.js: if: post: IN.')
65
            // zlog('--->axios: req.data:', req.data)
66 67 68 69
            let oldPostData = JSON.parse(JSON.stringify(req.data))
            let newPostData = {
                appUser: basicInfo.appUser,
                appCode: oldPostData.args.appCode,
70
                // ts: String(Date.parse(new Date())),
71 72
                // ts: '1563355520',
                ts: String(parseInt(new Date().getTime()/1000)),
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                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
            }

Z's avatar
Z committed
88
            // alert(`--->PostToEA: bRequestEa.js: if: end: req.data = ${JSON.stringify(postData)}`)
89
            req.data = postData
90
        }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        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
114 115
        }

116
        // zlog('--->axios: req.params: end:', req.params)
Z's avatar
Z committed
117
        return req
118 119
    },
    error => {
120
        // alert(`--->PostToEA: bRequestEa.js: err = ${JSON.stringify(error)}`)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        Promise.reject(error)
    }
)

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

xulili's avatar
xulili committed
136
export default service