马上注册,免费下载更多dz插件网资源。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
过年了,帝国CMS发红包功能让大家乐呵呵。直接上干货。代码走起- if($enews=="hongbao"){//红包if($user['zt']=="nologin"){echo json_encode(['msg' => '未登陆不能领取'], JSON_UNESCAPED_UNICODE);exit; }// 获取 POST 数据$userid = (int)$_POST['userid']; // 发红包的人userid$username = RepPostStr($_POST['username']); // 发红包的人$fusername = RepPostStr($_POST['fusername']); // 领红包的人$fuserid = (int)$_POST['fuserid']; // 领红包的人ID$amount = (int)$_POST['amount']; // 红包总金额$recipients = (int)$_POST['recipients']; // 红包个数$hongbao_id = RepPostStr($_POST['hongbaoid']) ? RepPostStr($_POST['hongbaoid']) : uniqid(); // 获取红包标识符// 检查参数有效性if ($amount <= 0 || $recipients <= 0) { echo json_encode(['msg' => '无效的参数'], JSON_UNESCAPED_UNICODE); exit;}// 检查红包是否存在$hongbao_state = $empire->fetch1("select hongbao_id,remaining_points, recipients, total_amount from {$dbtbpre}hongbao_info where hongbao_id ='$hongbao_id'");if (empty($hongbao_state['hongbao_id'])) {// 插入新红包$time=time();//获取当前时间$empire->query("insert into {$dbtbpre}hongbao_info(hongbao_id,sender_userid,sender_username,total_amount,recipients,remaining_points,time) values('$hongbao_id','$userid','$username','$amount','$recipients','$remainingPoints','$time');");$remainingPoints = $amount;}else{$remainingPoints = $hongbao_state['remaining_points']; }// 检查用户是否已经领取过该红包$alreadyClaimed=$empire->gettotal("select count(*) as total from {$dbtbpre}hongbao_history where hongbao_id ='$hongbao_id' and receiver_userid ='$fuserid'");if ($alreadyClaimed > 0) { $info = ['msg' => '您已经领取过了', 'hongbao_id' => $hongbao_id, 'remainingPoints' => $remainingPoints, 'amount' => $amount, 'fusername' => $fusername]; echo json_encode($info, JSON_UNESCAPED_UNICODE); exit;}// 检查红包是否已经被领取超过了指定次数$totalClaimed = $empire->fetch1("select count(*) as total_claimed from {$dbtbpre}hongbao_history where hongbao_id ='$hongbao_id'");if ($totalClaimed['total_claimed'] >= $recipients) {$info = ['msg' => '红包已被领取完', 'hongbao_id' => $hongbao_id, 'remainingPoints' => $remainingPoints, 'amount' => $amount, 'fusername' => $fusername,'title' =>'红包次数检查'];echo json_encode($info, JSON_UNESCAPED_UNICODE);exit;}// 计算随机分配的积分$minPoints = 1; // 最小积分$maxPoints = min($remainingPoints, ceil($amount / $recipients)); // 最大积分$randomPoints = rand($minPoints, $maxPoints);// 如果这是最后一个领取者,将剩余的积分全部分配给该用户if ($recipients - $totalClaimed['total_claimed'] == 1) {$randomPoints =$remainingPoints;}// 更新剩余积分$newRemainingPoints = $remainingPoints - $randomPoints;// 更新红包状态$sql = $empire->query("update {$dbtbpre}hongbao_info set remaining_points =$newRemainingPoints where hongbao_id ='$hongbao_id'"); // 插入领取记录$time=time();//获取当前时间// 检查剩余积分是否为0$updatedHongbaoState = $empire->fetch1("select remaining_points from {$dbtbpre}hongbao_info where hongbao_id = '$hongbao_id'");if ((int)$updatedHongbaoState['remaining_points'] <0 ||(int)$randomPoints<=0) {$info = ['msg' => '红包已被领取完', 'hongbao_id' => $hongbao_id, 'remainingPoints' => (int)$updatedHongbaoState['remaining_points'], 'amount' => $amount, 'fusername' => $fusername,'title' =>'点数金额检查'];echo json_encode($info, JSON_UNESCAPED_UNICODE);exit;} else { $empire->query("insert into {$dbtbpre}hongbao_history(hongbao_id,receiver_userid,receiver_username,userid,username,points,total_amount,recipients,ip,duankou,time) values('$hongbao_id','$fuserid','$fusername','$userid','$username','$randomPoints','$amount','$recipients','$ip','$duankou','$time');"); // 更新用户积分$sql = $empire->query("update {$dbtbpre}enewsmember set userfen = userfen - $randomPoints where userid = '$userid'"); // 扣除打赏者积分$sql1 = $empire->query("update {$dbtbpre}enewsmember set userfen = userfen + $randomPoints where userid = '$fuserid'"); // 领取红包增加积分$info = ['msg' => '获得 ' . $randomPoints . ' 元', 'hongbao_id' => $hongbao_id, 'remainingPoints' =>(int)$randomPoints, 'amount' => $amount, 'fusername' => $fusername];echo json_encode($info, JSON_UNESCAPED_UNICODE);exit;}}if($enews=="chahongbao"){//红包查询$hongbao_id = RepPostStr($_POST['hongbaoid']); // 获取红包标识符$sql=$empire->query("select * from {$dbtbpre}hongbao_history where hongbao_id='$hongbao_id' order by id");while($r=$empire->fetch($sql)){$memberrr = fetch_memcache0("select userpic from {$dbtbpre}enewsmemberadd where userid='$r[receiver_userid]' order by userid desc limit 1", 'Memcached', 'chatpublic', 3600*24*1); $memberrr = $memberrr[0]; $receiveruserpic=$memberrr['userpic']?$memberrr['userpic']:'/e/extend/pcpinglun/assets/nouserpic.gif';$receiveruserpic=$public_r['add_pcurl'].$receiveruserpic; $date[]= array( 'receiver_username'=>$r['receiver_username'], 'receiver_userid'=>$r['receiver_userid'], 'username'=>$r['username'], 'totalamount'=>$r['total_amount'], 'recipients'=>$r['recipients'], 'userid'=>$r['userid'], 'points'=>$r['points'], 'ip'=>$r['ip'], 'hongbao_id'=>$hongbao_id, 'time'=>date('Y-m-d H:i:s',$r['time']), 'headerimg'=>$userpic, 'receiver_headerimg'=>$receiveruserpic, );}if (empty($date)) {$response = ['code' => 400,'msg' => '还没有人领取红包','hongbao_id' => $hongbao_id];}else{$response = ['code' => 200,'data' => $date];}echo json_encode($response);exit; }if($enews=="sendredbaojilu"){//全网红包记录$num=(int)$_POST['num']?(int)$_POST['num']:'15';//前端传来的请求多少条$value=fetch_memcache0("select * from {$dbtbpre}hongbao_history order by id desc limit $num",'Memcached','hongbaohistoryall',3600);$jiangall=fetch_memcache0("select count(*) as total from {$dbtbpre}hongbao_history",'Memcached','listjiang',3600);$hongbaohistoryall=$jiangall[0]['total'];//红包领取总数$jiangallm=fetch_memcache0("select count(*) as total from {$dbtbpre}hongbao_info",'Memcached','listjiang',3600);$hongbaoall=$jiangallm[0]['total'];//红包发放总数foreach ($value as $r){$date[]= array('hongbao_id'=>$r['hongbao_id'],'receiver_userid'=>$r['receiver_userid'],'receiver_username'=>$r['receiver_username'],'userid'=>$r['userid'],'username'=>$r['username'],'points'=>$r['points'],'total_amount'=>$r['total_amount'],'ip'=>$r['ip'],'ipcity'=>citychatip($r['ip']),'time'=>date('Y-m-d H:i:s',$r['time']),'recipients'=>$r['recipients'],);}$response = ['code' => 200,'hongbaohistoryall'=>$hongbaohistoryall,//红包领取总数'hongbaoall'=>$hongbaoall,//红包发放总数'data'=> $date];$data = json_encode($response);echo $data;exit;}
复制代码- /********创建红包的信息表CREATE TABLE `[!db.pre!]hongbao_info` (`id` INT(11) NOT NULL AUTO_INCREMENT,`hongbao_id` VARCHAR(32) NOT NULL,`sender_userid` INT(11) NOT NULL,`sender_username` VARCHAR(255) NOT NULL,`total_amount` DECIMAL(10,2) NOT NULL,`recipients` INT(11) NOT NULL,`remaining_points` DECIMAL(10,2) DEFAULT 0.00,`time` int(11) NOT NULL COMMENT '时间',PRIMARY KEY (`id`),UNIQUE KEY `hongbao_id` (`hongbao_id`),KEY `time` (`time`),KEY `sender_userid` (`sender_userid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;*******创建红包的信息表*******************//********创建领取红包的记录表CREATE TABLE `[!db.pre!]hongbao_history` (`id` INT(11) NOT NULL AUTO_INCREMENT,`hongbao_id` VARCHAR(32) NOT NULL,`receiver_userid` INT(11) NOT NULL,`receiver_username` VARCHAR(255) NOT NULL,`userid` INT(11) NOT NULL,`username` VARCHAR(255) NOT NULL,`points` DECIMAL(10,2) NOT NULL,`total_amount` DECIMAL(10,2) NOT NULL,`recipients` INT(11) NOT NULL,`ip` VARCHAR(32) NOT NULL,`duankou` INT(11) NOT NULL,`time` int(11) NOT NULL COMMENT '时间', PRIMARY KEY (`id`),KEY `hongbao_id` (`hongbao_id`),KEY `time` (`time`),KEY `receiver_userid` (`receiver_userid`),KEY `userid` (`userid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;*******创建领取红包的记录表*******************/
复制代码 至于在什么地方使用那取决于你了,小编是用在聊天室的。聊天室前端先生成一个临时的红包ID。待WebSocket双向通讯接收到后端通知在更新为真实的红包ID。(快速响应,异步处理,前端逻辑与后端解耦,分布式架构支持,功能扩展便利)
©DZ插件网所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。 网站部分内容来源于网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,得到更好的正版服务。 您在本站任何的赞助购买、下载、查阅、回复等行为等均表示接受并同意签订《DZ插件网免责声明协议》。 如有侵权请邮件与我们联系处理: discuzaddons@vip.qq.com 并出示相关证明以便删除。敬请谅解!
|