zl程序教程

您现在的位置是:首页 >  Java

当前栏目

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

2023-02-18 16:47:29 时间

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

  首先

   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去查询,并且加减积分。

  添加右边的+号的时候,便是打卡签到成功,这里调用的是ajax签到,.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());//获得[当日][5]凌晨的时间戳
        if($today>$clocktime){ // 之前的打卡时间已经过了一天  
            $db->query($db->update('table.users')->rows(array('clocktime' =>(int)$nowtime))->where('uid = ?',$uid));
            return true;        
        }
        else{ return false; } // 之前的打卡时间处于当天,返回否

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

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

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

本文共 411 个字数,平均阅读时长 ≈ 2分钟