Browse Source

feat:实现发送短信验证码

Yin Bin 4 months ago
parent
commit
53d75597c4
5 changed files with 149 additions and 55 deletions
  1. 11 2
      app/Services/Client/AccountService.php
  2. 52 0
      app/Services/SmsService.php
  3. 1 0
      composer.json
  4. 55 53
      composer.lock
  5. 30 0
      config/sms.php

+ 11 - 2
app/Services/Client/AccountService.php

@@ -6,9 +6,17 @@ use App\Models\MemberSocialAccount;
 use App\Models\MemberUser;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Cache;
+use App\Services\SmsService;
 
 class AccountService
 {
+    protected $smsService;
+
+    public function __construct(SmsService $smsService)
+    {
+        $this->smsService = $smsService;
+    }
+
     /**
      * 发送验证码
      */
@@ -20,9 +28,10 @@ class AccountService
         // 保存验证码到缓存
         Cache::put("verify_code:{$mobile}", $code, 300);
 
-        // TODO: 调用短信服务发送验证码
+        // 发送验证码短信
+        $this->smsService->sendVerifyCode($mobile, $code);
 
-        return ['code' => $code];
+        return ['message' => '验证码发送成功'];
     }
 
     /**

+ 52 - 0
app/Services/SmsService.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Services;
+
+use Overtrue\EasySms\EasySms;
+use Illuminate\Support\Facades\Log;
+
+class SmsService
+{
+    protected $easySms;
+
+    public function __construct()
+    {
+        $this->easySms = new EasySms(config('sms'));
+    }
+
+    /**
+     * 发送验证码短信
+     *
+     * @param string $mobile 手机号
+     * @param string $code 验证码
+     * @return bool
+     */
+    public function sendVerifyCode(string $mobile, string $code): bool
+    {
+        try {
+            $templateId = env('TENCENT_SMS_TEMPLATE_ID');
+            $result = $this->easySms->send($mobile, [
+                'template' => $templateId,
+                'data' => [
+                    $code,
+                    5 // 验证码有效期(分钟)
+                ],
+            ]);
+
+            Log::info('SMS sent', [
+                'mobile' => $mobile,
+                'code' => $code,
+                'result' => $result
+            ]);
+
+            return true;
+        } catch (\Exception $e) {
+            Log::error('SMS sending failed', [
+                'mobile' => $mobile,
+                'error' => $e->getMessage()
+            ]);
+
+            throw $e;
+        }
+    }
+}

+ 1 - 0
composer.json

@@ -12,6 +12,7 @@
         "laravel/sanctum": "^4.0",
         "laravel/tinker": "^2.9",
         "monolog/monolog": "^3.5",
+        "overtrue/easy-sms": "^3.0",
         "rap2hpoutre/laravel-log-viewer": "^2.4",
         "simplesoftwareio/simple-qrcode": "^4.2",
         "slowlyo/owl-admin": "^4.0",

+ 55 - 53
composer.lock

@@ -4,60 +4,8 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "331b49e4d88dd88bdaefcaaff07223f1",
+    "content-hash": "52d29817693a9ea633e69dcdb6cfae7b",
     "packages": [
-        {
-            "name": "aoxiang/province-city-area",
-            "version": "1.2.8",
-            "source": {
-                "type": "git",
-                "url": "https://github.com/aoxiang594/laravel-province-city-area.git",
-                "reference": "29e0303e01876dc575e872a547df97a0bf513935"
-            },
-            "dist": {
-                "type": "zip",
-                "url": "https://api.github.com/repos/aoxiang594/laravel-province-city-area/zipball/29e0303e01876dc575e872a547df97a0bf513935",
-                "reference": "29e0303e01876dc575e872a547df97a0bf513935",
-                "shasum": ""
-            },
-            "require": {
-                "guzzlehttp/guzzle": "~7.0",
-                "guzzlehttp/psr7": "~2.0",
-                "php": "^7.0 || ^8.0"
-            },
-            "type": "library",
-            "extra": {
-                "laravel": {
-                    "providers": [
-                        "Aoxiang\\Pca\\ProvinceCityAreaServiceProvider"
-                    ],
-                    "alias": {
-                        "ProvinceCityArea": "Aoxiang\\Pca\\Facades\\ProvinceCityAreaFacades"
-                    }
-                }
-            },
-            "autoload": {
-                "psr-4": {
-                    "Aoxiang\\Pca\\": "src/"
-                }
-            },
-            "notification-url": "https://packagist.org/downloads/",
-            "license": [
-                "MIT"
-            ],
-            "authors": [
-                {
-                    "name": "Aoxiang",
-                    "email": "mail@aoxiang.me"
-                }
-            ],
-            "description": "China Province City Area From JD.COM",
-            "support": {
-                "issues": "https://github.com/aoxiang594/laravel-province-city-area/issues",
-                "source": "https://github.com/aoxiang594/laravel-province-city-area/tree/1.2.8"
-            },
-            "time": "2023-05-16T07:59:56+00:00"
-        },
         {
             "name": "bacon/bacon-qr-code",
             "version": "2.0.8",
@@ -3052,6 +3000,60 @@
             ],
             "time": "2024-10-15T16:15:16+00:00"
         },
+        {
+            "name": "overtrue/easy-sms",
+            "version": "3.0.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/overtrue/easy-sms.git",
+                "reference": "b97d8b785ae07fe8c181de8d9d1f7f146a5977f7"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/overtrue/easy-sms/zipball/b97d8b785ae07fe8c181de8d9d1f7f146a5977f7",
+                "reference": "b97d8b785ae07fe8c181de8d9d1f7f146a5977f7",
+                "shasum": ""
+            },
+            "require": {
+                "ext-json": "*",
+                "guzzlehttp/guzzle": "^6.2 || ^7.0",
+                "php": ">=8.0"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "^3.54",
+                "jetbrains/phpstorm-attributes": "^1.0",
+                "mockery/mockery": "^1.4.2",
+                "phpunit/phpunit": "^9.5.8"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Overtrue\\EasySms\\": "src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "overtrue",
+                    "email": "i@overtrue.me"
+                }
+            ],
+            "description": "The easiest way to send short message.",
+            "support": {
+                "issues": "https://github.com/overtrue/easy-sms/issues",
+                "source": "https://github.com/overtrue/easy-sms/tree/3.0.1"
+            },
+            "funding": [
+                {
+                    "url": "https://github.com/overtrue",
+                    "type": "github"
+                }
+            ],
+            "time": "2024-07-16T06:07:02+00:00"
+        },
         {
             "name": "phpoption/phpoption",
             "version": "1.9.3",

+ 30 - 0
config/sms.php

@@ -0,0 +1,30 @@
+<?php
+
+return [
+    // HTTP 请求的超时时间(秒)
+    'timeout' => 5.0,
+
+    // 默认发送配置
+    'default' => [
+        // 网关调用策略,默认:顺序调用
+        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
+
+        // 默认可用的发送网关
+        'gateways' => [
+            'tencent',
+        ],
+    ],
+
+    // 可用的网关配置
+    'gateways' => [
+        'errorlog' => [
+            'file' => storage_path('logs/easy-sms.log'),
+        ],
+        'tencent' => [
+            'sdk_app_id' => env('TENCENT_SMS_SDK_APP_ID'),  // SDK APP ID
+            'secret_id' => env('TENCENT_SMS_SECRET_ID'),     // SECRET ID
+            'secret_key' => env('TENCENT_SMS_SECRET_KEY'),   // SECRET KEY
+            'sign_name' => env('TENCENT_SMS_SIGN_NAME'),     // 短信签名
+        ],
+    ],
+];