<?php 
namespace App\Libs;
use  App\Libs\AliyunSms\ZsySms;
use  App\Modules\SMS\Models\SmsTemplate;

/**
 * 短信发送类
 * 新模板：阿里云
 */
class AliSMS
{

    //          发送短信(sendSms)接口返回的结果:
    //          stdClass Object
    //          (
    //                          [Message] => OK
    //                          [RequestId] => CB4899F8-BC9B-4D8D-8F21-0F3D37A3B703
    //                          [BizId] => 487402333696072292^0
    //                          [Code] => OK
    //          )
    //          stdClass Object
    //          (
    //                          [Message] => 模板不合法(不存在或被拉黑)
    //                          [RequestId] => BEC91851-B07B-4533-9826-7C97DFA99B31
    //                          [Code] => isv.SMS_TEMPLATE_ILLEGAL
    //          )
    // 阿里的短信发送要有固定模板，所以自定义的内容不行
    static function send($number,$template_type,$param=array(),$sign='追书云')
    {
    	$sign = env('SMS_SIGN');
        \Log::info('AliSMS_send:number:'.$number.' template_type:'.$template_type.' param:'.json_encode($param));
        // 根据类型找模板id
        $sms_template = SmsTemplate::get_sms_template($template_type);
    
        if(empty($sms_template)){
                \Log::info('AliSMS_send_invalid_template:number:'.$number.' template_type:'.$template_type.' param:'.json_encode($param));
                return false;
        }
        $template_id = isset($sms_template->template_id)?$sms_template->template_id:'';
        \Log::info('AliSMS_send:number:'.$number.' template_type:'.$template_type.' template_id:'.$template_id.' param:'.json_encode($param));
         
        $response = ZsySms::sendSms($number,$template_id,$param,$sign);
        \Log::info('send_response:'.json_encode($response));
        $status = isset($response->Code)?$response->Code:'';

        $status = (($status=='OK')?1:0);
        return $status;
    }

}