WechatOpenPlatformController.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Modules\Channel\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use EasyWeChat\OpenPlatform\Application;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Redis;
  10. use Modules\Channel\Models\WechatAuthorizationInfo;
  11. use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
  12. use Modules\Common\Errors\Errors;
  13. use Modules\Common\Exceptions\CommonBusinessException;
  14. use Modules\User\Http\Controllers\UserTrait;
  15. use Symfony\Component\Cache\Adapter\RedisAdapter;
  16. class WechatOpenPlatformController extends CatchController
  17. {
  18. use UserTrait;
  19. use ValidatesRequests;
  20. // 授权跳转页
  21. public function preauth(Request $request) {
  22. $this->validate($request, [
  23. 'user_id' => 'required'
  24. ]);
  25. $currentUser = $this->getCurrentUser();
  26. $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->id);
  27. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  28. $user_id = $request->input('user_id');
  29. $url = $app->createPreAuthorizationUrl(url('/api/channel/openPlatform/auth/'.$componentInfo->app_id.'/'.$user_id), []);
  30. return $url;
  31. }
  32. public function auth(Request $request, $component_appid, $user_id) {
  33. $auth_code = $request->input('auth_code');
  34. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  35. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  36. $client = $app->getClient();
  37. $response = $client->post('/cgi-bin/component/api_query_auth', [
  38. 'component_appid' => $component_appid,
  39. 'authorization_code' => $auth_code
  40. ]);
  41. $authorizer_appid = $response['authorization_info']['authorizer_appid'];
  42. $authorizer_refresh_token = $response['authorization_info']['authorizer_refresh_token'];
  43. $currentUser = $this->getCurrentUser();
  44. WechatAuthorizationInfo::updateOrCreate([
  45. 'authorizer_appid' => $authorizer_appid,
  46. 'component_appid' => $component_appid,
  47. 'user_id' => $user_id,
  48. 'puser_id' => $currentUser->id,
  49. ], [
  50. 'authorizer_refresh_token' => $authorizer_refresh_token
  51. ]);
  52. return view('wechat.openPlatform.authSuccess')->with('url', url('/'));
  53. }
  54. /**
  55. * 处理授权事件
  56. * @param Request $request
  57. * @param $component_appid
  58. */
  59. public function authorCommand(Request $request, $component_appid) {
  60. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  61. myLog('authorCommand')->info('start:'. $component_appid);
  62. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  63. $app->setCache(Cache::store('redis'));
  64. $server = $app->getServer();
  65. $server->handleVerifyTicketRefreshed(function($message, \Closure $next) {
  66. myLog('authorCommand')->info('handleVerifyTicketRefreshed', ['message' => $message]);
  67. return $next($message);
  68. });
  69. $server->handleAuthorized(function($message, \Closure $next) {
  70. myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
  71. return $next($message);
  72. });
  73. myLog('authorCommand')->info('end00000');
  74. return $server->serve();
  75. }
  76. public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
  77. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  78. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  79. $server = $app->getServer();
  80. return $server->serve();
  81. }
  82. }