Index.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\HttpController;
  3. use EasySwoole\Http\AbstractInterface\Controller;
  4. use App\Com\JdSdk;
  5. // 或者使用如下:
  6. /*go(function() {
  7. $index = new Index();
  8. $index->skuState();//状态
  9. $index->skuStock();//有无货
  10. $index->skuPrice();//价格
  11. // 用户可以在这里调用上述协程 API
  12. });
  13. go(function () {
  14. $ret = [];
  15. $wait = new \EasySwoole\Component\WaitGroup();
  16. $wait->add();
  17. // 启动第 1 个协程
  18. go(function () use ($wait, &$ret) {
  19. // 模拟耗时任务 1
  20. \co::sleep(0.1);
  21. $ret[] = time();
  22. $wait->done();
  23. });
  24. $wait->add();
  25. // 启动第 2 个协程
  26. go(function () use ($wait, &$ret) {
  27. // 模拟耗时任务 2
  28. \co::sleep(2);
  29. $ret[] = time();
  30. $wait->done();
  31. });
  32. // 挂起当前协程,等待所有任务完成后恢复
  33. $wait->wait();
  34. // 这里 $ret 包含了 2 个任务执行结果
  35. var_dump($ret);
  36. });*/
  37. class Index extends Controller
  38. {
  39. protected $JdSdk;
  40. protected $responseState;
  41. protected $responseStock;
  42. protected $responsePrice;
  43. public function index()
  44. {
  45. $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/welcome.html';
  46. if(!is_file($file)){
  47. $file = EASYSWOOLE_ROOT.'/src/Resource/Http/welcome.html';
  48. }
  49. $this->response()->write(file_get_contents($file));
  50. }
  51. /**
  52. * 3.5 商品上下架状态接口
  53. *
  54. * @param $skuArr
  55. *
  56. * @return array|bool
  57. */
  58. public function skuState()
  59. {
  60. $wait = new \EasySwoole\Component\WaitGroup();
  61. $wait->add();
  62. // 启动第 1 个协程
  63. go(function () use ($wait) {
  64. // 获取 `POST` 或者 `GET` 提交的所有参数
  65. $data = $this->request()->getRequestParam();
  66. $skuArr = $data['skuArr'];
  67. $this->JdSdk = new JdSdk();
  68. $this->responseState = $this->JdSdk->api_product_sku_state($skuArr);
  69. //$response = json_decode($response, true);
  70. // =====$response=====
  71. // { "success": true, "resultMessage": "操作成功", "resultCode": "0000", "result": [ { "state": 0, "sku": 5729524 } ], "code": 200 }
  72. //return $response;
  73. $wait->done();
  74. });
  75. $this->response()->write($this->responseState . PHP_EOL);
  76. }
  77. /**
  78. * 6.2 批量获取库存接口(建议订单详情页、下单使用)
  79. *
  80. * @param $skuNums
  81. * @param $area
  82. * $skuNums商品和数量 此是个String 再重构吧 [{skuId: 569172,num:101}], $area格式:1_0_0 (分别代表1、2、3级地址)
  83. * @return mixed
  84. */
  85. public function skuStock()
  86. {
  87. $wait = new \EasySwoole\Component\WaitGroup();
  88. $wait->add();
  89. // 启动第 1 个协程
  90. go(function () use ($wait) {
  91. // 获取 `POST` 或者 `GET` 提交的所有参数
  92. $data = $this->request()->getRequestParam();
  93. $skuNums = $data['skuNums'];
  94. $area = $data['area'];
  95. $this->JdSdk = new JdSdk();
  96. $this->responseStock = $this->JdSdk->api_stock_get_new_stock_by_id($skuNums, $area);
  97. //return $response;
  98. $wait->done();
  99. });
  100. $this->response()->write($this->responseStock . PHP_EOL);
  101. }
  102. /**
  103. * TODO 待测 5.2 批量查询协议价价格
  104. * HTTPS请求方式:POST
  105. *
  106. * @return string
  107. */
  108. function skuPrice()
  109. {
  110. $wait = new \EasySwoole\Component\WaitGroup();
  111. $wait->add();
  112. // 启动第 1 个协程
  113. go(function () use ($wait) {
  114. // 获取 `POST` 或者 `GET` 提交的所有参数
  115. $data = $this->request()->getRequestParam();
  116. $skuStr = $data['skuStr'];
  117. $this->JdSdk = new JdSdk();
  118. $this->responsePrice = $this->JdSdk->api_price_get_price($skuStr);
  119. //$this->response()->write($response . PHP_EOL);
  120. //return $response;
  121. $wait->done();
  122. });
  123. $this->response()->write($this->responsePrice . PHP_EOL);
  124. }
  125. function test()
  126. {
  127. $this->response()->write('this is test');
  128. }
  129. protected function actionNotFound(?string $action)
  130. {
  131. $this->response()->withStatus(404);
  132. $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/404.html';
  133. if(!is_file($file)){
  134. $file = EASYSWOOLE_ROOT.'/src/Resource/Http/404.html';
  135. }
  136. $this->response()->write(file_get_contents($file));
  137. }
  138. }