Преглед изворни кода

feat: EK-744: 发送短信

kevinElken пре 10 месеци
родитељ
комит
7c0b4ba750

+ 40 - 0
common/components/SilverStreet.php

@@ -0,0 +1,40 @@
+<?php
+namespace common\components;
+
+use yii\base\Component;
+use yii\helpers\Json;
+use yii\httpclient\Client;
+
+class SilverStreet extends Component {
+    public $username;
+    public $password;
+    public $apiUrl;
+
+    public function sendSms($mobile, $content): bool
+    {
+        $client = new Client();
+        $params = [
+            'username' => $this->username,
+            'password' => $this->password,
+            'mobile' => $mobile,
+            'content' => $content,
+        ];
+
+        try {
+            $response = $client->post($this->apiUrl, $params)->send();
+            if ($response->isOk) {
+                $result = Json::decode($response->content);
+                if ($result['status'] === 'success') {
+                    return true;
+                } else {
+                    return false;
+                }
+            } else {
+                return false;
+            }
+        } catch (\Exception $e) {
+            // 处理异常
+            return false;
+        }
+    }
+}

+ 7 - 0
common/config/main.php

@@ -116,6 +116,13 @@ return [
                 ],
             ],
         ],
+        'silverStreet' => [
+            'class' => 'common\components\SilverStreet',
+            'username' => 'Elken',
+            'password' => 'MLgmmZ7s',
+            'sender' => 'Elken',
+            'apiUrl' => 'https://api.silverstreet.com/send.php',
+        ],
     ],
     'controllerMap' => [
         'swoole_server' => [

+ 11 - 16
common/helpers/Sms.php

@@ -1,25 +1,20 @@
 <?php
 namespace common\helpers;
 
-use common\components\EknotifierSmsDriver;
+use Yii;
 
 class Sms
 {
-    const welcomePack = 'welcomePack';
-
-    /**
-     * Send the SMS.
-     *
-     * @param string $mobile
-     * @param string $message
-     * @param string $userId
-     * @param string $countryId
-     * @param string $classify
-     * @return bool
-     * @throws \Exception
-     */
-    public static function sendSMS(string $mobile, string $message, string $userId, string $countryId, string $classify): bool
+    public static function sendSMS($mobile, $message)
     {
-        return (new EknotifierSmsDriver)->send($mobile, $message, $userId, $countryId, $classify);
+        $mobile = '8618511880790';
+        $message = 'Elken hello AE!';
+
+        $silverStreet = Yii::$app->silverStreet;
+        if ($silverStreet->sendSms($mobile, $message)) {
+            echo '短信发送成功';
+        } else {
+            echo '短信发送失败';
+        }
     }
 }

+ 1 - 1
console/controllers/ToolController.php

@@ -214,7 +214,7 @@ class ToolController extends BaseController
      */
     public function actionSendSms()
     {
-        Sms::sendSMS('18511880790', 'welcome Elken!', '123123', '620699181451059200', Sms::welcomePack);
+        Sms::sendSMS('18511880790', 'hello AE!');
     }
 
     public function actionUpdatePercent() {