WeChatOaService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service\wechat;
  15. use EasyWeChat\Kernel\Exceptions\Exception;
  16. use EasyWeChat\OfficialAccount\Application;
  17. /**
  18. * 公众号相关
  19. * Class WeChatOaService
  20. * @package app\common\service\wechat
  21. */
  22. class WeChatOaService
  23. {
  24. protected $app;
  25. protected $config;
  26. public function __construct()
  27. {
  28. $this->config = $this->getConfig();
  29. $this->app = new Application($this->config);
  30. }
  31. /**
  32. * @notes easywechat服务端
  33. * @return \EasyWeChat\Kernel\Contracts\Server|\EasyWeChat\OfficialAccount\Server
  34. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  35. * @throws \ReflectionException
  36. * @throws \Throwable
  37. * @author 段誉
  38. * @date 2023/2/27 14:22
  39. */
  40. public function getServer()
  41. {
  42. return $this->app->getServer();
  43. }
  44. /**
  45. * @notes 配置
  46. * @return array
  47. * @throws Exception
  48. * @author 段誉
  49. * @date 2023/2/27 12:03
  50. */
  51. protected function getConfig()
  52. {
  53. $config = WeChatConfigService::getOaConfig();
  54. if (empty($config['app_id']) || empty($config['secret'])) {
  55. throw new Exception('请先设置公众号配置');
  56. }
  57. return $config;
  58. }
  59. /**
  60. * @notes 公众号-根据code获取微信信息
  61. * @param string $code
  62. * @return mixed
  63. * @throws Exception
  64. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  65. * @author 段誉
  66. * @date 2023/2/27 11:04
  67. */
  68. public function getOaResByCode(string $code)
  69. {
  70. $response = $this->app->getOAuth()
  71. ->scopes(['snsapi_userinfo'])
  72. ->userFromCode($code)
  73. ->getRaw();
  74. if (!isset($response['openid']) || empty($response['openid'])) {
  75. throw new Exception('获取openID失败');
  76. }
  77. return $response;
  78. }
  79. /**
  80. * @notes 公众号跳转url
  81. * @param string $url
  82. * @return mixed
  83. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  84. * @author 段誉
  85. * @date 2023/2/27 10:35
  86. */
  87. public function getCodeUrl(string $url)
  88. {
  89. return $this->app->getOAuth()
  90. ->scopes(['snsapi_userinfo'])
  91. ->redirect($url);
  92. }
  93. /**
  94. * @notes 创建公众号菜单
  95. * @param array $buttons
  96. * @param array $matchRule
  97. * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
  98. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  99. * @author 段誉
  100. * @date 2023/2/27 12:07
  101. */
  102. public function createMenu(array $buttons, array $matchRule = [])
  103. {
  104. if (!empty($matchRule)) {
  105. return $this->app->getClient()->postJson('cgi-bin/menu/addconditional', [
  106. 'button' => $buttons,
  107. 'matchrule' => $matchRule,
  108. ]);
  109. }
  110. return $this->app->getClient()->postJson('cgi-bin/menu/create', ['button' => $buttons]);
  111. }
  112. /**
  113. * @notes 获取jssdkConfig
  114. * @param $url
  115. * @param $jsApiList
  116. * @param array $openTagList
  117. * @param false $debug
  118. * @return mixed[]
  119. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  120. * @throws \Psr\SimpleCache\InvalidArgumentException
  121. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  122. * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  123. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  124. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  125. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  126. * @author 段誉
  127. * @date 2023/3/1 11:46
  128. */
  129. public function getJsConfig($url, $jsApiList, $openTagList = [], $debug = false)
  130. {
  131. return $this->app->getUtils()->buildJsSdkConfig($url, $jsApiList, $openTagList, $debug);
  132. }
  133. }