WechatOpenPlatformController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Redis;
  11. use Modules\Channel\Models\WechatAuthorizationInfo;
  12. use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
  13. use Modules\Common\Errors\Errors;
  14. use Modules\Common\Exceptions\CommonBusinessException;
  15. use Modules\User\Http\Controllers\UserTrait;
  16. use Symfony\Component\Cache\Adapter\RedisAdapter;
  17. class WechatOpenPlatformController extends CatchController
  18. {
  19. use UserTrait;
  20. use ValidatesRequests;
  21. /**
  22. * 三方授权跳转页
  23. * @param Request $request
  24. * @return string
  25. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  26. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  27. * @throws \Illuminate\Validation\ValidationException
  28. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  29. * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  30. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  31. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  32. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  33. */
  34. public function preauth(Request $request) {
  35. $this->validate($request, [
  36. 'user_id' => 'required'
  37. ]);
  38. $currentUser = $this->getCurrentUser();
  39. $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->id);
  40. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  41. $user_id = $request->input('user_id');
  42. $url = $app->getPreAuthorizationUrl(sprintf('%s/api/channel/openPlatform/auth/%s/%s',config('app.url'), $componentInfo->app_id, $user_id), []);
  43. return $url;
  44. }
  45. /**
  46. * 三方授权回调
  47. * @param Request $request
  48. * @param $component_appid
  49. * @param $user_id
  50. * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
  51. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  52. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  53. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  54. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  55. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  56. */
  57. public function auth(Request $request, $component_appid, $user_id) {
  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::create([
  83. 'authorizer_appid' => $authorizer_appid,
  84. 'component_appid' => $component_appid,
  85. 'user_id' => $user_id,
  86. 'puser_id' => $auth_user->pid,
  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. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  99. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  100. $server = $app->server;
  101. $server->push(function ($message) {
  102. myLog('authorCommand')->info('取消授权', [
  103. 'message' => $message,
  104. ]);
  105. WechatOpenPlatformService::handleUnauthorized($message);
  106. }, Guard::EVENT_UNAUTHORIZED);
  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->getServer();
  114. $message = $server->getDecryptedMessage();
  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->getOfficialAccountWithRefreshToken($authorizer_appid, $refreshToken)->getServer();
  123. $server->addMessageListener('text', function($message) {
  124. myLog("wx-xiaoxi")->info('----文本消息---');
  125. if ($message->Content == "pk"){
  126. return $message->FromUserName;
  127. }
  128. });
  129. $server->addEventListener('subscribe', function() { });
  130. myLog("wx-xiaoxi")->info('-------结束处理---'.get_date());
  131. return $server->serve();
  132. }
  133. }