WechatOpenPlatformController.php 3.7 KB

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