From 94836aff1903510b069f6b712997f1c1c2d57e1d Mon Sep 17 00:00:00 2001 From: weigang Date: Mon, 3 Aug 2020 10:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=95=86=E6=88=B7=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8E=A7=E5=88=B6=E5=99=A8=E3=80=81=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Commons/Log.php | 88 ++++++++++++++++++++++++++ app/Controller/AbstractController.php | 8 +++ app/Controller/StoreController.php | 20 ++++++ app/Controller/TestController.php | 60 ++++++++++++++++++ app/Model/Store.php | 28 ++++++++ app/Request/StoreApplyEntryRequest.php | 42 ++++++++++++ composer.json | 2 +- config/autoload/dependencies.php | 5 +- config/routes.php | 1 + 9 files changed, 252 insertions(+), 2 deletions(-) create mode 100644 app/Commons/Log.php create mode 100644 app/Controller/StoreController.php create mode 100644 app/Controller/TestController.php create mode 100644 app/Model/Store.php create mode 100644 app/Request/StoreApplyEntryRequest.php diff --git a/app/Commons/Log.php b/app/Commons/Log.php new file mode 100644 index 0000000..bf5fb24 --- /dev/null +++ b/app/Commons/Log.php @@ -0,0 +1,88 @@ +clientFactory = $clientFactory; + } + + public function getClient() + { + // $options 等同于 GuzzleHttp\Client 构造函数的 $config 参数 + $options = [ + 'timeout' => 2.0, + ]; + // $client 为协程化的 GuzzleHttp\Client 对象 + $client = $this->clientFactory->create($options); + + return $client; + } + + public function event($labels=null,$datas){ + + co(function () use ($labels,$datas){ + + $client = $this->getClient(); + $kv = []; + foreach ($datas as $key => $value) { + $kv[] = $key."=".$value; + } + $pushLabels = []; + + $event_name = 'event_'.env('APP_ENV'); + if(!empty($labels)) $pushLabels[$event_name] = $labels; + + /* + * data format: + curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \ + '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}' + */ + $ts = $this->getMsecTime() . '000000'; + $datas = implode("&",$kv); + $values = [[$ts,$datas]]; + $app_name = env('APP_NAME').'_'.env('APP_ENV'); + + $pushLabels['app']= $app_name; + $pushDatas = [ + 'streams'=>[ + [ + 'stream'=>$pushLabels, + 'values'=>$values, + ] + ] + ]; + $client->post( + 'http://39.96.12.39:3100/loki/api/v1/push', + [ + 'headers'=>[ + 'Content-Type'=>'application/json' + ], + 'body' => json_encode($pushDatas) + ] + ); + //var_dump(json_encode($pushDatas) ); + }); + } + + + public function push($datas){ + $this->event(null,$datas); + } + + public function getMsecTime() + { + list($msec, $sec) = explode(' ', microtime()); + $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); + return $msectime; + } + +} diff --git a/app/Controller/AbstractController.php b/app/Controller/AbstractController.php index be3a537..b7429ea 100644 --- a/app/Controller/AbstractController.php +++ b/app/Controller/AbstractController.php @@ -15,9 +15,17 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\ResponseInterface; use Psr\Container\ContainerInterface; +use App\Commons\Log; abstract class AbstractController { + + /** + * @Inject + * @var Log + */ + protected $log; + /** * @Inject * @var ContainerInterface diff --git a/app/Controller/StoreController.php b/app/Controller/StoreController.php new file mode 100644 index 0000000..220aa85 --- /dev/null +++ b/app/Controller/StoreController.php @@ -0,0 +1,20 @@ +get(TaskExecutor::class); + // $result = $exec->execute(new Task([MethodTask::class, 'handle'], [Coroutine::id()])); + + // $client = ApplicationContext::getContainer()->get(SSDBTask::class); + // $result = $client->exec("set","bar","1234"); + // $result = $client->exec("get","bar"); + + // $client = ApplicationContext::getContainer()->get(MethodTask::class); + // $result = $client->handle("set"); + + + //$log = ApplicationContext::getContainer()->get(Log::class); + $log = $this->log; + + $log->push(['test'=>1,'user_id'=>290,'msg'=>'order']); + $log->event('t1',['test'=>1,'user_id'=>290,'msg'=>'order']); + + //$this->name = 'index1 action '. $result; + return $this->name; + } + + public function index2(RequestInterface $request) + { + $this->name = 'index2 action'; + return $this->name; + } + + public function index3(RequestInterface $request) + { + return $this->name; + } +} \ No newline at end of file diff --git a/app/Model/Store.php b/app/Model/Store.php new file mode 100644 index 0000000..035cc75 --- /dev/null +++ b/app/Model/Store.php @@ -0,0 +1,28 @@ + 'required|nonempty', + 'market_id' => 'required|nonempty', + 'md_type' => 'required|nonempty', + 'address' => 'required|nonempty', + 'coordinates' => 'required|nonempty', + 'details' => 'required|nonempty', + 'link_name' => 'required|nonempty', + 'link_tel' => 'required|nonempty', + 'tel' => 'required|nonempty', + 'logo' => 'required|nonempty', + 'fm_img' => 'required|nonempty', + 'zm_img' => 'required|nonempty', + 'yyzz' => 'required|nonempty', + 'user_id' => 'required|nonempty', + 'mm_user_id' => 'required|nonempty', + ]; + } +} diff --git a/composer.json b/composer.json index 1ac7889..a52229e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "hyperf/config": "~2.0.0", "hyperf/db-connection": "~2.0.0", "hyperf/framework": "~2.0.0", - "hyperf/guzzle": "~2.0.0", + "hyperf/guzzle": "^2.0", "hyperf/http-server": "~2.0.0", "hyperf/logger": "~2.0.0", "hyperf/memory": "~2.0.0", diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index cccee93..80f707b 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -10,5 +10,8 @@ declare(strict_types=1); * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ return [ - \App\Service\ServiceEvaluateServiceInterface::class => \App\Service\ServiceEvaluateService::class + \App\Service\ServiceEvaluateServiceInterface::class => \App\Service\ServiceEvaluateService::class, + \App\Service\AttachmentServiceInterface::class => \App\Service\AttachmentService::class, + \App\Service\ParamsTokenServiceInterface::class => \App\Service\ParamsTokenSsdbService::class, + \App\Commons\Log::class => \App\Commons\Log::class, ]; diff --git a/config/routes.php b/config/routes.php index 706a9ab..ae203c4 100644 --- a/config/routes.php +++ b/config/routes.php @@ -23,4 +23,5 @@ Router::addGroup('/v1/',function (){ Router::post('ServiceEvaluate/isPersonnel', 'App\Controller\ServiceEvaluateController@isPersonnel'); Router::post('ServiceEvaluate/getPersonnelInfo', 'App\Controller\ServiceEvaluateController@getPersonnelInfo'); Router::post('ServiceEvaluate/getEvaluateList', 'App\Controller\ServiceEvaluateController@getEvaluateList'); + Router::post('Store/applyEntry', 'App\Controller\StoreController@applyEntry'); }); \ No newline at end of file