1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace Modules\Channel\Http\Controllers;
- use Catch\Base\CatchController;
- use EasyWeChat\OpenPlatform\Application;
- use Illuminate\Foundation\Validation\ValidatesRequests;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- use Modules\Channel\Models\WechatAuthorizationInfo;
- use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
- use Modules\Common\Errors\Errors;
- use Modules\Common\Exceptions\CommonBusinessException;
- use Modules\User\Http\Controllers\UserTrait;
- use Symfony\Component\Cache\Adapter\RedisAdapter;
- class WechatOpenPlatformController extends CatchController
- {
- use UserTrait;
- use ValidatesRequests;
- // 授权跳转页
- public function preauth(Request $request) {
- $this->validate($request, [
- 'user_id' => 'required'
- ]);
- $currentUser = $this->getCurrentUser();
- $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->id);
- $app = WechatOpenPlatformService::buildApplication($componentInfo);
- $user_id = $request->input('user_id');
- $url = $app->createPreAuthorizationUrl(url('/api/channel/openPlatform/auth/'.$componentInfo->app_id.'/'.$user_id), []);
- return $url;
- }
- public function auth(Request $request, $component_appid, $user_id) {
- $auth_code = $request->input('auth_code');
- $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
- $app = WechatOpenPlatformService::buildApplication($componentInfo);
- $client = $app->getClient();
- $response = $client->post('/cgi-bin/component/api_query_auth', [
- 'component_appid' => $component_appid,
- 'authorization_code' => $auth_code
- ]);
- $authorizer_appid = $response['authorization_info']['authorizer_appid'];
- $authorizer_refresh_token = $response['authorization_info']['authorizer_refresh_token'];
- $currentUser = $this->getCurrentUser();
- WechatAuthorizationInfo::updateOrCreate([
- 'authorizer_appid' => $authorizer_appid,
- 'component_appid' => $component_appid,
- 'user_id' => $user_id,
- 'puser_id' => $currentUser->id,
- ], [
- 'authorizer_refresh_token' => $authorizer_refresh_token
- ]);
- return view('wechat.openPlatform.authSuccess')->with('url', url('/'));
- }
- /**
- * 处理授权事件
- * @param Request $request
- * @param $component_appid
- */
- public function authorCommand(Request $request, $component_appid) {
- $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
- myLog('authorCommand')->info('start:'. $component_appid);
- $app = WechatOpenPlatformService::buildApplication($componentInfo);
- $app->setCache(Cache::store('redis'));
- $server = $app->getServer();
- $server->handleVerifyTicketRefreshed(function($message, \Closure $next) {
- myLog('authorCommand')->info('handleVerifyTicketRefreshed', ['message' => $message]);
- return $next($message);
- });
- $server->handleAuthorized(function($message, \Closure $next) {
- myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
- return $next($message);
- });
- myLog('authorCommand')->info('end00000');
- return $server->serve();
- }
- public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
- $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
- $app = WechatOpenPlatformService::buildApplication($componentInfo);
- $server = $app->getServer();
- return $server->serve();
- }
- }
|