WechatOpenPlatformController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  61. $app->setCache(new RedisAdapter(Redis::client()));
  62. $server = $app->getServer();
  63. $server->handleAuthorized(function($message, \Closure $next) {
  64. myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
  65. return $next($message);
  66. });
  67. return $server->serve();
  68. }
  69. public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
  70. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  71. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  72. $server = $app->getServer();
  73. return $server->serve();
  74. }
  75. }