SmsService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 array $errors = [
  9. '30' => '错误密码',
  10. '40' => '账号不存在',
  11. '41' => '余额不足',
  12. '43' => "IP地址限制",
  13. '50' => "内容含有敏感词",
  14. '51' => '手机号码不正确'
  15. ];
  16. /**
  17. * @param $phone
  18. * @throws Exception
  19. */
  20. public static function sendPhoneCode($phone)
  21. {
  22. $username = config('services.sms.username');
  23. $password = config('services.sms.password');
  24. $content = config("services.sms.content");
  25. $code = mt_rand(100000, 999999);
  26. $content = str_replace("{code}", $code, $content);
  27. $data = [
  28. 'username' => $username,
  29. 'password' => $password,
  30. 'phone' => $phone,
  31. 'content' => $content,
  32. ];
  33. $url = static::$uri . "?u=" . $username . "&p=" . md5($password);
  34. $url .= "&c=" . urlencode($content);
  35. $url .= "&m=" . $phone;
  36. $res = file_get_contents($url);
  37. $data['res'] = $res;
  38. return $data;
  39. // if (!empty(static::$errors[$res])) {
  40. // throw new Exception(static::$errors[$res], HttpStatus::CUSTOM_ERROR);
  41. // }
  42. // if ($res != 0) throw new Exception("发送失败", HttpStatus::CUSTOM_ERROR);
  43. // return $data;
  44. }
  45. }