首页 > 代码库 > 投票代码
投票代码
对外接口 2个
1、获取投票数json 2、投票
<?php class VoteController extends NodeController { /** * 获取队列投票数 全部 *@return JSON */ public function tickets() { $tickets = self::getService ()->getAllTickets (); $this->sendNotice ( "success", $tickets, true ); } /** * 投票 */ public function t() { $rs = self::getService ()->addTickets ( $_POST [‘id‘], $this->_user_global [‘uid‘] ); $this->sendNotice ( $rs, null, FALSE ); } /** * 获取投票服务类 * Enter description here ... */ public static function getService() { return new VoteService (); } }
//服务类
<?php class VoteService { //初始票数 private $_tickets = 1; //每日投票次数 private $_max = 10; //初始化时间 private $_startTime; /** * 每日投票数 */ public function __construct() { $this->_startTime = strtotime ( date ( "Ymd", time () ) ); } /** * 投票 * @param int $id 投票对象 * @param int $uid 投票人id */ public function addTickets($id, $uid) { if (! $uid) { return "请先登录"; } if ($id < 1) { return "投票失败,请选择投票对象"; } //检查是否已经投过票 if ($this->checkVoteTickets ( $uid )) { return "今天的投票数已用完"; } //检查是否已经参赛 没有就新增 有的更新 if ($this->checkUserExist ( $id )) { VoteModel::instance ()->saveTicketsById ( $id ); } else { VoteModel::instance ()->addUser ( array (‘id‘ => $id, ‘tickets‘ => $this->_tickets ) ); } //增加历史记录 $this->addVoteLog ( $uid, $id ); return "success"; } /** * 返回所有票数 * @return id tickets */ public function getAllTickets() { return VoteModel::instance ()->getAllTickets (); } /** * 检查投票次数 * @return true 超过了 false 没有 */ private function checkVoteTickets($uid) { $num = VoteModel::instance ()->countTicketsByTime ( $uid, $this->_startTime ); return $num [‘num‘] > $this->_max ? false : true; } /** * * @param int $uid 投票人id * @param int $gid 对象id */ private function addVoteLog($uid, $gid) { VoteModel::instance ()->addList ( array (‘uid‘ => $uid, ‘gid‘ => $gid, ‘add_time‘ => time () ) ); } /** * 检查用户是否存在 * @param int $id * @return ture 不存在 false 存在 */ private function checkUserExist($id) { $rs = VoteModel::instance ()->getTicketsById ( $id ); return empty ( $rs ); } }
//模型类
<?php /** * * 功能描述 * 不涉及任何展示功能 * @author Administrator * */ class VoteModel extends Db { //投票队列 谁投票 private $_vote_list = ‘w_vote_list‘; //参赛队员 private $_vote_user = ‘w_vote_user‘; public function getAllTickets() { return $this->getAll ( $this->_vote_user ); } public function addList($params) { $this->add ( $this->_vote_list, $params ); return $this->lastInsertId (); } public function saveTicketsById($id) { $sql = "UPDATE $this->_vote_user SET tickets=tickets+1 WHERE id=$id"; return $this->exec ( $sql ); } public function countTicketsByTime($uid, $startTime) { $sql = "SELECT count(id) num FROM $this->_vote_list WHERE add_time>$startTime AND uid=$uid"; return $this->fetch ( $sql ); } public function addUser($params) { $this->add ( $this->_vote_user, $params ); } public function getTicketsById($id) { return $this->getOne ( $this->_vote_user, array (‘id‘ => $id ) ); } /** * 返回VoteModel * @return VoteModel */ public static function instance() { return parent::_instance ( __CLASS__ ); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。