微信文档地址https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
前端:
引入js
<script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
var obj = new WxLogin({
self_redirect:true,
id:"login_container", //html对应的id
appid: "",
scope: "snsapi_login",
redirect_uri: "后台对应回调地址",
state: "",
style: "",
href: ""
});
public function wxlogin_notice(){
$code = $request->input('code');
$appId = config('scramble.wechat_setting.app_id');
$appSecret = config('scramble.wechat_setting.app_secret');
if (!empty($code)){
//通过code获得 access_token + openid
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid. "&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_code";
$jsonResult = file_get_contents($url);
$resultArray = json_decode($jsonResult, true);
$access_token = $resultArray["access_token"];
$openid = $resultArray["openid"];
//通过access_token + openid 获得用户所有信息,结果全部存储在$infoArray里,后面再写自己的代码逻辑
$infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;
$infoResult = file_get_contents($infoUrl);
$infoArray = json_decode($infoResult, true);
return $this->success($infoArray);
}
}