WechatOpenPlatformController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Modules\Channel\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use EasyWeChat\OpenPlatform\Server\Guard;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\DB;
  8. use Modules\Channel\Models\WechatAuthorizationInfo;
  9. use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
  10. use Modules\Common\Errors\Errors;
  11. use Modules\Common\Exceptions\CommonBusinessException;
  12. use Modules\User\Http\Controllers\UserTrait;
  13. class WechatOpenPlatformController extends CatchController
  14. {
  15. use UserTrait;
  16. use ValidatesRequests;
  17. /**
  18. * 三方授权跳转页
  19. * @param Request $request
  20. * @return string
  21. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  22. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  23. * @throws \Illuminate\Validation\ValidationException
  24. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  25. * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  26. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  27. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  28. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  29. */
  30. public function preauth(Request $request)
  31. {
  32. $this->validate($request, [
  33. 'user_id' => 'required'
  34. ]);
  35. $currentUser = $this->getCurrentUser();
  36. $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->id);
  37. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  38. $user_id = $request->input('user_id');
  39. $url = $app->getPreAuthorizationUrl(sprintf('%s/api/channel/openPlatform/auth/%s/%s', config('app.url'), $componentInfo->app_id, $user_id), []);
  40. return $url;
  41. }
  42. /**
  43. * 三方授权回调
  44. * @param Request $request
  45. * @param $component_appid
  46. * @param $user_id
  47. * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
  48. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  49. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  50. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  51. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  52. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  53. */
  54. public function auth(Request $request, $component_appid, $user_id)
  55. {
  56. $auth_code = $request->input('auth_code');
  57. $auth_user = DB::table('users')
  58. ->where([
  59. 'id' => $user_id, 'deleted_at' => 0, 'status' => 1
  60. ])
  61. ->where('pid', '<>', 0)
  62. ->select('pid', 'id')->first();
  63. if (!$auth_user) {
  64. CommonBusinessException::throwError(Errors::OPENPLATFORM_OPTIMIZER_INFO_ERROR);
  65. }
  66. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  67. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  68. $authorize = $app->handleAuthorize($auth_code);
  69. $authorizer_appid = $authorize['authorization_info']['authorizer_appid'];
  70. $authorizer_refresh_token = $authorize['authorization_info']['authorizer_refresh_token'];
  71. $gzhBaseInfo = $app->getAuthorizer($authorizer_appid);
  72. WechatAuthorizationInfo::where([
  73. 'authorizer_appid' => $authorizer_appid,
  74. 'component_appid' => $component_appid,
  75. 'puser_id' => $auth_user->pid,
  76. 'is_enabled' => 1,
  77. ])->update([
  78. 'is_enabled' => 0,
  79. ]);
  80. WechatAuthorizationInfo::updateOrCreate([
  81. 'authorizer_appid' => $authorizer_appid,
  82. 'component_appid' => $component_appid,
  83. 'user_id' => $user_id,
  84. 'puser_id' => $auth_user->pid,
  85. ], [
  86. 'is_enabled' => 1,
  87. 'authorizer_refresh_token' => $authorizer_refresh_token,
  88. 'nick_name' => $gzhBaseInfo['authorizer_info']['nick_name'],
  89. ]);
  90. return view('wechat.openPlatform.authSuccess')->with('url', sprintf('%s/#/user/advertiser', config('app.url')));
  91. }
  92. /**
  93. * 处理授权事件
  94. * @param Request $request
  95. * @param $component_appid
  96. */
  97. public function authorCommand(Request $request, $component_appid)
  98. {
  99. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  100. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  101. $server = $app->server;
  102. $server->push(function ($message) {
  103. myLog('authorCommand')->info('取消授权', [
  104. 'message' => $message,
  105. ]);
  106. WechatOpenPlatformService::handleUnauthorized($message);
  107. }, Guard::EVENT_UNAUTHORIZED);
  108. return $server->serve();
  109. }
  110. public function infoCommand(Request $request, $authorizer_appid, $component_appid)
  111. {
  112. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  113. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  114. myLog("wx-xiaoxi")->info('-------开始处理---' . get_date());
  115. $server = $app->getServer();
  116. $message = $server->getDecryptedMessage();
  117. myLog("wx-xiaoxi")->info([
  118. 'authorizer_appid' => $authorizer_appid,
  119. 'component_appid' => $component_appid,
  120. 'param' => $request->all(),
  121. 'msg' => $message,
  122. ]);
  123. $refreshToken = WechatOpenPlatformService::getRefreshToken($authorizer_appid, $component_appid);
  124. $server = $app->getOfficialAccountWithRefreshToken($authorizer_appid, $refreshToken)->getServer();
  125. $server->addMessageListener('text', function ($message) {
  126. myLog("wx-xiaoxi")->info('----文本消息---');
  127. if ($message->Content == "pk") {
  128. return $message->FromUserName;
  129. }
  130. });
  131. $server->addEventListener('subscribe', function () {
  132. });
  133. myLog("wx-xiaoxi")->info('-------结束处理---' . get_date());
  134. return $server->serve();
  135. }
  136. }