zhangxiaobin il y a 1 an
Parent
commit
9e90674a96

+ 45 - 44
application/api/controller/Money.php

@@ -12,6 +12,9 @@ use fast\Random;
 use app\common\model\RecharOrder;
 use kjpay\kjpay;
 use kjwithdraw\kjwithdraw;
+use think\Exception;
+use think\exception\PDOException;
+use think\exception\ValidateException;
 use think\Request;
 use think\Db;
 use app\common\library\Sms as Smslib;
@@ -915,62 +918,60 @@ class Money extends Common
     }
 
     /**
-     * 钻石兑换余额
+     * 余额兑换钻石
      */
     public function jewelExchange()
     {
-        $id = $this->request->param('id',0);
-        $jewel = $this->request->param('jewel',0);
-
-        // 接口防并发
-        if (!$this->apiLimit(1, 1000)) {
-            $this->error(__('Operation frequently'));
-        }
-        $exchange_id = $this->request->request("exchange_id");// 兑换声币配置ID
-        $sound_coin = $this->request->request("sound_coin");// 输入具体声币数量
-        if (!$sound_coin && !$exchange_id) {
-            $this->error(__('Invalid parameters'));
-        }
-
-        if ($sound_coin > 0) {
-            $scoin = $sound_coin;
-            // 获取配置信息
-            $conf = config("site.soundcoinTomoney");
-            $exchangeMoney = round(($sound_coin * ($conf / 100)) / 100, 2);
-            if ($exchangeMoney < 0.01) $this->error(__('声币兑换数量太小啦!'));
-        } else {
-            // 获取兑换配置信息
-            $exchangeModel = new \app\common\model\ExchangeConfig();
-            $exchangeInfo = $exchangeModel->get($exchange_id);
-            if (!$exchangeInfo) $this->error("配置信息未找到");
-            $exchangeMoney = $exchangeInfo["money"] / 100;
-            $scoin = $exchangeInfo["sound_coin"];
-
-        }
-
         Db::startTrans();
         try {
-            // 查询声币余额
+            // 接口防并发
+            if (!$this->apiLimit(1, 1000)) {
+                throw new Exception(__('Operation frequently'));
+            }
+            $exchange_id = $this->request->request("exchange_id");// 兑换声币配置ID
+            $money = $this->request->request("money");// 输入具体声币数量
+            if (!$money && !$exchange_id) {
+                throw new Exception(__('Invalid parameters'));
+            }
+            if ($money && $money < 1) {
+                throw new Exception('兑换金额异常');
+            }
+            if ($money > 0) {
+                $exchangeMoney = $money;
+                // 获取配置信息
+                $conf = config("site.money_to_jewel");
+                $scoin = $money * $conf;
+            } else {
+                // 获取兑换配置信息
+                $exchangeModel = new \app\common\model\ExchmoneyConfig();
+                $exchangeInfo = $exchangeModel->get($exchange_id);
+                if (!$exchangeInfo) throw new Exception("配置信息未找到");
+                $exchangeMoney = $exchangeInfo["money"] / 100;
+                $scoin = $exchangeInfo["jewel"];
+            }
+
+            $userId = $this->auth->id;
+            // 查询余额
             $userModel = new \app\common\model\User();
-            $userInfo = $userModel->lock('lock in share mode')->find($this->auth->id);
-            if ($scoin > $userInfo["sound_coin"]) {
-                $this->error("声币余额不足!", [], 100);
+            $userInfo = $userModel->lock('lock in share mode')->find($userId);
+            if ($scoin > $userInfo["money"]) {
+                throw new Exception("余额不足!");
             }
 
             $moneylogModel = new \app\common\model\UserMoneyLog();
-            $soundlogModel = new \app\common\model\UserSoundcoinLog();
+            $jewellogModel = new \app\common\model\UserJewelLog();
             $exchangelogModel = new \app\common\model\UserExchangeLog();
-            $detail = "声币兑换余额";
+            $detail = "余额兑换钻石";
             // 添加资金流水记录
-            $res1 = $moneylogModel->addRecord($this->auth->id, $exchangeMoney, "+", $userInfo["money"], $detail);
+            $res1 = $moneylogModel->addRecord($userId, $exchangeMoney, "-", $userInfo["money"], $detail,14);
             // 添加兑换记录
-            $res2 = $exchangelogModel->addExchangeLog($this->auth->id, $scoin, $exchangeMoney);
-            // 添加声币流水记录
-            $res3 = $soundlogModel->addUserSoundcoinLog($this->auth->id, $scoin, "-", $userInfo["sound_coin"], $detail, 3, $res2);
-            // 减去用户声币余额
-            $res4 = $userModel->where(["id" => $this->auth->id])->setDec("sound_coin", $scoin);
-            // 增加用户资金余额
-            $res5 = $userModel->where(["id" => $this->auth->id])->setInc("money", $exchangeMoney);
+            $res2 = $exchangelogModel->addExchangeLog($userId, $scoin, $exchangeMoney);
+            // 添加钻石流水记录
+            $res3 = $jewellogModel->addUserJewelLog($userId, $scoin, "+", $userInfo["jewel"], $detail, 14);
+            // 减去用户余额
+            $res4 = $userModel->where(["id" => $userId])->setDec("money", $exchangeMoney);
+            // 增加用户钻石余额
+            $res5 = $userModel->where(["id" => $userId])->setInc("jewel", $scoin);
             if ($res1 && $res2 && $res3 && $res4 && $res5) {
                 Db::commit();
                 $this->success("兑换成功!");

+ 19 - 1
application/common/model/UserMoneyLog.php

@@ -15,6 +15,24 @@ class UserMoneyLog extends Model
     // 定义时间戳字段名
     protected $createTime = 'createtime';
 
-
+    public function addRecord($userId=0, $money=0, $mode='+', $before=0, $detail='', $type = 1)
+    {
+        if ($mode == "+") {
+            $balance = $before + $money;
+        } else {
+            $balance = $before - $money;
+        }
+        // 添加当前用户钻石流水记录
+        $data = [];
+        $data["user_id"] = $userId;
+        $data['type'] = $type;
+        $data["value"] = $money;
+        $data["mode"] = $mode;
+        $data["before"] = $before;
+        $data["balance"] = $balance;
+        $data["detail"] = $detail;
+        $data["createtime"] = time();
+        return $this->insertGetId($data);
+    }
 
 }

+ 1 - 1
application/extra/wallet.php

@@ -18,7 +18,7 @@ return [
         11 => '私聊转账',  //减少
         12 => '私聊到账',  //增加
         13 => '抽奖箱子和转盘', //减少
-
+        14 => '余额兑换钻石', //增加
         //money
         101 => '获赠礼物', //增加
         102 => '房间礼物抽成', //增加