基于tp3.2.3用户签到代码

浏览:764 发布日期:2018/04/11 分类:功能实现 关键字: 用户签到 萌新代码 实用案例
项目需要,自己写的用户签到代码,适合萌新使用,大神勿喷!
本示例没有给用户增加虚拟货币,
方法:
/**
*用户签到方法
*$user 签到用户ID
*@return $data String
*/
function sign($user = ''){
$seedlings = 20; //第一次签到赠送树苗数量
$seedling3 = 3; //连续签到天数小于等于3天赠送的树苗数量
$seedling8 = 8; //连续签到天数大于8天赠送的树苗数量
$seedling15 = 12; //连续签到天数大于15天赠送的树苗数量
$seedling30 = 18; //连续签到天数大于30天赠送的树苗数量
$sign = M("signin");//实例化模型
$userSign = $sign -> where(array("user_id" => $user)) -> find();//用户签到数据

$signId = $userSign['id']; //用户在签到表的id
$tabletoday = $userSign['today_signin_time']; //签到表里的今日签到时间 不等效与今日时间
$today = date("Y-m-d",time());//今日签到时间
$lastday = $userSign['last_signin_time']; //上次签到时间

$st_tabletoday = strtotime($tabletoday);//签到表里的今日签到时间 不等效与今日时间
$st_today = strtotime(date("Y-m-d",time()));//序列化今日签到时间
$st_lastday = strtotime($userSign['last_signin_time']); //序列化上次签到时间

empty($tabletoday) ? $tabletoday = date("Y-m-d",strtotime("-1 day")) : $tabletoday = $tabletoday; //如果为空则是第一次签到,默认上一次签到为昨天
if(!$userSign){//首次签到
$signArr = array(
'user_id' => $user,
'last_signin_time' => $tabletoday,
'today_signin_time' => $today,
'continue_day' => 1,
'seedlings' => $seedlings //第一次签到增送树苗数量
);
$res = $sign -> add($signArr);
if($res){
$data = "首次签到获得了<i class='layui-icon'></i> ".$seedlings."奖励";
}
}else if(($st_today - $st_tabletoday) / 86400 == 1){//(今日签到时间-上次签到时间) / 86400 == 1;则为连续签到
if($userSign['continue_day'] <= 3){
$seed = $seedling3;
}else if($userSign['continue_day'] > 8){
$seed = $seedling8;
}else if($userSign['continue_day'] > 15){
$seed = $seedling15;
}else if($userSign['continue_day'] > 30){
$seed = $seedling30;
}
$signArr = array(
'id' => $signId,
'last_signin_time' => $tabletoday,
'today_signin_time' => $today,
'seedlings' => $seed
);
$res = $sign -> save($signArr);
if($res){
$sign -> where(array("id" => $signId)) -> setInc("continue_day");
$data = "签到成功,获得了<i class='layui-icon'></i> ".$seed."奖励";
}
}else if(($st_today - $st_tabletoday) / 86400 > 1){//(今日签到时间-上次签到时间) / 86400 > 1;连续签到中断
if($userSign['continue_day'] <= 3){
$seed = $seedling3;
}else if($userSign['continue_day'] > 8){
$seed = $seedling8;
}else if($userSign['continue_day'] > 15){
$seed = $seedling15;
}else if($userSign['continue_day'] > 30){
$seed = $seedling30;
}
$signArr = array(
'id' => $signId,
'last_signin_time' => $tabletoday,
'today_signin_time' => $today,
'continue_day' => 1,
'seedlings' => $seed
);
$res = $sign -> save($signArr);
if($res){
$data = "签到成功,获得了<i class='layui-icon'></i> ".$seed."奖励,上次签到时间:!".$tabletoday.",本次签到时间:".$today."连续签到中断了。";
}
}else{
$data = "今日已签到!";
}
return $data;
}
数据表:
--
-- 表的结构 `tr_signin`
--

CREATE TABLE IF NOT EXISTS `tr_signin` (
`id` int(255) NOT NULL AUTO_INCREMENT COMMENT 'id',
`user_id` int(255) NOT NULL COMMENT '用户id',
`last_signin_time` varchar(24) NOT NULL COMMENT '上次签到',
`today_signin_time` varchar(24) NOT NULL COMMENT '今日签到',
`continue_day` int(8) NOT NULL COMMENT '连续签到天数',
`seedlings` int(8) NOT NULL COMMENT '奖励树苗',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
评论( 相关
后面还有条评论,点击查看>>