审查视图

addons/qcloudsms/Qcloudsms.php 6.6 KB
SHW\戥岁。。 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
<?php

namespace addons\qcloudsms;

use addons\qcloudsms\library\SmsSingleSender;
use addons\qcloudsms\library\SmsVoicePromptSender;
use addons\qcloudsms\library\SmsVoiceverifyCodeSender;
use addons\qcloudsms\library\TtsVoiceSender;
use think\Addons;
use think\Config;

/**
 * 插件
 */
class Qcloudsms extends Addons
{
    private $appid = null;
    private $appkey = null;
    private $config = null;
    private $sender = null;
    private $sendError = '';

    public function ConfigInit()
    {
        $this->config = $this->getConfig();
        //如果使用语音短信  更换成语音短信模板
        if ($this->config['isVoice'] == 1) {
            $this->config['template'] = $this->config['voiceTemplate'];
            //语音短信 需要另行设置Aappid 与Appkey
            $this->appid = $this->config['voiceAppid'];
            $this->appkey = $this->config['voiceAppkey'];
        } else {
            $this->appid = $this->config['appid'];
            $this->appkey = $this->config['appkey'];
        }
    }

    /**
     * 短信发送行为
     * @param Sms $params
     * @return  boolean
     */
    public function smsSend(&$params)
    {
        $this->ConfigInit();
        try {
            if ($this->config['isTemplateSender'] == 1) {
                $templateID = $this->config['template'][$params->event];
                if ($this->config['isVoice'] != 1) {
                    //普通短信发送
                    $this->sender = new SmsSingleSender($this->appid, $this->appkey);
                    $result = $this->sender->sendWithParam("86", $params['mobile'], $templateID, ["{$params->code}"], $this->config['sign'], "", "");
                } else {
                    //语音短信发送
                    $this->sender = new TtsVoiceSender($this->appid, $this->appkey);
                    //参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
                    $result = $this->sender->send("86", $params['mobile'], $templateID, [$params->code]);
                }
            } else {
                //判断是否是语音短信
                if ($this->config['isVoice'] != 1) {
                    $this->sender = new SmsSingleSender($this->appid, $this->appkey);
                    //参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
                    $result = $this->sender->send($params['type'], '86', $params['mobile'], $params['msg'], "", "");
                } else {
                    $this->sender = new SmsVoiceVerifyCodeSender($this->appid, $this->appkey);
                    //参数:国家码、手机号、短信内容、播放次数(默认2次)、服务的原样返回的参数
                    $result = $this->sender->send('86', $params['mobile'], $params['msg']);
                }
            }

            $rsp = json_decode($result, true);
            if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
                return true;
            } else {
                //记录错误信息
                $this->setError($rsp);
                return false;
            }
        } catch (\Exception $e) {
            $this->setError($e->getMessage());
        }
        return false;
    }

    /**
     * 短信发送通知
     * @param array $params
     * @return  boolean
     */
    public function smsNotice(&$params)
    {
        $this->ConfigInit();
        try {
            if ($this->config['isTemplateSender'] == 1) {
                $templateID = $this->config['template'][$params['template']];

                if ($this->config['isVoice'] != 1) {
                    //普通短信发送
                    $this->sender = new SmsSingleSender($this->appid, $this->appkey);
                    $result = $this->sender->sendWithParam("86", $params['mobile'], $templateID, ["{$params['msg']}"], $this->config['sign'], "", "");
                } else {
                    //语音短信发送
                    $this->sender = new TtsVoiceSender($this->appid, $this->appkey);
                    //参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
                    $result = $this->sender->send("86", $params['mobile'], $templateID, [$params['msg']]);
                }
            } else {
                //判断是否是语音短信
                if ($this->config['isVoice'] != 1) {
                    $this->sender = new SmsSingleSender($this->appid, $this->appkey);
                    //参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
                    $result = $this->sender->send($params['type'], '86', $params['mobile'], $params['msg'], "", "");
                } else {
                    $this->sender = new SmsVoicePromptSender($this->appid, $this->appkey);
                    //参数:国家码、手机号、语音类型(目前固定为2)、短信内容、播放次数(默认2次)、服务的原样返回的参数
                    $result = $this->sender->send('86', $params['mobile'], 2, $params['msg']);
                }
            }
            $rsp = (array)json_decode($result, true);
            if ($rsp['result'] == 0 && $rsp['errmsg'] == 'OK') {
                return true;
            } else {
                //记录错误信息
                $this->setError($rsp);
                return false;
            }
        } catch (\Exception $e) {
            var_dump($e);
            exit();
        }
    }

    /**
     * 记录失败信息
     * @param [type] $err [description]
     */
    private function setError($err)
    {
        $this->sendError = $err;
    }

    /**
     * 获取失败信息
     * @return [type] [description]
     */
    public function getError()
    {
        return $this->sendError;
    }

    /**
     * 检测验证是否正确
     * @param Sms $params
     * @return  boolean
     */
    public function smsCheck(&$params)
    {
        return true;
    }

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        return true;
    }
}