123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\HttpController;
- use EasySwoole\Http\AbstractInterface\Controller;
- use Swoole\Coroutine\Channel;
- 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;
- public function index()
- {
- $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/welcome.html';
- if(!is_file($file)){
- $file = EASYSWOOLE_ROOT.'/src/Resource/Http/welcome.html';
- }
- $this->response()->write(file_get_contents($file));
- }
- /**
- * 3.5 商品上下架状态接口
- *
- * @param $skuArr
- *
- * @return array|bool
- */
- public function skuState()
- {
- $channel = new Channel(1);
- // 或者使用如下:
- go(function() use($channel){
- // 获取 `POST` 或者 `GET` 提交的所有参数
- $data = $this->request()->getRequestParam();
- $skuArr = $data['skuArr'];
- $this->JdSdk = new JdSdk();
- $response = $this->JdSdk->api_product_sku_state($skuArr);
- $channel->push($response);
- //$response = json_decode($response, true);
- // =====$response=====
- // { "success": true, "resultMessage": "操作成功", "resultCode": "0000", "result": [ { "state": 0, "sku": 5729524 } ], "code": 200 }
- //return $responseState;
- });
- $responseState = $channel->pop();
- $this->response()->write($responseState . PHP_EOL);
- }
- /**
- * 6.2 批量获取库存接口(建议订单详情页、下单使用)
- *
- * @param $skuNums
- * @param $area
- * $skuNums商品和数量 此是个String 再重构吧 [{skuId: 569172,num:101}], $area格式:1_0_0 (分别代表1、2、3级地址)
- * @return mixed
- */
- public function skuStock()
- {
- $channel = new Channel(1);
- go(function() use($channel){
- // 获取 `POST` 或者 `GET` 提交的所有参数
- $data = $this->request()->getRequestParam();
- $skuNums = $data['skuNums'];
- $area = $data['area'];
- $this->JdSdk = new JdSdk();
- $response = $this->JdSdk->api_stock_get_new_stock_by_id($skuNums, $area);
- $channel->push($response);
- //return $response;
- });
- $responseStock = $channel->pop();
- $this->response()->write($responseStock . PHP_EOL);
- }
- /**
- * TODO 待测 5.2 批量查询协议价价格
- * HTTPS请求方式:POST
- *
- * @return string
- */
- function skuPrice()
- {
- $channel = new Channel(1);
- go(function() use($channel){
- // 获取 `POST` 或者 `GET` 提交的所有参数
- $data = $this->request()->getRequestParam();
- $skuStr = $data['skuStr'];
- $this->JdSdk = new JdSdk();
- $response = $this->JdSdk->api_price_get_price($skuStr);
- //$this->response()->write($response . PHP_EOL);
- $channel->push($response);
- //return $response;
- });
- $responsePrice = $channel->pop();
- $this->response()->write($responsePrice . PHP_EOL);
- }
- function test()
- {
- $this->response()->write('this is test');
- }
- protected function actionNotFound(?string $action)
- {
- $this->response()->withStatus(404);
- $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/404.html';
- if(!is_file($file)){
- $file = EASYSWOOLE_ROOT.'/src/Resource/Http/404.html';
- }
- $this->response()->write(file_get_contents($file));
- }
- }
|