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->getPreAuthorizationUrl(sprintf('%s/api/channel/openPlatform/auth/%s/%s', config('app.url'), $componentInfo->app_id, $user_id), []); return $url; } /** * 三方授权回调 * @param Request $request * @param $component_appid * @param $user_id * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface */ public function auth(Request $request, $component_appid, $user_id) { $auth_code = $request->input('auth_code'); $auth_user = DB::table('users') ->where([ 'id' => $user_id, 'deleted_at' => 0, 'status' => 1 ]) ->where('pid', '<>', 0) ->select('pid', 'id')->first(); if (!$auth_user) { CommonBusinessException::throwError(Errors::OPENPLATFORM_OPTIMIZER_INFO_ERROR); } $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid); $app = WechatOpenPlatformService::buildApplication($componentInfo); $authorize = $app->handleAuthorize($auth_code); $authorizer_appid = $authorize['authorization_info']['authorizer_appid']; $authorizer_refresh_token = $authorize['authorization_info']['authorizer_refresh_token']; $gzhBaseInfo = $app->getAuthorizer($authorizer_appid); WechatAuthorizationInfo::where([ 'authorizer_appid' => $authorizer_appid, 'component_appid' => $component_appid, 'puser_id' => $auth_user->pid, 'is_enabled' => 1, ])->update([ 'is_enabled' => 0, ]); WechatAuthorizationInfo::updateOrCreate([ 'authorizer_appid' => $authorizer_appid, 'component_appid' => $component_appid, 'user_id' => $user_id, 'puser_id' => $auth_user->pid, ], [ 'is_enabled' => 1, 'authorizer_refresh_token' => $authorizer_refresh_token, 'nick_name' => $gzhBaseInfo['authorizer_info']['nick_name'], ]); return view('wechat.openPlatform.authSuccess')->with('url', sprintf('%s/#/user/advertiser',config('app.url'))); } /** * 处理授权事件 * @param Request $request * @param $component_appid */ public function authorCommand(Request $request, $component_appid) { $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid); $app = WechatOpenPlatformService::buildApplication($componentInfo); $server = $app->server; $server->push(function ($message) { myLog('authorCommand')->info('取消授权', [ 'message' => $message, ]); WechatOpenPlatformService::handleUnauthorized($message); }, Guard::EVENT_UNAUTHORIZED); return $server->serve(); } public function infoCommand(Request $request, $authorizer_appid, $component_appid) { $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid); $openPlatform = WechatOpenPlatformService::buildApplication($componentInfo); myLog("wx-xiaoxi")->info('-------开始处理---'.get_date()); $server = $openPlatform->server; $message = $server->getMessage(); myLog("wx-xiaoxi")->info([ 'authorizer_appid' => $authorizer_appid, 'component_appid' => $component_appid, 'param' => $request->all(), 'msg' => $message, ]); $appInfo = WechatOpenPlatformService::getRefreshToken($authorizer_appid,$component_appid); $wechatAppId = getProp($appInfo,'id'); $app = $openPlatform->officialAccount($authorizer_appid, getProp($appInfo,'authorizer_refresh_token')); unset($appInfo); $app->server->push(function ($message) use($app,$authorizer_appid,$wechatAppId) { myLog("wx-xiaoxi")->info("----文本消息---"); myLog("wx-xiaoxi")->info("体消息:"); myLog("wx-xiaoxi")->info($message); return WechatCommonService::handleMessage($app,$wechatAppId ,$authorizer_appid,$message); }); myLog("wx-xiaoxi")->info('-------结束处理---'.get_date()); return $app->server->serve()->send(); } }