const system = require("../system");
const settings = require("../../config/settings");
const DocBase = require("./doc.base");
const uuidv4 = require('uuid/v4');
const md5 = require("MD5");
class APIBase extends DocBase {
    constructor() {
        super();
        this.cacheManager = system.getObject("db.common.cacheManager");
        this.logCtl = system.getObject("web.common.oplogCtl");
        this.oplogSve = system.getObject("service.common.oplogSve");
    }
    getUUID() {
        var uuid = uuidv4();
        var u = uuid.replace(/\-/g, "");
        return u;
    }
    /**
     * 验证签名
     * @param {*} params 要验证的参数 
     * @param {*} app_key 应用的校验key
     */
    async verifySign(params, app_key) {
        if (!params) {
            return system.getResult(null, "请求参数为空");
        }
        if (!params.sign) {
            return system.getResult(null, "请求参数sign为空");
        }
        if (!params.times_tamp) {
            return system.getResult(null, "请求参数times_tamp为空");
        }
        var signArr = [];
        var keys = Object.keys(params).sort();
        if (keys.length == 0) {
            return system.getResult(null, "请求参数信息为空");
        }
        for (let k = 0; k < keys.length; k++) {
            const tKey = keys[k];
            if (tKey != "sign" && params[tKey]) {
                signArr.push(tKey + "=" + params[tKey]);
            }
        }
        if (signArr.length == 0) {
            return system.getResult(null, "请求参数组装签名参数信息为空");
        }
        var resultSignStr = signArr.join("&") + "&key=" + app_key;
        var resultTmpSign = md5(resultSignStr).toUpperCase();
        if (params.sign != resultTmpSign) {
            return system.getResult(null, "签名验证失败");
        }
        return system.getResultSuccess();
    }
    /**
 * 白名单验证
 * @param {*} gname 组名
 * @param {*} methodname 方法名
 */
    async isCheckWhiteList(gname, methodname) {
        var fullname = gname + "." + methodname;
        var lst = [
            "test.testApi"
        ];
        var x = lst.indexOf(fullname);
        return x >= 0;
    }
    async checkAcck(gname, methodname, pobj, query, req) {
        return system.getResultSuccess();
    }
    async doexec(gname, methodname, pobj, query, req) {
        var requestid = this.getUUID();
        try {
            var rtn = await this[methodname](pobj, query, req) || {};
            rtn.requestid = requestid;

            this.oplogSve.createDb({
                appid: req.headers["app_id"] || "",
                appkey: "",
                requestId: requestid,
                op: req.classname + "/" + methodname,
                content: JSON.stringify(pobj),
                resultInfo: JSON.stringify(rtn),
                clientIp: req.clientIp,
                agent: req.uagent,
                opTitle: "api服务提供方appKey:" + settings.appKey,
            });
            return rtn;
        } catch (e) {
            console.log(e.stack, "api调用出现异常，请联系管理员..........")
            this.logCtl.error({
                appid: "" + pobj.action_process,
                appkey: "",
                requestId: requestid,
                op: pobj.classname + "/" + methodname,
                content: e.stack,
                clientIp: pobj.clientIp,
                agent: req.uagent,
                optitle: "api调用出现异常，请联系管理员",
            });
            var rtnerror = system.getResultFail(-200, "出现异常，请联系管理员");
            rtnerror.requestid = requestid;
            return rtnerror;
        }
    }
}
module.exports = APIBase;

