EasyModule.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Master\Framework\Library\Easywechat;
  4. use Pengxuxu\HyperfWechat\EasyWechat;
  5. /**
  6. * 微信小程序开发组
  7. * class MiniAppService
  8. */
  9. class EasyModule
  10. {
  11. protected string $message = '';
  12. protected array $data = [];
  13. protected int $ttl = 1 * 24 * 60 * 60;
  14. public function __construct() {}
  15. /**
  16. * 小程序
  17. * @param array $config
  18. * @param string $name
  19. * @return \EasyWeChat\MiniApp\Application
  20. */
  21. protected function miniApp(array $config = [], string $name = 'default')
  22. {
  23. return EasyWechat::miniApp($name,$config);
  24. }
  25. /**
  26. * 公众号
  27. * @param array $config
  28. * @param string $name
  29. * @return \EasyWeChat\OfficialAccount\Application
  30. */
  31. protected function official(array $config = [], string $name = 'default')
  32. {
  33. return EasyWechat::officialAccount($name,$config);
  34. }
  35. /**
  36. * 支付
  37. * @param array $config
  38. * @param string $name
  39. * @return \EasyWeChat\Pay\Application
  40. */
  41. protected function pay(array $config = [], string $name = 'default')
  42. {
  43. return EasyWechat::pay($name,$config);
  44. }
  45. /**
  46. * 统一校验 返回
  47. * @param $response
  48. * @return bool
  49. */
  50. protected function response($response): bool
  51. {
  52. if ($response->isFailed()) {
  53. return $this->error($response->getContent());
  54. }
  55. return $this->success($response->getContent());
  56. }
  57. /**
  58. * 返回成功结果
  59. * @param string $response
  60. * @return bool
  61. */
  62. protected function success(string $response): bool
  63. {
  64. $this->set($response,true);
  65. return true;
  66. }
  67. /**
  68. * 返回失败结果
  69. * @param string $response
  70. * @return false
  71. */
  72. protected function error(string $response): bool
  73. {
  74. $this->set($response,false);
  75. return false;
  76. }
  77. /**
  78. * 存入结果
  79. * @param string $response
  80. * @return bool
  81. */
  82. protected function set(string $response, bool $status = true): bool
  83. {
  84. $this->data = $this->json($response);
  85. if (!empty($this->data['errmsg'])) {
  86. $this->message = $this->data['errmsg'];
  87. } elseif (!empty($this->data['message'])) {
  88. $this->message = $this->data['message'];
  89. } else {
  90. $this->message = $status ? 'success' : 'EasyModule控件有误,请输出response';
  91. }
  92. return true;
  93. }
  94. /**
  95. * 解析数据
  96. * @param string $response
  97. * @return mixed
  98. */
  99. protected function json(string $response): mixed
  100. {
  101. return json_decode($response, true);
  102. }
  103. /**
  104. * 获取成功数据
  105. * @return array
  106. */
  107. public function get(): array
  108. {
  109. return $this->data;
  110. }
  111. /**
  112. * 获取消息
  113. * @return string
  114. */
  115. public function getMessage(): string
  116. {
  117. return $this->message;
  118. }
  119. }