const system = require("../system");
const Core = require('@alicloud/pop-core');
class SmsClient {
  constructor() {
    this.smsTeml = "http://123.57.156.109:4103/api/Send";
    this.restClient = system.getObject("util.restClient");
    this.aliclient = new Core({
      accessKeyId: 'LTAI4FtNp3wcqFzaADvo1WtZ',
      accessKeySecret: 'VBKn1Anx4UmMF0LKNz7PVaCFG1phcg',
      endpoint: 'https://dysmsapi.aliyuncs.com',
      apiVersion: '2017-05-25'
    });

  }

  async aliSendMsg (to, tmplcode, signName, jsonContent) {
    var params = {
      "RegionId": "default",
      "PhoneNumbers": to,
      "SignName": signName,
      "TemplateCode": tmplcode,
      "TemplateParam": jsonContent
    }
    var requestOption = {
      method: 'POST'
    };
    this.aliclient.request('SendSms', params, requestOption).then((result) => {
      console.log(JSON.stringify(result));
    }, (ex) => {
      console.log(ex);
    })
  }

  async sendMsg (to, content) {
    var txtObj = {
      "appId": 8,
      "mobilePhone": to,
      "content": content
    }
    return this.restClient.execPost(txtObj, this.smsTeml);
  }
  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 = SmsClient;

//  var sms=new SmsClient();
// sms.aliSendMsg("13381139519","SMS_173946419","iboss",JSON.stringify({code:"hello"}));
