123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852 |
- <?php
- namespace Mobile\Controller;
- use http\Env\Request;
- class LevertadeController extends MobileController
- {
- protected function _initialize()
- {
- parent::_initialize();
- $allow_action = array('laverorder', "gcbm","gcb", "gcbk",'ctmarket', 'bond', "index", "trans", "update_levermoney", "UpOrderHandle", "tradlist", "clearorder", "closeorder", "getprice", "confimclose", 'real_time');
- if (!in_array(ACTION_NAME, $allow_action)) {
- $this->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'] = '<span style="color:#0ecb81;">' . $profit . '</span>';
- } else {
- $item['profit'] = '<span style="color:#f5465c;">' . $profit . '</span>';
- }
- }
- $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'] = '<span style="color:#0ecb81;">' . $profit . '</span>';
- } else {
- $item['profit'] = '<span style="color:#f5465c;">' . $profit . '</span>';
- }
- }
- 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;
- }
- }
- ?>
|