Middleware.Class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2019/9/1
  6. * Time: 2:12 PM
  7. */
  8. namespace JinDouYun\Model;
  9. use Mall\Framework\Core\Config;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Factory;
  12. class Middleware
  13. {
  14. static private $instance;
  15. private $resourceConfig;
  16. private $Swoole_Client;
  17. public function __construct($serviceName)
  18. {
  19. $this->resourceConfig = Config::getInstance()->get('dataResources');
  20. if (!empty($this->resourceConfig)) {
  21. $this->Swoole_Client = Factory::swoole($this->resourceConfig[$serviceName]);
  22. }
  23. }
  24. /**
  25. * 单例模式
  26. *
  27. * @param string $serviceName 服务名称
  28. *
  29. * @return object
  30. *
  31. * @throws \Exception
  32. */
  33. static public function getInstance($serviceName)
  34. {
  35. /*$serviceNameIndex = md5($serviceName);
  36. if (!isset(self::$instance[$serviceNameIndex])) {
  37. self::$instance[$serviceNameIndex] = new self($serviceName);
  38. }
  39. if (is_null(self::$instance[$serviceNameIndex]->resourceConfig)) {
  40. throw new \Exception('instance resourceConfig is null');
  41. }
  42. return self::$instance[$serviceNameIndex];*/
  43. return new self($serviceName);
  44. }
  45. /**
  46. * 发送swoole请求 目前统一封装为json消息体传输
  47. *
  48. * @param string $controller 控制器
  49. * @param string $action 方法
  50. * @param array $params 需要的参数
  51. *
  52. * @return mixed
  53. */
  54. public function sendSwoole($controller, $action, array $params = [])
  55. {
  56. //请求串号
  57. $serid = self::getRequestId();
  58. $msg = $this->structureMsg($controller, $action, $params, $serid);
  59. $result = $this->Swoole_Client->sendMsg($msg, $serid);
  60. if (!empty($result)) {
  61. return $result;
  62. } else {
  63. //todo 日志记录返回为空时得请求消息体
  64. return [
  65. 'state' => false,
  66. 'data' => 'swoole未返回消息',
  67. 'errorcode' => ErrorCode::$swooleRecvError
  68. ];
  69. }
  70. }
  71. /**
  72. * 生成请求串号
  73. * @return int
  74. */
  75. static function getRequestId()
  76. {
  77. $us = strstr(microtime(), ' ', true);
  78. return intval(strval($us * 1000 * 1000) . rand(100, 999));
  79. }
  80. /**
  81. * 构造发送消息体
  82. *
  83. * @param string $controller 控制器
  84. * @param string $action 方法
  85. * @param array $params 需要的参数
  86. * @param int $serid 请求串号
  87. *
  88. * @return string
  89. */
  90. private function structureMsg($controller, $action, $params = [], $serid, $uid = 0)
  91. {
  92. $data = json_encode([
  93. 'contorller' => ucfirst($controller),
  94. 'action' => $action,
  95. 'params' => $params
  96. ], JSON_UNESCAPED_UNICODE);
  97. return pack('NNN', strlen($data), $uid, $serid) . $data;
  98. }
  99. }