直接上代码
后台tp5
php
//判断是否扫码
public function getapiopenid(){
$sence_id = input('sence_id');
$user = db('apitem');
$where['str']=array('like','%'.$sence_id.'%');
$res_istrue = $user->where($where)->find();
$openid = $res_istrue['openid'];
Session::set('openid',$openid);
$data = array(
'data' => true,
'code' => 200,
'msg' => '成功 !',
);
// 修改失败 返回信息
if (!$res_istrue) {
$data = array(
'data' => false,
'code' => 500,
'msg' => '失败 !',
);
}
return $data;
}
// pc端扫码微信会给这个方法推送消息
private function getticket(){
$signature = input('signature');
// $timestamp =input('timestamp');
// $nonce = input('nonce');
// $echoStr = $_GET["echostr"];
// $token = 'kinder';
// $tmpArr = array($token, $timestamp, $nonce);
// sort($tmpArr, SORT_STRING);
// $tmpStr = implode( $tmpArr );
// $tmpStr = sha1( $tmpStr );
//
// if( $tmpStr == $signature ){
// echo $echoStr;
// }
// die;
//$GLOBALS["HTTP_RAW_POST_DATA"]; //这个用不了了;换成下面那个
//$GLOBALS["HTTP_RAW_POST_DATA"]; //这个用不了了;换成下面那个
$postStr = file_get_contents("php://input");
if($postStr){
$postObj = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
$openid = isset($postObj['FromUserName'])?$postObj['FromUserName']:0;;
$str = isset($postObj['EventKey'])?$postObj['EventKey']:0;
$list['openid'] = $openid;
$list['str'] = $str;
// 储存用户信息
$user = db('apitem');
$res_istrue = $user->where(['openid'=>$openid])->find();
if(!$res_istrue){
$user->insert($list);//主表添加
}else{
$user->where(['openid'=>$openid])->update($list);
}
};
return "success";
}
// 获取令牌
private function getactionGetToken(){
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.appid.'&secret='.appsecret;
$res = json_decode($this->actionCurlRequest($url));
return $res->access_token;
}
//curl请求,支持post和get
private function actionCurlRequest($url,$data=null){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
if(!empty($data)){
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
前台
$(document).ready(function(){
setInterval(getLoc,1000);
});
//对接后台查看是否登陆
function getLoc(){
var sence_id = $(".sence_id").html();
if(sence_id != 0){
// ajax 后台读取信息
$.ajax({
type:"get",
dataType:'json',
async:false,
url:'{:url("/index/apiopenid")}',
data:{"sence_id":sence_id},
success:function(dd){
if (dd.code == 200) {
// alert("报名成功");
// 跳转地址
location.href ='{:url("/index/sign")}'
return false;
}else{
console.log("未登录");
}
}
})
}
}
网友评论