WechatOpenPlatformController.php 6.4 KB

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