const request = require('request')
const crypto = require('crypto');
const md5 = crypto.createHash('md5');
var parseString = require('xml2js').parseString;
class httpClient {
    constructor() {}
    //顺丰运单号查询
    callSfExpressServiceByCSIM(reqURL, myReqXml, clientCode, checkword) {
        if (!reqURL) {
            reqURL = "https://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService"
        }
        //加密生成verifyCode;
        let verifyCode = md5.update(myReqXml + checkword, 'utf8').digest('base64');

        let promise = new Promise((resolve, reject) => {
            request.post(reqURL, {
                form: {
                    "xml": myReqXml,
                    "verifyCode": verifyCode
                }
            }, function (error, response, body) {
                if (error) {
                    return reject(error);
                }
                if (body) {
                    let data = {};
                    parseString(response.body, function (err, result) {
                        data.body = result.Response.Body;
                        data.msg = result.Response.Head[0];
                        if (data.msg == "ERR") {
                            data.reason = result.Response.ERROR[0]["_"];
                        }
                    });

                    return resolve(data);
                }
            })
        }).catch(error => {
            reject(error);
        });
        return promise;
    }

    
    /**
     * 圆通运单号查询
     * @param {*} reqURL 
     * @param {*} myReqXml 
     * @param {*} clientCode 
     * @param {*} checkword 
     */
    callYTExpressServiceByCSIM(reqURL, myReqXml, clientCode, checkword) {
    //     if (!reqURL) {
    //         reqURL = "https://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService"
    //     }
    //     //加密生成verifyCode;
    //     let verifyCode = md5.update(myReqXml + checkword, 'utf8').digest('base64');

    //     let promise = new Promise((resolve, reject) => {
    //         request.post(reqURL, {
    //             form: {
    //                 "xml": myReqXml,
    //                 "verifyCode": verifyCode
    //             }
    //         }, function (error, response, body) {
    //             if (error) {
    //                 return reject(error);
    //             }
    //             if (body) {
    //                 let data = {};
    //                 parseString(response.body, function (err, result) {
    //                     data.body = result.Response.Body;
    //                     data.msg = result.Response.Head[0];
    //                     if (data.msg == "ERR") {
    //                         data.reason = result.Response.ERROR[0]["_"];
    //                     }
    //                 });

    //                 return resolve(data);
    //             }
    //         })
    //     }).catch(error => {
    //         reject(error);
    //     });
    //     return promise;
    }
}

module.exports = httpClient;