Index.php 4.4 KB

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