1. 程式人生 > >destoon框架中的微信自動登陸

destoon框架中的微信自動登陸

tro 自動登陸 where 菜單 返回json module url salt 開發

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框架中的微信自動登陸