123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop开源商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
- // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | likeshop团队版权所有并拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshop.cn.team
- // +----------------------------------------------------------------------
-
- namespace app\common\server\sms\engine;
-
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
-
-
- /**
- * 阿里短信
- * Class AliSms
- */
- class AliSms extends Server
- {
-
- protected $config;
- protected $mobile; //下发手机号
- protected $template_code; //验证码
- protected $template_param; //模板参数
-
- public function __construct($config = [])
- {
- if (empty($config)) {
- $this->error = '请联系管理员配置参数';
- return false;
- }
- $this->config = $config;
- }
-
- /**
- * Notes: 设置手机号
- * @param string $mobile
- * @author 段誉(2021/5/18 17:39)
- * @return $this
- */
- public function setMobile($mobile = '')
- {
- $this->mobile = $mobile;
- return $this;
- }
-
-
- /**
- * Notes: 设置模板id
- * @param string $template_code
- * @author 段誉(2021/5/18 17:39)
- * @return $this
- */
- public function setTemplateId($template_code = '')
- {
- $this->template_code = $template_code;
- return $this;
- }
-
-
- /**
- * Notes: 设置模板参数
- * @param string $template_param
- * @author 段誉(2021/5/18 17:39)
- * @return $this
- */
- public function setTemplateParam($template_param = '')
- {
- $this->template_param = json_encode($template_param);
- return $this;
- }
-
-
- /**
- * Notes: 发送短信
- * @author 段誉(2021/5/18 17:40)
- * @return array|bool
- * @throws ClientException
- */
- public function send()
- {
- AlibabaCloud::accessKeyClient($this->config['app_key'], $this->config['secret_key'])
- ->regionId('cn-hangzhou')
- ->asDefaultClient();
-
- try {
- $result = AlibabaCloud::rpcRequest()
- ->product('Dysmsapi')
- ->host('dysmsapi.aliyuncs.com')
- ->version('2017-05-25')
- ->action('SendSms')
- ->method('POST')
- ->options([
- 'query' => [
- 'PhoneNumbers' => $this->mobile, //发送手机号
- 'SignName' => $this->config['sign'], //短信签名
- 'TemplateCode' => $this->template_code, //短信模板CODE
- 'TemplateParam' => $this->template_param, //自定义随机数
- ],
- ])
- ->request();
-
- $res = $result->toArray();
- if (isset($res['Code']) && $res['Code'] == 'OK') {
- return $res;
- }
- $message = $res['Message'] ?? $res;
- throw new \Exception('短信错误:' . $message);
-
- } catch (ClientException $e) {
- $this->error = $e->getErrorMessage();
- return false;
- } catch (ServerException $e) {
- $this->error = $e->getErrorMessage();
- return false;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- }
- }
-
|