From 72940d3d0d4f0c866022fe41fcc118b06516b749 Mon Sep 17 00:00:00 2001 From: jianghanbo Date: Tue, 10 Sep 2024 18:09:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/app/server/ThirdApiService.php | 48 +++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/service/app/server/ThirdApiService.php b/service/app/server/ThirdApiService.php index e83bfdb5..f40ed3d9 100644 --- a/service/app/server/ThirdApiService.php +++ b/service/app/server/ThirdApiService.php @@ -3,7 +3,9 @@ namespace app\server; use app\model\Orders; use app\model\ThirdMobileLogs; +use http\Client; use support\Log; +use support\Redis; /** * 阿里云市场相关接口 @@ -28,7 +30,7 @@ class ThirdApiService { $bodys = "mobile_number=" . $mobile; $url = $host . $path; - $aliData = $this->curlRequest($url, $method, $headers, $bodys); + $aliData = $this->curlMobileRequest($url, $method, $headers, $bodys); if (isset($aliData['code']) && $aliData['code'] == 200) { ThirdMobileLogs::query()->insert([ 'mobile' => $mobile, @@ -47,7 +49,7 @@ class ThirdApiService { * @param $bodys * @return mixed */ - private function curlRequest($url, $method, $headers, $bodys){ + private function curlMobileRequest($url, $method, $headers, $bodys){ $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); @@ -75,7 +77,7 @@ class ThirdApiService { */ public function weComNotice($os) { $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e138b55c-2886-4729-922e-9a8c753f75ee'; - $method = 'post'; + $method = 'POST'; $message = sprintf($this->notice, Orders::OSS[$os] ?? ''); $bodys = [ 'msgtype' => 'text', @@ -84,6 +86,44 @@ class ThirdApiService { ] ]; - return $this->curlRequest($url, $method, [], json_encode($bodys)); + if (Redis::setNx('notice-' . $os, 600, 1)) { + $this->curlRequest($url, $method, [], $bodys); + } + return true; + } + + /** + * @param $url + * @param $method + * @param $headers + * @param $bodys + * @return mixed + */ + private function curlRequest($url, $method, $headers, $bodys){ + $curl = curl_init(); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_FAILONERROR, false); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + //设定返回信息中是否包含响应信息头,启用时会将响应信息头作为数据流输出,true 表示输出信息头, false表示不输出信息头 + //如果想将响应结果json字符串转为json数组,建议将 CURLOPT_HEADER 设置成 false + curl_setopt($curl, CURLOPT_HEADER, true); + if (1 == strpos("$".$url, "https://")) + { + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + } + if (is_array($bodys)) { + $bodys = json_encode($bodys); + } + curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys); + curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge($headers, [ + 'Content-Type: application/json', + 'Content-Length: ' . strlen($bodys) + ])); + curl_close($curl); + list($header, $body) = explode("\r\n\r\n", curl_exec($curl), 2); + Log::info(sprintf('third req:%s, res:%s', $url, $body)); + return json_decode($body, true); } } \ No newline at end of file