destoon框架中的微信自动登陆
Posted coderj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了destoon框架中的微信自动登陆相关的知识,希望对你有一定的参考价值。
1.必须配置回调函数(在微信开发者管理中)
2.用户授权并获取code
在域名(前面配置的回调域名)根目录下,新建一个文件,命名为oauth.php(名字随便你取,下面的redirect_uri做相应修改即可)
url: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3baef8c48cce7fb3&redirect_uri=http%3a%2f%2fh5.tybxkj.cc%2foauth.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
3.使用code换取access_token
利用get请求函数访问url
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3baef8c48cce7fb3&secret=4ca91b29cf3370b2ad311565344b598a&code=$coded&grant_type=authorization_code
4.通过access_token、openid获取用户信息
url: https://api.weixin.qq.com/sns/userinfo?access_token=$acctke&openid=$appid
5.通过单击菜单带参数(state)判断用户跳转链接
查询数据库中有无注册记录
$pdcz = $db->get_one("select * from destoon_member where destoon_member.oppid=‘$appid‘");
有则直接登陆
$names = $pdcz[‘username‘];
$pasmd = $pdcz[‘password‘].$pdcz[‘passsalt‘];
$do = new member;
$user = $do->login($names,$pasmd,86300*30,true); //调用框架内部登陆
// 重定向
if($user){
header("Location: http://h5.tybxkj.cc/mobile/news/");
}
无则注册并登陆
$do = new member;
$data[‘regid‘] = ‘5‘;
$data[‘company‘] = null;
$data[‘username‘] = $nameu;
$data[‘password‘] = $pass;
$data[‘cpassword‘] = $pass;
$data[‘passport‘] = $nameu;
$data[‘truename‘] = $truenme;
$data[‘groupid‘] = ‘5‘;
$data[‘catids‘] = ‘111‘;
$data[‘catid‘] = ‘2‘;
$data[‘banner‘] = ‘11‘;
$data[‘thumb‘] =‘111‘ ;
$data[‘introduce‘] = ‘22‘;
$data[‘content‘] = ‘333‘;
$data[‘edittime‘] = 0;
$data[‘inviter‘] = ‘1111‘;
$data[‘oppid‘] = $appid;
$data[‘wxhead‘] = $imgtx;
$user = $do->add($data); //注册方法
if($user){
$goa = $do->login($data[‘username‘],$data[‘password‘],86300*30,true);
if($goa){
header("Location: http://h5.tybxkj.cc/mobile/news/"); //重定向
}
}
6.参考函数
//get请求
function curl_get_https($url){
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec($curl); //返回api的json对象
//关闭URL请求
curl_close($curl);
return $tmpInfo; //返回json对象
}
代码部分
<?php
require ‘common.inc.php‘;
$module ="member";
require DT_ROOT.‘/module/‘.$module.‘/common.inc.php‘;
require DT_ROOT.‘/include/post.func.php‘;
require DT_ROOT.‘/module/‘.$module.‘/member.class.php‘;
$coded=$_GET[‘code‘];
$statea=intval($_GET[‘state‘]); //获取到code和state参数
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3baef8c48cce7fb3&secret=4ca91b29cf3370b2ad311565344b598a&code=$coded&grant_type=authorization_code";
$json = curl_get_https($url); //获取access_token和apenid
$arre = json_decode($json,true);
$acctke = $arre[‘access_token‘];
$appid = $arre[‘openid‘]; //opid
$urlto = "https://api.weixin.qq.com/sns/userinfo?access_token=$acctke&openid=$appid"; //获取用户信息
$jsonto = curl_get_https($urlto);
$arreto = json_decode($jsonto,true);
$truenme = $arreto[‘nickname‘]; // 用户昵称
$imgtx = $arreto[‘headimgurl‘]; // 头像
if ($statea===1){
$pdcza = $db->get_one("select * from destoon_member where destoon_member.oppid=‘$appid‘");
if($pdcza){
$namesa = $pdcza[‘username‘];
$pasmda = $pdcza[‘password‘].$pdcza[‘passsalt‘];
$do = new member;
$user = $do->login($namesa,$pasmda,86300*30, true);
// 重定向
header("Location: http://h5.tybxkj.cc/mobile/news/");
}else{
$datac = inParaMeter($appid,$truenme,$imgtx);
$do = new member;
$usera = $do->add($datac);
$goaa = $do->login($datac[‘username‘],$datac[‘password‘],86300*30,true);
header("Location: http://h5.tybxkj.cc/mobile/news/");
}
}
if ($statea===2){
$pdczb = $db->get_one("select * from destoon_member where destoon_member.oppid=‘$appid‘");
if($pdczb){
$namesb = $pdczb[‘username‘];
$pasdfb = $pdczb[‘password‘].$pdczb[‘passsalt‘];
$do = new member;
$user = $do->login($namesb,$pasdfb,86300*30,true);
// 重定向
header("Location: http://h5.tybxkj.cc/mobile/member/map.php");
}else{
$datac = inParaMeter($appid,$truenme,$imgtx);
$do = new member;
$userb = $do->add($datac);
$goab = $do->login($datac[‘username‘],$datac[‘password‘],86300*30,true);
header("Location: http://h5.tybxkj.cc/mobile/member/map.php");
}
}
if ($statea===3){
$pdczc = $db->get_one("select * from destoon_member where destoon_member.oppid=‘$appid‘");
if($pdczc){
$namesc = $pdczc[‘username‘];
$pasdfc = $pdczc[‘password‘].$pdczc[‘passsalt‘];
$do = new member;
$user = $do->login($namesc,$pasdfc,86300*30,true);
// 重定向
header("Location: http://h5.tybxkj.cc/mobile/company/");
}else{
$datac = inParaMeter($appid,$truenme,$imgtx);
$do = new member;
$userc = $do->add($datac);
$goac = $do->login($datac[‘username‘],$datac[‘password‘],86300*30,true);
header("Location: http://h5.tybxkj.cc/mobile/company/");
}
}
//传入参数
function inParaMeter($appid,$truenme,$imgtx){
$wxxas = mt_rand(10000,999999);
$nameu = ‘wx‘.$wxxas; // 随机用户名
$randpas = mt_rand(10000,999999);
$randeps = mt_rand(10000,999999);
$passwordmdf = $randpas.$randeps; // 密码
$do = new member;
$datac = array();
$datac[‘regid‘] = ‘5‘;
$datac[‘company‘] = null;
$datac[‘username‘] = $nameu;
$datac[‘password‘] = $passwordmdf;
$datac[‘cpassword‘] = $passwordmdf;
$datac[‘passport‘] = $nameu;
$datac[‘truename‘] = $truenme;
$datac[‘groupid‘] = ‘5‘;
$datac[‘catids‘] = ‘111‘;
$datac[‘catid‘] = ‘2‘;
$datac[‘banner‘] = ‘11‘;
$datac[‘thumb‘] =‘111‘ ;
$datac[‘introduce‘] = ‘22‘;
$datac[‘content‘] = ‘333‘;
$datac[‘edittime‘] = 0;
$datac[‘inviter‘] = ‘1111‘;
$datac[‘oppid‘] = $appid;
$datac[‘wxhead‘] = $imgtx;
return $datac;
}
//get请求
function curl_get_https($url){
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec($curl); //返回api的json对象
//关闭URL请求
curl_close($curl);
return $tmpInfo; //返回json对象
}
?>
以上是关于destoon框架中的微信自动登陆的主要内容,如果未能解决你的问题,请参考以下文章
微信公众号开发之网页授权认证获取用户的详细信息,实现自动登陆