| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace App\Services;
- use App\Constants\HttpStatus;
- use Exception;
- class SmsService
- {
- private static string $uri = "https://api.smsbao.com/sms";
- private static array $errors = [
- '30' => '错误密码',
- '40' => '账号不存在',
- '41' => '余额不足',
- '43' => "IP地址限制",
- '50' => "内容含有敏感词",
- '51' => '手机号码不正确'
- ];
- /**
- * @param $phone
- * @return void
- * @throws Exception
- */
- public static function sendPhoneCode($phone): void
- {
- $username = config('services.sms.username');
- $password = config('services.sms.password');
- $content = config("services.sms.content");
- $code = mt_rand(100000, 999999);
- $content = str_replace("{code}", $code, $content);
- $url = static::$uri . "?u=" . $username . "&p=" . md5($password);
- $url .= "&c=" . urlencode($content);
- $url .= "&m=" . $phone;
- $code = file_get_contents($url);
- if (!empty(static::$errors[$code])) {
- throw new Exception(static::$errors[$code], HttpStatus::CUSTOM_ERROR);
- }
- if ($code != 0) throw new Exception("发送失败:$code", HttpStatus::CUSTOM_ERROR);
- }
- }
|