typecho制作打卡签到+积分功能实例教程

每次下载主题,都会扣除积分才能进行下载,因此特意添加了打卡功能,每天只能打卡一次,从而增加积分,当然了,也可以进行充值积分功能,但是这里主要还是说的打卡功能

首先

function socop($uid,$upsum) {
    $db = Typecho_Db::get();
    $cid = $uid;
    if (!array_key_exists('socials', $db->fetchRow($db->select()->from('table.users')))) {
        $db->query('ALTER TABLE `'.$db->getPrefix().'users` ADD `socials` INT(10) DEFAULT 10;');         
        }
    $exist = $db->fetchRow($db->select('socials')->from('table.users')->where('uid = ?', $cid))['socials']; 
         if(!$exist==0){
         $db->query($db->update('table.users')
                ->rows(array('socials' =>(int)$exist+$upsum))
                ->where('uid = ?',$cid));
         $exist = $db->fetchRow($db->select('socials')->from('table.users')->where('uid = ?', $cid))['socials']; 
         } 
}

这里主要是用来控制积分加减,通过会员id去查询,并且加减积分。

02312.png

添加右边的+号的时候,便是打卡签到成功,这里调用的是ajax签到,functions.php里面获取到传递的值后

if ($archive->request->getPathInfo() == "xxxx") {
    _getsoc($archive);
} 

便开始执行函数

function _getsoc($archive){
    $archive->response->setStatus(200);
    $cid=$_POST["gesoc"];
    $gesta=1;
    if(clocktime($cid)){
    socop($cid,1); //打卡+1
    }
    else{ $gesta=0; }
    $gesoc=sooff($cid);
    $getime=get_clocktime($cid);
    $archive->response->throwJson(array(
    "gesoc" => $gesoc,
    "getime" => $getime,
    "gesta" => $gesta
)); 
}

这里我返回了3个参数,分别是会员积分,打卡时间,积分状态(用来判断打卡状态)

然后下面是打卡签到状态的判断函数

/**
* 打卡时间存储 
**/
function clocktime($uid) {
    $db = Typecho_Db::get();
    if (!array_key_exists('clocktime', $db->fetchRow($db->select()->from('table.users')))) {
        $db->query('ALTER TABLE `'.$db->getPrefix().'users` ADD `clocktime` INT(10) DEFAULT NULL;');         
    }         
    $clocktime = $db->fetchRow($db->select('clocktime')->from('table.users')->where('uid = ?', $uid))['clocktime'];        
    $nowtime = time();//当前的打卡时间戳
    $today = strtotime(date("Y-m-d"),time());//获得当日凌晨的时间戳
    if($today>$clocktime){ // 之前的打卡时间已经过了一天  
        $db->query($db->update('table.users')->rows(array('clocktime' =>(int)$nowtime))->where('uid = ?',$uid));
        return true;        
    }
    else{ return false; } // 之前的打卡时间处于当天,返回否
}  

这里关键也是三个参数,一个是通过查询数据库得出上一次的打卡时间,一个是当日凌晨的时间戳,如果上一次的打卡时间大于当日的凌晨时间戳,那就是禁止打卡,因为已经打过卡了,而小于的时候,便是前一天之前打过款,那便可正常打卡

而这里还需获取当前的打卡时间戳,用以进行数据库存放更新,记录当前的打卡时间哦~

那么简单的一个打卡思路实例就完成了,演示的话,登录后查看自己的个人主页吧~

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1186
3 条评论
3.6k

发表评论

已有 3 条评论

  1. zc998800     Win 10 /    Chrome
    2022-05-16 21:08

    :滑稽:小灯泡太棒了,全是干货啊

    1. 2624633638     Win 10 /    Chrome
      2022-11-11 20:53

      @zc998800

      是啊

  2. 若志奕鑫     Win 10 /    Chrome
    2022-03-05 20:41

    :真棒:牛哇

!