数据表设计
DROP TABLE IF EXISTS `hcwt_members`; CREATE TABLE `hcwt_members` ( `id` int(11) NOT NULL AUTO_INCREMENT, `openid` varchar(50) DEFAULT NULL, `name` varchar(100) DEFAULT NULL, `gold` int(10) DEFAULT NULL, `imgtime` bigint(13 ) DEFAULT NULL, PRIMARY KEY (`id`), KEY `openid` (`openid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
主页面代码
<?php
session_start();
include 'hcWeChat/hcWeChat.php';
$hcWeChat = new hcWeChat();
//检查登录
if(empty($_SESSION['openid'])){
$backUrl = 'http://wx.hcoder.net/wxlogin.php';
$hcWeChat->wxLogin($backUrl);
}
//查询用户数据是否存在
$m = hcm('members');
$user = $m->where('openid = ?', array($_SESSION['openid']))->fetch();
//如果没有用户查询并添加用户
if(empty($user)){
$user = $hcWeChat->getUser($_SESSION['openid']);
$userAdd = array(
'openid' => $_SESSION['openid'],
'name' => $user['nickname'],
'gold' => 0,
'imgtime'=> 0
);
$uid = $m->add($userAdd);
//查询用户
$user = $m->where('id = ?', array($uid))->fetch();
}
//检查二维码有效期
$cTime = time();
//如果超时生成新的二维码
if($cTime >= $user['imgtime'] + 2592000){
$hcWeChat->makeQrcode($user['id'], 'qrcode/'.$user['id']);
//更新二维码生成时间
$m->where('id = ?', array($user['id']))->update(array('imgtime' => time()));
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>邀请关注获取奖励</title>
<style>
*{margin:0; padding:0; font-size:16px; color:#000000; outline:none; -webkit-text-size-adjust:100%; -webkit-font-smoothing:antialiased; -moz-osx-font-smoothing:grayscale; font-style:normal; -webkit-tap-highlight-color:transparent;}
div{overflow:hidden;}
::-webkit-scrollbar{display:none;}
a{color:#000000; text-decoration:none; -webkit-tap-highlight-color:rgba(200,200,200,0.2);}
a:active{color:#000000;}
img{border:none;}
ul{list-style-type:none;}
body{background:#3DB6CD; -webkit-user-select:none; -moz-user-select:none;}
</style>
</head>
<body>
<div style="padding-top:58px;">
<div style="width:188px; margin:0 auto;">
<img src="qrcode/<?php echo $user['id'];?>.png" width="100%" />
</div>
<div style="padding:15px; text-align:center; color:#FFFFFF;">
将二维码分享给好友获取奖励!
</div>
</div>
</body>
<?php
//分享配置准备
$jsConfig = $hcWeChat->getJsTicket();
?>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
wx.config({
debug: false,
appId: '<?php echo $hcWeChat->appID;?>', // 必填,公众号的唯一标识
timestamp: <?php echo $jsConfig["timestamp"];?>,
nonceStr: '<?php echo $jsConfig["nonceStr"];?>',
signature: '<?php echo $jsConfig["signature"];?>',
jsApiList: [
'downloadVoice', 'onMenuShareTimeline', 'onMenuShareAppMessage',
'onMenuShareQQ', 'onMenuShareQZone'
]
});
<?php
$title = '关注获取奖励';
$shareImg = 'http://wx.hcoder.net/demo/imgs/share.jpg';
?>
wx.ready(function(){
wx.onMenuShareTimeline({
title: '<?php echo $title;?>',
link: 'http://wx.hcoder.net/share.php?id=<?php echo $user['id'];?>', // 分享链接
imgUrl: '<?php echo $shareImg;?>', // 分享图标
success: function(){},
cancel: function(){}
});
wx.onMenuShareAppMessage({
title: '<?php echo $title;?>',
desc: 'power by hcoder.net',
link: 'http://wx.hcoder.net/share.php?id=<?php echo $user['id'];?>', // 分享链接
imgUrl: '<?php echo $shareImg;?>', // 分享图标
success: function(){},
cancel: function(){}
});
});
</script>
</html>
登录返回页面代码
<?php
/*
* 用户同意授权后返回页面
* 功能:获取用户和openid并返回
*/
session_start();
if(empty($_SESSION['state']) || $_SESSION['state'] != $_GET['state']){exit('登录数据错误');}
if(empty($_GET['code'])){exit('登录数据错误');}
include 'hcWeChat/hcWeChat.php';
$hcWeChat = new hcWeChat();
$hcWeChat->wxLoginBack();
header('location:./erweima.php');
分享页面代码
<?php
if(empty($_GET['id'])){exit();}
include 'hcWeChat/hcWeChat.php';
$hcWeChat = new hcWeChat();
//查询用户数据是否存在
$m = hcm('members');
$user = $m->where('id = ?', array($_GET['id']))->fetch();
if(empty($user)){exit();}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>邀请关注获取奖励</title>
<style>
*{margin:0; padding:0; font-size:16px; color:#000000; outline:none; -webkit-text-size-adjust:100%; -webkit-font-smoothing:antialiased; -moz-osx-font-smoothing:grayscale; font-style:normal; -webkit-tap-highlight-color:transparent;}
div{overflow:hidden;}
::-webkit-scrollbar{display:none;}
a{color:#000000; text-decoration:none; -webkit-tap-highlight-color:rgba(200,200,200,0.2);}
a:active{color:#000000;}
img{border:none;}
ul{list-style-type:none;}
body{background:#3DB6CD; -webkit-user-select:none; -moz-user-select:none;}
</style>
</head>
<body>
<div style="padding-top:58px;">
<div style="width:188px; margin:0 auto;">
<img src="qrcode/<?php echo $user['id'];?>.png" width="100%" />
</div>
<div style="padding:15px; text-align:center; color:#FFFFFF;">
将二维码分享给好友获取奖励!
</div>
</div>
</body>
<?php
//分享配置准备
$jsConfig = $hcWeChat->getJsTicket();
?>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
wx.config({
debug: false,
appId: '<?php echo $hcWeChat->appID;?>', // 必填,公众号的唯一标识
timestamp: <?php echo $jsConfig["timestamp"];?>,
nonceStr: '<?php echo $jsConfig["nonceStr"];?>',
signature: '<?php echo $jsConfig["signature"];?>',
jsApiList: [
'downloadVoice', 'onMenuShareTimeline', 'onMenuShareAppMessage',
'onMenuShareQQ', 'onMenuShareQZone'
]
});
<?php
$title = '关注获取奖励';
$shareImg = 'http://wx.hcoder.net/demo/imgs/share.jpg';
?>
wx.ready(function(){
wx.onMenuShareTimeline({
title: '<?php echo $title;?>',
link: 'http://wx.hcoder.net/share.php?id=<?php echo $user['id'];?>', // 分享链接
imgUrl: '<?php echo $shareImg;?>', // 分享图标
success: function(){},
cancel: function(){}
});
wx.onMenuShareAppMessage({
title: '<?php echo $title;?>',
desc: 'power by hcoder.net',
link: 'http://wx.hcoder.net/share.php?id=<?php echo $user['id'];?>', // 分享链接
imgUrl: '<?php echo $shareImg;?>', // 分享图标
success: function(){},
cancel: function(){}
});
});
</script>
</html>
用户关注奖励代码
if($hcWeChat->msgType == 'event'){
switch($hcWeChat->event){
//关注
case 'subscribe':
//查询用户是否存在
$m = hcm('members');
$user = $m->where('openid = ?', array($hcWeChat->openId))->fetch();
//如果没有用户查询并添加用户
if(empty($user)){
$user = $hcWeChat->getUser($hcWeChat->openId);
$userAdd = array(
'openid' => $hcWeChat->openId,
'name' => $user['nickname'],
'gold' => 0,
'imgtime'=> 0
);
$uid = $m->add($userAdd);
//查询用户
$user = $m->where('id = ?', array($uid))->fetch();
//查询新用户是否被他人推荐
if(!empty($hcWeChat->msg->EventKey)){
//查询推荐人qrscene_8
$hcWeChat->msg->EventKey = str_replace('qrscene_', '', $hcWeChat->msg->EventKey);
$tjUser = $m->where('id = ?', array($hcWeChat->msg->EventKey))->fetch();
if(!empty($tjUser)){
//进行奖励
$uper = array(
'gold' => $tjUser['gold'] + 100
);
$m->where('id = ?', array($hcWeChat->msg->EventKey))->update($uper);
}
}
}
$hcWeChat->reTextMsg('您好,感谢关注 ^_^');
break;
}
}