rabin 6 дней назад
Родитель
Сommit
1180d95cf7
2 измененных файлов с 74 добавлено и 27 удалено
  1. 11 2
      src/bao/connect/lib/Func/Base.php
  2. 63 25
      src/dai/seller/app/Api/Test.php

+ 11 - 2
src/bao/connect/lib/Func/Base.php

@@ -75,7 +75,16 @@ class Base
         }
         $sign = '';
         $this->sign($body, $sign);
-        $response = Dever::curl($url, $body, $method, $json, $header)->result();
+        $timeout = $this->connect['set']['timeout'] ?? Dever::config('setting')['channel_timeout'] ?? 5;
+        $connectTimeout = $this->connect['set']['connect_timeout'] ?? Dever::config('setting')['channel_connect_timeout'] ?? min($timeout, 3);
+        $curl = Dever::curl($url, $body, $method, $json, $header);
+        if ($connectTimeout > 0) {
+            $curl->setConnectTimeout($connectTimeout);
+        }
+        if ($timeout > 0) {
+            $curl->setTimeOut($timeout);
+        }
+        $response = $curl->result();
         $test = Dever::input('test');
         if ($test && $test == 1) {
             $result = array
@@ -516,4 +525,4 @@ class Base
     {
         return preg_replace("/<!--[^\!\[]*?(?<!\/\/)-->/","",$data);
     }
-}
+}

+ 63 - 25
src/dai/seller/app/Api/Test.php

@@ -1,35 +1,73 @@
 <?php namespace Seller\Api;
 use Dever;
 use Dever\Helper\Str;
-class Test
-{
-    # 测试商户回调
-    public function callback()
-    {
-        $input = Dever::input();
-        Dever::log($input, 'callback');
-        return 'ok';
-    }
-
-    public function run()
-    {
-        $order = Dever::input('order');
-        $info = Dever::db('seller/order')->find($order);
-        Dever::load(\Seller\Lib\Order::class)->handle($info);
+class Test
+{
+    protected static $sellerCache = [];
+    protected static $skuCache = [];
+    protected static $goodsCache = [];
+
+    # 测试商户回调
+    public function callback()
+    {
+        $input = Dever::input();
+        Dever::log($input, 'callback');
+        return 'ok';
+    }
+
+    protected function seller($id)
+    {
+        if (!isset(self::$sellerCache[$id])) {
+            self::$sellerCache[$id] = Dever::db('seller/info')->find($id);
+        }
+        return self::$sellerCache[$id];
+    }
+
+    protected function skuByCode($code)
+    {
+        if (!isset(self::$skuCache[$code])) {
+            self::$skuCache[$code] = Dever::db('goods/info_sku')->find(array('code' => $code));
+        }
+        return self::$skuCache[$code];
+    }
+
+    protected function goods($id)
+    {
+        if (!isset(self::$goodsCache[$id])) {
+            self::$goodsCache[$id] = Dever::db('goods/info')->find($id);
+        }
+        return self::$goodsCache[$id];
+    }
+
+    public function run()
+    {
+        $order = Dever::input('order');
+        $info = Dever::db('seller/order')->find($order);
+        Dever::load(\Seller\Lib\Order::class)->handle($info);
     }
 
     # 压力测试
     public function test()
     {
-        $code = 'lt10';
-        $info = Dever::db('seller/info')->find(1);
-        $sku = Dever::db('goods/info_sku')->find(array('code' => $code));
-        $goods = Dever::db('goods/info')->find($sku['info_id']);
-        $account = '1' . Str::rand(10, 0);
-        $order = Str::order('T');
-        $num = 1;
-        return Dever::load(\Seller\Lib\Order::class)->add($info, $goods, $sku, $account, $order, $num);
-    }
+        $code = Dever::input('code', 'lt10');
+        $sellerId = Dever::input('seller_id', 1);
+        $info = $this->seller($sellerId);
+        if (!$info) {
+            Dever::error('商户不存在');
+        }
+        $sku = $this->skuByCode($code);
+        if (!$sku) {
+            Dever::error('规格不存在');
+        }
+        $goods = $this->goods($sku['info_id']);
+        if (!$goods) {
+            Dever::error('商品不存在');
+        }
+        $account = '1' . Str::rand(10, 0);
+        $order = Str::order('T');
+        $num = 1;
+        return Dever::load(\Seller\Lib\Order::class)->add($info, $goods, $sku, $account, $order, $num);
+    }
 
     # 将当前订单置为失败
     public function test_no()
@@ -45,4 +83,4 @@ class Test
             }
         };
     }
-}
+}