OpenPlatform.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\wechat;
  12. use crmeb\services\wechat\config\OpenWebConfig;
  13. use EasyWeChat\Factory;
  14. use EasyWeChat\Kernel\Exceptions\BadRequestException;
  15. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  16. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  17. use EasyWeChat\OpenPlatform\Application;
  18. use Symfony\Component\Cache\Adapter\RedisAdapter;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use think\facade\Cache;
  21. use think\Response;
  22. use Yurun\Util\Swoole\Guzzle\SwooleHandler;
  23. /**
  24. * 开放平台
  25. * Class OpenPlatform
  26. * @package crmeb\services\wechat
  27. */
  28. class OpenPlatform extends BaseApplication
  29. {
  30. /**
  31. * @var OpenWebConfig
  32. */
  33. protected $config;
  34. /**
  35. * @var Application
  36. */
  37. protected $application;
  38. /**
  39. * OpenPlatform constructor.
  40. */
  41. public function __construct()
  42. {
  43. /** @var OpenWebConfig config */
  44. $this->config = app()->make(OpenWebConfig::class);
  45. $this->debug = DefaultConfig::value('logger');
  46. }
  47. /**
  48. * @return OpenPlatform
  49. */
  50. public static function instance()
  51. {
  52. return app()->make(static::class);
  53. }
  54. /**
  55. * @return Application
  56. */
  57. public function application()
  58. {
  59. if (!$this->application) {
  60. $this->application = Factory::openPlatform($this->config->all());
  61. $request = request();
  62. $this->application['guzzle_handler'] = SwooleHandler::class;
  63. $this->application->rebind('request', new Request($request->get(), $request->post(), [], [], [], $request->server(), $request->getContent()));
  64. $this->application->rebind('cache', new RedisAdapter(Cache::store('redis')->handler()));
  65. }
  66. return $this->application;
  67. }
  68. /**
  69. * 服务端
  70. * @return Response
  71. * @throws BadRequestException
  72. * @throws InvalidArgumentException
  73. * @throws InvalidConfigException
  74. * @throws \ReflectionException
  75. */
  76. public static function serve(): Response
  77. {
  78. $make = self::instance();
  79. $make->application()->server->push($make->pushMessageHandler);
  80. $response = $make->application()->server->serve();
  81. return response($response->getContent());
  82. }
  83. }