Ken 3 дней назад
Родитель
Сommit
3aad5b08dc
2 измененных файлов с 15 добавлено и 13 удалено
  1. 8 4
      app/Http/Controllers/api/ActivityReward.php
  2. 7 9
      app/Services/SmsService.php

+ 8 - 4
app/Http/Controllers/api/ActivityReward.php

@@ -31,10 +31,14 @@ class ActivityReward extends BaseController
                 'ip' => request()->ip(),
                 'action' => request()->action,
             ];
-//            $key = 'api_request_' . md5(json_encode($keys));
-//            if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
-//            Cache::put($key, true, 60);
-          SmsService::sendPhoneCode($params['phone']);
+            $key = 'api_request_' . md5(json_encode($keys));
+            if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
+            Cache::put($key, true, 60);
+            $code = SmsService::sendPhoneCode($params['phone']);
+
+
+
+
         } catch (ValidationException $e) {
 
             return $this->error($e->validator->errors()->first());

+ 7 - 9
app/Services/SmsService.php

@@ -19,26 +19,24 @@ class SmsService
 
     /**
      * @param $phone
+     * @return int
      * @throws Exception
      */
-    public static function sendPhoneCode($phone)
+    public static function sendPhoneCode($phone): int
     {
-
         $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);
+        $res = file_get_contents($url);
+        if (!empty(static::$errors[$res])) {
+            throw new Exception(static::$errors[$res], HttpStatus::CUSTOM_ERROR);
         }
-        if ($code != 0) throw new Exception("发送失败:$code", HttpStatus::CUSTOM_ERROR);
+        if ($res != 0) throw new Exception("发送失败", HttpStatus::CUSTOM_ERROR);
+        return $code;
     }
 }