where('is_hytrade','1') ->where('status','1') ->order('weigh','desc') ->select(); //查询所有收藏 $sc_data = Db::name('app_rate_user')->where("user_id", $this->auth->id)->select(); $collect = []; foreach ($sc_data as $key => $value) { $collect[] = $value['symbol']; } foreach ($data as $key => $value) { $value['logo_image'] = Config::get("site.image_url").$value['logo_image']; $value['exchange_rate'] = Config::get("site.exchange_rate"); if(in_array($value['symbol'], $collect)){ $value['is_sc'] = true; }else{ $value['is_sc'] = false; } $data[$key] = $value; } $currency = Db::name('app_currency_user') ->field('num,num_lh,id') ->where('user_id',$this->auth->id) ->where('curr_id',1)->find(); if(empty($currency)){ $currency['num'] = 0; $currency['num_lh'] = 0; } $setting = [ 'promise_price' => Config::get('site.promise_price'), 'contract_fee' => Config::get('site.contract_fee'), 'buy_num' => explode(',',Config::get('site.buy_num')), 'balance' => $currency['num'], 'balance_contract' => isset($currency['num_lh']) ? $currency['num_lh'] : 0, 'shizhi' => Config::get('site.shizhi'), ]; $this->success('ok',['coin'=>$data,'setting'=>$setting]); } //获取合约持仓列表 public function get_contract() { $params = $this->request->post(); $size = (isset($params['size']))?$params['size']:20; $symbol = $this->request->post("symbol"); $status = $this->request->post("status",0); $w['a.user_id'] = array("eq",$this->auth->id); if($symbol){ $w['b.symbol'] = array("eq",$symbol); } if($status != "all"){ $w['a.status'] = array("eq",$status); } $data = Db::name('app_contract a') ->field('a.*,b.coinname,b.symbol,b.close,b.bb_type') ->join('app_rate b','a.coin_id = b.id','left') ->where($w) ->order('id','desc') ->paginate($size,false,['query' => request()->param()]); foreach ($data as $key=>$value) { $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']); $value['updatetime'] = date('m-d H:i:s',$value['updatetime']); $value['pctime'] = $value['createtime']; if($value['status'] == '1') { $diffnum = $value['close'] - $value['price']; if ($value['type'] == 'more') { //计算收益 $income = $diffnum / 100 * $value['num'] * $value['multiple']* Config::get('site.shizhi') ; } else { if ($diffnum < 0) { $income = abs($diffnum) / 100 * $value['num'] * $value['multiple']* Config::get('site.shizhi'); } else { $income = 0 - $diffnum / 100 * $value['num'] * $value['multiple']* Config::get('site.shizhi'); } } $value['income'] = sprintf('%.6f', $income); } $data[$key] = $value; } $this->success('success',$data); } //购买合约 public function buy_contract() { $params = $this->request->post(); //实名认证判断 if(!controller("Common")->get_auth()){ $this->error(__("请先完成实名认证"),['jump'=>1]); } if(!isset($params['cart']) || empty($params['cart'])) //market市价 limit 限价 { $this->error(__('请选择交易模式')); } if($params['cart'] == 'limit' && (!isset($params['price']) || empty($params['price']))){ $this->error(__('限价模式,请输入价格')); } if(!isset($params['coin_id']) || empty($params['coin_id'])) //market市价 limit 限价 { $this->error(__('请选择交易币种')); } $coin = Db::name('app_rate')->where('id',$params['coin_id'])->where('is_hytrade','1')->find(); if(!$coin) $this->error(__('该交易币种已下架或不存在')); if(!isset($params['type']) || empty($params['type'])) //more free { $this->error(__('请选择交易方向')); } if(!isset($params['num']) || empty($params['num'])) { $this->error(__('请输入交易数量')); } if(!isset($params['multiple']) || empty($params['multiple'])) { $this->error(__('请选择交易倍数')); } $buy_num = explode(',',Config::get('site.buy_num')); if(!in_array($params['multiple'],$buy_num)) { $this->error(__('请选择正确的交易倍数')); } //redis防重复点击 $symbol = "contract" . $this->auth->id; $submited = pushRedis($symbol); if (!$submited) { $this->error(__("操作频繁")); } $currency = Db::name('app_currency_user') ->field('num_lh,id') ->where('user_id',$this->auth->id) ->where('curr_id',1)->find(); //需要保证金 $promise_price = Config::get('site.promise_price'); $contract_fee = Config::get('site.contract_fee'); $price = $coin['close']; if($params['cart'] == 'market') { //调整价格 $tradejson = json_decode($coin['tradectrl_json'],true); $jd = $coin['jd']; $hour = date('H'); $minute = date('i'); $prices = $num = 0; if($tradejson) { foreach ($tradejson as $key => $value) { $time1 = explode('-', $key); $time2 = explode(':', $time1[0]); $time3 = explode(':', $time1[1]); $pricearr = explode('-', $value); if ($hour >= $time2[0] && $hour <= $time3[0]) { if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue; $prices = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd); break; } } if ($prices > 0) $price = $prices; } //个人调整价格 $tradejsons = json_decode($this->auth->tradectrl_json,true); if($tradejsons) { $prices = 0; foreach ($tradejsons as $key => $value) { if($key == $coin['symbol']) { $jsonarr = explode('|',$value); if(!isset($jsonarr[0]) || !isset($jsonarr[1])) continue; $time1 = explode('-',$jsonarr[0]); $time2 = explode(':', $time1[0]); $time3 = explode(':', $time1[1]); $pricearr = explode('-', $jsonarr[1]); if(!isset($pricearr[0]) || !isset($pricearr[1])) continue; if ($hour >= $time2[0] && $hour <= $time3[0]) { if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue; $prices = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd); break; } } } if ($prices > 0) $price = $prices; } $release = Db::name("app_curr_release")->where("symbol",$coin['symbol'])->find(); if($release){ $trademulte_json = json_decode($release['trademulte_json'],true); if($trademulte_json && !$prices){ foreach ($trademulte_json as $kk => $vv) { $time1 = explode('-', $kk); $time2 = explode(':', $time1[0]); $time3 = explode(':', $time1[1]); if ($hour >= $time2[0] && $hour <= $time3[0]) { if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue; $datass = json_decode(http_curl('https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol='.$release['symbol'],'get'),true); if(!$datass || !isset($datass['data']) || empty($datass['data'])) { $price = $coin['close']; break; } $price = $datass['data'][0]['close'] * $vv; break; } } } } }else{ if(($params['type'] == 'more' && $params['price'] > $price) || ($params['type'] == 'free' && $params['price'] < $price)) { $this->error(__('限价模式,请输入正确价格')); } $price = $params['price']; } $promisefee = ($promise_price>1)?$promise_price:sprintf("%.6f",$price*$promise_price*$params['num']*100/$params['multiple']); //质押手续费折扣 $count_zhiya = Db::name("app_zhiya_user")->where("user_id", $this->auth->id)->where("status",1)->sum("num"); $zhiya_free_rebate = Config::get("site.zhiya_free_rebate"); $zhekou = 1; foreach ($zhiya_free_rebate as $key => $value) { if($count_zhiya > $key){ $zhekou = $value; } } $fee = sprintf('%.6f',$promisefee*$contract_fee*$zhekou); //是否开启APT手续费 if($this->auth->apy_pay == 1){ if($currency['num_lh'] < ($promisefee)) { lopRedis($symbol); $this->error(__('账号余额不足')); } //开启apt手续费支付 $apt_free = Config::get("site.apt_free"); $apy_curr = Db::name("app_currency")->where("id",2)->find(); $res_curr = Db::name("app_currency")->where("id",1)->find(); $apy_num = sprintf("%.6f",$fee*$res_curr['exchange']/$apy_curr['exchange']*$apt_free); $fee = $apy_num; $user_apy = Db::name('app_currency_user')->where('curr_id',2)->where('user_id',$this->auth->id)->find(); if($user_apy['num'] < $fee) { lopRedis($symbol); $this->error(__('手续费不足')); } $detailed[] = [ 'user_id' => $this->auth->id, 'curr_id' => $user_apy['curr_id'], 'price' => $fee, 'cart' => '2', 'type' => '6', 'serial_no' => time().Random::build('numeric',4), 'order_result' => 'result', 'description' => 'Contract Fee', 'createtime' => time(), 'notice' => 'Contract Fee', 'before_num' => $user_apy['num'], 'after_num' => $user_apy['num'] - $fee, ]; $lostnum = $currency['num_lh'] - $promisefee; $fee_type = 1; }else{ if($currency['num_lh'] < ($promisefee+$fee)) { lopRedis($symbol); $this->error(__('账号余额不足')); } $detailed[] = [ 'user_id' => $this->auth->id, 'curr_id' => 1, 'price' => $fee, 'cart' => '2', 'type' => '6', 'serial_no' => time().Random::build('numeric',4), 'order_result' => 'result', 'description' => 'Contract Fee', 'createtime' => time(), 'notice' => 'Contract Fee', 'before_num' => $currency['num_lh'], 'after_num' => $currency['num_lh'] - $fee, ]; $lostnum = $currency['num_lh'] - $promisefee - $fee; $fee_type = 1; } $contract = [ 'user_id' => $this->auth->id, 'coin_id' => $coin['id'], 'symbol' => $coin['symbol'], 'type' => $params['type'], 'status' => ($params['cart'] == 'market')?'1':'0', 'price' => $price, 'num' => $params['num'], 'multiple' => $params['multiple'], 'promise_fee' => $promisefee, 'fee' => $fee, 'createtime' => time(), 'fee_type' => $fee_type, ]; $detailed[] = [ 'user_id' => $this->auth->id, 'curr_id' => 1, 'price' => $promisefee, 'cart' => '2', 'type' => '6', 'serial_no' => time().Random::build('numeric',4), 'order_result' => 'result', 'description' => 'Contract Margin', 'createtime' => time(), 'notice' => 'Contract Margin', 'before_num' => $currency['num_lh'], 'after_num' => $lostnum, ]; Db::startTrans(); try { Db::name('app_currency_user')->where('id',$currency['id'])->update(['num_lh'=>$lostnum,'updatetime'=>time()]); if($this->auth->apy_pay == 1){ Db::name('app_currency_user')->where('id',$user_apy['id'])->update(['num'=>$user_apy['num'] - $fee]); } $ret1 = Db::name('app_contract')->insertGetId($contract); Db::name('app_detailed')->insertAll($detailed); lopRedis($symbol); Db::commit(); $return = $contract; $return['id'] = $ret1; $return['createtime'] = date('Y-m-d H:i:s',$return['createtime']); $this->success(__('提交成功'),$return); } catch (Exception $e) { Db::rollback(); $this->error(__('系统繁忙')); } } //设置止盈止损 public function set_contract() { $params = $this->request->post(); if(!isset($params['contract_id']) || empty($params['contract_id'])) { $this->error(__('请选择设置的合约订单')); } // if(!isset($params['zy_price']) || empty($params['zy_price'])) // { // $this->error(__('请设置止盈价格')); // } // if(!isset($params['zs_price']) || empty($params['zs_price'])) // { // $this->error(__('请设置止损价格')); // } $contract = Db::name('app_contract') ->where('user_id',$this->auth->id) ->where('id',$params['contract_id']) ->where('status','1') ->find(); if(!$contract) { $this->error(__('合约订单不存在或已平仓')); } if(!isset($params['zy_price']) || empty($params['zy_price']) || $params['zy_price'] <= 0){ $params['zy_price'] = $contract['zy_price']; } if($contract['type'] == 'more' && $params['zy_price'] < $contract['price'] && $params['zy_price'] > 0){ $this->error(__('请设置止盈价格')); } if($contract['type'] == 'free' && $params['zy_price'] > $contract['price'] && $params['zy_price'] > 0){ $this->error(__('请设置止盈价格')); } if(!isset($params['zs_price']) || empty($params['zs_price']) || $params['zs_price'] <= 0) { $params['zs_price'] = $contract['zs_price']; } if($contract['type'] == 'more' && $params['zs_price'] > $contract['price'] && $params['zs_price'] > 0){ $this->error(__('请设置止损价格')); } if($contract['type'] == 'free' && $params['zs_price'] < $contract['price'] && $params['zs_price'] > 0){ $this->error(__('请设置止损价格')); } $ret = Db::name('app_contract') ->where('id',$contract['id']) ->update(['zy_price'=>$params['zy_price'],'zs_price'=>$params['zs_price'],'updatetime'=>time()]); if($ret) { $this->success(__('设置成功')); }else{ $this->error(__('系统繁忙')); } } //平仓 public function pingcang() { $params = $this->request->post(); if(!isset($params['contract_id']) || empty($params['contract_id'])) { $this->error(__('请选择合约订单')); } $contract = Db::name('app_contract') ->where('user_id',$this->auth->id) ->where('id',$params['contract_id']) ->where('status','1') ->find(); if(!$contract) { $this->error(__('合约订单不存在或已平仓')); } //redis防重复点击 $symbol = "contract_pc" . $this->auth->id; $submited = pushRedis($symbol); if (!$submited) { $this->error(__("操作频繁")); } $coin = Db::name('app_rate')->where('id',$contract['coin_id'])->find(); $pc_price = $coin['usdt']; //调整价格 $tradejson = json_decode($coin['tradectrl_json'],true); $jd = $coin['jd']; $hour = date('H'); $minute = date('i'); $prices = $num = 0; if($tradejson) { foreach ($tradejson as $key => $value) { $time1 = explode('-', $key); $time2 = explode(':', $time1[0]); $time3 = explode(':', $time1[1]); $pricearr = explode('-', $value); if ($hour >= $time2[0] && $hour <= $time3[0]) { if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue; $prices = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd); break; } } if ($prices > 0) $pc_price = $prices; } //个人调整价格 $tradejsons = json_decode($this->auth->tradectrl_json,true); if($tradejsons) { $prices = 0; foreach ($tradejsons as $key => $value) { if($key == $coin['symbol']) { $jsonarr = explode('|',$value); if(!isset($jsonarr[0]) || !isset($jsonarr[1])) continue; $time1 = explode('-',$jsonarr[0]); $time2 = explode(':', $time1[0]); $time3 = explode(':', $time1[1]); $pricearr = explode('-', $jsonarr[1]); if(!isset($pricearr[0]) || !isset($pricearr[1])) continue; if ($hour >= $time2[0] && $hour <= $time3[0]) { if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue; $prices = (float)$this->randomFloat($pricearr[0], $pricearr[1], $jd); break; } } } if ($prices > 0) $pc_price = $prices; } $release = Db::name("app_curr_release")->where("symbol",$coin['symbol'])->find(); if($release){ $trademulte_json = json_decode($release['trademulte_json'],true); if($trademulte_json && !$prices){ foreach ($trademulte_json as $kk => $vv) { $time1 = explode('-', $kk); $time2 = explode(':', $time1[0]); $time3 = explode(':', $time1[1]); if ($hour >= $time2[0] && $hour <= $time3[0]) { if (($hour == $time3[0] && $minute > $time3[1]) || ($hour == $time2[0] && $minute < $time2[1])) continue; $datass = json_decode(http_curl('https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol='.$release['symbol'],'get'),true); if(!$datass || !isset($datass['data']) || empty($datass['data'])) { $pc_price = $coin['close']; break; } $pc_price = $datass['data'][0]['close'] * $vv; break; } } } } $diffnum = $pc_price - $contract['price']; if($contract['type'] == 'more') { //计算收益 // $income = $diffnum / 100 * $contract['num'] * $contract['multiple'] * Config::get('site.shizhi'); $income = $diffnum * $contract['num'] * Config::get('site.shizhi'); }else{ if($diffnum < 0) { // $income = abs($diffnum) / 100 * $contract['num'] * $contract['multiple'] * Config::get('site.shizhi'); $income = abs($diffnum) * $contract['num'] * Config::get('site.shizhi'); }else{ // $income = 0 - $diffnum / 100 * $contract['num'] * $contract['multiple'] * Config::get('site.shizhi'); $income = 0 - $diffnum * $contract['num'] * Config::get('site.shizhi'); } } $income = sprintf('%.6f',$income); $currency = Db::name('app_currency_user') ->field('num_lh,id') ->where('user_id',$this->auth->id) ->where('curr_id',1)->find(); $detailed = [ 'user_id' => $this->auth->id, 'curr_id' => 1, 'price' => abs($income), 'cart' => '2', 'type' => '6', 'serial_no' => time().Random::build('numeric',4), 'order_result' => 'result', 'description' => 'Contract Close', 'createtime' => time(), 'notice' => 'Contract Close', 'before_num' => $currency['num_lh'], 'after_num' => $currency['num_lh'] + $income, ]; if($income >0 ){ $detailed['cart'] = '1'; $detailed['description'] = 'Contract Close Profit'; }else{ $detailed['cart'] = '2'; $detailed['description'] = 'Contract Close Loss'; } $detaileds[] = $detailed; $detaileds[] = [ 'user_id' => $this->auth->id, 'curr_id' => 1, 'price' => $contract['promise_fee'], 'cart' => '1', 'type' => '6', 'serial_no' => time().Random::build('numeric',4), 'order_result' => 'result', 'description' => 'Contract Margin', 'createtime' => time(), 'notice' => 'Contract Margin', 'before_num' => $detailed['after_num'], 'after_num' => $detailed['after_num'] + $contract['promise_fee'], ]; $lostnum = sprintf("%.6f",($currency['num_lh'] + $income + $contract['promise_fee'])); Db::startTrans(); try { $ret = Db::name('app_currency_user') ->where('id',$currency['id'])->update(['num_lh'=>$lostnum,'updatetime'=>time()]); if(!$ret) { lopRedis($symbol); Db::rollback(); $this->error(__('系统繁忙')); } $ret1 = Db::name('app_contract')->where('id',$contract['id']) ->update(['status'=>'2','pc_price'=>$pc_price,'pctime'=>time(),'income'=>$income]); if(!$ret1) { lopRedis($symbol); Db::rollback(); $this->error(__('系统繁忙')); } $ret2 = Db::name('app_detailed')->insertAll($detaileds); if(!$ret2) { lopRedis($symbol); Db::rollback(); $this->error(__('系统繁忙')); } lopRedis($symbol); Db::commit(); $this->success(__('已平仓')); } catch (Exception $e) { Db::rollback(); $this->error(__('系统繁忙')); } } //撤销订单 public function cancel_cang() { $params = $this->request->post(); if (!isset($params['contract_id']) || empty($params['contract_id'])) { $this->error(__('请选择合约订单')); } $contract = Db::name('app_contract') ->where('user_id', $this->auth->id) ->where('id', $params['contract_id']) ->where('status', '0') ->find(); if (!$contract) { $this->error(__('合约订单不存在或已平仓')); } //redis防重复点击 $symbol = "contract_cancel" . $this->auth->id; $submited = pushRedis($symbol); if (!$submited) { $this->error(__("操作频繁")); } $currency = Db::name('app_currency_user') ->field('num_lh,id') ->where('user_id',$this->auth->id) ->where('curr_id',1)->find(); $detaileds = [ 'user_id' => $this->auth->id, 'curr_id' => 1, 'price' => $contract['promise_fee'] + $contract['fee'], 'cart' => '1', 'type' => '6', 'serial_no' => time().Random::build('numeric',4), 'order_result' => 'result', 'description' => 'Contract Margin & Fee Refund', 'createtime' => time(), 'notice' => 'Contract Margin & Fee Refund', 'before_num' => $currency['num_lh'], 'after_num' => $currency['num_lh'] + $contract['promise_fee'] + $contract['fee'], ]; $lostnum = sprintf("%.6f",($currency['num_lh'] + $contract['promise_fee'] + $contract['fee'])); Db::startTrans(); try { $ret = Db::name('app_currency_user') ->where('id',$currency['id'])->update(['num_lh'=>$lostnum,'updatetime'=>time()]); if(!$ret) { lopRedis($symbol); Db::rollback(); $this->error(__('系统繁忙')); } $ret1 = Db::name('app_contract')->where('id',$contract['id']) ->update(['status'=>'3','updatetime'=>time()]); if(!$ret1) { lopRedis($symbol); Db::rollback(); $this->error(__('系统繁忙')); } $ret2 = Db::name('app_detailed')->insert($detaileds); if(!$ret2) { lopRedis($symbol); Db::rollback(); $this->error(__('系统繁忙')); } lopRedis($symbol); Db::commit(); $this->success(__('已撤销')); } catch (Exception $e) { Db::rollback(); $this->error(__('系统繁忙')); } } //4位小数的随机数 function randomFloat($min = 0, $max = 10 , $localnum = 4) { if($localnum == 4) { if ($max - $min <= 0.0002) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.0001); $num = $min + $rand; $number = sprintf("%.4f", $num); if($number == $min){ $number += 0.0001; } }else if($localnum == 5){ if ($max - $min <= 0.00002) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.00001); $num = $min + $rand; $number = sprintf("%.5f", $num); if($number == $min){ $number += 0.00001; } }else if($localnum == 6){ if ($max - $min <= 0.000002) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.000001); $num = $min + $rand; $number = sprintf("%.6f", $num); if($number == $min){ $number += 0.000001; } }else if($localnum == 2){ if ($max - $min <= 0.02) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.01); $num = $min + $rand; $number = sprintf("%.2f", $num); if($number == $min){ $number += 0.01; } }else if($localnum == 3){ if ($max - $min <= 0.001) { return 0; } $rand = mt_rand() / mt_getrandmax() * ($max - $min-0.001); $num = $min + $rand; $number = sprintf("%.3f", $num); if($number == $min){ $number += 0.001; } } return $number; } /** * 获取币种资料 */ public function get_curr_con() { $symbol = $this->request->post("symbol"); $data = Db::name("app_rate")->field("id,symbol,name,logo_image,level,count_price,liutong_num,gongji_num,count_num,faxin_time,faxin_price,qukuai,desc,market_jd,price_jd,bb_type,close") ->where("symbol",$symbol) ->find(); if(empty($data)){ $this->error(__("未查询到币种信息")); } $data['jys'] = "An Piter"; $data['faxin_time'] = date("Y-m-d",$data['faxin_time']); $data['logo_image'] = Config::get("site.image_url").$data['logo_image']; $data['exchange_rate'] = Config::get("site.exchange_rate"); $data['h5_url'] = "https://h5.polyexchange.net/#/pages/transaction/index"; $this->success("ok",$data); } }