如下并非插件原理,而是继承方法,首先确认需求,签到的话,我只须要保存 以下信息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 (); } }