WechatOpenPlatformController.php 6.5 KB

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