<?php
namespace xh\unity;
use xh\private_\signaturehelper;
//短信验证码接口
//采用的阿里云短信平台
class sms{
    
    function __construct(){
        //载入阿里云官方的sdk
        require (PATH_PRIVATE .  'signaturehelper.php');
    }
    
    //发送验证码
    function send($phone,$code){
        $params = array ();
        $accessKeyId = cog::read('smsCog')['accessKeyId'];
        $accessKeySecret = cog::read('smsCog')['accessKeySecret'];
        $params["SignName"] = cog::read('smsCog')['SignName'];
        //$params["TemplateCode"] ="尊敬的客户您好，您的验证码是{$code}拿走不谢。切忌泄露验证码会影响资金安全。";
        $statusStr = array(
		"0" => "短信发送成功",
		"-1" => "参数不全",
		"-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
		"30" => "密码错误",
		"40" => "账号不存在",
		"41" => "余额不足",
		"42" => "帐户已过期",
		"43" => "IP地址限制",
		"50" => "内容含有敏感词"
	);	
		$smsapi = "https://api.smsbao.com/"; //短信网关
		$user = $accessKeyId; //短信平台帐号
		$pass = md5($accessKeySecret); //短信平台密码
		$content=$params["SignName"]."尊敬的客户您好，您的验证码是{$code}拿走不谢。切忌泄露验证码会影响资金安全。";//要发送的短信内容
		$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
		$result =file_get_contents($sendurl) ;     

    }
    
    //发送异常通知
    function sendError($phone,$name){
        $params = array ();
        // *** 需用户填写部分 ***
        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = cog::read('smsCog')['accessKeyId'];
        $accessKeySecret = cog::read('smsCog')['accessKeySecret'];
        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $phone;
        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = cog::read('smsCog')['SignName'];
        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = cog::read('smsCog')['TemplateErrorCode'];
        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array ("name" => $name,"time"=>date("Y/m/d H:i:s",time()));
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"]);
        }
        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new signaturehelper();
        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))
            );
        return $content;
    }
    
    
    //发送平台更新维护通知
    function sendDefend($phone,$time,$name,$restore_time,$content){
        $params = array ();
        // *** 需用户填写部分 ***
        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = cog::read('smsCog')['accessKeyId'];
        $accessKeySecret = cog::read('smsCog')['accessKeySecret'];
        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $phone;
        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = cog::read('smsCog')['SignName'];
        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = cog::read('smsCog')['TemplateDefend'];
        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array (
            "time" => $time,
            "name" => $name,
            "restore" => $restore_time,
            "content" => $content
            );
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"]);
        }
        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new signaturehelper();
        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))
            );
        return $content;
    }
    
}