```php
```php
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function index()
{
$appid = config('params.appid');
if (!session('?userinfo')) {
if (!isset($_GET['code'])) {
$redirect_uri = 'http://xxxx.cn';
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
header("location:$url");
exit();
} else {
$code = $_GET['code'];
$data = $this->get_access_token($code);
$data_all = $this->get_user_info($data['access_token'], $data['openid']);
return json($data_all);
}
} else {
$ret = session('userinfo');
return json($ret);
}
}
private function get_access_token($code)
{
$appid = config('params.appid');
$appsecret = config('params.appSecret');
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode . '
msg :' . $user->errmsg;
exit;
}
$data = json_decode(json_encode($user), true);
return $data;
}
private function get_user_info($access_token, $openid)
{
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode . 'msg :' . $user->errmsg;
exit;
}
$data = json_decode(json_encode($user), true);
return $data;
}
}