對外接口 2個
php
一、獲取投票數json 二、投票sql
<?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 (); } }
//服務類json
<?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 ); } }
//模型類fetch
<?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__ ); } }