SmsService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use Exception;
  5. class SmsService
  6. {
  7. private static string $uri = "https://api.smsbao.com/sms";
  8. private static string $username = "even7788";
  9. private static string $password = "Lpy@19920122";
  10. private static string $content = "【二八科技】您的验证码是{code}。如非本人操作,请忽略本短信";
  11. private static array $errors = [
  12. '30' => '错误密码',
  13. '40' => '账号不存在',
  14. '41' => '余额不足',
  15. '43' => "IP地址限制",
  16. '50' => "内容含有敏感词",
  17. '51' => '手机号码不正确'
  18. ];
  19. /**
  20. * @param $phone
  21. * @return bool
  22. * @throws Exception
  23. */
  24. public static function sendPhoneCode($phone): bool
  25. {
  26. $code = mt_rand(100000, 999999);
  27. $password = md5(static::$password);
  28. $content = str_replace("{code}", $code, static::$content);
  29. $url = static::$uri . "?u=" . static::$username . "&p=" . $password;
  30. $url .= "&c=" . urlencode($content);
  31. $url .= "&m=" . $phone;
  32. $code = file_get_contents($url);
  33. if (!empty(static::$errors[$code])) {
  34. throw new Exception(static::$errors[$code], HttpStatus::CUSTOM_ERROR);
  35. }
  36. if ($code == 0) return true;
  37. throw new Exception("发送失败", HttpStatus::CUSTOM_ERROR);
  38. }
  39. }