LevertadeController.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. namespace Home\Controller;
  3. class LevertadeController extends HomeController
  4. {
  5. protected function _initialize()
  6. {
  7. parent::_initialize(); $allow_action=array("index","trans","update_levermoney","UpOrderHandle","tradlist","clearorder","closeorder","getprice","confimclose","tradlist","ordinary","ordinaryh","cclist","wtlist","pclist");
  8. if(!in_array(ACTION_NAME,$allow_action)){
  9. $this->error(L("非法操作"));
  10. }
  11. }
  12. public function pclist(){
  13. $uid = userid();
  14. if($uid <= 0){
  15. $this->redirect('Login/index');
  16. }
  17. $finishlist = M("leverorder")->where("uid = {$uid} and (status = 3 or status = 3)")->order("id desc")->select();
  18. $this->assign('list',$finishlist);
  19. $this->display();
  20. }
  21. public function wtlist(){
  22. $uid = userid();
  23. if($uid <= 0){
  24. $this->redirect('Login/index');
  25. }
  26. //持仓订单
  27. $list = M("leverorder")->where(array('uid'=>$uid,'status'=>1))->order("id desc")->select();
  28. $this->assign('list',$list);
  29. $this->display();
  30. }
  31. public function cclist(){
  32. $uid = userid();
  33. if($uid <= 0){
  34. $this->redirect('Login/index');
  35. }
  36. //持仓订单
  37. $alllist = M("leverorder")->where(array('uid'=>$uid,'status'=>2))->order("id desc")->select();
  38. $this->assign('list',$alllist);
  39. $this->display();
  40. }
  41. public function tradlist(){
  42. $uid = userid();
  43. if($uid <= 0){
  44. $this->redirect('Login/index');
  45. }
  46. //委托订单
  47. $list = M("leverorder")->where(array('uid'=>$uid,'status'=>1))->order("id desc")->field("id,direction,symbol,wt_price,num,ggan,wttime")->select();
  48. $this->assign('list',$list);
  49. //持仓订单
  50. $alllist = M("leverorder")->where(array('uid'=>$uid,'status'=>2))->order("id desc")->select();
  51. $this->assign('alllist',$alllist);
  52. //成交订单
  53. $finishlist = M("leverorder")->where("uid = {$uid} and (status = 3 or status = 3)")->order("id desc")->select();
  54. $this->assign('finishlist',$finishlist);
  55. $this->display();
  56. }
  57. public function closeorder(){
  58. $uid = userid();
  59. if($uid <= 0){
  60. $this->redirect('Login/index');
  61. }
  62. if($_POST){
  63. $id = trim(I('post.id'));
  64. if($id == '' || $id <= 0){
  65. $this->ajaxReturn(['code'=>2,'info'=>L('缺少重要参数')]);exit();
  66. }
  67. $oinfo = M("leverorder")->where(array('id'=>$id,'status'=>2))->find();
  68. if(empty($oinfo)){
  69. $this->ajaxReturn(['code'=>2,'info'=>L('订单不存在')]);exit();
  70. }
  71. $uinfo = M("user")->where(array('id'=>$uid))->field("id,username")->find();
  72. $lowercoin = $oinfo['coinname'];
  73. if($lowercoin == 'ukbusdt'){
  74. $o_symbol = $oinfo['symbol'];
  75. $symbol_arr = explode('/',$o_symbol);
  76. $symbol = strtolower($symbol_arr[0])."_".strtolower($symbol_arr[1]);
  77. $mlist = M("market")->where(array('name'=>$symbol))->field("new_price,min_price")->find();
  78. $close = $mlist['new_price'];
  79. }else{
  80. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  81. $result = $this->getprice($url);
  82. $pdata = $result['data'][0];
  83. $close = $pdata['close'];//现价
  84. }
  85. //收益计算
  86. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  87. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  88. $direction = $oinfo['direction'];
  89. if($direction == 1){ //做多
  90. $profit = sprintf("%.2f",(($close - $oinfo['price']) * 100 * 0.001 * $oinfo['ggan']));
  91. }elseif($direction == 2){ //做空
  92. $profit = sprintf("%.2f",(($oinfo['price'] - $close) * 100 * 0.001 * $oinfo['ggan']));
  93. }
  94. if($profit >= 0){
  95. $yk_status = 1;
  96. }else{
  97. $yk_status = 2;
  98. }
  99. $tprofit = abs($profit);
  100. $data['pc_price'] = $close;
  101. $data['status'] = 3;
  102. $data['yk_status'] = $yk_status;
  103. $data['ylmoney'] = $tprofit;
  104. $data['endtime'] = date("Y-m-d H:i:s",time());
  105. $result = M("leverorder")->where(array('id'=>$id,'status'=>2))->save($data);
  106. if($result){
  107. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  108. if($profit >= 0){
  109. M("user_coin")->where(array('userid'=>$uid))->setInc('usdt',$tprofit);
  110. $ubill['uid'] = $uid;
  111. $ubill['username'] = $uinfo['username'];
  112. $ubill['num'] = $tprofit;
  113. $ubill['coinname'] = "usdt";
  114. $ubill['afternum'] = $minfo['usdt'] + $tprofit;
  115. $ubill['type'] = 20;
  116. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  117. $ubill['st'] = 1;
  118. $ubill['remark'] = L('合约交易手动平仓盈利收益');
  119. $ubillre = M("bill")->add($ubill);
  120. }else{
  121. if($minfo['usdt'] >= $tprofit){
  122. M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$tprofit);
  123. $ubill['uid'] = $uid;
  124. $ubill['username'] = $uinfo['username'];
  125. $ubill['num'] = $tprofit;
  126. $ubill['coinname'] = "usdt";
  127. $ubill['afternum'] = $minfo['usdt'] - $tprofit;
  128. $ubill['type'] = 20;
  129. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  130. $ubill['st'] = 2;
  131. $ubill['remark'] = L('合约交易手动平仓亏损');
  132. $ubillre = M("bill")->add($ubill);
  133. }else{
  134. //如果亏损金额大于账户余额,账户余额全扣完,剩下的部分扣合约账户
  135. $tpro_a = $minfo['usdt'];
  136. $tpro_b = $tprofit - $minfo['usdt'];
  137. M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$tpro_a);
  138. $ubill['uid'] = $uid;
  139. $ubill['username'] = $uinfo['username'];
  140. $ubill['num'] = $tpro_a;
  141. $ubill['coinname'] = "usdt";
  142. $ubill['afternum'] = $minfo['usdt'] - $tpro_a;
  143. $ubill['type'] = 20;
  144. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  145. $ubill['st'] = 2;
  146. $ubill['remark'] = L('合约交易手动平仓亏损');
  147. $ubillre = M("bill")->add($ubill);
  148. $levermoney_info = M("levermoney")->where(array('uid'=>$uid))->find();
  149. if($levermoney_info['money'] >= $tpro_b){
  150. M("levermoney")->where(array('uid'=>$uid))->setDec("money",$tpro_b);
  151. }else{
  152. M("levermoney")->where(array('uid'=>$uid))->save(array('money',0));
  153. }
  154. }
  155. }
  156. $this->ajaxReturn(['code'=>1,'info'=>L('操作成功')]);exit();
  157. }else{
  158. $this->ajaxReturn(['code'=>2,'info'=>L('操作失败')]);exit();
  159. }
  160. }else{
  161. $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
  162. }
  163. }
  164. public function confimclose(){
  165. $uid = userid();
  166. if($uid <= 0){
  167. $this->redirect('Login/index');
  168. }
  169. if($_POST){
  170. $id = trim(I('post.id'));
  171. if($id == '' || $id <= 0){
  172. $this->ajaxReturn(['code'=>2,'info'=>L('缺少重要参数')]);exit();
  173. }
  174. $oinfo = M("leverorder")->where(array('id'=>$id,'status'=>2))->find();
  175. if(empty($oinfo)){
  176. $this->ajaxReturn(['code'=>2,'info'=>L('订单不存在')]);exit();
  177. }
  178. $lowercoin = $oinfo['coinname'];
  179. if($lowercoin == 'ukbusdt'){
  180. $o_symbol = $oinfo['symbol'];
  181. $symbol_arr = explode('/',$o_symbol);
  182. $symbol = strtolower($symbol_arr[0])."_".strtolower($symbol_arr[1]);
  183. $mlist = M("market")->where(array('name'=>$symbol))->field("new_price,min_price")->find();
  184. $close = $mlist['new_price'];
  185. }else{
  186. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  187. $result = $this->getprice($url);
  188. $pdata = $result['data'][0];
  189. $close = $pdata['close'];//现价
  190. }
  191. //收益计算
  192. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  193. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  194. $direction = $oinfo['direction'];
  195. if($direction == 1){ //做多
  196. $profit = sprintf("%.2f",(($close - $oinfo['price']) * 100 * 0.001 * $oinfo['ggan']));
  197. }elseif($direction == 2){ //做空
  198. $profit = sprintf("%.2f",(($oinfo['price'] - $close) * 100 * 0.001 * $oinfo['ggan']));
  199. }
  200. if($profit >= 0){
  201. $html = '<span style="font-size:18px;color:#0ecb81;">'.L("现在结算预计盈利").':'.$profit.'USDT,'.L("确定要结算吗?").'</span>';
  202. }else{
  203. $html = '<span style="font-size:18px;color:#f5465c;">'.L("现在结算预计亏损").':'.$profit.'USDT,'.L("确定要结算吗?").'</span>';
  204. }
  205. $data['txt'] = $html;
  206. $data['id'] = $id;
  207. $data['code'] = 1;
  208. $this->ajaxReturn($data);exit();
  209. }
  210. }
  211. public function clearorder(){
  212. $uid = userid();
  213. if($uid <= 0){
  214. $this->redirect('Login/index');
  215. }
  216. if($_POST){
  217. $id = trim(I('post.id'));
  218. if($id == '' || $id <= 0){
  219. $this->ajaxReturn(['code'=>2,'info'=>L('缺少重要参数')]);exit();
  220. }
  221. $oinfo = M("leverorder")->where(array('id'=>$id))->find();
  222. if(empty($oinfo)){
  223. $this->ajaxReturn(['code'=>2,'info'=>L('订单不存在')]);exit();
  224. }
  225. if($oinfo['status'] != 1){
  226. $this->ajaxReturn(['code'=>2,'info'=>L('该合约不能撤消')]);exit();
  227. }
  228. $result = M("leverorder")->where(array('id'=>$id))->delete();
  229. if($result){
  230. $this->ajaxReturn(['code'=>1,'info'=>L('撤消成功')]);exit();
  231. }else{
  232. $this->ajaxReturn(['code'=>2,'info'=>L('撤消失败')]);exit();
  233. }
  234. }else{
  235. $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
  236. }
  237. }
  238. public function UpOrderHandle(){
  239. $uid = userid();
  240. if($uid <= 0){
  241. $this->redirect('Login/index');
  242. }
  243. if($_POST){
  244. $sysinfo = M("leversetting")->where(array('id'=>1))->find();
  245. if($sysinfo['lever_kg'] != 0){
  246. $this->ajaxReturn(['code'=>0,'info'=>L('暂停交易')]);
  247. }
  248. $lever_fee = $sysinfo['lever_fee'];//费率
  249. $uinfo = M("user")->where(array('id'=>$uid))->field('id,username')->find();
  250. $direction = trim(I('post.direction')); //交易方向1做多2做空
  251. $type = trim(I('post.type')); //1限价委托2市价委托
  252. $symbolstr = trim(I('post.symbol')); //交易对
  253. $mprice = trim(I('post.mprice')); //建仓或委托单价
  254. $ggan = trim(I('post.ggan')); //杠杆倍数
  255. $zyprice = trim(I('post.zyprice')); //止盈价格
  256. $zsprice = trim(I('post.zsprice')); //止损价格
  257. $num = trim(I('post.num')); //交易手数
  258. $arr = explode("/",$symbolstr);
  259. $symbol = strtolower($arr[0].$arr[1]);//交易对
  260. $data['orderid'] = date("YmdHis",time()).time().$uid;
  261. $data['uid'] = $uid;
  262. $data['username'] = $uinfo['username'];
  263. $data['direction'] = $direction;
  264. $data['coinname'] = $symbol;
  265. $data['symbol'] = $symbolstr;
  266. if($type == 1){
  267. $data['price'] = '';
  268. $data['wt_price'] = $mprice;
  269. $data['wttime'] = date('Y-m-d H:i:s',time());
  270. $data['addtime'] = '';
  271. $data['status'] = 1;
  272. $data['fee'] = '';
  273. }elseif($type == 2){
  274. $data['price'] = $mprice;
  275. $data['wt_price'] = $mprice;
  276. $data['wttime'] = date('Y-m-d H:i:s',time());
  277. $data['addtime'] = date('Y-m-d H:i:s',time());
  278. //手续费计算(开仓价格 * 开仓数量 * 手续费率) ,开仓时扣除手续费
  279. $data['fee'] = $fee = sprintf("%.2f",($mprice * $num * $lever_fee / 100));
  280. $data['status'] = 2;
  281. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  282. if($minfo['usdt'] < $fee){
  283. $this->ajaxReturn(['code'=>2,'info'=>L('账户余额不足')]);exit();
  284. }
  285. }
  286. $data['type'] = $type;
  287. $data['num'] = $num;
  288. $data['ggan'] = $ggan;
  289. $data['yk_status'] = '';
  290. $data['ylmoney'] = '';
  291. $data['endtime'] = '';
  292. $result = M("leverorder")->add($data);
  293. if($result){
  294. if($type == 2){
  295. //扣除手续费
  296. $decre = M("user_coin")->where(array('userid'=>$uid))->setDec("usdt",$fee);
  297. $ubill['uid'] = $uid;
  298. $ubill['username'] = $uinfo['username'];
  299. $ubill['num'] = $fee;
  300. $ubill['coinname'] = "usdt";
  301. $ubill['afternum'] = $minfo['usdt'] - $fee;
  302. $ubill['type'] = 19;
  303. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  304. $ubill['st'] = 2;
  305. $ubill['remark'] = L('合约交易开仓手续费');
  306. $ubillre = M("bill")->add($ubill);
  307. }
  308. $this->ajaxReturn(['code'=>1,'info'=>L('订单创建成功')]);exit();
  309. }else{
  310. $this->ajaxReturn(['code'=>2,'info'=>L('订单创建失败')]);exit();
  311. }
  312. $this->ajaxReturn(['code'=>2,'info'=>L('未开放')]);exit();
  313. }else{
  314. $this->ajaxReturn(['code'=>2,'info'=>L('非法操作')]);exit();
  315. }
  316. }
  317. //币币交易详情页面
  318. public function index(){
  319. $uid = userid();
  320. if($uid <= 0){
  321. $this->redirect('Login/index');
  322. }
  323. $symbol = trim(I('get.symbol'));
  324. if($symbol == ''){
  325. $symbol = "BTC";
  326. }
  327. $coin = strtolower($symbol);
  328. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  329. $usdt_blance = $minfo['usdt'];
  330. $coin_blance = $minfo[$coin];
  331. $this->assign('usdt_blance',$usdt_blance);
  332. $this->assign('coin_blance',$coin_blance);
  333. $this->assign('uid',$uid);
  334. $coinname = $symbol."/"."USDT";
  335. $market = $coin.'usdt';
  336. $this->assign('market',$market);
  337. $this->assign('coinname',$coinname);
  338. $this->assign('symbol',$symbol);
  339. $sysinfo = M("leversetting")->where(array('id'=>1))->find();
  340. $this->assign('sysinfo',$sysinfo);
  341. $lmoneyinfo = M("levermoney")->where(array('uid'=>$uid))->find();
  342. if(empty($lmoneyinfo)){
  343. $this->update_levermoney($uid);
  344. $levermoney = 0;
  345. }else{
  346. $levermoney = $lmoneyinfo['money'];
  347. }
  348. $this->assign('levermoney',$levermoney);
  349. //委托订单
  350. $wtlist = M("leverorder")->where(array('uid'=>$uid,'status'=>1))->order("id desc")->field("id,direction,symbol,wt_price,num,ggan,wttime")->select();
  351. $this->assign('wtlist',$wtlist);
  352. //持仓订单
  353. $cclist = M("leverorder")->where(array('uid'=>$uid,'status'=>2))->order("id desc")->select();
  354. $this->assign('cclist',$cclist);
  355. $this->display();
  356. }
  357. public function update_levermoney($uid){
  358. $uinfo = M("user")->where(array('id'=>$uid))->field("id,username")->find();
  359. $data['uid'] = $uid;
  360. $data['username'] = $uinfo['username'];
  361. $data['money'] = 0;
  362. M("levermoney")->add($data);
  363. }
  364. //合约K线
  365. public function trans(){
  366. $uid = userid();
  367. $this->assign('uid',$uid);
  368. $sytx = trim(I('get.sytx'));
  369. $txarr = explode('/',$sytx);
  370. $symbol = $txarr[0];
  371. $market = strtolower($txarr[0].$txarr[1]);
  372. if($symbol == ''){
  373. $symbol = 'btc';
  374. }
  375. if($market == ''){
  376. $market = 'btcusdt';
  377. }
  378. $upmarket = strtoupper($symbol)."/USDT";
  379. $this->assign('upmarket',$upmarket);
  380. $this->assign('market', $market);
  381. $this->assign('smybol',$symbol);
  382. $lowercoin = strtolower($symbol);
  383. $cmarket = M("ctmarket")->where(array('coinname'=>$lowercoin))->field("state")->find();
  384. $state = $cmarket['state'];
  385. $this->assign('state',$state);
  386. $this->display();
  387. }
  388. //获取行情数据
  389. public function getprice($api){
  390. $ch = curl_init();
  391. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  392. curl_setopt ($ch, CURLOPT_URL, $api );
  393. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  394. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
  395. $result = json_decode(curl_exec($ch),true);
  396. return $result;
  397. }
  398. /**
  399. * 普通K线图
  400. */
  401. public function ordinaryh($market = NULL)
  402. {
  403. if($market == ''){
  404. $market = "btcusdt";
  405. }
  406. $this->assign('market', $market);
  407. $this->display();
  408. }
  409. /**
  410. * 普通K线图
  411. */
  412. public function ordinary($market = NULL)
  413. {
  414. if($market == ''){
  415. $market = "btcusdt";
  416. }
  417. $this->assign('market', $market);
  418. $this->display();
  419. }
  420. }
  421. ?>