构造网页授权链接
如果企业需要在打开的网页里面携带用户的身份信息,第一步需要构造如下的链接来获取code参数:
注意:这里的appid一定要用企业的CorpID
$redirectUrl = urlencode('https://www.simon.com/callback');
https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww369a5ae569c16ce3&redirect_uri=$redirectUrl&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
获取access_token
注意:corpid一定要用企业的CorpID,corpsecret相关应用的Secret
$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww369a5ae569c16ce3&corpsecret=EHhTqrhROB52XRjwSl5URIYi7oCMdnwfX6empPFVrSS";
$result = $this->http_curl($url);
$res = json_decode($result, true);
if ($res['errcode'] != 0) {
\Log::info('获取企业微信access_token出错' . json_encode($res));
return false;
}
return $res['access_token'];
获取成员信息返回UserID
$code = request()->get('code');
$httpCurl = new AccessToken();
$accessToken = $httpCurl->getQyAccessToken('EHhTqrhROB52XRjwSl5URIYi7oCMdnwfX6empPFVrSS');
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" . $accessToken . "&code=" . $code;
$res = $httpCurl->http_curl($url);
$res = json_decode($res, true);
return $res['UserId']