Selaa lähdekoodia

7-4lib库下发送短信验证码类库封装

linzhijie 5 vuotta sitten
vanhempi
commit
d5f0325bac

+ 16 - 0
app/api/config/aliyun.php

@@ -0,0 +1,16 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User: 林志杰
+ * Email: [email protected]
+ * Time: 2020/1/11 17:47
+ */
+
+return [
+    'host' => 'dysmsapi.aliyuncs.com',
+    'access_key_id' => '******',
+    'access_key_secret' => '********',
+    'region_id' => 'cn-hangzhou',
+    'template_code' => 'SMS_180061495',
+    'sign_name' => 'singwa商城',
+];

+ 21 - 0
app/api/controller/Sms.php

@@ -0,0 +1,21 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User: 林志杰
+ * Email: [email protected]
+ * Time: 2020/1/11 17:54
+ */
+
+declare(strict_types=1);
+
+namespace app\api\controller;
+
+use app\BaseController;
+
+class Sms extends BaseController
+{
+    public function code(): object
+    {
+        return show(config('status.success'), 0);
+    }
+}

+ 11 - 0
app/api/route/api.php

@@ -0,0 +1,11 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User: 林志杰
+ * Email: [email protected]
+ * Time: 2020/1/11 17:53
+ */
+
+use think\facade\Route;
+
+Route::rule('smscode', 'sms/code', 'POST');

+ 19 - 0
app/common/business/Sms.php

@@ -0,0 +1,19 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User: 林志杰
+ * Email: [email protected]
+ * Time: 2020/1/11 17:56
+ */
+declare(strict_types=1);
+
+namespace app\common\business;
+
+
+class Sms
+{
+    public static function sendCode(): bool
+    {
+        return true;
+    }
+}

+ 67 - 0
app/common/lib/sms/AliSms.php

@@ -0,0 +1,67 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User: 林志杰
+ * Email: [email protected]
+ * Time: 2020/1/11 17:36
+ */
+
+declare(strict_types=1);
+
+namespace app\common\lib\sms;
+
+use AlibabaCloud\Client\AlibabaCloud;
+use AlibabaCloud\Client\Exception\ClientException;
+use AlibabaCloud\Client\Exception\ServerException;
+
+class AliSms
+{
+    /**
+     * 阿里云发送验证码的场景
+     * @param string $phone
+     * @param int $code
+     * @return bool
+     * @throws ClientException
+     */
+    public static function sendCode(string $phone, int $code): bool
+    {
+        if (empty($phone) || empty($code)) {
+            return false;
+        }
+        AlibabaCloud::accessKeyClient(config('aliyun.access_key_id'), config('aliyun.access_key_secret'))
+            ->regionId(config('aliyun.region_id'))
+            ->asDefaultClient();
+
+        $templateParam = [
+            "code" => $code
+        ];
+        try {
+            $result = AlibabaCloud::rpc()
+                ->product('Dysmsapi')
+                // ->scheme('https') // https | http
+                ->version('2017-05-25')
+                ->action('SendSms')
+                ->method('POST')
+                ->host(config('aliyun.host'))
+                ->options([
+                    'query' => [
+                        'RegionId' => "cn-hangzhou",
+                        'PhoneNumber' => $phone,
+                        'SingName' => config('aliyun.sign_name'),
+                        'TemplateCode' => config('aliyun.template_code'),
+                        'TemplateParam' => json_encode($templateParam)
+                    ],
+                ])
+                ->request();
+            print_r($result->toArray());
+        } catch (ClientException $e) {
+            return false;
+//            echo $e->getErrorMessage() . PHP_EOL;
+        } catch (ServerException $e) {
+            return false;
+//            echo $e->getErrorMessage() . PHP_EOL;
+        }
+
+        return true;
+    }
+}