const Client = require('aliyun-api-gateway').Client;
var RPCClient = require('@alicloud/pop-core').RPCClient;
const system = require("../system");
class aliyunClient {
  constructor() {

    this.pushlogSve = system.getObject("service.common.pushlogSve");
  }
  getAliClient() {
    return new RPCClient({
      accessKeyId: "LTAI1pJs7KQizBe2",//对应的aliuid 是 30039057
      accessKeySecret: "MMNibebAPqR9AnX5YWHnSL2tHMSIoL",
      endpoint: "https://companyreg.aliyuncs.com",
      apiVersion: "2020-03-06"
    });
  }
  async post(aliReqUrl, key, secret, actionBody) {
    const client = new Client(key, secret);
    var param = {
      data: actionBody,
      timeout: 20000,
      headers: {
        accept: 'application/json'
      }
    };
    console.log(JSON.stringify(param), "______________峰擎---阿里云参数_______");
    var result = await client.post(aliReqUrl, param);
    console.log(JSON.stringify(result), "______________峰擎---阿里云返回结果_______");
    return result;
  }
  //阿里接口
  async reqbyget(obj, cbk) {
    var action = obj.action;
    var reqbody = obj.reqbody;
    try {
      var reqAliclient = this.getAliClient();// { ... this.aliclient };
      if (obj.apiVersion) {
        reqAliclient.apiVersion = obj.apiVersion;
      }
      var res = await reqAliclient.request(action, reqbody, {
        timeout: 3000, // default 3000 ms
        formatAction: true, // default true, format the action to Action
        formatParams: true, // default true, format the parameter name to first letter upper case
        method: 'GET', // set the http method, default is GET
        headers: {}, // set the http request headers
      });
      this.pushlogSve.createDb({
        op: "old-reqbyget",
        content: JSON.stringify(obj),//推送的参数信息
        resultInfo: JSON.stringify(res),
        returnType: '1',
        logLevel: '1',
        opTitle: "old-reqbyget推送阿里信息返回成功"
      });
      return system.getResultSuccess(res);
    } catch (e) {
      this.pushlogSve.createFailLogDb({
        appid: obj.appInfo ? obj.appInfo.uapp_id || "" : "",
        appkey: obj.appInfo ? obj.appInfo.uapp_key || "" : "",
        requestId: obj.requestId || "",
        content: JSON.stringify(obj),//推送的参数信息
        resultInfo: "error:" + JSON.stringify(e),
        clientIp: obj.clientIp || "",
        failType: 1,
        opTitle: "old-reqbyget推送阿里信息返回异常",
        pushNumber: obj.pushNumber || 1
      });
      return system.getResultFail(-200, "出现异常，error：" + e.stack);
    }

  }
  async reqCustomByGet(obj, cbk) {
    var action = obj.action;
    var reqbody = obj.reqbody;
    var rpcParam = obj.rpcParam;
    var reqAliclient = this.getAliClient();// { ...self.aliclient };
    if (rpcParam.accessKeyId) {
      reqAliclient.accessKeyId = rpcParam.accessKeyId;
    }
    if (rpcParam.accessKeySecret) {
      reqAliclient.accessKeySecret = rpcParam.accessKeySecret;
    }
    if (rpcParam.endpoint) {
      reqAliclient.endpoint = rpcParam.endpoint;
    }
    if (rpcParam.apiVersion) {
      reqAliclient.apiVersion = rpcParam.apiVersion;
    }
    var res = await reqAliclient.request(action, reqbody, {
      timeout: 3000, // default 3000 ms
      formatAction: true, // default true, format the action to Action
      formatParams: true, // default true, format the parameter name to first letter upper case
      method: 'GET', // set the http method, default is GET
      headers: {}, // set the http request headers
    });
    console.log(action, "action...........");
    console.log(reqbody, "reqbody...........");
    return system.getResultSuccess(res);
  }
  // async reqCustomByPost(obj, cbk) {
  //   var action = obj.action;
  //   var reqbody = obj.reqbody;
  //   var rpcParam = obj.rpcParam;
  //   var reqAliclient = { ... this.aliclient };
  //   if (rpcParam.accessKeyId) {
  //     reqAliclient.accessKeyId = rpcParam.accessKeyId;
  //   }
  //   if (rpcParam.accessKeySecret) {
  //     reqAliclient.accessKeySecret = rpcParam.accessKeySecret;
  //   }
  //   if (rpcParam.endpoint) {
  //     reqAliclient.endpoint = rpcParam.endpoint;
  //   }
  //   if (rpcParam.apiVersion) {
  //     reqAliclient.apiVersion = rpcParam.apiVersion;
  //   }
  //   var res = await reqAliclient.request(action, reqbody, {
  //     timeout: 3000, // default 3000 ms
  //     formatAction: true, // default true, format the action to Action
  //     formatParams: true, // default true, format the parameter name to first letter upper case
  //     method: 'POST', // set the http method, default is GET
  //     headers: {}, // set the http request headers
  //   });
  //   return system.getResultSuccess(res);
  // }
}
module.exports = aliyunClient;
