ContractController.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. namespace Home\Controller;
  3. class ContractController extends HomeController
  4. {
  5. protected function _initialize()
  6. {
  7. parent::_initialize(); $allow_action=array("index","creatorder","ty_creatorder","get_maket_api","ctbill","cbillinfo","contractpc","contractjc","gethyorder","contract_ty","gethyorder_ty");
  8. if(!in_array(ACTION_NAME,$allow_action)){
  9. $this->error(L("非法操作"));
  10. }
  11. }
  12. //获取休验合约记录
  13. public function gethyorder_ty(){
  14. $uid = userid();
  15. $list = M("tyhyorder")->where(array('uid'=>$uid))->field("is_win,sellprice,ploss,id,num,hyzd,coinname,buytime,status,selltime,buyprice,time")->order("id desc")->select();
  16. $data = array();
  17. foreach($list as $k=>$v){
  18. $data[$k]['sellprice'] = $v['sellprice'];
  19. if($v['is_win'] == 0){
  20. $data[$k]['ploss'] = '<i class="fred1" >'. $v['ploss'] .'</i>';
  21. }elseif($v['is_win'] == 1){
  22. $data[$k]['ploss'] = '<i class="fred2">'. $v['ploss'] .'</i>';
  23. }else{
  24. $data[$k]['ploss'] = '<i class="fred3">'. $v['ploss'] .'</i>';
  25. }
  26. $data[$k]['id'] = $v['id'];
  27. $data[$k]['coinanme'] = $v['coinname'];
  28. $data[$k]['num'] = $v['num'];
  29. if($v['status'] == 1){
  30. $data[$k]['statusstr'] = L("待结算");
  31. }elseif($v['status'] == 2){
  32. $data[$k]['statusstr'] = L("已结算");
  33. }elseif($v['status'] == 3){
  34. $data[$k]['statusstr'] = L("无效结算");
  35. }
  36. if($v['hyzd'] == 1){
  37. $data[$k]['hyzdstr'] = '<i class="fgreen">'. L("买涨") .'</i>';
  38. }elseif($v['hyzd'] == 2){
  39. $data[$k]['hyzdstr'] = '<i class="fred">'. L("买跌") .'</i>';
  40. }
  41. $data[$k]['buyprice'] = $v['buyprice'];
  42. $data[$k]['buytime'] = $v['buytime'];
  43. $alltime = $v['time'] * 60;
  44. $endtime = strtotime($v['selltime']);
  45. $t = $endtime - time();
  46. if($t <= 0){
  47. $t = 0;
  48. }
  49. $data[$k]['t'] = $t;
  50. }
  51. $this->ajaxReturn(['code'=>1,'data'=>$data]);
  52. }
  53. //获取合约记录
  54. public function gethyorder(){
  55. $uid = userid();
  56. $list = M("hyorder")->where(array('uid'=>$uid,"status"=>1))->field("id,num,hyzd,coinname,buytime,status,selltime,buyprice,time")->order("id desc")->select();
  57. $data = array();
  58. foreach($list as $k=>$v){
  59. $data[$k]['id'] = $v['id'];
  60. $data[$k]['coinanme'] = $v['coinname'];
  61. $data[$k]['num'] = $v['num'];
  62. if($v['status'] == 1){
  63. $data[$k]['statusstr'] = L("待结算");
  64. }elseif($v['status'] == 2){
  65. $data[$k]['statusstr'] = L("已结算");
  66. }elseif($v['status'] == 3){
  67. $data[$k]['statusstr'] = L("无效结算");
  68. }
  69. if($v['hyzd'] == 1){
  70. $data[$k]['hyzdstr'] = '<i class="fgreen">'. L("买涨") .'</i>';
  71. }elseif($v['hyzd'] == 2){
  72. $data[$k]['hyzdstr'] = '<i class="fred">'. L("买跌") .'</i>';
  73. }
  74. $data[$k]['buyprice'] = $v['buyprice'];
  75. $data[$k]['buytime'] = $v['buytime'];
  76. $alltime = $v['time'] * 60;
  77. $endtime = strtotime($v['selltime']);
  78. $t = $endtime - time();
  79. if($t <= 0){
  80. $t = 0;
  81. }
  82. $data[$k]['t'] = $t;
  83. }
  84. $this->ajaxReturn(['code'=>1,'data'=>$data]);
  85. }
  86. //合约体验订单
  87. public function contract_ty(){
  88. $uid = userid();
  89. if($uid <= 0){
  90. $this->redirect('Login/index');
  91. }
  92. $hylist = M("tyhyorder")->where(array('uid'=>$uid))->order("id desc")->select();
  93. $this->assign("list",$hylist);
  94. $this->display();
  95. }
  96. //合约未平仓订单
  97. public function contractjc(){
  98. $uid = userid();
  99. if($uid <= 0){
  100. $this->redirect('Login/index');
  101. }
  102. $hylist = M("hyorder")->where(array('uid'=>$uid,"status"=>1))->order("id desc")->select();
  103. $this->assign("list",$hylist);
  104. $this->display();
  105. }
  106. //合约已平仓订单
  107. public function contractpc(){
  108. $uid = userid();
  109. if($uid <= 0){
  110. $this->redirect('Login/index');
  111. }
  112. $where['uid'] = $uid;
  113. $where['status'] = array('neq',1);
  114. $hylist = M("hyorder")->where($where)->order("id desc")->select();
  115. $this->assign("list",$hylist);
  116. $this->display();
  117. }
  118. //购买合约详情
  119. public function cbillinfo(){
  120. $bid = trim(I('get.bid'));
  121. if($bid <= 0){
  122. $this->redirect('Contract/ctbill');
  123. }
  124. $uid = userid();
  125. $info = M("hyorder")->where(array('uid'=>$uid,'id'=>$bid))->find();
  126. $this->assign('info',$info);
  127. $this->display();
  128. }
  129. //合约购买记录
  130. public function ctbill(){
  131. $uid = userid();
  132. if($uid <= 0){
  133. $this->redirect('Login/index');
  134. }
  135. $fields = "num,coinname,buytime,buyprice,time,status,id";
  136. $hylist = M("hyorder")->where(array('uid'=>$uid))->field($fields)->order("id desc")->limit(20)->select();
  137. $this->assign("list",$hylist);
  138. $this->display();
  139. }
  140. //获取行情数据
  141. public function get_maket_api($api){
  142. $ch = curl_init();
  143. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  144. curl_setopt ($ch, CURLOPT_URL, $api );
  145. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  146. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
  147. $result = json_decode(curl_exec($ch),true);
  148. return $result;
  149. }
  150. //快速合约建仓
  151. public function ty_creatorder(){
  152. if($_POST){
  153. $uid = userid();
  154. $uinfo = M("user")->where(array('id'=>$uid))->field("id,username,money,rzstatus,invit_1,is_tax,taxstatus")->find();
  155. if(empty($uinfo)){
  156. $this->ajaxReturn(['code'=>0,'msg'=> L('请先登陆')]);
  157. }
  158. if($uinfo['rzstatus'] != 2){
  159. $this->ajaxReturn(['code'=>0,'msg'=>L('请先完成实名认证')]);
  160. }
  161. //检查该会员是不是需要缴纳税金,如有需要检查有没有缴纳
  162. // if($uinfo['is_tax'] == 1){
  163. // if($uinfo['taxstatus'] != 1){
  164. // $this->ajaxReturn(['code'=>3]);
  165. // }
  166. // }
  167. $puid = $uinfo['invit_1'];
  168. $puser = M("user")->where(array('id'=>$puid))->field("invit")->find();
  169. $ctime = trim(I('post.ctime'));
  170. if($ctime <= 0){
  171. $this->ajaxReturn(['code'=>0,'msg'=> L('请选择结算时间')]);
  172. }
  173. $ctzed = trim(I('post.ctzed'));
  174. if($ctzed <= 0){
  175. $this->ajaxReturn(['code'=>0,'msg'=> L('请选择投资额度')]);
  176. }
  177. $ccoinname = trim(I('post.ccoinname'));
  178. if($ccoinname == ''){
  179. $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
  180. }
  181. $ctzfx = trim(I('post.ctzfx'));
  182. if($ctzfx <= 0){
  183. $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
  184. }
  185. $cykbl = trim(I('post.cykbl'));
  186. if($ctzfx <= 0){
  187. $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
  188. }
  189. //获取会员资产
  190. //$minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  191. $usdtnum = $uinfo['money'];
  192. //获取合约手续费比例
  193. $setting = M("hysetting")->where(array('id'=>1))->field("hy_sxf,hy_min")->find();
  194. $hymin = $setting['hy_min'];
  195. if($hymin > $ctzed){
  196. $this->ajaxReturn(['code'=>0,'msg'=> L('不能小于最低投资额度')]);
  197. }
  198. $sxf = $setting['hy_sxf'];
  199. $tmoney = $ctzed + $ctzed * $sxf / 100;
  200. if($tmoney > $usdtnum){
  201. $this->ajaxReturn(['code'=>0,'msg' => L('体验金余额不足')]);
  202. }
  203. //获取当前交易对的单价
  204. $coinarr = explode('/',$ccoinname);
  205. $symbol = strtolower($coinarr[0].$coinarr[1]);
  206. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  207. $result = $this->get_maket_api($coinapi);
  208. $price_arr = $result['data'][0];
  209. $close = $price_arr['close'];//现价
  210. //创建订单
  211. $odata['uid'] = $uid;
  212. $odata['username'] = $uinfo['username'];
  213. $odata['num'] = $ctzed;
  214. $odata['hybl'] = $cykbl;
  215. $odata['hyzd'] = $ctzfx;
  216. $odata['coinname'] = $ccoinname;
  217. $odata['status'] = 1;
  218. $odata['is_win'] = 0;
  219. $odata['buytime'] = date("Y-m-d H:i:s",time());
  220. $odata['selltime'] = date("Y-m-d H:i:s",(time()+$ctime*60));
  221. $odata['intselltime'] = time() + $ctime*60;
  222. $odata['buyprice'] = $close;
  223. $odata['sellprice'] = '';
  224. $odata['ploss'] = 0;
  225. $odata['time'] = $ctime;
  226. $odata['kongyk'] = 0;
  227. if($puser['invit'] == ''){
  228. $puser['invit'] = 0;
  229. }
  230. $odata['invit'] = $puser['invit'];
  231. $order = M("tyhyorder")->add($odata);
  232. //扣除体验金
  233. $decre = M("user")->where(array('id'=>$uid))->setDec('money',$tmoney);
  234. //创建财务日志
  235. // $bill['uid'] = $uid;
  236. // $bill['username'] = $uinfo['username'];
  237. // $bill['num'] = $ctzed;
  238. // $bill['coinname'] = "usdt";
  239. // $bill['afternum'] = $minfo['usdt'] - $ctzed;
  240. // $bill['type'] = 3;
  241. // $bill['addtime'] = date("Y-m-d H:i:s",time());
  242. // $bill['st'] = 2;
  243. // $bill['remark'] = L('购买').$ccoinname.L('快速合约');
  244. // $billre = M("bill")->add($bill);
  245. if($order && $decre){
  246. $this->ajaxReturn(['code'=>1,'msg' => L('体验订单建仓成功')]);
  247. }else{
  248. $this->ajaxReturn(['code'=>0,'msg' => L('体验订单建仓失败')]);
  249. }
  250. }else{
  251. $this->ajaxReturn(['code'=>0,'msg' => L('网络错误')]);
  252. }
  253. }
  254. //快速合约建仓
  255. public function creatorder(){
  256. if($_POST){
  257. $uid = userid();
  258. $uinfo = M("user")->where(array('id'=>$uid))->field("id,username,rzstatus,invit_1,is_tax,taxstatus")->find();
  259. if(empty($uinfo)){
  260. $this->ajaxReturn(['code'=>0,'msg'=> L('请先登陆')]);
  261. }
  262. if($uinfo['rzstatus'] != 2){
  263. $this->ajaxReturn(['code'=>0,'msg'=>L('请先完成实名认证')]);
  264. }
  265. if($uinfo['is_tax'] == 1){
  266. if($uinfo['taxstatus'] != 1){
  267. $this->ajaxReturn(['code'=>3]);
  268. }
  269. }
  270. $puid = $uinfo['invit_1'];
  271. $puser = M("user")->where(array('id'=>$puid))->field("invit")->find();
  272. $ctime = trim(I('post.ctime'));
  273. if($ctime <= 0){
  274. $this->ajaxReturn(['code'=>0,'msg'=> L('请选择结算时间')]);
  275. }
  276. $ctzed = trim(I('post.ctzed'));
  277. if($ctzed <= 0){
  278. $this->ajaxReturn(['code'=>0,'msg'=> L('请选择投资额度')]);
  279. }
  280. $ccoinname = trim(I('post.ccoinname'));
  281. if($ccoinname == ''){
  282. $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
  283. }
  284. $ctzfx = trim(I('post.ctzfx'));
  285. if($ctzfx <= 0){
  286. $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
  287. }
  288. $cykbl = trim(I('post.cykbl'));
  289. if($ctzfx <= 0){
  290. $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
  291. }
  292. //获取会员资产
  293. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  294. $usdtnum = $minfo['usdt'];
  295. //获取合约手续费比例
  296. $setting = M("hysetting")->where(array('id'=>1))->field("hy_sxf,hy_min")->find();
  297. $hymin = $setting['hy_min'];
  298. if($hymin > $ctzed){
  299. $this->ajaxReturn(['code'=>0,'msg'=> L('不能小于最低投资额度')]);
  300. }
  301. $sxf = $setting['hy_sxf'];
  302. $tmoney = $ctzed + $ctzed * $sxf / 100;
  303. if($tmoney > $usdtnum){
  304. $this->ajaxReturn(['code'=>0,'msg' => L('USDT余额不足')]);
  305. }
  306. //获取当前交易对的单价
  307. $coinarr = explode('/',$ccoinname);
  308. $symbol = strtolower($coinarr[0].$coinarr[1]);
  309. $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  310. $result = $this->get_maket_api($coinapi);
  311. $price_arr = $result['data'][0];
  312. $close = $price_arr['close'];//现价
  313. //创建订单
  314. $odata['uid'] = $uid;
  315. $odata['username'] = $uinfo['username'];
  316. $odata['num'] = $ctzed;
  317. $odata['hybl'] = $cykbl;
  318. $odata['hyzd'] = $ctzfx;
  319. $odata['coinname'] = $ccoinname;
  320. $odata['status'] = 1;
  321. $odata['is_win'] = 0;
  322. $odata['buytime'] = date("Y-m-d H:i:s",time());
  323. $odata['selltime'] = date("Y-m-d H:i:s",(time()+$ctime*60));
  324. $odata['intselltime'] = time() + $ctime*60;
  325. $odata['buyprice'] = $close;
  326. $odata['sellprice'] = '';
  327. $odata['ploss'] = 0;
  328. $odata['time'] = $ctime;
  329. $odata['kongyk'] = 0;
  330. if($puser['invit'] == ''){
  331. $puser['invit'] = 0;
  332. }
  333. $odata['invit'] = $puser['invit'];
  334. $order = M("hyorder")->add($odata);
  335. //扣除USDT额度
  336. $decre = M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$tmoney);
  337. //创建财务日志
  338. $bill['uid'] = $uid;
  339. $bill['username'] = $uinfo['username'];
  340. $bill['num'] = $ctzed;
  341. $bill['coinname'] = "usdt";
  342. $bill['afternum'] = $minfo['usdt'] - $ctzed;
  343. $bill['type'] = 3;
  344. $bill['addtime'] = date("Y-m-d H:i:s",time());
  345. $bill['st'] = 2;
  346. $bill['remark'] = L('购买').$ccoinname.L('快速合约');
  347. $billre = M("bill")->add($bill);
  348. if($order && $decre && $billre){
  349. $this->ajaxReturn(['code'=>1,'msg' => L('建仓成功')]);
  350. }else{
  351. $this->ajaxReturn(['code'=>0,'msg' => L('建仓失败')]);
  352. }
  353. }else{
  354. $this->ajaxReturn(['code'=>0,'msg' => L('网络错误')]);
  355. }
  356. }
  357. //快速合约首页面
  358. public function index(){
  359. $uid = userid();
  360. if($uid <= 0 || $uid == ''){
  361. $uid = 0;
  362. }
  363. $this->assign('uid',$uid);
  364. $smybol = trim(I('get.coin'));
  365. if($smybol != ''){
  366. $map['status'] = 1;
  367. $map['coinname'] = array('like',"%$smybol%");
  368. $list = M("ctmarket")->where($map)->field("coinname")->find();
  369. if(!empty($list)){
  370. $smybol = $list['coinname'];
  371. }else{
  372. $smybol = "btc";
  373. }
  374. }else{
  375. $smybol = "btc";
  376. }
  377. $market = $smybol."usdt";
  378. $upmarket = strtoupper($smybol)."/USDT";
  379. $this->assign('upmarket',$upmarket);
  380. $this->assign('market',$market);
  381. $this->assign('smybol',$smybol);
  382. //获取合约设置项
  383. $hyset = M("hysetting")->where(array('id'=>1))->field("hy_time,hy_ykbl,hy_tzed")->find();
  384. $hy_time = $hyset['hy_time'];
  385. $hy_ykbl = $hyset['hy_ykbl'];
  386. $hy_tzed = $hyset['hy_tzed'];
  387. $time_arr = explode(',',$hy_time);
  388. $ykbl_arr = explode(',',$hy_ykbl);
  389. $tzed_arr = explode(',',$hy_tzed);
  390. $cd=array();
  391. $ed=array();
  392. $len = count($time_arr);
  393. for($i=0;$i<$len;$i++){
  394. $cd[$i]['sort'] = $i+1;
  395. $cd[$i]['time'] = $time_arr[$i];
  396. $cd[$i]['ykbl'] = $ykbl_arr[$i];
  397. $ed[$i]['sort'] = $i+1;
  398. $ed[$i]['tzed'] = $tzed_arr[$i];
  399. }
  400. $this->assign("cd",$cd);
  401. $this->assign('ed',$ed);
  402. //获取会员资产
  403. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  404. $usdt_blan = $minfo['usdt'];
  405. $this->assign('eusdt_blan',$usdt_blan);
  406. //体验金
  407. $uinfo = M("user")->where(array('id'=>$uid))->find();
  408. $tj_money = $uinfo['money'];
  409. $this->assign('tj_money',$tj_money);
  410. $this->display();
  411. }
  412. }
  413. ?>