PHP实现签到功能

时间:2024-04-17 15:14:13
1、表设计
    1)、主表
        CREATE TABLE `sign` (
          `id` int(11) NOT NULL AUTO_INCREMENT COMMENT \'主键自增\',
          `uid` varchar(50) NOT NULL DEFAULT \'0\' COMMENT \'用户ID 唯一\',
          `sign_count` int(11) NOT NULL DEFAULT \'0\' COMMENT \'连续签到次数\',
          `last_time` int(11) NOT NULL DEFAULT \'0\' COMMENT \'最近一次签到时间\',
          `create_time` int(11) NOT NULL DEFAULT \'0\' COMMENT \'创建时间\',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=\'签到主表\';
    2)、详情表
        CREATE TABLE `sign_detail` (
          `id` int(11) NOT NULL AUTO_INCREMENT COMMENT \'主键自增\',
          `uid` varchar(50) NOT NULL DEFAULT \'0\' COMMENT \'用户ID 唯一\',

`sign_time` int(11) NOT NULL DEFAULT \'0\' COMMENT \'签到时间\', PRIMARY KEY (`id`)
) 
ENGINE
=InnoDB DEFAULT CHARSET=utf8 COMMENT=\'签到详情表\';
//主表
                   $signinfo = Db::table(\'ims_ewei_shop_sign_user\')->where(\'openid\',$openid)->order(\'id\',\'desc\')->find();
if($signinfo)
{
//连续签到的次数

$sign_count = $signinfo[\'sign_count\'];

//最后签到时间
$last_time = date(\'Y-m-d\',$signinfo[\'last_time\']);

$yesterday = date(\'Y-m-d\',strtotime(\'-1 day\')); //昨天的日期格式 2019-12-12
//总的签到天数
$sum = $signinfo [\'sum\'];


if($last_time == date(\'Y-m-d\'))
{ //最后时间等于当前的时间,表示今天已经签过
return json([\'code\'=>200,\'msg\'=>\'已经签过到了!\']);
}else if( $last_time == $yesterday) //用户昨天签到,连续签到次数加1,否者重置为0
{ //最后签到到的时间等于昨天的时间 ,表示是连续签到 连续签到的次数 $sign_count 加1, 最后签到的时间不等于昨天的时间 表示不是连续签到,连续签到的字段 sign_count 重置为0

$sign_count +=1;



}else{

$sign_count = 0;
}

$data = [


\'sign_count\' => $sign_count,
\'sum\' => $sum +=1,
\'last_time\' => time(),
\'signdate\' => time(),

];

$sign_id = Db::table(\'ims_ewei_shop_sign_user\')->where(\'openid\',$openid)->update($data);

if($sign_id)
{
//向详情表入数据
$param =[
\'uniacid\' =>4,
\'time\' => time(),
\'openid\' => $openid,
\'credit\' => $signtime,
\'log\' => \'日常签到+\'.$signtime,
\'type\' => 0,
\'day\' =>0,
];

$detail = Db::table(\'ims_ewei_shop_sign_records\')->insertGetId($param);

if($detail){
//更新用户总的时间表
$membertime = Db::table(\'ims_mc_members_times\')->where(\'uid\',$uid)->find();
if($membertime)
{
$datamember = [

\'time\' => $membertime [\'time\'] + $signtime,
\'update_time\' => time(),
];

$att = Db::table(\'ims_mc_members_times\') ->where(\'uid\',$uid)->update($datamember);

if($att >0)
{
$row = Db ::table(\'ims_ewei_shop_sign_records\')->where(\'openid\',$openid)->order(\'id\',\'desc\')->field(\'time,credit,log,openid\')->find();

$signdate = date(\'Y-m-d\',$row[\'time\']);
$row[\'date\'] = $signdate;

$datas [\'code\'] = 200;

$datas [\'data\'] = $row;



return json($datas);
}
}
}
}

}else{

$data = [

\'uniacid\' => 4,
\'openid\' => $openid,
\'sign_count\' => 1,
\'sum\' => 1,
\'last_time\' => time(),
\'signdate\' => time(),

];

$sign_id = Db::table(\'ims_ewei_shop_sign_user\')->insertGetId($data);

$result = $sign_id;

if($result)
{
//向详情表入数据
$param =[
\'uniacid\' =>4,
\'time\' => time(),
\'openid\' => $openid,
\'credit\' => $signtime,
\'log\' => \'日常签到+\'.$signtime,
\'type\' => 0,
\'day\' =>0,
];

$signDetail = Db::table(\'ims_ewei_shop_sign_records\')->insertGetId($param);

if($signDetail){
//更新用户总的时间表
$membertime = Db::table(\'ims_mc_members_times\')->where(\'uid\',$uid)->find();
if($membertime)
{
$datamember = [

\'time\' => $membertime [\'time\'] + $signtime,
\'update_time\' => time(),
];

$att = Db::table(\'ims_mc_members_times\') ->where(\'uid\',$uid)->update($datamember);

if($att >0)
{
$res = Db ::table(\'ims_ewei_shop_sign_records\')->where(\'openid\',$openid)->order(\'id\',\'desc\')->field(\'time,credit,log,openid\')->find();

$signdate = date(\'Y-m-d\',$res[\'time\']);
$res[\'date\'] = $signdate;

$data [\'code\'] = 200;

$data [\'data\'] = $res;



return json($data);
}
}
}

}else{

return json([\'code\' => 100,\'msg\'=>\'签到失败\']);
}
}