WechatOpenPlatformController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 redirect($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->post('/cgi-bin/component/api_query_auth', [
  47. 'component_appid' => $component_appid,
  48. 'authorization_code' => $auth_code
  49. ]);
  50. $authorizer_appid = $response['authorization_info']['authorizer_appid'];
  51. $authorizer_refresh_token = $response['authorization_info']['authorizer_refresh_token'];
  52. WechatAuthorizationInfo::updateOrCreate([
  53. 'authorizer_appid' => $authorizer_appid,
  54. 'component_appid' => $component_appid,
  55. 'user_id' => $user_id,
  56. 'puser_id' => $auth_user->pid,
  57. ], [
  58. 'authorizer_refresh_token' => $authorizer_refresh_token
  59. ]);
  60. return view('wechat.openPlatform.authSuccess')->with('url', sprintf('%s/#/user/advertiser',config('app.url')));
  61. }
  62. /**
  63. * 处理授权事件
  64. * @param Request $request
  65. * @param $component_appid
  66. */
  67. public function authorCommand(Request $request, $component_appid) {
  68. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  69. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  70. $server = $app->getServer();
  71. $server->handleAuthorized(function($message, \Closure $next) {
  72. myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
  73. return $next($message);
  74. });
  75. return $server->serve();
  76. }
  77. public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
  78. $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
  79. $app = WechatOpenPlatformService::buildApplication($componentInfo);
  80. $server = $app->getServer();
  81. return $server->serve();
  82. }
  83. }