EasyModule.php 2.9 KB

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