<?php
/**
 * Created by NetBeans.
 * User : hj
 * Date : 2017.12.6
 * Time : 16:14
 * Intro: 
 */

namespace app\api\untils;

class RongLianYun {
    protected $accountSid;
    protected $accountToken;
    protected $appId;         //应用appid，第三方注册时分配
    protected $serverIP;
    protected $serverPort;
    protected $softVersion;
    
    public function __construct($data_arr) {
        $this->accountSid    = $data['accountSid'];
        $this->accountToken  = $data['accountToken'];
        $this->appId         = $data['appId'];
        $this->serverIP      = $data['serverIP'];
        $this->serverPort    = $data['serverPort'];
        $this->softVersion   = $data['softVersion'];
    }
    
    /**
     * 短信验证码
     * qin 2017-05-03
     * @param $to
     * @param $datas
     * @param string $tempId
     * @return SimpleXMLElement[]
     */
    function sendTemplateSMS($to, $datas, $tempId = '79972') {
        header('Access-Control-Allow-Origin:*');
        $this->layout = "";
        $this->autoRender = false;

        $accountSid = '8a48b55153eae51101540e763d3b3888';
        $accountToken = '26b220de299d4a56a6f54dd5792a30e4';
        $appId = '8a48b55153eae51101540e77d1603891';
        $serverIP = 'app.cloopen.com';
        $serverPort = '8883';
        $softVersion = '2013-12-26';


        // 初始化REST SDK
        //global $accountSid, $accountToken, $appId, $serverIP, $serverPort, $softVersion;
        $rest = new REST($serverIP, $serverPort, $softVersion);
        $rest->setAccount($accountSid, $accountToken);
        $rest->setAppId($appId);

        // 发送模板短信
        $this->log("Sending TemplateSMS to $to ");
        $result = $rest->sendTemplateSMS($to, $datas, $tempId);
        if ($result == NULL) {
            $this->log("result error!");
            //exit;
        }
        if ($result->statusCode != 0) {
            $this->log("error code :" . $result->statusCode . "");
            $this->log("error msg :" . $result->statusMsg . "");
            //TODO 添加错误处理逻辑
            return $result->statusCode;
        } else {
            $this->log("Sendind TemplateSMS success!");
            // 获取返回信息
            $smsmessage = $result->TemplateSMS;
            $this->log("dateCreated:" . $smsmessage->dateCreated . "");
            $this->log("smsMessageSid:" . $smsmessage->smsMessageSid . "");
            //TODO 添加成功处理逻辑
        }
    }

}
