123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- declare(strict_types=1);
- namespace App\Master\Framework\Library\Easywechat;
- use Pengxuxu\HyperfWechat\EasyWechat;
- /**
- * 微信小程序开发组
- * class MiniAppService
- */
- class EasyModule
- {
- protected string $message = '';
- protected array $data = [];
- protected int $ttl = 1 * 24 * 60 * 60;
- public function __construct() {}
- /**
- * 小程序
- * @param array $config
- * @param string $name
- * @return \EasyWeChat\MiniApp\Application
- */
- protected function miniApp(array $config = [], string $name = 'default')
- {
- return EasyWechat::miniApp($name,$config);
- }
- /**
- * 公众号
- * @param array $config
- * @param string $name
- * @return \EasyWeChat\OfficialAccount\Application
- */
- protected function official(array $config = [], string $name = 'default')
- {
- return EasyWechat::officialAccount($name,$config);
- }
- /**
- * 支付
- * @param array $config
- * @param string $name
- * @return \EasyWeChat\Pay\Application
- */
- protected function pay(array $config = [], string $name = 'default')
- {
- return EasyWechat::pay($name,$config);
- }
- /**
- * 统一校验 返回
- * @param $response
- * @return bool
- */
- protected function response($response): bool
- {
- if ($response->isFailed()) {
- return $this->error($response->getContent());
- }
- return $this->success($response->getContent());
- }
- /**
- * 返回成功结果
- * @param string $response
- * @return bool
- */
- protected function success(string $response): bool
- {
- $this->set($response,true);
- return true;
- }
- /**
- * 返回失败结果
- * @param string $response
- * @return false
- */
- protected function error(string $response): bool
- {
- $this->set($response,false);
- return false;
- }
- /**
- * 存入结果
- * @param string $response
- * @return bool
- */
- protected function set(string $response, bool $status = true): bool
- {
- $this->data = $this->json($response);
- if (!empty($this->data['errmsg'])) {
- $this->message = $this->data['errmsg'];
- } elseif (!empty($this->data['message'])) {
- $this->message = $this->data['message'];
- } else {
- $this->message = $status ? 'success' : 'EasyModule控件有误,请输出response';
- }
- return true;
- }
- /**
- * 解析数据
- * @param string $response
- * @return mixed
- */
- protected function json(string $response): mixed
- {
- return json_decode($response, true);
- }
- /**
- * 获取成功数据
- * @return array
- */
- public function get(): array
- {
- return $this->data;
- }
- /**
- * 获取消息
- * @return string
- */
- public function getMessage(): string
- {
- return $this->message;
- }
- }
|