WechatOpenPlatformController.php 3.2 KB

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