版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhongyuchuan147/article/details/75228345
首先打开thinkphp公共function.php文件
//获取服务器的access_token
function GetSeverToken(){
// $content=file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_appid').'&secret='.C('WX_appsecret').'');
$url= 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_appid').'&secret='.C('WX_appsecret').'';
// echo $url;
$data=posthttps($url);
$content=explode('access_token":"',$data)[1];
// 获取access_token
$access_token=explode('","',$content)[0];
return $access_token;
}
//请求https
function posthttps($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HEADER, false); //表示需要response header
curl_setopt($curl, CURLOPT_NOBODY, FALSE); //表示需要response body
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
$data = curl_exec($curl);
curl_close($curl);
// var_dump($data);
return $data;
}
//随机字符串
function generate_password( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
$chars ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password ='';
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
以上三个函数为后面的登录注册 做准备
自动登录注册登录函数实
//获取用户信息 并且登录如果没有则注册
function getuserinfo(){
$code=isset($_GET['code'])?$_GET['code']:'0';
if($code){
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('WX_appid').'&secret='.C('WX_appsecret').'&code='.$code.'&grant_type=authorization_code';
$data=posthttps($url);
$access_token=explode('"access_token":"',$data)[1];
$access_token=explode('"',$access_token)[0];
$refresh_token=explode('"refresh_token":"',$data)[1];
$refresh_token=explode('"',$refresh_token)[0];
$openid=explode('"openid":"',$data)[1];
$openid=explode('"',$openid)[0];
$list=getuser($access_token,$openid,$refresh_token);
$json=json_decode($list, true);
// var_dump($json);exit;
// login($data);
return $json;
}else{
return 0;
}
}
自动登录函数封装
//用户自动注册登录
//微信注册登录验证
function login($urls){
$token=cookie('token');
//测试------------------------------------------------------------------
$list=M('user')->where('openid="o_DIR010aYD79IWEJdAQudjw19Bs"')->find();
return $list;exit;
//测试------------------------------------------------------------------
if($token){
// echo 'cookie存在';
$list=M('user')->where('token="'.$token.'"')->find();
if(!$list){
cookie('token',null);
echo '您已经被挤下线了';
exit;
}
return $list;
exit;
}
$arr=getuserinfo();
//判断openid是否存在
if(!$arr['openid']){
if($urls){
header("Location: ".echourl($urls));
}else{
header("Location: ".echourl());
}
exit;
}
$list=M('user')->where('openid="'.$arr['openid'].'"')->find();
if($list['openid']){
$salt=generate_password(4);
$token= MD5($arr['openid'].$salt.microtime());
M('user')->where('openid="'.$arr['openid'].'"')->save(array('token'=>$token));
// echo '已注册';
cookie('token', $token,60*30);
return $list;exit;
}else{
$token= MD5($arr['openid'].$salt.microtime());
$data=array(
'openid'=>$arr['openid'],
'username'=>$arr['nickname'],
'header'=>$arr['headimgurl'],
'token'=>$token
);
$id=M('user')->add($data);
$data['uid']=$id;
// echo '注册成功';
cookie('token', $token,60*30);
return $data;exit;
}
}
$user=login();
var_dump($user);