/**
 * Created by Martin on 2017/10/24.
 */

const wxToken = 'Martin201710232344';
const wxAppId = '';
const wxSecret = '';

const crypto = require('crypto');
const request = require('superagent');
const tuLingRobotAppKey = 'fe1a7ebad1e4d5ce85454b2c2f858a90';
const xml2js = require('xml2js');
const parseXMLString = xml2js.parseString;

function validateToken(req, res) {
    console.log('>>>> validate token ...');
    const signature = req.query.signature;
    const timestamp = req.query.timestamp;
    const nonce = req.query.nonce;
    const echostr = req.query.echostr;

    /*  加密/校验流程如下： */
    //1. 将token、timestamp、nonce三个参数进行字典序排序
    let array = new Array(wxToken,timestamp,nonce);
    array.sort();
    let str = array.toString().replace(/,/g,"");

    //2. 将三个参数字符串拼接成一个字符串进行sha1加密
    let sha1Code = crypto.createHash("sha1");
    let code = sha1Code.update(str,'utf-8').digest("hex");

    //3. 开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
    if(code===signature){
        res.send(echostr)
    }else{
        res.send("error");
    }
}

function getMessageFromGongZhongHao(req, res) {
    let _da;
    req.on("data",function(data){
        /*微信服务器传过来的是xml格式的，是buffer类型，因为js本身只有字符串数据类型，所以需要通过toString把xml转换为字符串*/
        _da = data.toString("utf-8");

    });
    req.on("end",function(){
        //console.log("end");
        parseXMLString(_da, function (err, result) {
            console.log('>>>> result : >>>');
            console.log(result);
            if (err)
            {
                res.end('error');
                return;
            }
            let isReturnError = false;
            console.log('1. step >>>>>>>');
            if (result.xml && result.xml.MsgType)
            {
                console.log('2. step >>>>>>>');
                if (Array.isArray(result.xml.MsgType) && result.xml.MsgType.length > 0)
                {
                    console.log('3. step >>>>>>>');
                    const msgType = result.xml.MsgType[0];
                    const userId = result.xml.FromUserName[0];
                    if (msgType == 'text')
                    {
                        console.log('4. step >>>>>>>');
                        const content = result.xml.Content[0];
                        console.log('>>>> Content : ' + Content);
                        request.post('http://www.tuling123.com/openapi/api')
                            .send({key: tuLingRobotAppKey,
                                info: content + '',
                                userid: userId + ''})
                            .end(function (err, response) {
                                console.log('5. step >>>>>>>');
                                console.log('tuling >>>>>>' + response);
                                if (err)
                                {
                                    res.end('error');
                                    return;
                                }
                                console.log('6. step >>>>>>>');
                                let responseJson = JSON.parse(response.text);
                                let replyContent = null;
                                if (responseJson.code == 100000)
                                {
                                    console.log('7. step >>>>>>>');
                                    replyContent = responseJson.text;
                                    let temp = result.xml.FromUserName;
                                    result.xml.ToUserName = result.xml.FromUserName;
                                    result.xml.FromUserName = temp;

                                    parseXMLString(result, function (error, xmlString) {
                                        console.log('8. step >>>>>>>');
                                        if (error)
                                        {
                                            res.end('error');
                                            return;
                                        }
                                        console.log('9. step >>>>>>>');
                                        res.end(xmlString);
                                    });
                                };
                            });
                    }
                }
                else
                {
                    isReturnError = true;
                }
            }
            else
            {
                isReturnError = true;
            }

            if (isReturnError)
            {
                res.end('error');
            }

        });
    });
};


// request.post('http://www.tuling123.com/openapi/api')
//     .send({key: 'fe1a7ebad1e4d5ce85454b2c2f858a90',
//         info: '你好',
//         userid: 'liulongdev'})
//     .end(function (err, res) {
//         if (err)
//         {
//             console.log('>>> error');
//             console.log(err);
//         }
//         console.log('tuling >>>>>>' + res);
//         console.log(res.text);
//         let responseText = res.text;
//
//     });

function getXMLNodeValue(node_name,xml){
    let tmp = xml.split("<"+node_name+">");
    let _tmp = tmp[1].split("</"+node_name+">");
    return _tmp[0];
}

exports.validateToken = validateToken;
exports.getMessageFromGongZhongHao = getMessageFromGongZhongHao;
@@ -329,16 +329,57 @@
 eString;
+%0Aconst builderXML = new xml2js.Builder();
 %0A%0Afuncti
@@ -2265,74 +2265,8 @@
 0%5D;%0A
-                        console.log('%3E%3E%3E%3E Content : ' + Content);%0A
     
@@ -3568,17 +3568,16 @@
 = temp;%0A
-%0A
         
@@ -3608,457 +3608,129 @@
     
-parseXMLString(result, function (error, xmlString) %7B%0A                                        console.log('8. step %3E%3E%3E%3E%3E%3E%3E');%0A                                        if (error)%0A                                        %7B%0A                                            res.end('error');%0A                                            return;%0A                                        %7D%0A                                        console.log('9. step %3E%3E%3E%3E%3E%3E%3E');%0A   
+result.xml.Content = replyContent + '';%0A                                    let xmlStr = builderXML.buildObject(result);%0A
     
@@ -3753,33 +3753,32 @@
                 
- 
 res.end(xmlStrin
@@ -3779,51 +3779,8 @@
 lStr
-ing);%0A                                    %7D
 );%0A 
@@ -4201,468 +4201,8 @@
 );%0A%7D
-;%0A%0A%0A// request.post('http://www.tuling123.com/openapi/api')%0A//     .send(%7Bkey: 'fe1a7ebad1e4d5ce85454b2c2f858a90',%0A//         info: '%E4%BD%A0%E5%A5%BD',%0A//         userid: 'liulongdev'%7D)%0A//     .end(function (err, res) %7B%0A//         if (err)%0A//         %7B%0A//             console.log('%3E%3E%3E error');%0A//             console.log(err);%0A//         %7D%0A//         console.log('tuling %3E%3E%3E%3E%3E%3E' + res);%0A//         console.log(res.text);%0A//         let responseText = res.text;%0A//%0A//     %7D);
 %0A%0Afu

test tuling