LevertadeController.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace Admin\Controller;
  3. class LevertadeController extends AdminController
  4. {
  5. protected function _initialize(){
  6. parent::_initialize();
  7. $allow_action=array("index","setting","ccinfo","closeorder","getprice","pclist","trustlist","clearorder","addorder");
  8. if(!in_array(ACTION_NAME,$allow_action)){
  9. $this->error("页面不存在!");
  10. }
  11. }
  12. public function addorder(){
  13. if($_POST){
  14. $id = trim(I('post.id'));
  15. if($id == '' || $id <= 0){
  16. $this->ajaxReturn(['code'=>2,'info'=>'缺少重要参数']);exit();
  17. }
  18. $oinfo = M("leverorder")->where(array('id'=>$id))->find();
  19. if(empty($oinfo)){
  20. $this->ajaxReturn(['code'=>2,'info'=>'订单不存在']);exit();
  21. }
  22. if($oinfo['status'] != 1){
  23. $this->ajaxReturn(['code'=>2,'info'=>'订单状态已更改,无法开仓']);exit();
  24. }
  25. $uid = $oinfo['uid'];
  26. $user = M("levermoney")->where(array('uid'=>$uid))->find();
  27. $uinfo = M("user")->where(array('id'=>$uid))->field("id,username")->find();
  28. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  29. $sysinfo = M("leversetting")->where(array('id'=>1))->find();
  30. $lever_fee = $sysinfo['lever_fee'];//费率
  31. $oy_list = do_request(oy_url($oinfo['coinname'], 'api/v5/market/index-tickers?instId='), []);
  32. $oy_list = $oy_list->data[0];
  33. // $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  34. // $result = $this->getprice($url);
  35. // $pdata = $result['data'][0];
  36. $close = $oy_list->idxPx;//现价
  37. //手续费计算(开仓价格 * 开仓数量 * 手续费率) ,开仓时扣除手续费
  38. $data['fee'] = $fee = $oinfo['fee'];
  39. if($fee > $user['money']){
  40. $this->ajaxReturn(['code'=>2,'info'=>'该会员账户余额不足,请撤消委托']);exit();
  41. }
  42. $data['price'] = $close;
  43. $data['status'] = 2;
  44. $data['addtime'] = date("Y-m-d H:i:s",time());
  45. $result = M("leverorder")->where(array('id'=>$id))->save($data);
  46. if($result){
  47. M("levermoney")->where(array('uid'=>$uid))->setDec('money',$fee);
  48. $ubill['uid'] = $uid;
  49. $ubill['username'] = $uinfo['username'];
  50. $ubill['num'] = $fee;
  51. $ubill['coinname'] = "usdt";
  52. $ubill['afternum'] = $user['money'] - $fee;
  53. $ubill['type'] = 25;
  54. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  55. $ubill['st'] = 2;
  56. $ubill['remark'] = L('合约交易开仓手续费');
  57. $ubillre = M("bill")->add($ubill);
  58. $this->ajaxReturn(['code'=>1,'info'=>'操作成功']);exit();
  59. }else{
  60. $this->ajaxReturn(['code'=>2,'info'=>'操作失败']);exit();
  61. }
  62. }else{
  63. $this->ajaxReturn(['code'=>2,'info'=>'非法操作']);exit();
  64. }
  65. }
  66. public function clearorder(){
  67. if($_POST){
  68. $id = trim(I('post.id'));
  69. if($id == '' || $id <= 0){
  70. $this->ajaxReturn(['code'=>2,'info'=>'缺少重要参数']);exit();
  71. }
  72. $oinfo = M("leverorder")->where(array('id'=>$id))->find();
  73. if(empty($oinfo)){
  74. $this->ajaxReturn(['code'=>2,'info'=>'订单不存在']);exit();
  75. }
  76. if($oinfo['status'] != 1){
  77. $this->ajaxReturn(['code'=>2,'info'=>'该合约不能撤消']);exit();
  78. }
  79. $order = M("leverorder")->where(array('id'=>$id))->find();
  80. $user = M('levermoney')->where(['uid' => $order['uid']])->find();
  81. $bond_ubill = [
  82. 'uid' => $order['uid'],
  83. 'username' => $user['username'],
  84. 'num' => $order['bond'],
  85. 'coinname' => 'lever',
  86. 'afternum' => $user['money'] + $order['bond'],
  87. 'type' => 25,
  88. 'addtime' => date("Y-m-d H:i:s",time()),
  89. 'st' => 1,
  90. 'remark' => '撤销委托返还合约账户'
  91. ];// 扣除合约保证金
  92. M("levermoney")->where(array('uid'=>$order['uid']))->setInc('money', $order['bond']);
  93. M("levermoney")->where(array('uid' => $order['uid']))->setDec('moneyd', $order['bond']);
  94. M("bill")->add($bond_ubill);
  95. $result = M("leverorder")->where(array('id'=>$id))->delete();
  96. if($result){
  97. $this->ajaxReturn(['code'=>1,'info'=>'撤消成功']);exit();
  98. }else{
  99. $this->ajaxReturn(['code'=>2,'info'=>'撤消失败']);exit();
  100. }
  101. }else{
  102. $this->ajaxReturn(['code'=>2,'info'=>'非法操作']);exit();
  103. }
  104. }
  105. public function trustlist(){
  106. $where = array();
  107. if(I('get.username') != '' || I('get.username') != null){
  108. $username = trim(I('get.username'));
  109. $where['username'] = $username;
  110. }
  111. if(I('get.direction') > 0){
  112. $hyzd = trim(I('get.direction'));
  113. $where['direction'] = $hyzd;
  114. }
  115. $where['status'] = 1;
  116. $count = M('leverorder')->where($where)->count();
  117. $Page = new \Think\Page($count, 15);
  118. $show = $Page->show();
  119. $list = M('leverorder')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
  120. $this->assign('list', $list);
  121. $this->assign('page', $show);
  122. $this->display();
  123. }
  124. public function pclist(){
  125. $where = '';
  126. if(I('get.username') != '' || I('get.username') != null){
  127. $username = trim(I('get.username'));
  128. $where .= "username = {$username} and";
  129. }
  130. if(I('get.direction') > 0){
  131. $hyzd = trim(I('get.direction'));
  132. $where .= "direction = {$hyzd} and";
  133. }
  134. if(I('get.yk_status') > 0){
  135. $yk_status = trim(I('get.yk_status'));
  136. $where .= "yk_status = {$yk_status} and";
  137. }
  138. $where .= "(status = 3 or status = 4)";
  139. $count = M('leverorder')->where($where)->count();
  140. $Page = new \Think\Page($count, 15);
  141. $show = $Page->show();
  142. $list = M('leverorder')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
  143. $this->assign('list', $list);
  144. $this->assign('page', $show);
  145. $this->display();
  146. }
  147. public function closeorder(){
  148. if($_POST){
  149. $id = trim(I('post.id'));
  150. if($id == '' || $id <= 0){
  151. $this->ajaxReturn(['code'=>2,'info'=>'缺少重要参数']);exit();
  152. }
  153. $oinfo = M("leverorder")->where(array('id'=>$id,'status'=>2))->find();
  154. if(empty($oinfo)){
  155. $this->ajaxReturn(['code'=>2,'info'=>'订单不存在']);exit();
  156. }
  157. $uid = $oinfo['uid'];
  158. $uinfo = M("user")->where(array('id'=>$uid))->field("id,username")->find();
  159. $lowercoin = $oinfo['coinname'];
  160. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$lowercoin;
  161. $result = $this->getprice($url);
  162. $pdata = $result['data'][0];
  163. $close = $pdata['close'];//现价
  164. //收益计算
  165. //例:BTC/USDT永续合约开多仓100张,成交价格为10000 USDT,以11000 USDT价格平仓,
  166. //则:平仓盈亏 = 收益(平仓) =( 11000 – 10000 )* 100 * 0.001 = 100 USDT。
  167. $direction = $oinfo['direction'];
  168. if($direction == 1){ //做多
  169. $profit = sprintf("%.2f",(($close - $oinfo['price']) * 100 * 0.001 * $oinfo['ggan']));
  170. }elseif($direction == 2){ //做空
  171. $profit = sprintf("%.2f",(($oinfo['price'] - $close) * 100 * 0.001 * $oinfo['ggan']));
  172. }
  173. if($profit >= 0){
  174. $yk_status = 1;
  175. }else{
  176. $yk_status = 2;
  177. }
  178. $tprofit = abs($profit);
  179. $data['pc_price'] = $close;
  180. $data['status'] = 3;
  181. $data['yk_status'] = $yk_status;
  182. $data['ylmoney'] = $tprofit;
  183. $data['endtime'] = date("Y-m-d H:i:s",time());
  184. $result = M("leverorder")->where(array('id'=>$id,'status'=>2))->save($data);
  185. if($result){
  186. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  187. if($profit >= 0){
  188. M("user_coin")->where(array('userid'=>$uid))->setInc('usdt',$tprofit);
  189. $ubill['uid'] = $uid;
  190. $ubill['username'] = $uinfo['username'];
  191. $ubill['num'] = $tprofit;
  192. $ubill['coinname'] = "usdt";
  193. $ubill['afternum'] = $minfo['usdt'] + $tprofit;
  194. $ubill['type'] = 20;
  195. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  196. $ubill['st'] = 1;
  197. $ubill['remark'] = "合约交易手动平仓盈利收益";
  198. $ubillre = M("bill")->add($ubill);
  199. }else{
  200. if($minfo['usdt'] >= $tprofit){
  201. M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$tprofit);
  202. $ubill['uid'] = $uid;
  203. $ubill['username'] = $uinfo['username'];
  204. $ubill['num'] = $tprofit;
  205. $ubill['coinname'] = "usdt";
  206. $ubill['afternum'] = $minfo['usdt'] - $tprofit;
  207. $ubill['type'] = 20;
  208. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  209. $ubill['st'] = 2;
  210. $ubill['remark'] = "合约交易手动平仓亏损";
  211. $ubillre = M("bill")->add($ubill);
  212. }else{
  213. //如果亏损金额大于账户余额,账户余额全扣完,剩下的部分扣合约账户
  214. $tpro_a = $minfo['usdt'];
  215. $tpro_b = $tprofit - $minfo['usdt'];
  216. M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$tpro_a);
  217. $ubill['uid'] = $uid;
  218. $ubill['username'] = $uinfo['username'];
  219. $ubill['num'] = $tpro_a;
  220. $ubill['coinname'] = "usdt";
  221. $ubill['afternum'] = $minfo['usdt'] - $tpro_a;
  222. $ubill['type'] = 20;
  223. $ubill['addtime'] = date("Y-m-d H:i:s",time());
  224. $ubill['st'] = 2;
  225. $ubill['remark'] = "合约交易手动平仓亏损";
  226. $ubillre = M("bill")->add($ubill);
  227. $levermoney_info = M("levermoney")->where(array('uid'=>$uid))->find();
  228. if($levermoney_info['money'] >= $tpro_b){
  229. M("levermoney")->where(array('uid'=>$uid))->setDec("money",$tpro_b);
  230. }else{
  231. M("levermoney")->where(array('uid'=>$uid))->save(array('money',0));
  232. }
  233. }
  234. }
  235. $this->ajaxReturn(['code'=>1,'info'=>'操作成功']);exit();
  236. }else{
  237. $this->ajaxReturn(['code'=>2,'info'=>'操作失败']);exit();
  238. }
  239. }else{
  240. $this->ajaxReturn(['code'=>2,'info'=>'非法操作']);exit();
  241. }
  242. }
  243. public function ccinfo(){
  244. $id = trim(I('get.id'));
  245. $info = M("leverorder")->where(array("id"=>$id))->find();
  246. $this->assign('info',$info);
  247. $this->display();
  248. }
  249. public function index(){
  250. $where = array();
  251. if(I('get.username') != '' || I('get.username') != null){
  252. $username = trim(I('get.username'));
  253. $where['username'] = $username;
  254. }
  255. if(I('get.direction') > 0){
  256. $hyzd = trim(I('get.direction'));
  257. $where['direction'] = $hyzd;
  258. }
  259. $where['status'] = 2;
  260. $count = M('leverorder')->where($where)->count();
  261. $Page = new \Think\Page($count, 15);
  262. $show = $Page->show();
  263. $list = M('leverorder')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
  264. $this->assign('list', $list);
  265. $this->assign('page', $show);
  266. $this->display();
  267. }
  268. //永续合约设置
  269. public function setting(){
  270. if($_POST){
  271. $result = M("leversetting")->where(array('id'=>1))->save($_POST);
  272. if($result){
  273. $this->success('修改成功');
  274. }else{
  275. $this->error('修改失败');
  276. }
  277. }else{
  278. $info = M("leversetting")->where(array('id'=>1))->find();
  279. $this->assign("info",$info);
  280. $this->display();
  281. }
  282. }
  283. //获取行情数据
  284. public function getprice($api){
  285. $ch = curl_init();
  286. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  287. curl_setopt ($ch, CURLOPT_URL, $api );
  288. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  289. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
  290. $result = json_decode(curl_exec($ch),true);
  291. return $result;
  292. }
  293. }
  294. ?>