Bladeren bron

旺店通 service

cqp 3 maanden geleden
bovenliggende
commit
093d58e88a
1 gewijzigde bestanden met toevoegingen van 109 en 0 verwijderingen
  1. 109 0
      app/Service/WDTService.php

+ 109 - 0
app/Service/WDTService.php

@@ -0,0 +1,109 @@
+<?
+
+namespace App\Service;
+
+use Illuminate\Support\Facades\Log;
+
+class WDTService extends Service
+{
+    private $appSecret = "";
+    private $apiParas = [];
+    private $config = [];
+
+    public function __construct($wdt = 0)
+    {
+        $config = config("wdt")[$wdt] ?? [];
+        if(empty($config["sid"])) throw new \Exception("旺店通sid不能为空");
+        if(empty($config["appKey"])) throw new \Exception("旺店通appKey不能为空");
+        if(empty($config["appSecret"])) throw new \Exception("旺店通appSecret不能为空");
+        $this->config = $config;
+        $this->appSecret = $config['appSecret'];
+        $this->apiParas = [
+            'sid' => $config['sid'],
+            'appkey' => $config['appKey'],
+            'timestamp' => time(),
+        ];
+    }
+
+    private function makeSign(&$apiParas, $appSecret){
+        $sign = md5($this->packData($apiParas) . $appSecret);
+        $apiParas['sign'] = $sign;
+    }
+
+    private function packData(&$req)
+    {
+        ksort($req);
+        $arr = array();
+        foreach($req as $key => $val)
+        {
+            if($key == 'sign') continue;
+            if(count($arr))
+                $arr[] = ';';
+            $arr[] = sprintf("%02d", iconv_strlen($key, 'UTF-8'));
+            $arr[] = '-';
+            $arr[] = $key;
+            $arr[] = ':';
+            $arr[] = sprintf("%04d", iconv_strlen($val, 'UTF-8'));
+            $arr[] = '-';
+            $arr[] = $val;
+        }
+        return implode('', $arr);
+    }
+
+    private function putMultiApiParam($params)
+    {
+        return array_merge($this->apiParas,$params);
+    }
+
+    private function organizationParam($data){
+        $params = $this->putMultiApiParam($data);
+        $this->makeSign($params, $this->appSecret);
+        $postData = http_build_query($params,'','&');
+
+        return $postData;
+    }
+
+    public function getOrderListAuto($data){
+        $timeStamp = time();
+        $data['start_time'] = date("Y-m-d H:i:s", $timeStamp - 60 * 60);
+        $data['end_time'] = date("Y-m-d H:i:s", $timeStamp);
+        $postData = $this->organizationParam($data);
+        list($status, $msg) = $this->post_wdt_helper($this->config['order_query'], $postData);
+        dd($status, $msg);
+    }
+
+
+    //发送请求
+    public function post_wdt_helper($url, $data, $timeout = 20){
+        $header = array("Content-Type: application/x-www-form-urlencoded");
+
+        Log::channel('apiLog')->info('旺店通POST', ["api" => $url , "param" => $data ,"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('旺店通POST结果', ["message" => $message]);
+            return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
+        }
+        curl_close($ch);
+
+        Log::channel('apiLog')->info('旺店通POST结果', ["message" => json_decode($r, true)]);
+
+        return [true, json_decode($r, true)];
+    }
+}