如下並非插件原理,而是繼承方法,首先確認需求,簽到的話,我只須要保存 以下信息php
用戶模型設計mysql
<?php class AmupperModel extends MemberModel { private $_amuppler = 'd_amupper'; public function save($params) { return $this->add ( $this->_amuppler, $params ); } public function getUserByWhere($where) { return $this->getOne ( $this->_amuppler, $where ); } /** * 返回時間 * Enter description here ... * @param unknown_type $days */ public function getListByDays($days) { $sql = "SELECT a.uid,a.add_time,b.username,b.real_name,b.face,b.area FROM $this->_amuppler a LEFT JOIN $this->_member_list b ON a.uid=b.uid WHERE a.days='$days'"; return $this->fetchAll ( $sql ); } /** * * @return AmupperModel */ public static function instance() { return parent::_instance ( __CLASS__ ); } }
業務邏輯設計sql
<?php //簽到 class AmupperService { const SUCCESS = 'success'; const ERROR = 'error'; public function addUser($uid) { if (! $this->checkToday ( $uid )) { return "你已經簽到過了哦!"; } $rs = AmupperModel::instance ()->save ( array ('uid' => $uid, 'add_time' => time (), 'days' => date ( "Ymd", time () ) ) ); return $rs > 0 ? self::SUCCESS : self::ERROR; } /** * * @param int $day 1今天 2昨天 * @return Array */ public function getListByDays($day) { switch ($day) { case 1 : $time = date ( "Ymd", time () ); break; case 2 : $time = date ( "Ymd", strtotime ( '-1 day' ) ); break; default : $time = date ( "Ymd", time () ); break; } return AmupperModel::instance ()->getListByDays ( $time ); } /** * 檢查是否已經打過卡 * @return boolean true 沒有 false 已經打過卡 */ public function checkToday($uid) { $rs = AmupperModel::instance ()->getUserByWhere ( array ('days' => date ( "Ymd", time () ), 'uid' => $uid ) ); return empty ( $rs ); } }
控制器設計fetch
<?php class AmupperController extends NodeController { //簽到 public function d() { $rs = self::getAmupperService ()->addUser ( $this->_user_global ['uid'] ); if ($rs == self::SUCEESS) { self::getLogService ()->add ( $this->_user_global ['username'], "簽到成功" ); } else { self::getLogService ()->add ( $this->_user_global ['username'], "簽到失敗$rs" ); } $this->sendNotice ( $rs ); } public function check() { $rs = self::getAmupperService ()->checkToday ( $this->_user_global ['uid'] ); $message = ! $rs ? self::SUCEESS : self::ERROR; $this->sendNotice ( $message ); } /** * 查看列表 * */ public function listing() { $rs = self::getAmupperService ()->getListByDays ( $_GET ['days'] ); $this->view()->assign('user',$rs); $this->view()->display("mysql:amupper.tpl"); } public static function getAmupperService() { return new AmupperService (); } public static function getLogService() { return new LogService (); } }