过年了,帝国CMS发红包功能让大家乐呵呵

查看 90|回复 13
作者:美文苑文学网   
过年了,帝国CMS发红包功能让大家乐呵呵。直接上干货。代码走起
[ol]
  • 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  '无效的参数'], 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']  '红包已被领取完', '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;
  • }[/ol]复制代码[ol]
  • /********创建红包的信息表
  • 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;
  • *******创建领取红包的记录表*******************/[/ol]复制代码至于在什么地方使用那取决于你了,小编是用在聊天室的。聊天室前端先生成一个临时的红包ID。待WebSocket双向通讯接收到后端通知在更新为真实的红包ID。(快速响应,异步处理,前端逻辑与后端解耦,分布式架构支持,功能扩展便利)

    红包, 的人

  • hjc404   
    有撒用哦!
    美文苑文学网
    OP
      
    hjc404 发表于 2025-1-14 13:05
    有撒用哦!

    天天嚷着有啥用!你倒是分享点干货出来再说。本不想回复的,看到喷子有点多。难道我不可以发个帖子领取点金币么?
    KKDDCC   
    谢谢老板

    阿战   
    感谢老板
    sldss   

    感谢老板
    雨天榕树   
    收藏  只要是技术流发 都值得收藏起来
    哪怕现在用不到  也值得啊
    Estrella   
    感谢老板
    蓝天有月   
    火钳刘明
    我思故我在   
    美文苑卖了现在又搞啥项目

    您需要登录后才可以回帖 登录 | 立即注册

    返回顶部