public function getCodeUserInfo($tem)
{
if($_SERVER['REQUEST_METHOD']=='OPTIONS') exit;
//获取code
$redirect_uri = urlencode("http://******/控制器/方法(getWxUserInfo)");//回调地址
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->gappid . "&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=$tem&connect_redirect=1#wechat_redirect"; //state 可任意
$this->redirect($url, 302);// tp5的重定向方法
}
public function getWxUserInfo()
{
if($_SERVER['REQUEST_METHOD']=='OPTIONS') exit;
//换取网页授权access_token
//获取openid
$code = $this->request->param('code');
$state = 123;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->gappid . "&secret=" . $this->gappsecret . "&code=" . $code . "&grant_type=authorization_code";
$rjson = $this->curl($url);
$rjson = json_decode($rjson);
$access_token = $rjson->access_token;//得到access_token ( 网页的)
$openId = $rjson->openid;//得到openid
$userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=" . $this->gappid . "&lang=zh_CN";//获取用户信息
$result = json_decode($this->curl($userInfoUrl));
return json(['status' => 'ok', 'openid' => $result->openid]);
}