WechatOpenPlatformController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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(sprintf('%s/api/channel/openPlatform/auth/%s/%s',config('app.url'), $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. $auth_user = DB::table('users')
  35. ->where([
  36. 'id' => $user_id, 'deleted_at' => 0, 'status' => 1
  37. ])
  38. ->where('pid', '<>', 0)
  39. ->select('pid', 'id')->first();
  40. if(!$auth_user) {
  41. CommonBusinessException::throwError(Errors::OPENPLATFORM_OPTIMIZER_INFO_ERROR);
  42. }
  43. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  44. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  45. $client = $app->getClient();
  46. $response = $client->postJson('/cgi-bin/component/api_query_auth', [
  47. 'component_appid' => $component_appid,
  48. 'authorization_code' => $auth_code
  49. ]);
  50. $parsedContent = \json_decode($response->getContent(), true);
  51. $authorizer_appid = $parsedContent['authorization_info']['authorizer_appid'];
  52. $authorizer_refresh_token = $parsedContent['authorization_info']['authorizer_refresh_token'];
  53. $response = $client->postJson('/cgi-bin/component/api_get_authorizer_info', [
  54. 'component_appid' => $component_appid,
  55. 'authorizer_appid' => $authorizer_appid
  56. ]);
  57. $parsedContent = \json_decode($response->getContent(), true);
  58. WechatAuthorizationInfo::updateOrCreate([
  59. 'authorizer_appid' => $authorizer_appid,
  60. 'component_appid' => $component_appid,
  61. 'user_id' => $user_id,
  62. 'puser_id' => $auth_user->pid,
  63. ], [
  64. 'authorizer_refresh_token' => $authorizer_refresh_token,
  65. 'nick_name' => $parsedContent['authorizer_info']['nick_name'],
  66. ]);
  67. return view('wechat.openPlatform.authSuccess')->with('url', sprintf('%s/#/user/advertiser',config('app.url')));
  68. }
  69. /**
  70. * 处理授权事件
  71. * @param Request $request
  72. * @param $component_appid
  73. */
  74. public function authorCommand(Request $request, $component_appid) {
  75. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  76. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  77. $server = $app->getServer();
  78. $server->handleAuthorized(function($message, \Closure $next) {
  79. myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
  80. return $next($message);
  81. });
  82. $server->handleUnauthorized(function($message, \Closure $next) {
  83. WechatOpenPlatformService::handleUnauthorized($message);
  84. return $next($message);
  85. });
  86. return $server->serve();
  87. }
  88. public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
  89. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  90. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  91. $server = $app->getServer();
  92. return $server->serve();
  93. }
  94. }