现在的位置: 首页 > 综合 > 正文

thinkphp 微信抽奖类

2017年11月11日 ⁄ 综合 ⁄ 共 2125字 ⁄ 字号 评论关闭
<?php
class IndexAction extends Action {
	var $textTpl = "<xml>
			<ToUserName><![CDATA[%s]]></ToUserName>
			<FromUserName><![CDATA[%s]]></FromUserName>
			<CreateTime>%s</CreateTime>
			<MsgType><![CDATA[%s]]></MsgType>
			<Content><![CDATA[%s]]></Content>
			<FuncFlag>0</FuncFlag>
			</xml>";

	private function lottery($fromUsername,$toUsername){
		$user=D('wx_user');
		$username=(array)$fromUsername;

		//获取会员ID
		$id=$user->where("openid='$username[0]'")->getField('id');				
		//对lottey操作
		$lottery=D('lottery');
		$level=$lottery->where("wx_id='$id' and activity='活动2号'")->getField('level'); 
		
		//设置中奖概率
		
		$a = array_fill(0,200, 3);
		$b = array_fill(0,400, 2);
		$c = array_fill(0,600, 1);
		$d = array_fill(0,10000, 0);
		
		$arr = array_merge($a, $b, $c,$d);
		$e = mt_rand(0,11200);
		$num= $arr[$e];				

		//获取抽奖次数
		$count=$user->where("openid='$username[0]' ")->getField('lottery_count');
		//只有抽奖次数大于1才能抽奖
		if($count>0){
			//将获奖数统计
			$info=D(lottery_info);
			//配置查询语句
			$condition['name'] = '活动2号';
			$condition['level'] = $num;
			$condition['_logic'] = 'AND';
			//获取中奖次数
			$lottery_num=$info->where($condition)->getField('num');
			$limit=$info->where($condition)->getField('limit');
			//设置限额
			if($lottery_num>=$limit){
				$res_str="很遗憾,您此次没有获奖!每人共有三次抽奖机会,加油哦!";
			}else{
			
			//如果用户获得了较高奖项,则不会获得其他奖项
				if($num<=$level){	
					$res_str="很遗憾,您此次没有获奖!每人共有三次抽奖机会,加油哦!";
				}else{
					if($num==3){										
						$res_str='恭喜您获得一等奖!'.'您的会员ID为'.$id;
					}

					if($num==2){	
						$res_str='恭喜您获得二等奖!'.'您的会员ID为'.$id;					
					}

					if($num==1){
						$res_str='恭喜您获得三等奖!'.'您的会员ID为'.$id;				
					}

					if($num==0){
						//最后一次必中三等奖 可以根据自己的实际情况来更改
						if($count==1){
							$num=1;
							$res_str='恭喜您获得三等奖!'.'您的会员ID为'.$id;
						}esle{
							$res_str="很遗憾,您此次没有获奖!每人共有三次抽奖机会,加油哦!";	
						}
					}
				}																		
			}//较高奖结束

				//使总获奖数获奖数增加1
				$info->where($condition)->setInc('num');
				//用户抽奖次数减少
				$user-> where("openid='$fromUsername[0]'")->setDec('lottery_count');
				//更新用户获奖时间
				$last['mtime']=time();
				$info->where($condition)->save($last);

				//定义获奖用户信息
				$data['level']=$num;
				$data['wx_id']=$id;
				$data['ctime']=time();
				$data['activity']="活动2号";			
		} //次数结束

		if($count==0){
			 $res_str='对不起,您的抽奖次数已经用完。';
		}
		
		//在lottery_info中记录中奖信息
		
		
		if(!empty($level)){
			if($num>=$level){				
				$lottery->where("wx_id='$id'")->save($data);				
			}
		}else{
			if($num>0){
				$lottery->add($data);				
			}
		}
		//输出提示xml语句		
		$msgType="text";
		$textTpl=$this->textTpl;
		$resultStr=sprintf($textTpl,$fromUsername,$toUsername,time(),$msgType,$res_str);
		echo $resultStr;
		
	}

抱歉!评论已关闭.