public function actionCallback() { $this->enableCsrfValidation = false; $config = [ 'corp_id' => 'wxa81293306951ce69', 'debug' => true, 'agents' => [ 'foox' => [ 'agent_id' => 1000004, 'secret' => 'mXyyz0haLT8Y-uDyT1ST_8zGOvUSJhQC-5_PiEdI-v0', 'token' => 'oNKodUuPdreVu1ZcrSA9v', 'aes_key' => '8kjF1guL1mmCza6NkH7H595o2goAklcfvWTfdUvqFM4', ], ], 'oauth' => [ 'scopes' => ['snsapi_base'], 'callback' => 'https://oa.abc.com/sign-in/callback', ], 'log' => [ 'level' => 'debug', 'permission' => 0777, 'file' => '/home/wwwroot/oa.abc.com/a.log', ], ]; $app = Factory::work($config); $oauth = $app->oauth; // 獲取 OAuth 受權結果用戶信息 $user = $oauth->user(); $session=Yii::$app->session; $session->set('wechat_user', $user->toArray());//這裏是錯誤緣由,這個數據太大塞不進session!!! $targetUrl = empty($session->get('target_url')) ? '/' : $session->get('target_url'); header('location:'. $targetUrl); } public function actionLoginwx() { //var_dump($_SESSION);die; $this->enableCsrfValidation = false; $config = [ 'corp_id' => 'wxa81293306951ce69', 'debug' => true, // 應用列表 'agents' => [ 'foox' => [ 'agent_id' => 1000004, 'secret' => 'mXyyz0haLT8Y-uDyT1ST_8zGOvUSJhQC-5_PiEdI-v0', // server config 'token' => 'oNKodUuPdreVu1ZcrSA9v', 'aes_key' => '8kjF1guL1mmCza6NkH7H595o2goAklcfvWTfdUvqFM4', //... ], //... ], 'oauth' => [ 'scopes' => ['snsapi_base'], 'callback' => 'https://oa.abc.com/sign-in/callback', ], 'log' => [ 'level' => 'debug', 'permission' => 0777, 'file' => '/home/wwwroot/oa.qyzg.com/a.log', ], //... ]; $app = Factory::work($config); $oauth = $app->oauth; // 未登陸 $session=Yii::$app->session; if (empty($session->get('wechat_user'))) { $session->set('target_url', 'https://oa.abc.com/sign-in/loginwx'); return $oauth->redirect(); // 這裏不必定是return,若是你的框架action不是返回內容的話你就得使用 // $oauth->redirect()->send(); } // 已經登陸過 $user = $session->get('wechat_user'); var_dump($user); /* $work = Factory::work($config); $app = $work->agent('foox'); $app->server->push(function(){ return 'Hello easywechat.'; }); $response = $app->server->serve(); $response->send();*/ }