WechatOpenPlatformController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Modules\Channel\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use Illuminate\Foundation\Validation\ValidatesRequests;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Modules\Channel\Models\WechatAuthorizationInfo;
  8. use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
  9. use Modules\Common\Errors\Errors;
  10. use Modules\Common\Exceptions\CommonBusinessException;
  11. use Modules\User\Http\Controllers\UserTrait;
  12. use Modules\WechatPlatform\Services\WechatCommonService;
  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. $this->validate($request, [
  32. 'user_id' => 'required'
  33. ]);
  34. $currentUser = $this->getCurrentUser();
  35. $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->id);
  36. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  37. $user_id = $request->input('user_id');
  38. $url = $app->createPreAuthorizationUrl(sprintf('%s/api/channel/openPlatform/auth/%s/%s',config('app.url'), $componentInfo->app_id, $user_id), []);
  39. return $url;
  40. }
  41. /**
  42. * 三方授权回调
  43. * @param Request $request
  44. * @param $component_appid
  45. * @param $user_id
  46. * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
  47. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  48. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  49. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  50. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  51. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  52. */
  53. public function auth(Request $request, $component_appid, $user_id) {
  54. $auth_code = $request->input('auth_code');
  55. $auth_user = DB::table('users')
  56. ->where([
  57. 'id' => $user_id, 'deleted_at' => 0, 'status' => 1
  58. ])
  59. ->where('pid', '<>', 0)
  60. ->select('pid', 'id')->first();
  61. if(!$auth_user) {
  62. CommonBusinessException::throwError(Errors::OPENPLATFORM_OPTIMIZER_INFO_ERROR);
  63. }
  64. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  65. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  66. $client = $app->getClient();
  67. $response = $client->postJson('/cgi-bin/component/api_query_auth', [
  68. 'component_appid' => $component_appid,
  69. 'authorization_code' => $auth_code
  70. ]);
  71. $parsedContent = \json_decode($response->getContent(), true);
  72. $authorizer_appid = $parsedContent['authorization_info']['authorizer_appid'];
  73. $authorizer_refresh_token = $parsedContent['authorization_info']['authorizer_refresh_token'];
  74. $response = $client->postJson('/cgi-bin/component/api_get_authorizer_info', [
  75. 'component_appid' => $component_appid,
  76. 'authorizer_appid' => $authorizer_appid
  77. ]);
  78. $parsedContent = \json_decode($response->getContent(), true);
  79. WechatAuthorizationInfo::updateOrCreate([
  80. 'authorizer_appid' => $authorizer_appid,
  81. 'component_appid' => $component_appid,
  82. 'user_id' => $user_id,
  83. 'puser_id' => $auth_user->pid,
  84. ], [
  85. 'authorizer_refresh_token' => $authorizer_refresh_token,
  86. 'nick_name' => $parsedContent['authorizer_info']['nick_name'],
  87. ]);
  88. return view('wechat.openPlatform.authSuccess')->with('url', sprintf('%s/#/user/advertiser',config('app.url')));
  89. }
  90. /**
  91. * 处理授权事件
  92. * @param Request $request
  93. * @param $component_appid
  94. */
  95. public function authorCommand(Request $request, $component_appid) {
  96. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  97. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  98. $server = $app->getServer();
  99. $server->handleAuthorized(function($message, \Closure $next) {
  100. myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
  101. return $next($message);
  102. });
  103. $server->handleUnauthorized(function($message, \Closure $next) {
  104. WechatOpenPlatformService::handleUnauthorized($message);
  105. return $next($message);
  106. });
  107. return $server->serve();
  108. }
  109. public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
  110. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  111. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  112. myLog("wx-xiaoxi")->info('-------开始处理---'.get_date());
  113. $server = $app->server;
  114. $message = $server->getMessage();
  115. myLog("wx-xiaoxi")->info([
  116. 'authorizer_appid' => $authorizer_appid,
  117. 'component_appid' => $component_appid,
  118. 'param' => $request->all(),
  119. 'msg' => $message,
  120. ]);
  121. $refreshToken = WechatOpenPlatformService::getRefreshToken($authorizer_appid,$component_appid);
  122. $server = $app->officialAccount($authorizer_appid, $refreshToken);
  123. // $server->addMessageListener('text', function($message) {
  124. // myLog("wx-xiaoxi")->info('----文本消息---');
  125. // if ($message->Content == "pk"){
  126. // return $message->FromUserName;
  127. // }
  128. // });
  129. $server->server->push(function ($message) use($authorizer_appid,$component_appid) {
  130. myLog("wx-xiaoxi")->info("----文本消息---");
  131. myLog("wx-xiaoxi")->info("体消息:");
  132. myLog("wx-xiaoxi")->info($message);
  133. return WechatCommonService::handleMessage($authorizer_appid,$message);
  134. });
  135. myLog("wx-xiaoxi")->info('-------结束处理---'.get_date());
  136. return $server->serve();
  137. }
  138. }