曹え 5811 发布于:2019-03-22 16:18:23
reg.html 和 login.html
<input type="hidden" name="openid" value="{$_GET['openid']}">
底部js判断自动获取openid
<script> //判断是否是微信浏览器的函数 function iswx(){ //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息,这个属性可以用来判断浏览器类型 var ua = window.navigator.userAgent.toLowerCase(); //通过正则表达式匹配ua中是否含有MicroMessenger字符串 if(ua.match(/MicroMessenger/i) == 'micromessenger'){ return true; }else{ return false; } } $(function(){ // 判断微信浏览 openid = '{$_GET['openid']}'; console.log(iswx()); if(iswx() && !openid){ window.location.href="index.php?a=wxlogin&m=login"; } }) </script>
index.php 里面加俩函数用来 获取openid
function wxlogin(){ $appid='wx1fc44448137cb1'; $redirect_rui=urlencode('http://13.jianku.com.cn/index.php?&a=wxlogin2&m='.$this->syArgs("m",1)); $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_rui.'&response_type=code&scope=snsapi_userinfo&state=asd123#wechat_redirect'; header('Location:'.$url); } function wxlogin2(){ $appid='wx1fc4444437cb1'; $appsecret='02286028tttt88cbb5cdbdc3471'; $code=$_GET['code']; $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'; $result=file_get_contents($url); $result=json_decode($result,true); $access_token=$result['access_token']; $openid=$result['openid']; // 查询是否已经绑定过,绑定过直接登录 $conditions = array('token' => $openid); $m = syDB('member')->find($conditions,null,'id,user,token'); if($m){ $_SESSION['member'] = array( 'user' => $m['user'], 'id' => $m['id'], 'gid' => $r['gid'], ); $this->gourl='?c=member'; jump($this->gourl); } else { // 没有绑定过,新注册或者绑定已注册账号 // 默认到注册页面 jump("index.php?c=member&a=".$this->syArgs("m",1)."&openid=".$openid); } }
member.php 更新数据库绑定用户的openid
login()
// 登录绑定openid if($this->syArgs("openid",1)) syDB('member')->update(array('id'=>$r['id']),array('token'=>$this->syArgs("openid",1)));
// 手机登录微信 function wxlogin2(){ $appid='wx32ec333304af0a'; $redirect_rui=urlencode('http://guangde.jianzhan8.cn/index.php?a=reponse&id='.$this->syArgs('id')); $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_rui.'&response_type=code&scope=snsapi_userinfo&state=asd123#wechat_redirect'; header('Location:'.$url); } function reponse(){ header("Content-Type:text/html;charset=utf-8"); $appid='wx32ec66666af0a'; $appsecret='33605555bc681617cc7b64c57'; $code=$_GET['code']; $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'; $result=file_get_contents($url); $result=json_decode($result,true); $access_token=$result['access_token']; $openid=$result['openid']; // 获取用户信息 $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; $result=file_get_contents($url); $o2=json_decode($result,true); $openid = $o2['openid']; // 查询是否已经绑定过,绑定过直接登录 //$conditions = array('token' => $openid); //$m = syDB('member')->find($conditions,null,'id,user,token'); if($openid){ setcookie("user",$result, time()+3600*24); $this->gourl='?c=article&id='.$this->syArgs('id'); jump($this->gourl); } else { // 没有绑定过,新注册或者绑定已注册账号 // 默认到注册页面 jump("/"); } }
登录后可以留言提问!
微信扫码登录