瀏覽代碼

聊天框送礼物

lizhen_gitee 1 年之前
父節點
當前提交
a28451e6e5
共有 2 個文件被更改,包括 108 次插入42 次删除
  1. 107 41
      application/api/controller/Gift.php
  2. 1 1
      application/api/controller/Party.php

+ 107 - 41
application/api/controller/Gift.php

@@ -279,8 +279,9 @@ class Gift extends Api
         $user_id = input('user_id');// 赠送对象
         $user_id = input('user_id');// 赠送对象
         $gift_id = input('gift_id');// 礼物ID
         $gift_id = input('gift_id');// 礼物ID
         $number  = input('number',1,'intval');//数量
         $number  = input('number',1,'intval');//数量
+        $is_back = input("is_back",0);// 是否背包赠送: 1=是,0=否
 
 
-        if (!$user_id || !$gift_id || $number < 1)
+        if (!$user_id || !$gift_id || $number < 1 || !in_array($is_back,[0,1]) )
         {
         {
             $this->error();
             $this->error();
         }
         }
@@ -291,14 +292,6 @@ class Gift extends Api
             $this->error("不可以赠送给自己");
             $this->error("不可以赠送给自己");
         }
         }
 
 
-        // 获取礼物信息
-        $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
-        if (!$giftinfo)
-        {
-            $this->error("请选择礼物");
-        }
-        $giftvalue = bcmul($giftinfo['price'],$number);
-
         //被赠送人信息
         //被赠送人信息
         $touserinfo = Db::name('user')->where('id',$user_id)->find();
         $touserinfo = Db::name('user')->where('id',$user_id)->find();
         if (!$touserinfo)
         if (!$touserinfo)
@@ -306,57 +299,130 @@ class Gift extends Api
             $this->error("不存在的用户");
             $this->error("不存在的用户");
         }
         }
 
 
-        // 判断当前用户余额
-        $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
-        if($user_gold < $giftvalue)
-        {
-            $this->error("您的金币余额不足");
+        $backGiftId = 0; //背包礼物表的 gift_id
+        if($is_back == 1) {
+            // 获取背包礼物信息
+            $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
+            if(!$giftInfo){
+                $this->error("背包礼物获取失败");
+            }
+            $backGiftId = $giftInfo['gift_id'];
+
+            // 随机获取一个礼物
+            $allCount = $number;
+            $giftbackList = Db::name('gift_back')->field('id,value,name,image,gif_image,number')->where(["gift_id"=>$backGiftId,"user_id"=>$this->auth->id,'is_use'=>0])->limit($allCount)->order('id asc')->select();
+            $giftinfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
+            $giftcount = 0;
+            $giftList = [];
+
+            if(!empty($giftbackList)) {
+                foreach($giftbackList as $k => $v) {
+                    $giftList[$k] = $v;
+
+                    $giftcount += $v["number"];
+                    if($giftcount >= $allCount) {
+                        break;
+                    }
+                }
+            }
+
+            if($giftcount < $allCount)
+            {
+                $this->error("背包数量不足");
+            }
+
+            $giftvalue = $giftinfo["value"] * $number;
+            $getValue  = $giftvalue;
+        }else{
+            // 获取礼物信息
+            $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
+            if (!$giftinfo)
+            {
+                $this->error("请选择礼物");
+            }
+            $giftvalue = $giftinfo["value"] * $number;
+            $getValue  = $giftvalue;
+
+
+            // 判断当前用户余额
+            $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
+            if($user_jewel < $giftvalue)
+            {
+                $this->error("您的金币余额不足");
+            }
         }
         }
 
 
 
 
         Db::startTrans();
         Db::startTrans();
 
 
