php 微信扫码登录网站
程序员文章站
2024-01-24 18:31:28
...
前期准备:
微信开放平台:https://open.weixin.qq.com/
前端代码:
<a onclick='towxLogin()' openwindowheight="520" openwindowwidth="450" data-third="weixin" class="wx icon" pbtag="微信" pbflag="头部" title="微信登录"></a>
<div id="login_container" style="z-index: 99999;position: absolute;background-color: white;"></div>
js拉起微信登录使div浮于最上层
<script type="text/javascript" src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
<script type="text/javascript">
function towxLogin()
{
var obj = new WxLogin({
self_redirect:false, //false 登录之后的页面不在frame内打开,在最上层打开
id:"login_container",
appid: "APPID",
scope: "snsapi_login",
redirect_uri: "{$wxCallback}", //带协议的完整的登录回调地址
state: "sf_Wxlogin",
style: "black",
href: ""
});
}
</script>
后端代码:
public function wechat_login()
{
$wxConfig = C('THINK_SDK_WEIXIN');
$appid = $wxConfig['APP_KEY'];
$appsecret = $wxConfig['APP_SECRET'];
$auth = new WechatAuth($appid, $appsecret);
$token = $auth->getAccessToken("code",$_GET['code']);
$userInfo = $auth->getUserInfo($token['openid']);
//获取用户信息成功后的内部操作
}