OaService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 OaService 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. $this->app = new Application([
  31. 'app_id' => $this->offiAccountConfig['app_id'],
  32. 'secret' => $this->offiAccountConfig['secret'],
  33. ]);
  34. $accessToken = $this->app->getAccessToken();
  35. $this->accessToken = $accessToken->getToken();
  36. }
  37. /**
  38. * 公众号网页授权 发起授权
  39. * https://easywechat.com/6.x/oauth.html
  40. * @throws \Exception
  41. * @author jsjxsz
  42. */
  43. public function getRedirectUrl(): string
  44. {
  45. try {
  46. $redirectUrl = $this->offiAccountConfig['redirect_url'];
  47. return $this->app->getOAuth()
  48. ->scopes(['snsapi_userinfo'])
  49. ->redirect($redirectUrl);
  50. } catch (InvalidArgumentException $e) {
  51. $this->result($e->getMessage());
  52. }
  53. }
  54. /**
  55. * 公众号网页授权 处理授权回调
  56. * https://easywechat.com/6.x/oauth.html
  57. * @param string $code
  58. * @throws \Exception
  59. * @author jsjxsz
  60. */
  61. public function oauthCallback(string $code): array
  62. {
  63. try {
  64. return $this->app->getOAuth()
  65. ->scopes(['snsapi_userinfo'])
  66. ->userFromCode($code)
  67. ->getRaw();
  68. } catch (InvalidArgumentException $e) {
  69. $this->result($e->getMessage());
  70. }
  71. }
  72. /**
  73. * 公众号发送模板消息
  74. * @param string $openid 接收者openid
  75. * @param string $template_id 模板ID
  76. * @param string $url 模板跳转链接(海外账号没有跳转能力)不跳转可不填写
  77. * @param array $miniprogram 跳小程序所需数据,不需跳小程序可不用传该数据(appid、pagepath),小程序appid必须已绑定关联当前公众号
  78. * @param array $data 模板数据
  79. * @return bool
  80. * @throws ClientExceptionInterface
  81. * @throws RedirectionExceptionInterface
  82. * @throws ServerExceptionInterface
  83. * @throws TransportExceptionInterface
  84. * @throws \Exception
  85. * @author jsjxsz
  86. */
  87. public function sendTemplateMessage(string $openid, string $template_id, array $data, string $url = '', array $miniprogram = []): bool
  88. {
  89. // halt($this->accessToken);
  90. $response = $this->app->getClient()->postJson('cgi-bin/message/template/send?access_token=' . $this->accessToken, [
  91. 'touser' => $openid,
  92. 'template_id' => $template_id,
  93. 'url' => $url,
  94. 'miniprogram' => $miniprogram,
  95. 'data' => $data
  96. ]);
  97. // return true;
  98. // halt([$response->getContent(),$this->accessToken]);
  99. if ($response->isFailed()) {
  100. return false;
  101. // 出错了,处理异常
  102. // $this->result($response->getContent());
  103. }
  104. $result = json_decode($response->getContent(), true);
  105. if ($result['errcode'] == 0) {
  106. return true;
  107. }
  108. return false;
  109. }
  110. }