+        if($is_back == 1) {
+            $b=0;
+            foreach($giftList as $k => $v) {
+                for($a=1;$a<=$v["number"];$a++) {
+                    $b++;
+                    $num = $v["number"] - $a;
+                    if($num > 0) {
+                        $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number");
+                    } else {
+//                                $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
+                        $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete();
+                    }
+                    if($b == $number) break;
+                }
+
+            }
+            $res2 = true;
+        }else{
+            if($giftvalue > 0){
+
+                // 扣除当前用户余额
+                $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$giftvalue,'-',0,"赠送礼物:'" . $giftinfo["name"] . "',扣除" . $giftvalue . "金币!",3,'jewel');
+                if($wallet_rs['status'] === false){
+                    Db::rollback();
+                    $this->error($wallet_rs['msg']);
+                }
+
+                // 添加赠送用户余额
+                $money_to_jewel = config('site.money_to_jewel');
+                $gift_plat_scale = config('site.gift_plat_scale');
+
+                $giftmoney = bcdiv($giftvalue,$money_to_jewel,2);
+
+                $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
+                $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,'获得礼物:'.$giftinfo["name"],103,'money');
+                if($wallet_rs['status'] === false){
+                    Db::rollback();
+                    $this->error($wallet_rs['msg']);
+                }
+            }
+        }
 
 
         // 添加礼物赠送记录表
         // 添加礼物赠送记录表
         $data = [
         $data = [
             'user_id'     => $this->auth->id,
             'user_id'     => $this->auth->id,
             'user_to_id'  => $user_id,
             'user_to_id'  => $user_id,
-            'gift_id'     => $giftinfo['id'],
+            'gift_id'     => $backGiftId > 0 ? $backGiftId : $gift_id,
+            'gift_give_type' => $is_back ? 1 : 2,
             'gift_name'   => $giftinfo['name'],
             'gift_name'   => $giftinfo['name'],
+            'gift_gif_image'   => $giftinfo['image'],
             'number'      => $number,
             'number'      => $number,
-            'price'       => $giftinfo['price'],
-            'total_price' => $giftvalue,
+            'price'       => $giftinfo['value'],
+            'value'       => $giftvalue,
+            'task_status' => 1,
             'createtime'  => time(),
             'createtime'  => time(),
         ];
         ];
-        $log_id = Db::name('gift_user_typing')->insertGetId($data);
+
+        //每个礼物都要计算平台抽成和房主抽成
+        $platRate = 10;
+        $data['platvalue']    = bcmul($platRate/100  ,$data["value"],2);//平台抽成
+        $data['getvalue']     = bcsub($data["value"] ,$data['platvalue'],2);//减去抽成剩余价值
+
+        $log_id = Db::name('gift_user_party')->insertGetId($data);
         if(!$log_id){
         if(!$log_id){
             Db::rollback();
             Db::rollback();
             $this->error('赠送失败');
             $this->error('赠送失败');
         }
         }
 
 
-        if($giftvalue > 0){
-
-            // 扣除当前用户余额
-            $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,53,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
-            if($wallet_rs['status'] === false){
-                Db::rollback();
-                $this->error($wallet_rs['msg']);
-            }
-
-            // 添加赠送用户余额
-            $money_to_gold = config('site.money_to_gold');
-            $gift_plat_scale = config('site.gift_plat_scale');
-
-            $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
-
-            $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
-            $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,54,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
-            if($wallet_rs['status'] === false){
-                Db::rollback();
-                $this->error($wallet_rs['msg']);
-            }
-        }
-
 
 
 
 
         Db::commit();
         Db::commit();

+ 1 - 1
application/api/controller/Party.php

@@ -1533,7 +1533,7 @@ class Party extends Common
                     $res2 = true;
                     $res2 = true;
 
 
                     // 扣除当前用户钻石余额
                     // 扣除当前用户钻石余额
-                    $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', 0, "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3,'jewel');
+                    $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', 0, "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "金币!", 3,'jewel');
                     if($rs_wallet['status'] == false){
                     if($rs_wallet['status'] == false){
                         Db::rollback();
                         Db::rollback();
                         $this->error($rs_wallet['msg']);
                         $this->error($rs_wallet['msg']);