WechatResponse.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 EasyWeChat\Kernel\Support\Collection;
  13. /**
  14. * 微信错误
  15. * Class WechatResponse
  16. * @package crmeb\services\wechat
  17. */
  18. class WechatResponse extends Collection
  19. {
  20. /**
  21. * @var \Throwable
  22. */
  23. protected $e;
  24. /**
  25. * @var string
  26. */
  27. protected $response;
  28. /**
  29. * 是否抛出默认错误
  30. * @var bool
  31. */
  32. protected $error = true;
  33. /**
  34. * WechatResponse constructor.
  35. * @param array $items
  36. */
  37. public function __construct(array $items = [])
  38. {
  39. parent::__construct($items);
  40. $this->wechatError();
  41. }
  42. /**
  43. * 错误统一处理
  44. */
  45. public function wechatError()
  46. {
  47. if (!$this->error) {
  48. return;
  49. }
  50. if (isset($this->items['errcode']) && 0 !== $this->items['errcode']) {
  51. throw new WechatException(
  52. ErrorMessage::getWorkMessage(
  53. $this->items['errcode'] ?? 0,
  54. $this->items['errmsg'] ?? null
  55. )
  56. );
  57. }
  58. }
  59. /**
  60. * @param bool $boole
  61. * @return $this
  62. */
  63. public function serError(bool $boole)
  64. {
  65. $this->error = $boole;
  66. return $this;
  67. }
  68. /**
  69. * 正确处理
  70. * @param callable $then
  71. * @param bool $error
  72. * @return $this
  73. */
  74. public function then(callable $then, bool $error = null)
  75. {
  76. $error = $error ?: $this->error;
  77. if (0 !== $this->items['errcode'] && $error) {
  78. throw new WechatException($this->items['errmsg']);
  79. }
  80. try {
  81. $this->response = $then(new static($this->items));
  82. } catch (\Throwable $e) {
  83. $this->e = $e;
  84. }
  85. return $this;
  86. }
  87. /**
  88. * 异常处理
  89. * @param callable $catch
  90. * @return $this
  91. */
  92. public function catch(callable $catch)
  93. {
  94. if (!$this->e) {
  95. $this->e = new WechatException('success');
  96. }
  97. $catch($this->e, $this->items);
  98. return $this;
  99. }
  100. /**
  101. * 获取返回值
  102. * @return string
  103. */
  104. public function getResponse()
  105. {
  106. return $this->response;
  107. }
  108. }