const Client = require('aliyun-api-gateway').Client;
// const client = new Client('203727307', 'b6za34qem6k9a3s3jguvh24nc3ridlnh');
const client = new Client('203732072', '3r0dn583041bco48lq0xgsw09r0hh6ew');
class aliyunClient {
  constructor() {
    this.aliReqUrl = "https://aliapi.gongsibao.com/tm/springboard";
  }
  async post(actionType, actionBody) {
    var reqParam = {};
    reqParam["ActionProcess"] = "AliTm";
    reqParam["ActionType"] = actionType;
    reqParam["ActionBody"] = actionBody;
    if (!actionBody.reqOnlyCode) {
      actionBody["reqOnlyCode"] = await this.getCreateOrderNum();
    }
    var param = {
      data: reqParam,
      timeout: 20000,
      headers: {
        accept: 'application/json'
      }
    };
    console.log(JSON.stringify(param), "______________峰擎---阿里云参数_______");
    var result = await client.post(this.aliReqUrl, param);
    console.log(JSON.stringify(result), "______________峰擎---阿里云返回结果_______");
    return result;
  }

  async getCreateOrderNum() {
    var createOrderNum = await this.getUidStr(5, 36);//====================订单号生成
    return createOrderNum + "" + Date.now();
  }
  async getUidStr(len, radix) {
    var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
    var uuid = [], i;
    radix = radix || chars.length;
    if (len) {
      for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
      var r;
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
      uuid[14] = '4';
      for (i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | Math.random() * 16;
          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
        }
      }
    }
    return uuid.join('');
  }

}
module.exports = aliyunClient;
