var settings = require("../../config/settings");
const axios = require('axios');

//工商查询操作
class UtilESignBaoService {
    constructor() {
        this.eSignBaoBankVerifyUrl = settings.apiconfig.eSignBaoBankVerifyUrl();
    }


    async personTwo(params, opName) {
        var result = {
            code: 1,
            message: "success",
            data: {}
        };
        var reqUrl = this.eSignBaoBankVerifyUrl + "/infoauth/psn/identity";
        var tResult = await this.returnResult(params, reqUrl, opName, "personTwo", result);
        if (tResult.errCode != 0) {
            result.code = -110;
            if (tResult.message && tResult.message.indexOf(":") > 0) {
                var tmpListStr = tResult.message.split(':');
                result.message = tmpListStr[1] ? tmpListStr[1] : tmpListStr[0];
            } else {
                result.message = "个人二要素验证失败";
            }
            return result;
        }
        if (tResult.errCode == 0 && tResult.data && !tResult.data.pass) {
            result.code = -120;
            result.message = "姓名或身份证错误";
            return result;
        }
        result.data = tResult.data;
        return result;
    }

    async bankthree(params, opName) { //三要素验证
        var result = {
            code: 1,
            message: "success",
            data: {}
        };
        var reqUrl = this.eSignBaoBankVerifyUrl + "/infoauth/psn/bankthree";
        var tResult = await this.returnResult(params, reqUrl, opName, "createAccountId", result);
        if (tResult.errCode != 0) {
            result.code = -110;
            if (tResult.message && tResult.message.indexOf(":") > 0) {
                var tmpListStr = tResult.message.split(':');
                result.message = tmpListStr[1] ? tmpListStr[1] : tmpListStr[0];
            } else {
                result.message = "三要素验证失败，错误信息[" + tResult.message + "]";
            }
            return result;
        }
        if (tResult.errCode == 0 && tResult.data && !tResult.data.pass) {
            result.code = -120;
            result.message = "亲，提供的信息不正确，请验证后再试！";
            return result;
        }
        result.data = tResult.data;
        return result;
    }

    async bankfour(params, opName) {
        var result = {
            code: 1,
            message: "success",
            data: {}
        };
        var reqUrl = this.eSignBaoBankVerifyUrl + "/infoauth/psn/bankfour";
        var tResult = await this.returnResult(params, reqUrl, opName, "createAccountId", result);
        if (tResult.errCode != 0) {
            result.code = -110;
            if (tResult.message && tResult.message.indexOf(":") > 0) {
                var tmpListStr = tResult.message.split(':');
                result.message = tmpListStr[1] ? tmpListStr[1] : tmpListStr[0];
            } else {
                result.message = "接口请求错误";
            }
            return result;
        }
        if (tResult.errCode == 0 && tResult.data && !tResult.data.pass) {
            result.code = -120;
            result.message = "亲，提供的信息不正确，请验证后再试！";
            return result;
        }
        result.data = tResult.data;
        return result;
    }


    async returnResult(params, reqUrl, opClassName, opMethod, result) {
        var data = JSON.stringify(params);
        var rtn = null;
        try {
            if (settings.env == 'dev') {
                let rs = await axios({
                    method: 'post',
                    url: reqUrl,
                    data: params,
                    headers: {
                        'X-Tsign-Open-App-Id': '5111588557',
                        'X-Tsign-Open-App-Secret': '1595787e8d5b7d19f7b6798f16f41fc2'
                    },
                });
                console.log(rs);
                if (rs.data.errCode != 0) {
                    result.code = -110;
                    result.message = "接口请求错误";
                    return result;
                }
                result.errCode = 0;
                result.data = rs.data.data;
                return result;
            }
            rtn = await this.restClient.execPostESignBao(data, reqUrl);
            console.log(rtn, "rtn---------------------------------e签宝--------execPostESignBao---------------->>>>>>>>>>>>>>>>");
            if (rtn.stdout == null || rtn.stdout == "" || rtn.stdout == "undefined") {
                result.errCode = -100;
                result.code = -100;
                result.message = "请求结果返回为空";
                return result;
            }
            return JSON.parse(rtn.stdout);
        } catch (e) {
            console.log(e.stack);
            //日志记录
            logCtl.error({
                optitle: "e签宝opClassName=" + opClassName + "，opMethod=" + opMethod + "操作异常异常error",
                op: "base/service/impl/utilesignbaoSve/returnResult，请求url：" + reqUrl,
                content: "请求参数：" + data + "，返回结果参数rtn.stdout：" + rtn.stdout + "，异常信息error：" + e.stack,
                clientIp: ""
            });
            result.errCode = -200;
            result.code = -200;
            result.message = "操作异常"
            return result;
        }
    }
}

module.exports = UtilESignBaoService;
