Przeglądaj źródła

add: 支持 aliyun 短信

joe 4 lat temu
rodzic
commit
529f839039

+ 25 - 2
config/sms.php

@@ -5,7 +5,7 @@
 
 return [
     //默认支付模式
-    'default' => 'sms',
+    'default' => 'aliyun',
     //单个手机每日发送上限
     'maxPhoneCount' => 10,
     //验证码每分钟发送上线
@@ -68,8 +68,31 @@ return [
         ],
         //阿里云
         'aliyun' => [
-            'template_id' => [
+            'sign' => '美天旺',
+            'access_key' => 'LTAI4G1K5gg45Z6A3CjL9dAP',
+            'access_secret' => 'WNJjzkXJ752AC2W8BmRLNqnm91ZbZ5',
 
+            'template_id' => [
+                //验证码
+                'VERIFICATION_CODE' => 'SMS_204755043',
+                //支付成功
+                'PAY_SUCCESS_CODE' => 520268,
+                //发货提醒
+                'DELIVER_GOODS_CODE' => 520269,
+                //确认收货提醒
+                'TAKE_DELIVERY_CODE' => 520271,
+                //管理员下单提醒
+                'ADMIN_PLACE_ORDER_CODE' => 520272,
+                //管理员退货提醒
+                'ADMIN_RETURN_GOODS_CODE' => 520274,
+                //管理员支付成功提醒
+                'ADMIN_PAY_SUCCESS_CODE' => 520273,
+                //管理员确认收货
+                'ADMIN_TAKE_DELIVERY_CODE' => 520422,
+                //改价提醒
+                'PRICE_REVISION_CODE' => 528288,
+                //订单未支付
+                'ORDER_PAY_FALSE' => 528116,
             ]
         ]
     ]

+ 1 - 1
crmeb/repositories/ShortLetterRepositories.php

@@ -34,7 +34,7 @@ class ShortLetterRepositories
                 'site_url' => sys_config('site_url')
             ]);
             $res = $sms->send($phone, $template, $data);
-            if ($res === false) {
+            if ($res === false || $res === null) {
                 $errorSmg = $sms->getError();
                 Log::info($logMsg ?? $errorSmg);
                 return $errorSmg;

+ 69 - 2
crmeb/services/sms/storage/Aliyun.php

@@ -1,19 +1,86 @@
 <?php
-
 namespace crmeb\services\sms\storage;
 
 use crmeb\basic\BaseSms;
+use think\facade\Config;
+use AlibabaCloud\Client\AlibabaCloud;
+use AlibabaCloud\Client\Exception\ClientException;
+use AlibabaCloud\Client\Exception\ServerException;
 
 class Aliyun extends BaseSms
 {
+    protected $server = 'dysmsapi.aliyuncs.com';
+    protected $accessKey;
+    protected $accessSecret;
+    protected $client;
+    protected $sign;
 
     protected function initialize(array $config)
     {
         parent::initialize($config); // TODO: Change the autogenerated stub
+        $this->accessKey = Config::get($this->configFile . '.stores.' . $this->name . '.access_key', '');
+        $this->accessSecret = Config::get($this->configFile . '.stores.' . $this->name . '.access_secret', '');
+        $this->sign = Config::get($this->configFile . '.stores.' . $this->name . '.sign', '');
+        $this->client = AlibabaCloud::accessKeyClient($this->accessKey, $this->accessSecret)
+            ->regionId('cn-hangzhou')
+            ->asDefaultClient();
+
     }
 
     public function send(string $phone, string $templateId, array $data = [])
     {
-        // TODO: Implement send() method.
+        if (!$this->accessKey || !$this->accessSecret || !$this->sign) {
+            return $this->setError('aliyun 短信参数未设置');
+        }
+        if (!$phone) {
+            return $this->setError('手机号为空');
+        }
+        $templateCode = $this->getTemplateCode($templateId);
+        if (!$templateCode) {
+            return $this->setError('找不到短信模板');
+        }
+        try {
+            $res = AlibabaCloud::rpc()
+                ->product('Dysmsapi')
+                ->version('2017-05-25')
+                ->action('SendSms')
+                ->method('POST')
+                ->host($this->server)
+                ->options([
+                    'query' => [
+                        'RegionId' => 'cn-hangzhou',
+                        'PhoneNumbers' => $phone,
+                        'SignName' => $this->sign,
+                        'TemplateCode' => $templateCode,
+                        'TemplateParam' => json_encode($data),
+                    ],
+                ])
+                ->request();
+            if (!$res) {
+                return $this->setError('返回空值');
+            }
+            $res = $res->toArray();
+            if (!isset($res['Code'])) {
+                return $this->setError('异常返回值');
+            }
+            if ($res['Code'] != 'OK') {
+                $err = isset($res['Message']) ? $res['Message'] : '';
+                return $this->setError($err);
+            }
+            /*
+             * res: ['Message':'OK','RequestId':'string36','BizId':'string20','Code':'OK']
+             */
+            return [
+                'data' => [
+                    'content' => $templateId,
+                    'template' => $templateId,
+                    'id' => intval(substr($res['BizId'],8)),
+                ],
+            ];
+        } catch (ClientException $e) {
+            return $this->setError($e->getErrorMessage());
+        } catch (ServerException $e) {
+            return $this->setError($e->getErrorMessage());
+        } // try...catch...
     }
 }