diff --git a/app/Utils/API/SendCode.php b/app/Utils/API/SendCode.php index 67550d8..b7512d3 100644 --- a/app/Utils/API/SendCode.php +++ b/app/Utils/API/SendCode.php @@ -12,6 +12,29 @@ class SendCode { protected static $token = 'druid_ccefa62b-243a-4bdb-8460-89f3b36e79f8'; + //单例复用http客户端 + public static $client = null; + /** + * 获取 Guzzle 客户端 + * 用于复用 客户端常驻内存 + * + * @return object + */ + public static function get_client() + { + if (self::$client == null) { + self::$client = new Client([ + 'curl' => [ + CURLOPT_FRESH_CONNECT => false, + CURLOPT_FORBID_REUSE => false, + ], + 'headers' => [ + 'Connection' => 'keep-alive', + ], + ]); + } + return self::$client; + } /** * 发送验证码 * code 状态码 0 成功 1 token无效或已过期 2 端口不足 3 账号已存在 int @@ -19,7 +42,7 @@ class SendCode */ public static function send_code($phone) { - $client = new Client(); // 创建 Guzzle 客户端 + $client = self::get_client(); // 创建 Guzzle 客户端 // 请求数据 $data = [ "token" => self::$token, @@ -42,7 +65,7 @@ class SendCode */ public static function get_code($phone) { - $client = new Client(); // 创建 Guzzle 客户端 + $client = self::get_client(); // 请求数据 $data = [ @@ -66,12 +89,12 @@ class SendCode * code 0 成功 1token无效 2WS号不存在 * status 当code=0时返回,0表示在线 1表示离线 * 返回 0在线 1离线 2 token无效 3 ws号不存在 + * 对象注入解决tcp 复用问题 */ public static function get_ws_status($phone) { - $client = new Client(); // 创建 Guzzle 客户端 - + $client = self::get_client(); $url = "https://dx1.rocketgo.vip/ex-api/biz/api/wsStatus?wsNumber=" . $phone . "&token=".self::$token; // 发送 POST 请求 @@ -99,7 +122,7 @@ class SendCode public static function delWS($phone) { - $client = new Client(); // 创建 Guzzle 客户端 + $client = self::get_client(); // 请求数据 $data = [ diff --git a/app/controller/api/TaskController.php b/app/controller/api/TaskController.php index b33f718..4421d38 100644 --- a/app/controller/api/TaskController.php +++ b/app/controller/api/TaskController.php @@ -23,7 +23,7 @@ use App\dao\UserRewardDao; use App\Utils\API\Facebook; use Tinywan\Jwt\JwtToken; use GuzzleHttp\Client; - +use support\Log; use hg\apidoc\annotation as Apidoc; @@ -61,7 +61,13 @@ class TaskController public function send_code(Request $request) { $phone = $request->post('phone'); - $res = SendCode::send_code($phone); + try { + $res = SendCode::send_code($phone); + } catch (\Exception $e) { + Log::error("发送验证码失败 phone {$phone}: " . $e->getMessage()); + return ApiResponseApp::error([], '获取验证码失败'); + } + if ($res['code'] == 3) { return ApiResponseApp::error([], '您已登陆'); } @@ -107,7 +113,15 @@ class TaskController // 状态为 2,表示正在等待上号,检查上号状态 if ($GetLodeLog->status == 2) { - $ws_build_status = SendCode::get_ws_status($phone); + // 获取用户的在线状态 + try { + $ws_build_status = SendCode::get_ws_status($phone); + } catch (\Exception $e) { + Log::error("查询手机号上号状态失败 phone {$phone}: " . $e->getMessage()); + $ws_build_status=30; + // return ApiResponseApp::error([], '获取验证码失败'); + } + // $ws_build_status = SendCode::get_ws_status($phone); // 如果上号成功 if ($ws_build_status == 0) { diff --git a/process/Task.php b/process/Task.php index 689ac0e..c1858b5 100644 --- a/process/Task.php +++ b/process/Task.php @@ -7,6 +7,7 @@ use app\model\UserPhone; use app\dao\UserRewardDao; use Workerman\Crontab\Crontab; use app\model\GetLodeLog; +use support\Log; /** * 查询用户是否在线类 @@ -15,46 +16,21 @@ class Task { public function onWorkerStart() { - - // // 每秒钟执行一次 - // new Crontab('*/1 * * * * *', function(){ - // echo date('Y-m-d H:i:s')."\n"; - // }); - - // // 每5秒执行一次 - // new Crontab('*/5 * * * * *', function(){ - // echo date('Y-m-d H:i:s')."\n"; - // }); - - // // 每分钟执行一次 - // new Crontab('0 */1 * * * *', function(){ - // echo date('Y-m-d H:i:s')."\n"; - // }); - - // // 每5分钟执行一次 - // new Crontab('0 */5 * * * *', function(){ - // echo date('Y-m-d H:i:s')."\n"; - // }); - - // // 每分钟的第一秒执行 - // new Crontab('1 * * * * *', function(){ - // echo date('Y-m-d H:i:s')."\n"; - // }); - - // // 每天的7点50执行,注意这里省略了秒位 - // new Crontab('50 7 * * *', function(){ - // echo date('Y-m-d H:i:s')."\n"; - // }); // 每1秒执行一次 收取验证码 new Crontab('*/1 * * * * *', function () { $GetLodeLog = GetLodeLog::where('status', 0)->get(); foreach ($GetLodeLog as $key => $value) { - $res = SendCode::get_code($value->phone); - // var_dump($res); - if ($res['status'] == 1 && $res['scanCode'] != null) { - $value->status = 1; - $value->code = $res['scanCode']; - $value->save(); + try { + $res = SendCode::get_code($value->phone); + // var_dump($res); + if ($res['status'] == 1 && $res['scanCode'] != null) { + $value->status = 1; + $value->code = $res['scanCode']; + $value->save(); + } + } catch (\Exception $e) { + // 记录错误日志,避免任务中断 + Log::error("Error processing phone {$value->phone}: " . $e->getMessage()); } } }); diff --git a/process/Task2.php b/process/Task2.php index e0b8ca4..a092efd 100644 --- a/process/Task2.php +++ b/process/Task2.php @@ -8,7 +8,7 @@ use app\model\UserPhone; use app\dao\UserRewardDao; use support\Log; use Workerman\Crontab\Crontab; - +use GuzzleHttp\Client; /** * 查询用户是否在线类 */ @@ -20,7 +20,6 @@ class Task2 new Crontab('0 */1 * * * *', function () { $phones = UserPhone::where('created_at', '<', \Carbon\Carbon::now()->subMinutes(10))->where('status','!=',2)->get(); $start_time = time(); - foreach ($phones as $key => $value) { try {