MenuService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\common\library\xmwechat\offiaccount;
  3. use app\common\library\xmwechat\SingletonTrait;
  4. use app\common\library\xmwechat\WechatService;
  5. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  6. use EasyWeChat\OfficialAccount\Application;
  7. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  8. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  9. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  10. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  11. use think\exception\HttpResponseException;
  12. use think\Response;
  13. /**
  14. * 菜单管理
  15. */
  16. class MenuService extends WechatService
  17. {
  18. protected ?Application $app;
  19. protected string $accessToken;
  20. use SingletonTrait;
  21. /**
  22. * @throws InvalidArgumentException
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. if (empty($this->offiAccountConfig['app_id']) || empty($this->offiAccountConfig['secret'])) {
  28. $this->result('请配置公众号appid或secret');
  29. }
  30. $offiAccountConfig = [
  31. 'app_id' => $this->offiAccountConfig['app_id'],
  32. 'secret' => $this->offiAccountConfig['secret'],
  33. ];
  34. $this->app = new Application($offiAccountConfig);
  35. $accessToken = $this->app->getAccessToken();
  36. $this->accessToken = $accessToken->getToken();
  37. }
  38. /**
  39. * 获取已发布的菜单
  40. * @return mixed|void
  41. * @throws TransportExceptionInterface
  42. * @throws ClientExceptionInterface
  43. * @throws RedirectionExceptionInterface
  44. * @throws ServerExceptionInterface
  45. * @throws \Exception
  46. * @author jsjxsz
  47. */
  48. public function getMenu()
  49. {
  50. try {
  51. $response = $this->app->getClient()->get('cgi-bin/menu/get?access_token=' . $this->accessToken);
  52. if ($response->isFailed()) {
  53. // 出错了,处理异常
  54. $this->result($response->getContent());
  55. }
  56. $data = json_decode($response->getContent(), true);
  57. return $data['menu']['button'];
  58. } catch (\Exception $e) {
  59. $this->result($e->getMessage());
  60. }
  61. }
  62. /**
  63. * 保存并发布菜单 params menu matchRule
  64. * @param $params
  65. * @return bool
  66. * @throws \Exception
  67. * @author jsjxsz
  68. */
  69. public function saveAndPublish($params): bool
  70. {
  71. try {
  72. if (isset($params['matchRule']) && !empty($params['matchRule'])) {
  73. $response = $this->app->getClient()->postJson('cgi-bin/menu/addconditional', [
  74. 'button' => $params['button'],
  75. 'matchrule' => $params['matchrule'],
  76. ]);
  77. } else {
  78. $response = $this->app->getClient()->postJson('cgi-bin/menu/create', ['button' => $params['button']]);
  79. }
  80. if ($response->isFailed()) {
  81. // 出错了,处理异常
  82. $this->result($response->getContent());
  83. }
  84. $result = json_decode($response->getContent(), true);
  85. if ($result['errcode'] == 0) {
  86. return true;
  87. }
  88. } catch (TransportExceptionInterface $e) {
  89. $this->result($e->getMessage());
  90. }
  91. return false;
  92. }
  93. }