Files
mier-php/application/api/wxapi/Wechatlogin.php
2025-08-11 10:22:05 +08:00

301 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
include "request/Object.class.php";
include "WxClient.class.php";
include "request/GetWechatCodeRequest.class.php";
include "request/AccessTokenCodeRequest.class.php";
include "request/UserInfoRequest.class.php";
session_start();
class Wxchatlogin extends Base{
public function getWxUserInfo(){
$WxClient =new WxClient();
$WxClient->appID='wxb5a15da3a637ba28';
$WxClient->appsecret='4253603410d62d7942ffbeab5fc7d05c';
if(!isset($_GET['code'])){
$req = new GetWechatCodeRequest();
$req->setAppId($WxClient->appID);
$req->setScope(GetWechatCodeRequest::SNSAPI_USERINFO);
$url=$req->run();
header('location:'.$url);
return ;
}else{
$req = new AccessTokenCodeRequest();
$req->setAppId($WxClient->appID);
$req->setSecret($WxClient->appsecret);
$req->setCode($_GET['code']);
$resp=$WxClient->execute($req);
}
if(isset($resp->errcode)){
$host=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
header('Location:'.$host);
exit;
}
$UserInfoRequest = new UserInfoRequest();
$UserInfoRequest->setOpenid($resp->openid);
$UserInfoRequest->setAccessToken($resp->access_token);
$UserInfo=$WxClient->execute($UserInfoRequest);
$wx_user_info['open_id'] =$_SESSION['open_id']= $UserInfo->openid;
$wx_user_info['headimgurl'] = $UserInfo->headimgurl;
$wx_user_info['sex'] = $UserInfo->sex;
$wx_user_info['nickname'] = $UserInfo->nickname;
//$wx_user_info['parentId'] = isset($_GET['uid'])?$_GET['uid']:0;
$host=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
//$_GET['openid']='ocNXe1QOCfC1d6rc7gcIDevCdVYa';
//$wechat=file_get_contents($host.'/index.php/user/getwechat?open_id='.$_GET['openid']);
//$wx_user_info='{"parentId":"10","open_id":"ocNXe1QOCfC1d6rc7gcIDevCdVYa","wechat_name":"\u5434\u6d0b","sex":"1","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/Q68wBPJfsuicsKavRpWaluyNRZaGwfHIZtgp3ztK5Mt4pjVwt67p9EGobmwfxvMSR1aF46TFKAPCQOPTibcLGJjQM6cejLicwlz\/"}';
/*$wx_user_info['open_id'] =$_SESSION['open_id']='ocNXe1QOCfC1d6rc7gcIDevCdVYae';
$wx_user_info['headimgurl'] = 'http://www.baidu.com';
$wx_user_info['sex'] = '1';
$wx_user_info['nickname'] = 'eeeee';
$wx_user_info['parentId'] = 37;*/
$wechat=$this->post_curl($host.'/index.php/user/wechat',$wx_user_info);
$wechat=json_decode($wechat,true);
return $wechat;
}
public function post_curl($url, $postFields = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//https 请求
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if (is_array($postFields) && 0 < count($postFields))
{
$postBodyString = "";
$postMultipart = false;
foreach ($postFields as $k => $v)
{
if("@" != substr($v, 0, 1))//判断是不是文件上传
{
$postBodyString .= "$k=" . urlencode($v) . "&";
}
else//文件上传用multipart/form-data否则用www-form-urlencoded
{
$postMultipart = true;
}
}
unset($k, $v);
curl_setopt($ch, CURLOPT_POST, true);
if ($postMultipart)
{
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
else
{
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
}
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
$reponse = curl_exec($ch);
if (curl_errno($ch))
{
throw new \Exception(curl_error($ch),0);
}
else
{
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode)
{
throw new \Exception($reponse,$httpStatusCode);
}
}
curl_close($ch);
return $reponse;
}
public function get_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //以文件流的形式返回,不要直接输出
//curl_setopt($ch, CURLOPT_FAILONERROR, false);
if ($this->readTimeout) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
}
if ($this->connectTimeout) {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
}
//https 请求
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$reponse = curl_exec($ch);
if (curl_errno($ch))
{
throw new \Exception(curl_error($ch),0);
}
else
{
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode)
{
throw new \Exception($reponse,$httpStatusCode);
}
}
curl_close($ch);
return $reponse;
}
}
$url=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
$Wxchatlogin = new Wxchatlogin();
$act = isset($_GET['act'])?$_GET['act']:'default';
//$_SESSION['code']=10010;
//echo $_SESSION['code'];
if($act=='default'){
// $_SESSION['user']=65442;
//header('location:'.$url);
$wechat=$Wxchatlogin->getWxUserInfo();
if($wechat['phone']){
$user=file_get_contents($url.'/index.php/user/getusername?phone='.$wechat['phone']);
if($user){
$_SESSION['user']=$wechat['phone'];
header('location:'.$url);
return ;
}
}
if($wechat['parentId']){
$_GET['uid']=$wechat['parentId'];
}
}else{
$user_info=file_get_contents($url.'/index.php/user/getuid?uid='.$_GET['uid']);
if($user_info=='0'){
$arr['code']='111';
$arr['mess']='推荐人不存在';
echo json_encode($arr);
return ;
}
$wx_user_info['open_id'] =$_SESSION['open_id'];
$wx_user_info['parentId'] = $_GET['uid'];
$Wxchatlogin->post_curl($url.'/index.php/user/wechat',$wx_user_info);
$code=$_GET['QRcode'];
if($_SESSION['code']==$code){
//if(1){
$_SESSION['user']=$_GET['phone'];
$wechat=$Wxchatlogin->get_curl($url.'/index.php/user/re/?username='.$_SESSION['user'].'&open_id='.$_SESSION['open_id']);
//$wechat=json_decode($wechat,true);
echo $wechat;
exit;
//$user1=file_get_contents('http://yule.360sikao.cn/index.php/user/re/?username='.$_SESSION['user']);
}else{
$arr['code']='110';
echo json_encode($arr);
return ;
}
}
$_GET['uid']=$_GET['uid']?$_GET['uid']:'123456';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title></title>
<script src="../js/initparam.js" type="text/javascript"></script>
</head>
<body ng-app="myApp" class="light-bg" style="background:#fff">
<div class="login_content" style="padding-bottom: 30px;">
<!-- <div class="close_login_btn" ng-click="closeUserLogin('false')"></div> -->
<div class="login_logo">
<h3>请绑定您的手机号</h3>
</div>
<div class="login_body">
<div class="login_phone_input" style="display:<?php if($wechat['parentId']){echo 'none';}else{echo 'block';}?>;">
<input type="text" id="meUid" value="<?php echo isset($_GET['uid'])?$_GET['uid']:'123456';?>" style="float: left;clear: right;font-size: 2.5rem;border: none;padding: 0;margin: 0;" onfocus="if(placeholder=='请输入推荐人ID') {placeholder=''}" onblur="if (value==''&amp;&amp;placeholder=='') {placeholder='请输入推荐人ID'}" placeholder="请输入推荐人ID" required="" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required">
</div>
<div class="login_phone_input">
<input type="number" id="mePhoneNum" ng-model="input.phone" style="display: block;float: left;clear: right;font-size: 2.5rem;border: none;padding: 0;margin: 0;" onfocus="if(placeholder=='请输入您的手机号') {placeholder=''}" onblur="if (value==''&amp;&amp;placeholder=='') {placeholder='请输入您的手机号'}" placeholder="请输入您的手机号" required="" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required">
</div>
<div class="changgePhoneNum_header_second login_phone_input">
<div id="inputbox">
<input type="number" id="yzhm" ng-model="input.QRcode" style="display: block;float: left;clear: right;font-size: 2.5rem;border: none;padding: 0;margin: 0;" onfocus="if(placeholder=='请输入验证码') {placeholder=''}" onblur="if (value==''&amp;&amp;placeholder=='') {placeholder='请输入验证码'}" placeholder="请输入验证码" required="" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required">
</div>
<div ng-class="{true: 'changgePhoneNum_identify', false: 'changgePhoneNum_identify_gray'}[isActive]" id="yzm" ng-click="sendVerfiCode('userLogin')" class="ng-binding changgePhoneNum_identify">获取验证码</div>
</div>
<div class="lang" style="padding:0">
<div class="lang_btn lang_btn_bg_color_red lang_btn_color_white" id="userlogin" >
登录
</div>
</div>
</div>
</div>
</body>
<script src="../js/jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#userlogin").click(function(){
var phone = document.getElementById("mePhoneNum").value;
var QRcode = document.getElementById("yzhm").value;
var meUid = document.getElementById("meUid").value;
if (meUid == "") {
alert("请输入推荐人ID");
return
}
if (phone == "") {
alert("请填写手机号码");
return
}
if (phone.length != 11) {
alert("手机号码必须为11位");
return
}
if (QRcode == "") {
alert("请输入验证码");
return
}
$.get('/wxapi/Wechatlogin.php?act=LoginReg&phone='+phone+"&uid="+meUid+"&QRcode="+QRcode,function(res){
//alert(res);
var ress=$.parseJSON(res);
if(ress.code=="110"){
alert("验证码不正确");
}else if(ress.code=="111"){
alert(ress.mess);
}else{
localStorage.setItem("phone",phone);
localStorage.setItem("sessionObj",'{"uflag":"Z0GG3x4qRW2bK1JhAk7ssg==","sessionId":"/wF9RsnXG1GL6BzaWDvLttbeIPrYmqSebZEH3DjpHsuIULvJ8vM6AcowLeAr5yUOiFC7yfLzOgHKMC3gK+clDg=="}');
window.location.href='<?php echo $url;?>';
}
});
return false;
});
});
</script>
<script src="../js/index.js"></script>
</html>