Browse Source

收送礼物都升级

lizhen_gitee 11 months ago
parent
commit
fd9c46b092
2 changed files with 70 additions and 8 deletions
  1. 13 8
      application/api/controller/Gift.php
  2. 57 0
      application/common/model/User.php

+ 13 - 8
application/api/controller/Gift.php

@@ -58,7 +58,7 @@ class Gift extends Api
     public function givegift_typing() {
         // 接口防并发
         if (!$this->apiLimit(1, 1000)) {
-            $this->error(__('Operation frequently'));
+            $this->error('休息一下吧');
         }
 
         $user_id = input('user_id');// 赠送对象
@@ -91,17 +91,17 @@ class Gift extends Api
             $this->error("不存在的用户");
         }
 
+        Db::startTrans();
+
         // 判断当前用户余额
         $user_gold = model('wallet')->getWallettotal($this->auth->id);
         if($user_gold < $giftvalue)
         {
+            Db::rollback();
             $this->error("您的金币余额不足");
         }
 
 
-        Db::startTrans();
-
-
         // 添加礼物赠送记录表
         $data = [
             'user_id'     => $this->auth->id,
@@ -115,8 +115,8 @@ class Gift extends Api
         ];
         //每个礼物都要计算平台抽成和房主抽成
         $gift_plat_scale = config('site.gift_plat_scale');
-        $data['platvalue']    = bcmul($gift_plat_scale/100  ,$data["total_price"],1);//平台抽成
-        $data['getvalue']     = bcsub($data["total_price"] ,$data['platvalue'],1);//减去抽成剩余价值
+        $data['platvalue']    = bcmul($gift_plat_scale/100  ,$data['total_price'],1);//平台抽成
+        $data['getvalue']     = bcsub($data['total_price'] ,$data['platvalue'],1);//减去抽成剩余价值
 
         $log_id = Db::name('gift_user_typing')->insertGetId($data);
         if(!$log_id){
@@ -127,7 +127,7 @@ class Gift extends Api
         if($giftvalue > 0){
 
             // 扣除当前用户余额
-            $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,14,'赠送给'.$touserinfo['username'].'礼物:'.$giftinfo["name"].'X'.$number,'gift_user_typing',$log_id);
+            $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,14,'赠送给'.$touserinfo['username'].'礼物:'.$giftinfo['name'].'X'.$number,'gift_user_typing',$log_id);
             if($wallet_rs['status'] === false){
                 Db::rollback();
                 $this->error($wallet_rs['msg']);
@@ -136,13 +136,18 @@ class Gift extends Api
 
         if($data['getvalue'] > 0){
 
-            $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'jewel',$data['getvalue'],24,$this->auth->username.'送给我礼物:'.$giftinfo["name"].'X'.$number,'gift_user_typing',$log_id);
+            $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'jewel',$data['getvalue'],24,$this->auth->username.'送给我礼物:'.$giftinfo['name'].'X'.$number,'gift_user_typing',$log_id);
             if($wallet_rs['status'] === false){
                 Db::rollback();
                 $this->error($wallet_rs['msg']);
             }
         }
 
+        //增加送礼用户的财富等级
+        $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$giftvalue);
+        //魅力等级
+        $res_wealth = \app\common\model\User::add_charm_level($user_id,$giftvalue);
+
         Db::commit();
         $this->success('赠送成功');
 

+ 57 - 0
application/common/model/User.php

@@ -231,6 +231,63 @@ class User extends Model
         return $value;
     }
 
+    /**
+     * 增加魅力等级
+     */
+    public static function add_charm_level($user_id,$value) {
+        if($value <= 0) return false;
+        // 获取用户
+        $userInfo = Db::name('user')->field('id,charm_level,charm_value')->where('id',$user_id)->find();
+        if(!$userInfo) return false;
+
+        // 增加之后的经验值
+        $new_value = $userInfo['charm_value'] + $value;
+
+        // 查询等级配置信息
+        $charm_level = Db::name('charm_level')->where('value','elt',$new_value)->order('value', 'desc')->value('level');
+
+        if(empty($charm_level))  {
+            $new_level = $userInfo['charm_level'];
+        } else {
+            $new_level = $charm_level;
+        }
+
+        // 更新用户等级信息和经验值
+        $data = [
+            'charm_level' => $new_level,
+            'charm_value' => $new_value,
+        ];
+        $res = Db::name('user')->where('id',$user_id)->update($data);
+    }
+    /**
+     * 增加财富等级
+     */
+    public static function add_wealth_level($user_id,$value) {
+        if($value <= 0) return false;
+        // 获取用户
+        $userInfo = Db::name('user')->field('id,wealth_level,wealth_value')->where('id',$user_id)->find();
+        if(!$userInfo) return false;
+
+        // 增加之后的经验值
+        $new_value = $userInfo['wealth_value'] + $value;
+
+        // 查询等级配置信息
+        $wealth_level = Db::name('wealth_level')->where('value','elt',$new_value)->order('value', 'desc')->value('level');
+
+        if(!$wealth_level)  {
+            $new_level = $userInfo['wealth_level'];
+        } else {
+            $new_level = $wealth_level;
+        }
+
+        // 更新用户等级信息和经验值
+        $data = [
+            'wealth_level' => $new_level,
+            'wealth_value' => $new_value,
+        ];
+        $res = Db::name('user')->where('id',$user_id)->update($data);
+    }
+