SmsService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Services;
  3. use Overtrue\EasySms\EasySms;
  4. use Illuminate\Support\Facades\Log;
  5. class SmsService
  6. {
  7. protected $easySms;
  8. public function __construct()
  9. {
  10. $this->easySms = new EasySms(config('sms'));
  11. }
  12. /**
  13. * 发送验证码短信
  14. *
  15. * @param string $mobile 手机号
  16. * @param string $code 验证码
  17. * @return bool
  18. */
  19. public function sendVerifyCode(string $mobile, string $code): bool
  20. {
  21. try {
  22. $templateId = env('TENCENT_SMS_TEMPLATE_ID');
  23. // TODO: 发送短信方法,记得打开
  24. // $result = $this->easySms->send($mobile, [
  25. // 'template' => $templateId,
  26. // 'data' => [
  27. // $code,
  28. // 5 // 验证码有效期(分钟)
  29. // ],
  30. // ]);
  31. $result = true;
  32. Log::info('SMS sent', [
  33. 'mobile' => $mobile,
  34. 'code' => $code,
  35. 'result' => $result
  36. ]);
  37. return true;
  38. } catch (\Exception $e) {
  39. Log::error('SMS sending failed', [
  40. 'mobile' => $mobile,
  41. 'error' => $e->getMessage()
  42. ]);
  43. throw $e;
  44. }
  45. }
  46. }