|  | @@ -12,6 +12,9 @@ use fast\Random;
 | 
											
												
													
														|  |  use app\common\model\RecharOrder;
 |  |  use app\common\model\RecharOrder;
 | 
											
												
													
														|  |  use kjpay\kjpay;
 |  |  use kjpay\kjpay;
 | 
											
												
													
														|  |  use kjwithdraw\kjwithdraw;
 |  |  use kjwithdraw\kjwithdraw;
 | 
											
												
													
														|  | 
 |  | +use think\Exception;
 | 
											
												
													
														|  | 
 |  | +use think\exception\PDOException;
 | 
											
												
													
														|  | 
 |  | +use think\exception\ValidateException;
 | 
											
												
													
														|  |  use think\Request;
 |  |  use think\Request;
 | 
											
												
													
														|  |  use think\Db;
 |  |  use think\Db;
 | 
											
												
													
														|  |  use app\common\library\Sms as Smslib;
 |  |  use app\common\library\Sms as Smslib;
 | 
											
										
											
												
													
														|  | @@ -915,62 +918,60 @@ class Money extends Common
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      /**
 |  |      /**
 | 
											
												
													
														|  | -     * 钻石兑换余额
 |  | 
 | 
											
												
													
														|  | 
 |  | +     * 余额兑换钻石
 | 
											
												
													
														|  |       */
 |  |       */
 | 
											
												
													
														|  |      public function jewelExchange()
 |  |      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();
 |  |          Db::startTrans();
 | 
											
												
													
														|  |          try {
 |  |          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();
 |  |              $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();
 |  |              $moneylogModel = new \app\common\model\UserMoneyLog();
 | 
											
												
													
														|  | -            $soundlogModel = new \app\common\model\UserSoundcoinLog();
 |  | 
 | 
											
												
													
														|  | 
 |  | +            $jewellogModel = new \app\common\model\UserJewelLog();
 | 
											
												
													
														|  |              $exchangelogModel = new \app\common\model\UserExchangeLog();
 |  |              $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) {
 |  |              if ($res1 && $res2 && $res3 && $res4 && $res5) {
 | 
											
												
													
														|  |                  Db::commit();
 |  |                  Db::commit();
 | 
											
												
													
														|  |                  $this->success("兑换成功!");
 |  |                  $this->success("兑换成功!");
 |