WechatOpenPlatformController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. $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. $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::create([
  81. 'authorizer_appid' => $authorizer_appid,
  82. 'component_appid' => $component_appid,
  83. 'user_id' => $user_id,
  84. 'puser_id' => $auth_user->pid,
  85. 'authorizer_refresh_token' => $authorizer_refresh_token,
  86. 'nick_name' => $gzhBaseInfo['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->server;
  99. $server->push(function ($message) {
  100. myLog('authorCommand')->info('取消授权', [
  101. 'message' => $message,
  102. ]);
  103. WechatOpenPlatformService::handleUnauthorized($message);
  104. }, Guard::EVENT_UNAUTHORIZED);
  105. return $server->serve();
  106. }
  107. public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
  108. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  109. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  110. myLog("wx-xiaoxi")->info('-------开始处理---'.get_date());
  111. $server = $app->server;
  112. $message = $server->getMessage();
  113. myLog("wx-xiaoxi")->info([
  114. 'authorizer_appid' => $authorizer_appid,
  115. 'component_appid' => $component_appid,
  116. 'param' => $request->all(),
  117. 'msg' => $message,
  118. ]);
  119. $refreshToken = WechatOpenPlatformService::getRefreshToken($authorizer_appid,$component_appid);
  120. $server = $app->officialAccount($authorizer_appid, $refreshToken);
  121. // $server->addMessageListener('text', function($message) {
  122. // myLog("wx-xiaoxi")->info('----文本消息---');
  123. // if ($message->Content == "pk"){
  124. // return $message->FromUserName;
  125. // }
  126. // });
  127. $server->server->push(function ($message) use($authorizer_appid,$component_appid) {
  128. myLog("wx-xiaoxi")->info("----文本消息---");
  129. myLog("wx-xiaoxi")->info("体消息:");
  130. myLog("wx-xiaoxi")->info($message);
  131. return WechatCommonService::handleMessage($authorizer_appid,$message);
  132. });
  133. myLog("wx-xiaoxi")->info('-------结束处理---'.get_date());
  134. return $server->serve();
  135. }
  136. }