cqp пре 1 недеља
родитељ
комит
7a90546dbf
3 измењених фајлова са 211 додато и 0 уклоњено
  1. 38 0
      app/Http/Controllers/Api/TestController.php
  2. 169 0
      app/Service/qyWechatService.php
  3. 4 0
      routes/api.php

+ 38 - 0
app/Http/Controllers/Api/TestController.php

@@ -2,8 +2,10 @@
 
 namespace App\Http\Controllers\Api;
 
+use App\Service\qyWechatService;
 use App\Service\TestService;
 use Illuminate\Http\Request;
+use Illuminate\Support\Str;
 
 class TestController extends BaseController
 {
@@ -113,4 +115,40 @@ class TestController extends BaseController
     //	}
     //}
     // 计量单位 http://localhost:8060/api/Inventory/GetComputationUnit  ?CodeorName=0502  为空返回所有数据
+
+
+    public function getSignatures(Request $request)
+    {
+        $data = $request->all();
+        $url  = $data['url'];
+        if(empty($url)) return $this->json_return(201, 'URL不能为空');
+        $nonceStr = Str::random();
+        $timestamp = time();
+
+        // 1. 企业级签名 (wx.config)
+        $wechat = new qyWechatService();
+        list($status, $ticket) = $wechat->getJsApiTicket();
+        if(! $status) {
+            return $this->json_return(201, $ticket);
+        }
+        $configSignature = $wechat->makeSignature($ticket, $nonceStr, $timestamp, $url);
+
+        // 2. 应用级签名 (wx.agentConfig)
+        list($status, $agentTicket) = $wechat->getAgentTicket();
+        if(! $status) {
+            return $this->json_return(201, $agentTicket);
+        }
+        $agentSignature = $wechat->makeSignature($agentTicket, $nonceStr, $timestamp, $url);
+
+        $return = [
+            'corpid'    => $wechat->corpId,
+            'agentid'   => $wechat->agentId,
+            'timestamp' => $timestamp,
+            'nonceStr'  => $nonceStr,
+            'configSignature' => $configSignature,
+            'agentSignature'  => $agentSignature,
+        ];
+
+        return $this->json_return(200,'',$return);
+    }
 }

+ 169 - 0
app/Service/qyWechatService.php

