1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| public $cOnfig= [
'corp_id' => 'wxa81293306951999',
'debug' => false,
'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/weixin/callback',
],
'log' => [
'level' => 'debug',
'permission' => 0777,
'file' => '/home/www/oa.abc.com/a.log',
],
];
public function init()
{
$this->enableCsrfValidation = false;
$this->layout = false;
}
public function actionCallback()
{
$app = Factory::work($this->config);
$oauth = $app->oauth;
// 获取 OAuth 授权结果用户信息
$user = $oauth->user();
$session=Yii::$app->session;
$session->set('wechat_user', $user->toArray());
$targetUrl = empty($session->get('target_url')) ? '/' : $session->get('target_url');
// var_dump( $session->get('wechat_user'));die;//这里都打印了正确的内容
//var_dump($targetUrl);die;//这里都打印了正确的内容
header('location:'.$targetUrl);
}
public function actionLoginwx()
{
$app = Factory::work($this->config);
$oauth = $app->oauth;
$session=Yii::$app->session;
// 未登录
if (empty($session->get('wechat_user'))) {
$session->set('target_url', 'https://oa.abc.com/weixin/loginwx');
return $oauth->redirect('https://oa.abc.com/weixin/callback');
}
// 已经登录过
$user = $session->get('wechat_user');
var_dump($user );
} |