1. 程式人生 > >微信開發之投票功能

微信開發之投票功能

此次學習通過條件來限制投票次數,當投票後,參與投票的微信使用者的openid存入資料庫中。

投票規則  通過時間戳來計算,以此來生成where條件

//投票規則
	private function buildwhere($type,$openid,$groupid){
		//查詢條件
		$where = array();

		$y = date("Y");
		$m = date("m");
		$d = date("d");

		//將今天開始的年月日時分秒,轉換成unix時間戳(開始示例:2018-4-28 00:00:00)
        $todayStart = mktime(0,0,0,$m,$d,$y);
        $todayEnd = mktime(23,59,59,$m,$d,$y);

        switch ($type){
          case 1:
            //每個小組僅能投一次
            $where['openid'] = $openid;
            $where['groupid'] = $groupid;
            break;

          case 2:
            //僅能投一次
            $where['openid'] = $openid;
            break;

          case 3:
            //給每個組一天僅能投一次
          $where['openid'] = $openid;
          $where['groupid'] = $groupid;
          $where['vote_time'] = array('between',"$todayStart,$todayEnd");
            break;
          
          case 4:
            //每天僅能投一次
          $where['openid'] = $openid;
          $where['vote_time'] = array('between',"$todayStart,$todayEnd");
          break;
          default;

             break;

        }
        return $where;
	}
}

groupid 小組的編號  通過投票規則來限制投的次數

	public function save_vote($groupid){

		$openid = "";

		$where = $this->buildwhere(1,$openid,$groupid);

		$data = M('vote')->where($where)->find();

		if(empty($data)){
			M('vote')->add(array('openid'=>$openid,'groupid'=>$groupid,'vote_time'=>time()));
			$this->ajaxReturn(array('errcode'=>0,'msg'=>'投票成功,謝謝參與'));
		}else{
			$this->ajaxReturn(array('errcode'=>1,'msg'=>'您已經投過票,每個小組僅能投一次'));
		}

	}