error(L("非法操作"));
}
}
public function laverorder($type = null)
{
$uid = userid();
if ($uid <= 0) {
$this->ajaxReturn(['code' => 3, 'data' => '', 'info' => L('未登录')]);
}
$page = I('get.page');
if (!$page) $page = 1;
$limit = I('get.limit');
if (!$limit) $limit = 10;
$list = [];
if ($type == 1) {
//委托订单
$list = M("leverorder")->where(array('uid' => $uid, 'status' => 1))->page($page, $limit)->order("id desc")->select();
} elseif ($type == 2) {
//持仓订单
$list = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->page($page, $limit)->order("id desc")->select();
// foreach ($list as &$item) {
// $oy_list = do_request(oy_url($item['coinname'], 'api/v5/market/index-tickers?instId='), []);
// $oy_list = $oy_list->data[0];
//// $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
//// $result = $this->getprice($url);
//// $pdata = $result['data'][0];
// $close = $oy_list->idxPx;//现价
// if ($item['direction'] == 1) { //做多
// $item['profit'] = sprintf("%.2f", ($close - $item['price']) * $item['num']);
// } elseif ($item['direction'] == 2) { //做空
// $item['profit'] = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
// }
// }
} elseif ($type == 3) {
//成交订单
$list = M("leverorder")->where("uid = {$uid} and (status = 3 or status = 4)")->page($page, $limit)->order("id desc")->select();
}
$data = [
'code' => 1,
'list' => $list,//委托
'info' => ''
];
$this->ajaxReturn($data);
}
/**
* 市场配置
* @return void
*/
public function ctmarket()
{
$list = M('ctmarket')->where(['status' => 1])->select();
$data = [
'code' => 1,
'list' => $list,//委托
'info' => ''
];
$this->ajaxReturn($data);
}
/**
* 计算保证金
* @return void
*/
public function bond()
{
$uid = userid();
if ($uid <= 0) {
$this->ajaxReturn(['code' => 3, 'data' => '', 'info' => L('未登录')]);
}
$name = I('get.name');//币种
$num = I('get.num');//手数
$gann = I('get.gann');//杠杆
$oy_list = do_request(oy_url($name, 'api/v5/market/ticker?instId='), []);
$oy_list = $oy_list->data[0];
$bong = ($oy_list->last * $num) / $gann;
$fee = ($oy_list->last * $num) * 0.0001;
$this->ajaxReturn(['code' => 1, 'data' => ['bong' => $bong, 'fee' => $fee], 'info' => '']);
}
public function tradlist()
{
$uid = userid();
if ($uid <= 0) {
$this->redirect('Login/index');
}
//委托订单
$list = M("leverorder")->where(array('uid' => $uid, 'status' => 1))->order("id desc")->field("id,direction,symbol,wt_price,num,ggan,wttime")->select();
$this->assign('list', $list);
//持仓订单
$alllist = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->order("id desc")->select();
$this->assign('alllist', $alllist);
//成交订单
$finishlist = M("leverorder")->where("uid = {$uid} and (status = 3 or status = 4)")->order("id desc")->select();
$this->assign('finishlist', $finishlist);
$this->display();
}
public function closeorder()
{
$uid = userid();
if ($uid <= 0) {
$this->ajaxReturn(['code' => 3, 'data' => '', 'info' => L('未登录')]);
}
// if($_POST){
$id = trim(I('get.id'));
if ($id == '' || $id <= 0) {
$this->ajaxReturn(['code' => 2, 'info' => L('缺少重要参数')]);
exit();
}
$oinfo = M("leverorder")->where(array('id' => $id, 'status' => 2))->find();
if (empty($oinfo)) {
$this->ajaxReturn(['code' => 2, 'info' => L('订单不存在')]);
exit();
}
$uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
$lowercoin = $oinfo['coinname'];
if ($lowercoin == 'ukbusdt') {
$o_symbol = $oinfo['symbol'];
$symbol_arr = explode('/', $o_symbol);
$symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
$mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
$close = $mlist['new_price'];
} else {
$oy_list = do_request(oy_url($oinfo['coinname'], 'api/v5/market/ticker?instId='), []);
$oy_list = $oy_list->data[0];
// $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
// $result = $this->getprice($url);
// $pdata = $result['data'][0];
$close = $oy_list->last;//现价
}
if ($close){
//收益计算
//例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
//则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
$direction = $oinfo['direction'];
if ($direction == 1) { //做多
$profit = sprintf("%.2f", ($close - $oinfo['price']) * $oinfo['num']);
} elseif ($direction == 2) { //做空
$profit = sprintf("%.2f", ($oinfo['price'] - $close) * $oinfo['num']);
}
if ($profit >= 0) {
$yk_status = 1;
} else {
$yk_status = 2;
}
$tprofit = abs($profit);
$data['pc_price'] = $close;
$data['status'] = 3;
$data['yk_status'] = $yk_status;
$data['ylmoney'] = $tprofit;
$data['endtime'] = date("Y-m-d H:i:s", time());
$result = M("leverorder")->where(array('id' => $id, 'status' => 2))->save($data);
$user = M('levermoney')->where(['uid' => $uid])->find();
if ($result) {
// $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
if ($profit >= 0) {
M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
$ubill['uid'] = $uid;
$ubill['username'] = $uinfo['username'];
$ubill['num'] = $tprofit;
$ubill['coinname'] = "lever";
$ubill['afternum'] = $user['money'] + $tprofit;
$ubill['type'] = 20;
$ubill['addtime'] = date("Y-m-d H:i:s", time());
$ubill['st'] = 1;
$ubill['remark'] = L('合约交易手动平仓盈利收益');
$ubillre = M("bill")->add($ubill);
$bond_ubill = [
'uid' => $uid,
'username' => $uinfo['username'],
'num' => $oinfo['bond'],
'coinname' => 'lever',
'afternum' => $user['money'] + $oinfo['bond'],
'type' => 19,
'addtime' => date("Y-m-d H:i:s", time()),
'st' => 1,
'remark' => '合約手動平倉返還佔用保證金'
];// 扣除合约保证金
M("levermoney")->where(array('uid' => $uid))->setInc('money', $oinfo['bond']);
M("bill")->add($bond_ubill);
} else {
if ($user['money'] >= $tprofit) {
$price = ($user['money']+$oinfo['bond']) - $tprofit;
M("levermoney")->where(array('uid' => $uid))->setInc('money', $oinfo['bond']);
$ubill['uid'] = $uid;
$ubill['username'] = $uinfo['username'];
$ubill['num'] = $oinfo['bond'];
$ubill['coinname'] = "lever";
$ubill['afternum'] = $user['money'] + $oinfo['bond'];
$ubill['type'] = 20;
$ubill['addtime'] = date("Y-m-d H:i:s", time());
$ubill['st'] = 1;
$ubill['remark'] = '合約交易手動平倉,退還保證金';
$ubillre = M("bill")->add($ubill);
M("levermoney")->where(array('uid' => $uid))->setDec('money', $tprofit);
$ubill['num'] = $tprofit;
$ubill['afternum'] = $price;
$ubill['type'] = 20;
$ubill['st'] = 2;
$ubill['remark'] = '合約交易手動平倉虧損';
$ubillre = M("bill")->add($ubill);
} else {
$bond = $user['money'] + $oinfo['bond'];
if ($bond >= $tprofit) {
$user_bond = $oinfo['bond'] - ($tprofit - $user['money']);
M("levermoney")->where(array('uid' => $uid))->setInc('money', $user_bond);
$ubill['uid'] = $uid;
$ubill['username'] = $uinfo['username'];
$ubill['num'] = $user_bond;
$ubill['coinname'] = "lever";
$ubill['afternum'] = $user_bond;
$ubill['type'] = 20;
$ubill['addtime'] = date("Y-m-d H:i:s", time());
$ubill['st'] = 1;
$ubill['remark'] = '合約交易手動平倉虧損退回剩餘保證金';
$ubillre = M("bill")->add($ubill);
M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
$ubill['num'] = $user['money'];
$ubill['afternum'] = 0;
$ubill['type'] = 20;
$ubill['st'] = 2;
$ubill['remark'] = '合約交易手動平倉虧損';
$ubillre = M("bill")->add($ubill);
} else {
M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
$ubill['uid'] = $uid;
$ubill['username'] = $uinfo['username'];
$ubill['num'] = $user['money'];
$ubill['coinname'] = "lever";
$ubill['afternum'] = 0;
$ubill['type'] = 20;
$ubill['addtime'] = date("Y-m-d H:i:s", time());
$ubill['st'] = 1;
$ubill['remark'] = '合約交易手動平倉虧損完';
$ubillre = M("bill")->add($ubill);
}
}
}
M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $oinfo['bond']);
$this->ajaxReturn(['code' => 1, 'info' => L('操作成功')]);
exit();
} else {
$this->ajaxReturn(['code' => 2, 'info' => L('操作失败')]);
exit();
}
}else{
$this->ajaxReturn(['code' => 2, 'info' => L('未獲取到線上數據,平倉失敗')]);
}
// }else{
// $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
// }
}
public function confimclose()
{
$uid = userid();
if ($uid <= 0) {
$this->ajaxReturn(['code' => 3, 'data' => '', 'info' => '未登錄']);
}
if ($_POST) {
$id = trim(I('post.id'));
if ($id == '' || $id <= 0) {
$this->ajaxReturn(['code' => 2, 'info' => L('缺少重要参数')]);
exit();
}
$oinfo = M("leverorder")->where(array('id' => $id, 'status' => 2))->find();
if (empty($oinfo)) {
$this->ajaxReturn(['code' => 2, 'info' => L('订单不存在')]);
exit();
}
$lowercoin = $oinfo['coinname'];
if ($lowercoin == 'ukbusdt') {
$o_symbol = $oinfo['symbol'];
$symbol_arr = explode('/', $o_symbol);
$symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
$mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
$close = $mlist['new_price'];
} else {
$oy_list = do_request(oy_url($oinfo['coinname'], 'api/v5/market/ticker?instId='), []);
$oy_list = $oy_list->data[0];
// $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
// $result = $this->getprice($url);
// $pdata = $result['data'][0];
$close = $oy_list->last;//现价
}
//收益计算
//例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
//则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
$direction = $oinfo['direction'];
if ($direction == 1) { //做多
$profit = sprintf("%.2f", ($close - $oinfo['price']) * $oinfo['num']);
} elseif ($direction == 2) { //做空
$profit = sprintf("%.2f", ($oinfo['price'] - $close) * $oinfo['num']);
}
$data['data'] = $profit;
$data['code'] = 1;
$this->ajaxReturn($data);
exit();
}
}
public function clearorder()
{
$uid = userid();
if ($uid <= 0) {
$this->ajaxReturn(['code' => 3, 'data' => '', 'info' => '未登錄']);
}
// if($_POST){
$id = trim(I('get.id'));
if ($id == '' || $id <= 0) {
$this->ajaxReturn(['code' => 2, 'info' => L('缺少重要参数')]);
exit();
}
$oinfo = M("leverorder")->where(array('id' => $id))->find();
if (empty($oinfo)) {
$this->ajaxReturn(['code' => 2, 'info' => L('订单不存在')]);
exit();
}
if ($oinfo['status'] != 1) {
$this->ajaxReturn(['code' => 2, 'info' => L('该合约不能撤消')]);
exit();
}
$order = M("leverorder")->where(array('id' => $id))->find();
$user = M('levermoney')->where(['uid' => $uid])->find();
$bond_ubill = [
'uid' => $uid,
'username' => $user['username'],
'num' => $order['bond'],
'coinname' => 'lever',
'afternum' => $user['money'] + $order['bond'],
'type' => 20,
'addtime' => date("Y-m-d H:i:s", time()),
'st' => 1,
'remark' => '撤銷委託返還合約帳戶'
];// 扣除合约保证金
M("levermoney")->where(array('uid' => $uid))->setInc('money', $order['bond']);
M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $order['bond']);
M("bill")->add($bond_ubill);
$result = M("leverorder")->where(array('id' => $id))->delete();
if ($result) {
$this->ajaxReturn(['code' => 1, 'info' => L('撤消成功')]);
exit();
} else {
$this->ajaxReturn(['code' => 2, 'info' => L('撤消失败')]);
exit();
}
// }else{
// $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
// }
}
public function UpOrderHandle()
{
$uid = userid();
if ($uid <= 0) {
$this->ajaxReturn(['code' => 3, 'info' => '未登錄']);
}
// if($_POST){
$sysinfo = M("leversetting")->where(array('id' => 1))->find();
if ($sysinfo['lever_kg'] != 0) {
$this->ajaxReturn(['code' => 0, 'info' => L('暂停交易')]);
}
$lever_fee = $sysinfo['lever_fee'];//费率
$uinfo = M("user")->where(array('id' => $uid))->field('id,username')->find();
$direction = trim(I('get.direction')); //交易方向1做多2做空
$type = trim(I('get.type')); //1限价委托2市价委托
$symbolstr = trim(I('get.symbol')); //交易对
$mprice = trim(I('get.mprice')); //建仓或委托单价
$ggan = trim(I('get.ggan')); //杠杆倍数
$zyprice = trim(I('get.zyprice')); //止盈价格
if (!$zyprice) $zyprice = 0;
$zsprice = trim(I('get.zsprice')); //止损价格
if (!$zsprice) $zsprice = 0;
$num = trim(I('get.num')); //交易手数
$arr = explode("/", $symbolstr);
$symbol = strtolower($arr[0]);//交易对
$data['bond'] = trim(I('get.upbondmoney'));//保证金额
$data['fee'] = trim(I('get.fee'));
$oy_list = do_request(oy_url($symbol, 'api/v5/market/ticker?instId='), []);
$oy_list = $oy_list->data[0];
// $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
// $result = $this->getprice($url);
// $pdata = $result['data'][0];
$close = $oy_list->last;//现价
if ($type == 2) {
$mprice = $close;
$data['bond'] = ($oy_list->last * $num) / $ggan;
$data['fee'] = ($oy_list->last * $num) * 0.0001;
}
$fee = $data['fee'];
if ($mprice == 0) $this->ajaxReturn(['code' => 2, 'info' => '请输入委托价格']);
if ($zyprice > 0) {
if ($direction == 1) {
if ($zyprice < $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止盈價格,需大於委託價格']);
} elseif ($direction == 2) {
if ($zyprice > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止盈價格,需小於委託價格']);
}
}
if ($zsprice > 0) {
if ($direction == 1) {
if ($zsprice > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止損價格,需小於委託價格']);
} elseif ($direction == 2) {
if ($zsprice < $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止損價格,需大於委託價格']);
}
}
// if ($type == 1){
// if ($direction == 1){
// if ($close > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '做多,委託價格不能小於現價']);
// }elseif ($direction == 2){
// if ($close > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '做空,委託價格不能大於現價']);
// }
// }
$data['orderid'] = date("YmdHis", time()) . time() . $uid;
$data['uid'] = $uid;
$data['username'] = $uinfo['username'];
$data['direction'] = $direction;
$data['coinname'] = $symbol;
$data['symbol'] = $symbolstr;
$userMoney = M('levermoney')->where(['uid' => $uid])->find();
if ($type == 1) {
$data['price'] = 0;
$data['wt_price'] = $mprice;
$data['wttime'] = date('Y-m-d H:i:s', time());
$data['addtime'] = date('Y-m-d H:i:s', time());
$data['status'] = 1;
} elseif ($type == 2) {
$data['price'] = $mprice;
$data['wt_price'] = $mprice;
$data['wttime'] = date('Y-m-d H:i:s', time());
$data['addtime'] = date('Y-m-d H:i:s', time());
//手续费计算(开仓价格 * 开仓数量 * 手续费率) ,开仓时扣除手续费
$data['status'] = 2;
$minfo = M("user_coin")->where(array('userid' => $uid))->find();
if (($userMoney['money'] - $data['bond']) < $fee) {
$this->ajaxReturn(['code' => 2, 'info' => L('账户余额不足')]);
exit();
}
}
$data['type'] = $type;
$data['num'] = $num;
$data['ggan'] = $ggan;
$data['zyprice'] = $zyprice;
$data['zsprice'] = $zsprice;
$data['yk_status'] = '';
$data['ylmoney'] = 0;
$data['endtime'] = date('Y-m-d H:i:s', time());
if (!$userMoney) $this->ajaxReturn(['code' => 2, 'info' => L('合约账户不足')]);
if ($userMoney['money'] < $data['fee']) $this->ajaxReturn(['code' => 2, 'info' => L('合約賬戶不足')]);
if ($userMoney['money'] < $data['bond']) $this->ajaxReturn(['code' => 2, 'info' => L('合約賬戶不足')]);
$bond_ubill = [
'uid' => $uid,
'username' => $uinfo['username'],
'num' => $data['bond'],
'coinname' => 'lever',
'afternum' => $userMoney['money'] - $data['bond'],
'type' => 19,
'addtime' => date("Y-m-d H:i:s", time()),
'st' => 2,
'remark' => '提交委託扣除合約保證金'
];// 扣除合约保证金
M("levermoney")->where(array('uid' => $uid))->setDec('money', $data['bond']);
M("levermoney")->where(array('uid' => $uid))->setInc('moneyd', $data['bond']);
M("bill")->add($bond_ubill);
$result = M("leverorder")->add($data);
if ($result) {
if ($type == 2) {
//扣除手续费
$decre = M("levermoney")->where(array('uid' => $uid))->setDec("money", $fee);
$ubill['uid'] = $uid;
$ubill['username'] = $uinfo['username'];
$ubill['num'] = $fee;
$ubill['coinname'] = "lever";
$ubill['afternum'] = ($userMoney['money'] - $data['bond'] - $fee);
$ubill['type'] = 25;
$ubill['addtime'] = date("Y-m-d H:i:s", time());
$ubill['st'] = 2;
$ubill['remark'] = L('合约交易开仓手续费');
$ubillre = M("bill")->add($ubill);
}
$this->ajaxReturn(['code' => 1, 'info' => L('订单创建成功')]);
exit();
} else {
$this->ajaxReturn(['code' => 2, 'info' => L('订单创建失败')]);
exit();
}
// }else{
// $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
// }
}
//币币交易详情页面
public function index()
{
$uid = userid();
if ($uid <= 0) {
$this->redirect('Login/index');
}
$symbol = trim(I('get.symbol'));
if ($symbol == '') {
$symbol = "BTC";
}
$coin = strtolower($symbol);
$minfo = M("user_coin")->where(array('userid' => $uid))->find();
$usdt_blance = $minfo['usdt'];
$coin_blance = $minfo[$coin];
$this->assign('usdt_blance', $usdt_blance);
$this->assign('coin_blance', $coin_blance);
$this->assign('uid', $uid);
$coinname = $symbol . "/" . "USDT";
$this->assign('coinname', $coinname);
$this->assign('symbol', $symbol);
$sysinfo = M("leversetting")->where(array('id' => 1))->find();
$this->assign('sysinfo', $sysinfo);
$lmoneyinfo = M("levermoney")->where(array('uid' => $uid))->find();
if (empty($lmoneyinfo)) {
$this->update_levermoney($uid);
$levermoney = 0;
} else {
$levermoney = $lmoneyinfo['money'];
}
$this->assign('levermoney', $levermoney);
//委托订单
$wtlist = M("leverorder")->where(array('uid' => $uid, 'status' => 1))->order("id desc")->select();
$this->assign('wtlist', $wtlist);
//持仓订单
$cclist = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->order("id desc")->select();
foreach ($cclist as &$item) {
$lowercoin = $item['coinname'];
if ($lowercoin == 'ukbusdt') {
$o_symbol = $item['symbol'];
$symbol_arr = explode('/', $o_symbol);
$symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
$mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
$close = $mlist['new_price'];
} else {
$url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $lowercoin;
$result = $this->getprice($url);
$pdata = $result['data'][0];
$close = $pdata['close'];//现价
}
//收益计算
//例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
//则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
$direction = $item['direction'];
if ($direction == 1) { //做多
$profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
} elseif ($direction == 2) { //做空
$profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
}
if ($profit >= 0) {
$item['profit'] = '' . $profit . '';
} else {
$item['profit'] = '' . $profit . '';
}
}
$this->assign('cclist', $cclist);
$this->display();
}
public function real_time()
{
$uid = userid();
//持仓订单
$cclist = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->order("id desc")->select();
foreach ($cclist as &$item) {
$lowercoin = $item['coinname'];
if ($lowercoin == 'ukbusdt') {
$o_symbol = $item['symbol'];
$symbol_arr = explode('/', $o_symbol);
$symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
$mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
$close = $mlist['new_price'];
} else {
$url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $lowercoin;
$result = $this->getprice($url);
$pdata = $result['data'][0];
$close = $pdata['close'];//现价
}
//收益计算
//例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
//则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
$direction = $item['direction'];
if ($direction == 1) { //做多
$profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
} elseif ($direction == 2) { //做空
$profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
}
if ($profit >= 0) {
$item['profit'] = '' . $profit . '';
} else {
$item['profit'] = '' . $profit . '';
}
}
return $this->ajaxReturn($cclist);
}
//获取单个币种资产(usdz)
public function getmoneylever()
{
$uid = userid();
$wallinfo = M("levermoney")->where(array('uid' => $uid))->find();
$sy = 0;
$list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
foreach ($list as $item) {
if ($item['coinname'] == 'ukbusdt') {
$o_symbol = $item['symbol'];
$symbol_arr = explode('/', $o_symbol);
$symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
$mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
$close = $mlist['new_price'];
} else {
$url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $item['coinname'];
$result = $this->getprice($url);
$pdata = $result['data'][0];
$close = $pdata['close'];//现价
}
$direction = $item['direction'];
if ($direction == 1) { //做多
$profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
} elseif ($direction == 2) { //做空
$profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
}
if ($profit >= 0) {
$sy += $profit;
} else {
$sy -= $profit;
}
}
$where['name'] = "ukb_usdt";
$marketinfo = M("market")->where($where)->field("new_price")->find();
$usdzusdt = $marketinfo['new_price'];
$money = M('leverorder')->where(['uid' => $uid])->where('status = 1 or status = 2')->sum('bond');
$re['num'] = $wallinfo['money'] + $sy;
$re['numd'] = $money;
$re['zhe'] = 0;
$re['code'] = 1;
$this->ajaxReturn($re);
}
public function update_levermoney($uid)
{
$uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
$data['uid'] = $uid;
$data['username'] = $uinfo['username'];
$data['money'] = 0;
M("levermoney")->add($data);
}
//合约K线
public function trans()
{
$uid = userid();
$this->assign('uid', $uid);
$sytx = trim(I('get.sytx'));
$txarr = explode('/', $sytx);
$symbol = $txarr[0];
$market = strtolower($txarr[0] . $txarr[1]);
if ($symbol == '') {
$symbol = 'btc';
}
if ($market == '') {
$market = 'btcusdt';
}
$upmarket = strtoupper($symbol) . "/USDT";
$this->assign('upmarket', $upmarket);
$this->assign('market', $market);
$this->assign('smybol', $symbol);
$lowercoin = strtolower($symbol);
$cmarket = M("ctmarket")->where(array('coinname' => $lowercoin))->field("state")->find();
$state = $cmarket['state'];
$this->assign('state', $state);
$this->display();
}
/**
* gcb价格
* @return void
*/
public function gcb()
{
$market = M('market')->where(['name' => 'gcb_usdt'])->find();
$list1 = M('trade_log')->where(['market' => 'gcb_usdt'])->where(['type' => 1])->limit(6)->order('id DESC')->select();
$list2 = M('trade_log')->where(['market' => 'gcb_usdt'])->where(['type' => 2])->limit(6)->order('id DESC')->select();
$data = [
'name' => 'GCB-USDT',
'last' => number_format($market['new_price'], 2),
'sodUtc0' => number_format($market['hou_price'], 2),
'hou_price' => number_format($market['hou_price'], 2),
'min_price' => number_format($market['min_day'],2),
'max_price' => number_format($market['max_day'], 2),
'purchase' => $list1,
'sell' => $list2
];
$this->ajaxReturn(['code' => 1, 'data' => $data]);
}
/**
* gcb价格
* @return void
*/
public function gcbK()
{
$bar = I('get.bar');
$limit = I('get.limit');
if (!$limit) $limit = 100;
$type = 1;
if ($bar == '1m'){
$type = 1;
}elseif ($bar == '15m'){
$type = 15;
}elseif ($bar == '30m'){
$type = 30;
}elseif ($bar == '1H'){
$type = 60;
}elseif ($bar == '4H'){
$type = 240;
}elseif ($bar == '1D'){
$type = 1440;
}
$market = M('trade_json')->where(['market' => 'gcb_usdt'])->where(['type' => $type])->limit($limit)->order('id DESC')->select();
$data = [];
foreach ($market as $item)
{
$li = json_decode($item['data']);
$li[1] = number_format($li[1], 2);
$li[2] = number_format($li[2], 2);
$li[3] = number_format($li[3], 2);
$li[4] = number_format($li[4], 2);
$data[] =$li;
}
$this->ajaxReturn(['code' => 1, 'data' => $data]);
}
/**GCB历史
* @return void
*/
public function gcbm()
{
$list1 = M('trade_log')->where(['market' => 'gcb_usdt'])->limit(50)->order('id DESC')->select();
$this->ajaxReturn(['code' => 1, 'data' => $list1]);
}
//获取行情数据
public function getprice($api)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$result = json_decode(curl_exec($ch), true);
return $result;
}
}
?>