SmsService.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // $result = $this->easySms->send($mobile, [
  24. // 'template' => $templateId,
  25. // 'data' => [
  26. // $code,
  27. // 5 // 验证码有效期(分钟)
  28. // ],
  29. // ]);
  30. $result = true;
  31. Log::info('SMS sent', [
  32. 'mobile' => $mobile,
  33. 'code' => $code,
  34. 'result' => $result
  35. ]);
  36. return true;
  37. } catch (\Exception $e) {
  38. Log::error('SMS sending failed', [
  39. 'mobile' => $mobile,
  40. 'error' => $e->getMessage()
  41. ]);
  42. throw $e;
  43. }
  44. }
  45. }