WechatOpenPlatformController.php 3.8 KB

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