123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace JinDouYun\Model;
- use Mall\Framework\Core\Config;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Factory;
- class Middleware
- {
- static private $instance;
- private $resourceConfig;
- private $Swoole_Client;
- public function __construct($serviceName)
- {
- $this->resourceConfig = Config::getInstance()->get('dataResources');
- if (!empty($this->resourceConfig)) {
- $this->Swoole_Client = Factory::swoole($this->resourceConfig[$serviceName]);
- }
- }
-
- static public function getInstance($serviceName)
- {
-
- return new self($serviceName);
- }
-
- public function sendSwoole($controller, $action, array $params = [])
- {
-
- $serid = self::getRequestId();
- $msg = $this->structureMsg($controller, $action, $params, $serid);
- $result = $this->Swoole_Client->sendMsg($msg, $serid);
- if (!empty($result)) {
- return $result;
- } else {
-
- return [
- 'state' => false,
- 'data' => 'swoole未返回消息',
- 'errorcode' => ErrorCode::$swooleRecvError
- ];
- }
- }
-
- static function getRequestId()
- {
- $us = strstr(microtime(), ' ', true);
- return intval(strval($us * 1000 * 1000) . rand(100, 999));
- }
-
- private function structureMsg($controller, $action, $params = [], $serid, $uid = 0)
- {
- $data = json_encode([
- 'contorller' => ucfirst($controller),
- 'action' => $action,
- 'params' => $params
- ], JSON_UNESCAPED_UNICODE);
- return pack('NNN', strlen($data), $uid, $serid) . $data;
- }
- }
|