Ver código fonte

协程走起来

reloadnike 2 anos atrás
pai
commit
592c0234cc
1 arquivos alterados com 100 adições e 23 exclusões
  1. 100 23
      App/HttpController/Index.php

+ 100 - 23
App/HttpController/Index.php

@@ -7,9 +7,54 @@ namespace App\HttpController;
 use EasySwoole\Http\AbstractInterface\Controller;
 use App\Com\JdSdk;
 
+// 或者使用如下:
+/*go(function() {
+
+    $index = new Index();
+
+    $index->skuState();//状态
+    $index->skuStock();//有无货
+    $index->skuPrice();//价格
+
+    // 用户可以在这里调用上述协程 API
+});
+
+go(function () {
+    $ret = [];
+
+    $wait = new \EasySwoole\Component\WaitGroup();
+
+    $wait->add();
+    // 启动第 1 个协程
+    go(function () use ($wait, &$ret) {
+        // 模拟耗时任务 1
+        \co::sleep(0.1);
+        $ret[] = time();
+        $wait->done();
+    });
+
+    $wait->add();
+    // 启动第 2 个协程
+    go(function () use ($wait, &$ret) {
+        // 模拟耗时任务 2
+        \co::sleep(2);
+        $ret[] = time();
+        $wait->done();
+    });
+
+    // 挂起当前协程,等待所有任务完成后恢复
+    $wait->wait();
+
+    // 这里 $ret 包含了 2 个任务执行结果
+    var_dump($ret);
+});*/
+
+
+
 class Index extends Controller
 {
     protected $JdSdk;
+    protected $response;
 
     public function index()
     {
@@ -20,6 +65,7 @@ class Index extends Controller
         $this->response()->write(file_get_contents($file));
     }
 
+
     /**
      * 3.5  商品上下架状态接口
      *
@@ -29,22 +75,34 @@ class Index extends Controller
      */
     public function skuState()
     {
-        // 获取 `POST` 或者 `GET` 提交的所有参数
-        $data = $this->request()->getRequestParam();
 
-        $skuArr = $data['skuArr'];
+        $wait = new \EasySwoole\Component\WaitGroup();
+
+        $wait->add();
+        // 启动第 1 个协程
+        go(function () use ($wait) {
 
-        $this->JdSdk = new JdSdk();
+            // 获取 `POST` 或者 `GET` 提交的所有参数
+            $data = $this->request()->getRequestParam();
 
-        $response = $this->JdSdk->api_product_sku_state($skuArr);
+            $skuArr = $data['skuArr'];
 
-        $this->response()->write($response . PHP_EOL);
+            $this->JdSdk = new JdSdk();
 
-        //$response = json_decode($response, true);
+            $this->response = $this->JdSdk->api_product_sku_state($skuArr);
+
+
+
+            //$response = json_decode($response, true);
 //        =====$response=====
 //        { "success": true, "resultMessage": "操作成功", "resultCode": "0000", "result": [ { "state": 0, "sku": 5729524 } ], "code": 200 }
 
-        //return $response;
+            //return $response;
+
+            $wait->done();
+        });
+
+        $this->response()->write($this->response . PHP_EOL);
 
     }
 
@@ -58,20 +116,28 @@ class Index extends Controller
      */
     public function skuStock()
     {
-        // 获取 `POST` 或者 `GET` 提交的所有参数
-        $data = $this->request()->getRequestParam();
+        $wait = new \EasySwoole\Component\WaitGroup();
 
-        $skuArr = $data['skuArr'];
+        $wait->add();
+        // 启动第 1 个协程
+        go(function () use ($wait) {
 
-        $area = $data['area'];
+            // 获取 `POST` 或者 `GET` 提交的所有参数
+            $data = $this->request()->getRequestParam();
 
-        $this->JdSdk = new JdSdk();
+            $skuNums = $data['skuNums'];
 
-        $response = $this->JdSdk->api_stock_get_new_stock_by_id($skuArr, $area);
+            $area = $data['area'];
 
-        $this->response()->write($response . PHP_EOL);
+            $this->JdSdk = new JdSdk();
 
-        //return $response;
+            $response = $this->JdSdk->api_stock_get_new_stock_by_id($skuNums, $area);
+
+            //return $response;
+            $wait->done();
+        });
+
+        $this->response()->write($this->response . PHP_EOL);
     }
 
     /**
@@ -82,18 +148,27 @@ class Index extends Controller
      */
     function skuPrice()
     {
-        // 获取 `POST` 或者 `GET` 提交的所有参数
-        $data = $this->request()->getRequestParam();
+        $wait = new \EasySwoole\Component\WaitGroup();
+
+        $wait->add();
+        // 启动第 1 个协程
+        go(function () use ($wait) {
 
-        $skuArr = $data['skuArr'];
+            // 获取 `POST` 或者 `GET` 提交的所有参数
+            $data = $this->request()->getRequestParam();
 
-        $this->JdSdk = new JdSdk();
+            $skuStr = $data['skuStr'];
 
-        $response = $this->JdSdk->api_price_get_price($skuArr);
+            $this->JdSdk = new JdSdk();
 
-        $this->response()->write($response . PHP_EOL);
+            $response = $this->JdSdk->api_price_get_price($skuStr);
+
+            //$this->response()->write($response . PHP_EOL);
 
         //return $response;
+            $wait->done();
+        });
+        $this->response()->write($this->response . PHP_EOL);
     }
 
 
@@ -111,4 +186,6 @@ class Index extends Controller
         }
         $this->response()->write(file_get_contents($file));
     }
-}
+
+
+}