LevertadeController.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <?php
  2. namespace Mobile\Controller;
  3. use http\Env\Request;
  4. class LevertadeController extends MobileController
  5. {
  6. protected function _initialize()
  7. {
  8. parent::_initialize();
  9. $allow_action = array('laverorder', "gcbm","gcb", "gcbk",'ctmarket', 'bond', "index", "trans", "update_levermoney", "UpOrderHandle", "tradlist", "clearorder", "closeorder", "getprice", "confimclose", 'real_time');
  10. if (!in_array(ACTION_NAME, $allow_action)) {
  11. $this->error(L("非法操作"));
  12. }
  13. }
  14. public function laverorder($type = null)
  15. {
  16. $uid = userid();
  17. if ($uid <= 0) {
  18. $this->ajaxReturn(['code' => 3, 'data' => '', 'info' => L('未登录')]);
  19. }
  20. $page = I('get.page');
  21. if (!$page) $page = 1;
  22. $limit = I('get.limit');
  23. if (!$limit) $limit = 10;
  24. $list = [];
  25. if ($type == 1) {
  26. //委托订单
  27. $list = M("leverorder")->where(array('uid' => $uid, 'status' => 1))->page($page, $limit)->order("id desc")->select();
  28. } elseif ($type == 2) {
  29. //持仓订单
  30. $list = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->page($page, $limit)->order("id desc")->select();
  31. // foreach ($list as &$item) {
  32. // $oy_list = do_request(oy_url($item['coinname'], 'api/v5/market/index-tickers?instId='), []);
  33. // $oy_list = $oy_list->data[0];
  34. //// $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  35. //// $result = $this->getprice($url);
  36. //// $pdata = $result['data'][0];
  37. // $close = $oy_list->idxPx;//现价
  38. // if ($item['direction'] == 1) { //做多
  39. // $item['profit'] = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  40. // } elseif ($item['direction'] == 2) { //做空
  41. // $item['profit'] = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  42. // }
  43. // }
  44. } elseif ($type == 3) {
  45. //成交订单
  46. $list = M("leverorder")->where("uid = {$uid} and (status = 3 or status = 4)")->page($page, $limit)->order("id desc")->select();
  47. }
  48. $data = [
  49. 'code' => 1,
  50. 'list' => $list,//委托
  51. 'info' => ''
  52. ];
  53. $this->ajaxReturn($data);
  54. }
  55. /**
  56. * 市场配置
  57. * @return void
  58. */
  59. public function ctmarket()
  60. {
  61. $list = M('ctmarket')->where(['status' => 1])->select();
  62. $data = [
  63. 'code' => 1,
  64. 'list' => $list,//委托
  65. 'info' => ''
  66. ];
  67. $this->ajaxReturn($data);
  68. }
  69. /**
  70. * 计算保证金
  71. * @return void
  72. */
  73. public function bond()
  74. {
  75. $uid = userid();
  76. if ($uid <= 0) {
  77. $this->ajaxReturn(['code' => 3, 'data' => '', 'info' => L('未登录')]);
  78. }
  79. $name = I('get.name');//币种
  80. $num = I('get.num');//手数
  81. $gann = I('get.gann');//杠杆
  82. $oy_list = do_request(oy_url($name, 'api/v5/market/ticker?instId='), []);
  83. $oy_list = $oy_list->data[0];
  84. $bong = ($oy_list->last * $num) / $gann;
  85. $fee = ($oy_list->last * $num) * 0.0001;
  86. $this->ajaxReturn(['code' => 1, 'data' => ['bong' => $bong, 'fee' => $fee], 'info' => '']);
  87. }
  88. public function tradlist()
  89. {
  90. $uid = userid();
  91. if ($uid <= 0) {
  92. $this->redirect('Login/index');
  93. }
  94. //委托订单
  95. $list = M("leverorder")->where(array('uid' => $uid, 'status' => 1))->order("id desc")->field("id,direction,symbol,wt_price,num,ggan,wttime")->select();
  96. $this->assign('list', $list);
  97. //持仓订单
  98. $alllist = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->order("id desc")->select();
  99. $this->assign('alllist', $alllist);
  100. //成交订单
  101. $finishlist = M("leverorder")->where("uid = {$uid} and (status = 3 or status = 4)")->order("id desc")->select();
  102. $this->assign('finishlist', $finishlist);
  103. $this->display();
  104. }
  105. public function closeorder()
  106. {
  107. $uid = userid();
  108. if ($uid <= 0) {
  109. $this->ajaxReturn(['code' => 3, 'data' => '', 'info' => L('未登录')]);
  110. }
  111. // if($_POST){
  112. $id = trim(I('get.id'));
  113. if ($id == '' || $id <= 0) {
  114. $this->ajaxReturn(['code' => 2, 'info' => L('缺少重要参数')]);
  115. exit();
  116. }
  117. $oinfo = M("leverorder")->where(array('id' => $id, 'status' => 2))->find();
  118. if (empty($oinfo)) {
  119. $this->ajaxReturn(['code' => 2, 'info' => L('订单不存在')]);
  120. exit();
  121. }
  122. $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
  123. $lowercoin = $oinfo['coinname'];
  124. if ($lowercoin == 'ukbusdt') {
  125. $o_symbol = $oinfo['symbol'];
  126. $symbol_arr = explode('/', $o_symbol);
  127. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  128. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  129. $close = $mlist['new_price'];
  130. } else {
  131. $oy_list = do_request(oy_url($oinfo['coinname'], 'api/v5/market/ticker?instId='), []);
  132. $oy_list = $oy_list->data[0];
  133. // $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  134. // $result = $this->getprice($url);
  135. // $pdata = $result['data'][0];
  136. $close = $oy_list->last;//现价
  137. }
  138. if ($close){
  139. //收益计算
  140. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  141. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  142. $direction = $oinfo['direction'];
  143. if ($direction == 1) { //做多
  144. $profit = sprintf("%.2f", ($close - $oinfo['price']) * $oinfo['num']);
  145. } elseif ($direction == 2) { //做空
  146. $profit = sprintf("%.2f", ($oinfo['price'] - $close) * $oinfo['num']);
  147. }
  148. if ($profit >= 0) {
  149. $yk_status = 1;
  150. } else {
  151. $yk_status = 2;
  152. }
  153. $tprofit = abs($profit);
  154. $data['pc_price'] = $close;
  155. $data['status'] = 3;
  156. $data['yk_status'] = $yk_status;
  157. $data['ylmoney'] = $tprofit;
  158. $data['endtime'] = date("Y-m-d H:i:s", time());
  159. $result = M("leverorder")->where(array('id' => $id, 'status' => 2))->save($data);
  160. $user = M('levermoney')->where(['uid' => $uid])->find();
  161. if ($result) {
  162. // $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  163. if ($profit >= 0) {
  164. M("levermoney")->where(array('uid' => $uid))->setInc('money', $tprofit);
  165. $ubill['uid'] = $uid;
  166. $ubill['username'] = $uinfo['username'];
  167. $ubill['num'] = $tprofit;
  168. $ubill['coinname'] = "lever";
  169. $ubill['afternum'] = $user['money'] + $tprofit;
  170. $ubill['type'] = 20;
  171. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  172. $ubill['st'] = 1;
  173. $ubill['remark'] = L('合约交易手动平仓盈利收益');
  174. $ubillre = M("bill")->add($ubill);
  175. $bond_ubill = [
  176. 'uid' => $uid,
  177. 'username' => $uinfo['username'],
  178. 'num' => $oinfo['bond'],
  179. 'coinname' => 'lever',
  180. 'afternum' => $user['money'] + $oinfo['bond'],
  181. 'type' => 19,
  182. 'addtime' => date("Y-m-d H:i:s", time()),
  183. 'st' => 1,
  184. 'remark' => '合約手動平倉返還佔用保證金'
  185. ];// 扣除合约保证金
  186. M("levermoney")->where(array('uid' => $uid))->setInc('money', $oinfo['bond']);
  187. M("bill")->add($bond_ubill);
  188. } else {
  189. if ($user['money'] >= $tprofit) {
  190. $price = ($user['money']+$oinfo['bond']) - $tprofit;
  191. M("levermoney")->where(array('uid' => $uid))->setInc('money', $oinfo['bond']);
  192. $ubill['uid'] = $uid;
  193. $ubill['username'] = $uinfo['username'];
  194. $ubill['num'] = $oinfo['bond'];
  195. $ubill['coinname'] = "lever";
  196. $ubill['afternum'] = $user['money'] + $oinfo['bond'];
  197. $ubill['type'] = 20;
  198. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  199. $ubill['st'] = 1;
  200. $ubill['remark'] = '合約交易手動平倉,退還保證金';
  201. $ubillre = M("bill")->add($ubill);
  202. M("levermoney")->where(array('uid' => $uid))->setDec('money', $tprofit);
  203. $ubill['num'] = $tprofit;
  204. $ubill['afternum'] = $price;
  205. $ubill['type'] = 20;
  206. $ubill['st'] = 2;
  207. $ubill['remark'] = '合約交易手動平倉虧損';
  208. $ubillre = M("bill")->add($ubill);
  209. } else {
  210. $bond = $user['money'] + $oinfo['bond'];
  211. if ($bond >= $tprofit) {
  212. $user_bond = $oinfo['bond'] - ($tprofit - $user['money']);
  213. M("levermoney")->where(array('uid' => $uid))->setInc('money', $user_bond);
  214. $ubill['uid'] = $uid;
  215. $ubill['username'] = $uinfo['username'];
  216. $ubill['num'] = $user_bond;
  217. $ubill['coinname'] = "lever";
  218. $ubill['afternum'] = $user_bond;
  219. $ubill['type'] = 20;
  220. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  221. $ubill['st'] = 1;
  222. $ubill['remark'] = '合約交易手動平倉虧損退回剩餘保證金';
  223. $ubillre = M("bill")->add($ubill);
  224. M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
  225. $ubill['num'] = $user['money'];
  226. $ubill['afternum'] = 0;
  227. $ubill['type'] = 20;
  228. $ubill['st'] = 2;
  229. $ubill['remark'] = '合約交易手動平倉虧損';
  230. $ubillre = M("bill")->add($ubill);
  231. } else {
  232. M("levermoney")->where(array('uid' => $uid))->setDec('money', $user['money']);
  233. $ubill['uid'] = $uid;
  234. $ubill['username'] = $uinfo['username'];
  235. $ubill['num'] = $user['money'];
  236. $ubill['coinname'] = "lever";
  237. $ubill['afternum'] = 0;
  238. $ubill['type'] = 20;
  239. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  240. $ubill['st'] = 1;
  241. $ubill['remark'] = '合約交易手動平倉虧損完';
  242. $ubillre = M("bill")->add($ubill);
  243. }
  244. }
  245. }
  246. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $oinfo['bond']);
  247. $this->ajaxReturn(['code' => 1, 'info' => L('操作成功')]);
  248. exit();
  249. } else {
  250. $this->ajaxReturn(['code' => 2, 'info' => L('操作失败')]);
  251. exit();
  252. }
  253. }else{
  254. $this->ajaxReturn(['code' => 2, 'info' => L('未獲取到線上數據,平倉失敗')]);
  255. }
  256. // }else{
  257. // $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
  258. // }
  259. }
  260. public function confimclose()
  261. {
  262. $uid = userid();
  263. if ($uid <= 0) {
  264. $this->ajaxReturn(['code' => 3, 'data' => '', 'info' => '未登錄']);
  265. }
  266. if ($_POST) {
  267. $id = trim(I('post.id'));
  268. if ($id == '' || $id <= 0) {
  269. $this->ajaxReturn(['code' => 2, 'info' => L('缺少重要参数')]);
  270. exit();
  271. }
  272. $oinfo = M("leverorder")->where(array('id' => $id, 'status' => 2))->find();
  273. if (empty($oinfo)) {
  274. $this->ajaxReturn(['code' => 2, 'info' => L('订单不存在')]);
  275. exit();
  276. }
  277. $lowercoin = $oinfo['coinname'];
  278. if ($lowercoin == 'ukbusdt') {
  279. $o_symbol = $oinfo['symbol'];
  280. $symbol_arr = explode('/', $o_symbol);
  281. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  282. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  283. $close = $mlist['new_price'];
  284. } else {
  285. $oy_list = do_request(oy_url($oinfo['coinname'], 'api/v5/market/ticker?instId='), []);
  286. $oy_list = $oy_list->data[0];
  287. // $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  288. // $result = $this->getprice($url);
  289. // $pdata = $result['data'][0];
  290. $close = $oy_list->last;//现价
  291. }
  292. //收益计算
  293. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  294. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  295. $direction = $oinfo['direction'];
  296. if ($direction == 1) { //做多
  297. $profit = sprintf("%.2f", ($close - $oinfo['price']) * $oinfo['num']);
  298. } elseif ($direction == 2) { //做空
  299. $profit = sprintf("%.2f", ($oinfo['price'] - $close) * $oinfo['num']);
  300. }
  301. $data['data'] = $profit;
  302. $data['code'] = 1;
  303. $this->ajaxReturn($data);
  304. exit();
  305. }
  306. }
  307. public function clearorder()
  308. {
  309. $uid = userid();
  310. if ($uid <= 0) {
  311. $this->ajaxReturn(['code' => 3, 'data' => '', 'info' => '未登錄']);
  312. }
  313. // if($_POST){
  314. $id = trim(I('get.id'));
  315. if ($id == '' || $id <= 0) {
  316. $this->ajaxReturn(['code' => 2, 'info' => L('缺少重要参数')]);
  317. exit();
  318. }
  319. $oinfo = M("leverorder")->where(array('id' => $id))->find();
  320. if (empty($oinfo)) {
  321. $this->ajaxReturn(['code' => 2, 'info' => L('订单不存在')]);
  322. exit();
  323. }
  324. if ($oinfo['status'] != 1) {
  325. $this->ajaxReturn(['code' => 2, 'info' => L('该合约不能撤消')]);
  326. exit();
  327. }
  328. $order = M("leverorder")->where(array('id' => $id))->find();
  329. $user = M('levermoney')->where(['uid' => $uid])->find();
  330. $bond_ubill = [
  331. 'uid' => $uid,
  332. 'username' => $user['username'],
  333. 'num' => $order['bond'],
  334. 'coinname' => 'lever',
  335. 'afternum' => $user['money'] + $order['bond'],
  336. 'type' => 20,
  337. 'addtime' => date("Y-m-d H:i:s", time()),
  338. 'st' => 1,
  339. 'remark' => '撤銷委託返還合約帳戶'
  340. ];// 扣除合约保证金
  341. M("levermoney")->where(array('uid' => $uid))->setInc('money', $order['bond']);
  342. M("levermoney")->where(array('uid' => $uid))->setDec('moneyd', $order['bond']);
  343. M("bill")->add($bond_ubill);
  344. $result = M("leverorder")->where(array('id' => $id))->delete();
  345. if ($result) {
  346. $this->ajaxReturn(['code' => 1, 'info' => L('撤消成功')]);
  347. exit();
  348. } else {
  349. $this->ajaxReturn(['code' => 2, 'info' => L('撤消失败')]);
  350. exit();
  351. }
  352. // }else{
  353. // $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
  354. // }
  355. }
  356. public function UpOrderHandle()
  357. {
  358. $uid = userid();
  359. if ($uid <= 0) {
  360. $this->ajaxReturn(['code' => 3, 'info' => '未登錄']);
  361. }
  362. // if($_POST){
  363. $sysinfo = M("leversetting")->where(array('id' => 1))->find();
  364. if ($sysinfo['lever_kg'] != 0) {
  365. $this->ajaxReturn(['code' => 0, 'info' => L('暂停交易')]);
  366. }
  367. $lever_fee = $sysinfo['lever_fee'];//费率
  368. $uinfo = M("user")->where(array('id' => $uid))->field('id,username')->find();
  369. $direction = trim(I('get.direction')); //交易方向1做多2做空
  370. $type = trim(I('get.type')); //1限价委托2市价委托
  371. $symbolstr = trim(I('get.symbol')); //交易对
  372. $mprice = trim(I('get.mprice')); //建仓或委托单价
  373. $ggan = trim(I('get.ggan')); //杠杆倍数
  374. $zyprice = trim(I('get.zyprice')); //止盈价格
  375. if (!$zyprice) $zyprice = 0;
  376. $zsprice = trim(I('get.zsprice')); //止损价格
  377. if (!$zsprice) $zsprice = 0;
  378. $num = trim(I('get.num')); //交易手数
  379. $arr = explode("/", $symbolstr);
  380. $symbol = strtolower($arr[0]);//交易对
  381. $data['bond'] = trim(I('get.upbondmoney'));//保证金额
  382. $data['fee'] = trim(I('get.fee'));
  383. $oy_list = do_request(oy_url($symbol, 'api/v5/market/ticker?instId='), []);
  384. $oy_list = $oy_list->data[0];
  385. // $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  386. // $result = $this->getprice($url);
  387. // $pdata = $result['data'][0];
  388. $close = $oy_list->last;//现价
  389. if ($type == 2) {
  390. $mprice = $close;
  391. $data['bond'] = ($oy_list->last * $num) / $ggan;
  392. $data['fee'] = ($oy_list->last * $num) * 0.0001;
  393. }
  394. $fee = $data['fee'];
  395. if ($mprice == 0) $this->ajaxReturn(['code' => 2, 'info' => '请输入委托价格']);
  396. if ($zyprice > 0) {
  397. if ($direction == 1) {
  398. if ($zyprice < $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止盈價格,需大於委託價格']);
  399. } elseif ($direction == 2) {
  400. if ($zyprice > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止盈價格,需小於委託價格']);
  401. }
  402. }
  403. if ($zsprice > 0) {
  404. if ($direction == 1) {
  405. if ($zsprice > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止損價格,需小於委託價格']);
  406. } elseif ($direction == 2) {
  407. if ($zsprice < $mprice) $this->ajaxReturn(['code' => 2, 'info' => '止損價格,需大於委託價格']);
  408. }
  409. }
  410. // if ($type == 1){
  411. // if ($direction == 1){
  412. // if ($close > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '做多,委託價格不能小於現價']);
  413. // }elseif ($direction == 2){
  414. // if ($close > $mprice) $this->ajaxReturn(['code' => 2, 'info' => '做空,委託價格不能大於現價']);
  415. // }
  416. // }
  417. $data['orderid'] = date("YmdHis", time()) . time() . $uid;
  418. $data['uid'] = $uid;
  419. $data['username'] = $uinfo['username'];
  420. $data['direction'] = $direction;
  421. $data['coinname'] = $symbol;
  422. $data['symbol'] = $symbolstr;
  423. $userMoney = M('levermoney')->where(['uid' => $uid])->find();
  424. if ($type == 1) {
  425. $data['price'] = 0;
  426. $data['wt_price'] = $mprice;
  427. $data['wttime'] = date('Y-m-d H:i:s', time());
  428. $data['addtime'] = date('Y-m-d H:i:s', time());
  429. $data['status'] = 1;
  430. } elseif ($type == 2) {
  431. $data['price'] = $mprice;
  432. $data['wt_price'] = $mprice;
  433. $data['wttime'] = date('Y-m-d H:i:s', time());
  434. $data['addtime'] = date('Y-m-d H:i:s', time());
  435. //手续费计算(开仓价格 * 开仓数量 * 手续费率) ,开仓时扣除手续费
  436. $data['status'] = 2;
  437. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  438. if (($userMoney['money'] - $data['bond']) < $fee) {
  439. $this->ajaxReturn(['code' => 2, 'info' => L('账户余额不足')]);
  440. exit();
  441. }
  442. }
  443. $data['type'] = $type;
  444. $data['num'] = $num;
  445. $data['ggan'] = $ggan;
  446. $data['zyprice'] = $zyprice;
  447. $data['zsprice'] = $zsprice;
  448. $data['yk_status'] = '';
  449. $data['ylmoney'] = 0;
  450. $data['endtime'] = date('Y-m-d H:i:s', time());
  451. if (!$userMoney) $this->ajaxReturn(['code' => 2, 'info' => L('合约账户不足')]);
  452. if ($userMoney['money'] < $data['fee']) $this->ajaxReturn(['code' => 2, 'info' => L('合約賬戶不足')]);
  453. if ($userMoney['money'] < $data['bond']) $this->ajaxReturn(['code' => 2, 'info' => L('合約賬戶不足')]);
  454. $bond_ubill = [
  455. 'uid' => $uid,
  456. 'username' => $uinfo['username'],
  457. 'num' => $data['bond'],
  458. 'coinname' => 'lever',
  459. 'afternum' => $userMoney['money'] - $data['bond'],
  460. 'type' => 19,
  461. 'addtime' => date("Y-m-d H:i:s", time()),
  462. 'st' => 2,
  463. 'remark' => '提交委託扣除合約保證金'
  464. ];// 扣除合约保证金
  465. M("levermoney")->where(array('uid' => $uid))->setDec('money', $data['bond']);
  466. M("levermoney")->where(array('uid' => $uid))->setInc('moneyd', $data['bond']);
  467. M("bill")->add($bond_ubill);
  468. $result = M("leverorder")->add($data);
  469. if ($result) {
  470. if ($type == 2) {
  471. //扣除手续费
  472. $decre = M("levermoney")->where(array('uid' => $uid))->setDec("money", $fee);
  473. $ubill['uid'] = $uid;
  474. $ubill['username'] = $uinfo['username'];
  475. $ubill['num'] = $fee;
  476. $ubill['coinname'] = "lever";
  477. $ubill['afternum'] = ($userMoney['money'] - $data['bond'] - $fee);
  478. $ubill['type'] = 25;
  479. $ubill['addtime'] = date("Y-m-d H:i:s", time());
  480. $ubill['st'] = 2;
  481. $ubill['remark'] = L('合约交易开仓手续费');
  482. $ubillre = M("bill")->add($ubill);
  483. }
  484. $this->ajaxReturn(['code' => 1, 'info' => L('订单创建成功')]);
  485. exit();
  486. } else {
  487. $this->ajaxReturn(['code' => 2, 'info' => L('订单创建失败')]);
  488. exit();
  489. }
  490. // }else{
  491. // $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
  492. // }
  493. }
  494. //币币交易详情页面
  495. public function index()
  496. {
  497. $uid = userid();
  498. if ($uid <= 0) {
  499. $this->redirect('Login/index');
  500. }
  501. $symbol = trim(I('get.symbol'));
  502. if ($symbol == '') {
  503. $symbol = "BTC";
  504. }
  505. $coin = strtolower($symbol);
  506. $minfo = M("user_coin")->where(array('userid' => $uid))->find();
  507. $usdt_blance = $minfo['usdt'];
  508. $coin_blance = $minfo[$coin];
  509. $this->assign('usdt_blance', $usdt_blance);
  510. $this->assign('coin_blance', $coin_blance);
  511. $this->assign('uid', $uid);
  512. $coinname = $symbol . "/" . "USDT";
  513. $this->assign('coinname', $coinname);
  514. $this->assign('symbol', $symbol);
  515. $sysinfo = M("leversetting")->where(array('id' => 1))->find();
  516. $this->assign('sysinfo', $sysinfo);
  517. $lmoneyinfo = M("levermoney")->where(array('uid' => $uid))->find();
  518. if (empty($lmoneyinfo)) {
  519. $this->update_levermoney($uid);
  520. $levermoney = 0;
  521. } else {
  522. $levermoney = $lmoneyinfo['money'];
  523. }
  524. $this->assign('levermoney', $levermoney);
  525. //委托订单
  526. $wtlist = M("leverorder")->where(array('uid' => $uid, 'status' => 1))->order("id desc")->select();
  527. $this->assign('wtlist', $wtlist);
  528. //持仓订单
  529. $cclist = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->order("id desc")->select();
  530. foreach ($cclist as &$item) {
  531. $lowercoin = $item['coinname'];
  532. if ($lowercoin == 'ukbusdt') {
  533. $o_symbol = $item['symbol'];
  534. $symbol_arr = explode('/', $o_symbol);
  535. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  536. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  537. $close = $mlist['new_price'];
  538. } else {
  539. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $lowercoin;
  540. $result = $this->getprice($url);
  541. $pdata = $result['data'][0];
  542. $close = $pdata['close'];//现价
  543. }
  544. //收益计算
  545. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  546. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  547. $direction = $item['direction'];
  548. if ($direction == 1) { //做多
  549. $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  550. } elseif ($direction == 2) { //做空
  551. $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  552. }
  553. if ($profit >= 0) {
  554. $item['profit'] = '<span style="color:#0ecb81;">' . $profit . '</span>';
  555. } else {
  556. $item['profit'] = '<span style="color:#f5465c;">' . $profit . '</span>';
  557. }
  558. }
  559. $this->assign('cclist', $cclist);
  560. $this->display();
  561. }
  562. public function real_time()
  563. {
  564. $uid = userid();
  565. //持仓订单
  566. $cclist = M("leverorder")->where(array('uid' => $uid, 'status' => 2))->order("id desc")->select();
  567. foreach ($cclist as &$item) {
  568. $lowercoin = $item['coinname'];
  569. if ($lowercoin == 'ukbusdt') {
  570. $o_symbol = $item['symbol'];
  571. $symbol_arr = explode('/', $o_symbol);
  572. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  573. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  574. $close = $mlist['new_price'];
  575. } else {
  576. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $lowercoin;
  577. $result = $this->getprice($url);
  578. $pdata = $result['data'][0];
  579. $close = $pdata['close'];//现价
  580. }
  581. //收益计算
  582. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  583. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  584. $direction = $item['direction'];
  585. if ($direction == 1) { //做多
  586. $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  587. } elseif ($direction == 2) { //做空
  588. $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  589. }
  590. if ($profit >= 0) {
  591. $item['profit'] = '<span style="color:#0ecb81;">' . $profit . '</span>';
  592. } else {
  593. $item['profit'] = '<span style="color:#f5465c;">' . $profit . '</span>';
  594. }
  595. }
  596. return $this->ajaxReturn($cclist);
  597. }
  598. //获取单个币种资产(usdz)
  599. public function getmoneylever()
  600. {
  601. $uid = userid();
  602. $wallinfo = M("levermoney")->where(array('uid' => $uid))->find();
  603. $sy = 0;
  604. $list = M('leverorder')->where(['uid' => $uid])->where(['status' => 2])->select();
  605. foreach ($list as $item) {
  606. if ($item['coinname'] == 'ukbusdt') {
  607. $o_symbol = $item['symbol'];
  608. $symbol_arr = explode('/', $o_symbol);
  609. $symbol = strtolower($symbol_arr[0]) . "_" . strtolower($symbol_arr[1]);
  610. $mlist = M("market")->where(array('name' => $symbol))->field("new_price,min_price")->find();
  611. $close = $mlist['new_price'];
  612. } else {
  613. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=" . $item['coinname'];
  614. $result = $this->getprice($url);
  615. $pdata = $result['data'][0];
  616. $close = $pdata['close'];//现价
  617. }
  618. $direction = $item['direction'];
  619. if ($direction == 1) { //做多
  620. $profit = sprintf("%.2f", ($close - $item['price']) * $item['num']);
  621. } elseif ($direction == 2) { //做空
  622. $profit = sprintf("%.2f", ($item['price'] - $close) * $item['num']);
  623. }
  624. if ($profit >= 0) {
  625. $sy += $profit;
  626. } else {
  627. $sy -= $profit;
  628. }
  629. }
  630. $where['name'] = "ukb_usdt";
  631. $marketinfo = M("market")->where($where)->field("new_price")->find();
  632. $usdzusdt = $marketinfo['new_price'];
  633. $money = M('leverorder')->where(['uid' => $uid])->where('status = 1 or status = 2')->sum('bond');
  634. $re['num'] = $wallinfo['money'] + $sy;
  635. $re['numd'] = $money;
  636. $re['zhe'] = 0;
  637. $re['code'] = 1;
  638. $this->ajaxReturn($re);
  639. }
  640. public function update_levermoney($uid)
  641. {
  642. $uinfo = M("user")->where(array('id' => $uid))->field("id,username")->find();
  643. $data['uid'] = $uid;
  644. $data['username'] = $uinfo['username'];
  645. $data['money'] = 0;
  646. M("levermoney")->add($data);
  647. }
  648. //合约K线
  649. public function trans()
  650. {
  651. $uid = userid();
  652. $this->assign('uid', $uid);
  653. $sytx = trim(I('get.sytx'));
  654. $txarr = explode('/', $sytx);
  655. $symbol = $txarr[0];
  656. $market = strtolower($txarr[0] . $txarr[1]);
  657. if ($symbol == '') {
  658. $symbol = 'btc';
  659. }
  660. if ($market == '') {
  661. $market = 'btcusdt';
  662. }
  663. $upmarket = strtoupper($symbol) . "/USDT";
  664. $this->assign('upmarket', $upmarket);
  665. $this->assign('market', $market);
  666. $this->assign('smybol', $symbol);
  667. $lowercoin = strtolower($symbol);
  668. $cmarket = M("ctmarket")->where(array('coinname' => $lowercoin))->field("state")->find();
  669. $state = $cmarket['state'];
  670. $this->assign('state', $state);
  671. $this->display();
  672. }
  673. /**
  674. * gcb价格
  675. * @return void
  676. */
  677. public function gcb()
  678. {
  679. $market = M('market')->where(['name' => 'gcb_usdt'])->find();
  680. $list1 = M('trade_log')->where(['market' => 'gcb_usdt'])->where(['type' => 1])->limit(6)->order('id DESC')->select();
  681. $list2 = M('trade_log')->where(['market' => 'gcb_usdt'])->where(['type' => 2])->limit(6)->order('id DESC')->select();
  682. $data = [
  683. 'name' => 'GCB-USDT',
  684. 'last' => number_format($market['new_price'], 2),
  685. 'sodUtc0' => number_format($market['hou_price'], 2),
  686. 'hou_price' => number_format($market['hou_price'], 2),
  687. 'min_price' => number_format($market['min_day'],2),
  688. 'max_price' => number_format($market['max_day'], 2),
  689. 'purchase' => $list1,
  690. 'sell' => $list2
  691. ];
  692. $this->ajaxReturn(['code' => 1, 'data' => $data]);
  693. }
  694. /**
  695. * gcb价格
  696. * @return void
  697. */
  698. public function gcbK()
  699. {
  700. $bar = I('get.bar');
  701. $limit = I('get.limit');
  702. if (!$limit) $limit = 100;
  703. $type = 1;
  704. if ($bar == '1m'){
  705. $type = 1;
  706. }elseif ($bar == '15m'){
  707. $type = 15;
  708. }elseif ($bar == '30m'){
  709. $type = 30;
  710. }elseif ($bar == '1H'){
  711. $type = 60;
  712. }elseif ($bar == '4H'){
  713. $type = 240;
  714. }elseif ($bar == '1D'){
  715. $type = 1440;
  716. }
  717. $market = M('trade_json')->where(['market' => 'gcb_usdt'])->where(['type' => $type])->limit($limit)->order('id DESC')->select();
  718. $data = [];
  719. foreach ($market as $item)
  720. {
  721. $li = json_decode($item['data']);
  722. $li[1] = number_format($li[1], 2);
  723. $li[2] = number_format($li[2], 2);
  724. $li[3] = number_format($li[3], 2);
  725. $li[4] = number_format($li[4], 2);
  726. $data[] =$li;
  727. }
  728. $this->ajaxReturn(['code' => 1, 'data' => $data]);
  729. }
  730. /**GCB历史
  731. * @return void
  732. */
  733. public function gcbm()
  734. {
  735. $list1 = M('trade_log')->where(['market' => 'gcb_usdt'])->limit(50)->order('id DESC')->select();
  736. $this->ajaxReturn(['code' => 1, 'data' => $list1]);
  737. }
  738. //获取行情数据
  739. public function getprice($api)
  740. {
  741. $ch = curl_init();
  742. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  743. curl_setopt($ch, CURLOPT_URL, $api);
  744. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  745. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  746. $result = json_decode(curl_exec($ch), true);
  747. return $result;
  748. }
  749. }
  750. ?>