WechatOpenPlatformController.php 3.0 KB

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