<?php
namespace servers;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;


/**
 * Class aliyunSms
 * @package servers
 */
class aliyunSms
{
    /**
     * 保存错误信息
     * @var string
     */
     public $error;

    /**
     * @var mixed|string
     */
     private $accessKeyId = '';

    /**
     * @var mixed|string
     */
     private $accessKeySecret = '';

    /**
     * 签名
     * @var mixed|string
     */
     private $signName = '';

    /**
     * 模版ID
     * @var mixed|string
     */
     private $templateCode = '';

    /**
     * aliyunSms constructor.
     * @param $type
     * @throws \Exception
     */
     public function __construct($type)
     {
         if (empty($type)) {
             throw new \Exception('参数错误');
        }

         $config = $this->getConfig($type);

         if(empty($config)){
             throw new \Exception('没有配置阿里云短信服务');
         }
        // 配置参数
        $this->accessKeyId = $config ['accessKeyId'];
        $this->accessKeySecret = $config ['accessKeySecret'];
        $this->signName = $config ['signName'];
        $this->templateCode = $config ['templateCode'];
    }

    /**
     * 单条短信下发
     * @param $mobile
     * @param $code
     * @return array|false
     * @throws ClientException
     */
    public function smsaliyun ($mobile, $code)
    {
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
        try {
            // ->scheme('https') // https | http
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $mobile,
                        'SignName' => $this->signName,
                        'TemplateCode' => $this->templateCode,
                        'TemplateParam' => '{"code":"' . $code . '"}'
                    ],
                ])
                ->request();
             return $result->toArray();
         } catch (ClientException $e) {
            $this->error = $e->getErrorMessage();
            //TODO $e->getErrorMessage();记录日志
             return false;
         } catch (ServerException $e) {
            $this->error = $e->getErrorMessage();
            //TODO $e->getErrorMessage();记录日志
             return false;
         }
     }

    /**
     * 根据类型获取对应的配置
     * @param $type
     * @return array
     */
     private function getConfig($type)
     {
         return isset(\mvc::$cfg['sms'][$type]) ? \mvc::$cfg['sms'][$type] : [];
     }

    /**
     * 当返回false 时，访问这个方法获取错误
     * @return mixed
     */
     public function getError()
     {
         return $this->error;
     }
 }
