PHP實現簽到功能的方法

來源:互聯網
上載者:User
這篇文章主要介紹了PHP實現簽到功能的方法,執行個體分析了mysql資料表的構造及thinkPHP簽到功能的具體實現技巧,需要的朋友可以參考下

具體如下:

資料表:

CREATE TABLE `members_sign` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `uid` int(11) unsigned NOT NULL COMMENT '使用者id', `days` tinyint(2) unsigned NOT NULL DEFAULT '0' COMMENT '連續簽到的天數', `is_share` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否分享過', `is_sign` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否簽到過', `stime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '簽到的時間', `atime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '添加時間', PRIMARY KEY (`id`), KEY `index_uid` (`uid`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8 COMMENT='簽到分享表';

Controller:

<?phpnamespace Member\Controller;use Member\Controller\MController;class IndexController extends MController {  /**  * 使用者中心  * @param  */  public function index(){    $pre = C('DB_PREFIX');        // 行事曆清單    $monthSign = $this->getMonthSign();    $dayList = $this->showDays($monthSign);    // 今天簽到    $data = $this->todayData();    if($data['is_sign'] == 1){      $this->assign('isSign',true);    }    $this->display();  }  /**  * 執行當天簽到  * @return json 簽到成功返回 {status:1,info:'已簽到'}  */  public function sign(){    $todayData = $this->todayData();    if($todayData['is_sign'] == 1){      $this->successMsg('已簽到');    }else{      $data = $this->getInsertData($this->uid);      // 無今天資料      if($todayData == NULL){        $data['uid'] = $this->uid;        $data['atime'] = time();        $id = M('members_sign')->add($data);      }else{        $save = M('members_sign')->where("id = {$todayData['id']}")->save($data);      }      if($id or $save){        $score = $this->getTodayScores($data['days']);        // 為該使用者添加積分        addScore($this->uid,$score);        $this->successMsg('已簽到',array('score' => $score,'days'=>$data['days']));      }else{        $this->errorMsg('簽到失敗,請重新整理後重試!');      }    }  }  /**  * 返回每次簽到要插入的資料  *  * @param int $uid 使用者id  * @return array(  *  'days'   =>  '天數',  *  'is_sign'  =>  '是否簽到,用1表示已經簽到',  *  'stime'   =>  '簽到時間',  * );  */  protected function getInsertData($uid){    // 昨天的連續簽到天數    $start_time = strtotime(date('Y-m-d 0:0:0',time()-86400))-1;    $end_time  = strtotime(date('Y-m-d 23:59:59',time()-86400))+1;    $days = M('members_sign')->where("uid = $uid and atime > $start_time and atime < $end_time")->getField('days');    if($days){      $days++;      if($days > 30){        $days = 1;      }    }else{      $days = 1;    }    return array(      'days'    => $days,      'is_sign'  => 1,      'stime'   => time()    );  }  /**  * 使用者當天簽到的資料  * @return array 簽到資訊 is_sign,stime 等  */  protected function todayData(){    $time = time();    $start_stime  = strtotime(date('Y-m-d 0:0:0',$time))-1;    $end_stime = strtotime(date('Y-m-d 23:59:59',$time))+1;    return M('members_sign')->field('atime',true)->where("uid = {$this->uid} and atime > $start_stime and atime < $end_stime")->find();  }  /**  * 積分規則,返回連續簽到的天數對應的積分  *  * @param int $days 當天應該得的分數  * @return int 積分  */  protected function getTodayScores($days){    if($days == 30){      return 50;    }else if($days > 19){      return 8;    }else if($days > 9){      return 5;    }else{      return 3;    }  }  /**  * 顯示簽到列表  *  * @param array  $signDays 某月簽到的日期 array(1,2,3,4,5,12,13)  * @param int $year    可選,年份  * @param int $month   可選,月份  * @return string 日期列表<li>1</li>....  */  protected function showDays($signDays,$year,$month){    $time = time();    $year = $year ? $year : date('Y',$time);    $month = $month ? $month : date('m',$time);    $daysTotal = date('t', mktime(0, 0, 0, $month, 1, $year));    $now = date('Y-m-d',$time);    $str = '';    for ($j = 1; $j <= $daysTotal; $j++) {      $i++;      $someDay = date('Y-m-d',strtotime("$year-$month-$j"));      // 小於今天的日期樣式      if ($someDay <= $now){        // 當天日期樣式 tdc = todayColor        if($someDay == $now){          // 當天簽到過的          if(in_array($j,$signDays)){            $str .= '<li class="current fw tdc">'.$j.'</li>';          }else{            $str .= '<li class="today fw tdc">'.$j.'</li>';          }        }else{          // 簽到過的日期樣式 current bfc = beforeColor , fw = font-weight          if(in_array($j,$signDays)){            $str .= '<li class="current fw bfc">'.$j.'</li>';          }else{            $str .= '<li class="fw bfc">'.$j.'</li>';          }        }      }else{        $str .= '<li>'.$j.'</li>';      }    }    return $str;  }  /**  * 擷取當月簽到的天數,與 $this->showDays() 配合使用  * @return 當月簽到日期 array(1,2,3,4,5,12,13)  */  protected function getMonthSign(){    $time  = time();    $year  = date('Y',$time);    $month = date('m',$time);    $day  = date("t",strtotime("$year-$month"));    $start_stime  = strtotime("$year-$month-1 0:0:0")-1;    $end_stime = strtotime("$year-$month-$day 23:59:59")+1;    $list = M('members_sign')->where("uid = {$this->uid} and stime > $start_stime and stime < $end_stime")->order('stime asc')->getField('stime',true);    foreach ($list as $key => $value){      $list[$key] = date('j',$value);    }    return $list;  }}

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.