@@ -0,0 +1,169 @@
+<?php
+
+namespace App\Service;
+
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
+
+class qyWechatService
+{
+    public $corpId = "ww0e2580a34523500b";
+    public $agentId = "1000046";
+    protected $secret = "8YFLG89PjXQ20CTF2DDq1Pwng8vvSHrC37C_6lgV6mY";
+
+    public function __construct()
+    {
+//        $this->corpId = config('services.wechat.corp_id');
+//        $this->agentId = config('services.wechat.agent_id');
+//        $this->secret = config('services.wechat.secret');
+    }
+
+    /**
+     * Step 1: 获取 AccessToken
+     */
+    public function getAccessToken()
+    {
+        // 1. 先从缓存拿
+        $wechat_access_token = "wechat_access_token" . $this->corpId . $this->agentId;
+        $cacheToken = Cache::get($wechat_access_token);
+        if ($cacheToken) return [true, $cacheToken];
+
+        // 2. 缓存没有则请求接口
+        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$this->corpId."&corpsecret=".$this->secret;
+        list($status, $res) = $this->get_helper($url);
+        if (!$status) return [false, $res];
+
+        if (isset($res['access_token'])) {
+            Cache::put($wechat_access_token, $res['access_token'], 7000);
+            return [true, $res['access_token']];
+        }
+
+        return [false, $res['errmsg'] ?? '获取AccessToken失败'];
+    }
+
+    /**
+     * 获取 企业级 Ticket (用于 wx.config)
+     */
+    public function getJsApiTicket()
+    {
+        $cacheTicketKey = "wechat_jsapi_ticket" . $this->corpId . $this->agentId;
+        $cacheTicket = Cache::get($cacheTicketKey);
+        if ($cacheTicket) return [true, $cacheTicket];
+
+        list($status, $token) = $this->getAccessToken();
+        if (!$status) return [false, $token];
+
+        $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" . $token;
+        list($status, $res) = $this->get_helper($url);
+
+        if (!$status) return [false, $res];
+
+        if (isset($res['ticket'])) {
+            Cache::put($cacheTicketKey, $res['ticket'], 7000);
+            return [true, $res['ticket']];
+        }
+
+        return [false, $res['errmsg'] ?? '获取JsApiTicket失败'];
+    }
+
+    /**
+     * 获取 应用级 Ticket (用于 wx.agentConfig)
+     */
+    public function getAgentTicket()
+    {
+        $cacheTicketKey = "wechat_agent_ticket" . $this->corpId . $this->agentId;
+        $cacheTicket = Cache::get($cacheTicketKey);
+        if ($cacheTicket) return [true, $cacheTicket];
+
+        list($status, $token) = $this->getAccessToken();
+        if (!$status) return [false, $token];
+
+        // 注意这里 type=agent_config
+        $url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token={$token}&type=agent_config";
+        list($status, $res) = $this->get_helper($url);
+
+        if (!$status) return [false, $res];
+
+        if (isset($res['ticket'])) {
+            Cache::put($cacheTicketKey, $res['ticket'], 7000);
+            return [true, $res['ticket']];
+        }
+
+        return [false, $res['errmsg'] ?? '获取AgentTicket失败'];
+    }
+
+    /**
+     * 生成签名通用方法
+     */
+    public function makeSignature($ticket, $nonceStr, $timestamp, $url)
+    {
+        $string = "jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";
+        return sha1($string);
+    }
+
+    public function post_helper($url, $data, $header = [], $timeout = 20, $title = ""){
+        Log::channel('apiLog')->info($title . 'POST', ["api" => $url , "param" => json_decode($data,true) ,"header" => $header]);
+
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_ENCODING, '');
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+
+        if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+
+            Log::channel('apiLog')->info($title . 'POST结果', ["message" => $message ]);
+            return [false, $message];
+        }
+        curl_close($ch);
+
+        Log::channel('apiLog')->info($title . 'POST结果', ["message" => json_decode($r, true) ]);
+        return [true, json_decode($r, true)];
+    }
+
+    public function get_helper($url,$header=[],$timeout = 20){
+        $ch = curl_init();
+        curl_setopt_array($ch, array(
+            CURLOPT_URL => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_ENCODING => '',
+            CURLOPT_MAXREDIRS => 10,
+            CURLOPT_TIMEOUT => $timeout,
+            CURLOPT_FOLLOWLOCATION => true,
+            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+            CURLOPT_CUSTOMREQUEST => 'GET',
+            CURLOPT_SSL_VERIFYPEER => false,
+            CURLOPT_HTTPHEADER => $header,
+        ));
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+            Log::channel('apiLog')->info('企业微信GET结果', ["message" => $message]);
+            return [false, $message];
+        }
+
+        curl_close($ch);
+        Log::channel('apiLog')->info('企业微信GET结果', ["message" => json_decode($r, true)]);
+
+        return [true, json_decode($r, true)];
+    }
+}

+ 4 - 0
routes/api.php

@@ -71,6 +71,10 @@ Route::group(['middleware'=> ['CheckU8']],function ($route){
     $route->any('productInAddU8', 'Api\TestController@productInAddU8')->middleware('U8Deal');
     $route->any('dispatchAddU8', 'Api\TestController@dispatchAddU8')->middleware('U8Deal');
 });
+
+//重庆普健
+Route::any('getSignatures', 'Api\TestController@getSignatures');
+
 Route::group(['middleware'=> ['CheckJRFIDLogin']],function ($route){
     //站点获取登录后
     $route->any('getSiteByLogin', 'Api\JRFIDController@getSite2');