123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- <?php
- namespace Mobile\Controller;
- class ContractController extends MobileController
- {
- protected function _initialize()
- {
- parent::_initialize(); $allow_action=array("index","creatorder","ty_creatorder","get_maket_api","ctbill","cbillinfo","gethyorder","get_tyhyorder","cbillinfo_ty","get_tyhyorder_one","get_hyorder_one");
- if(!in_array(ACTION_NAME,$allow_action)){
- $this->error(L("非法操作"));
- }
-
- }
-
- //体验合约倒计时
- public function get_tyhyorder_one(){
- $uid = userid();
- $id = trim(I('post.oid'));
- if($id == '' || $id <= 0){
- $info = M("tyhyorder")->where(array('uid'=>$uid,'status'=>1))->order('id desc')->find();
- if(empty($info)){
- $info = M("tyhyorder")->where(array('uid'=>$uid))->order('id desc')->find();
- }
-
- }else{
- $info = M("tyhyorder")->where(array('id'=>$id))->find();
- }
-
-
- if(!empty($info)){
- if($info['status'] == 1){
- $alltime = $info['time'] * 60;
- $endtime = strtotime($info['selltime']);
- $t = $endtime - time();
- if($t <= 0){
- //表示已结算
- $data['statusstr'] = '<span style="font-size:16px;font-weight:bold;">'. L("正在结算中"). '...</span>';
- $data['code'] = 2;
- $this->ajaxReturn($data);
- }else{
- //获取当前交易对的单价
- $coinarr = explode('/',$info['coinname']);
- $symbol = strtolower($coinarr[0].$coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
- $result = $this->get_maket_api($coinapi);
- $price_arr = $result['data'][0];
- $close = $price_arr['close'];//现价
-
- $data['code'] = 1;
- $data['time'] = $t;
- $data['timer_newprice'] = $close;
- $this->ajaxReturn($data);
- }
-
- }else{
- //表示已经结算,则显示盈亏即可
- if($info['is_win'] == 1){
- $data['statusstr'] = '<span style="font-size:24px;font-weight:bold;color:green;">+'. $info['ploss'] .'</span>';
- }elseif($info['is_win'] == 2){
- $data['statusstr'] = '<span style="font-size:24px;font-weight:bold;color:red;">-'. $info['ploss'] .'</span>';
- }
-
- $data['code'] = 2;
- $this->ajaxReturn($data);
- }
- }else{
- $this->ajaxReturn(['code'=>0]);
- }
- }
-
-
- //合约倒计时
- public function get_hyorder_one(){
-
- $uid = userid();
- $id = trim(I('post.oid'));
- if($id == '' || $id <= 0){
- $info = M("hyorder")->where(array('uid'=>$uid,'status'=>1))->order('id desc')->find();
- if(empty($info)){
- $info = M("hyorder")->where(array('uid'=>$uid))->order('id desc')->find();
- }
-
- }else{
- $info = M("hyorder")->where(array('id'=>$id))->find();
- }
- if(!empty($info)){
- if($info['status'] == 1){
-
- $alltime = $info['time'] * 60;
- $endtime = strtotime($info['selltime']);
- $t = $endtime - time();
- if($t <= 0){
- //表示已结算
- $data['statusstr'] = '<span style="font-size:16px;font-weight:bold;">'. L("正在结算中"). '...</span>';
- $data['code'] = 2;
- $this->ajaxReturn($data);
- }else{
- //获取当前交易对的单价
- $coinarr = explode('/',$info['coinname']);
- $symbol = strtolower($coinarr[0].$coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
- $result = $this->get_maket_api($coinapi);
- $price_arr = $result['data'][0];
- $close = $price_arr['close'];//现价
-
- $data['code'] = 1;
- $data['time'] = $t;
- $data['timer_newprice'] = $close;
- $this->ajaxReturn($data);
- }
-
- }else{
- //表示已经结算,则显示盈亏即可
- if($info['is_win'] == 1){
- $data['statusstr'] = '<span style="font-size:24px;font-weight:bold;color:green;">+'. $info['ploss'] .'</span>';
- }elseif($info['is_win'] == 2){
- $data['statusstr'] = '<span style="font-size:24px;font-weight:bold;color:red;">-'. $info['ploss'] .'</span>';
- }
-
- $data['code'] = 2;
- $this->ajaxReturn($data);
- }
- }else{
- $this->ajaxReturn(['code'=>0]);
- }
- }
-
-
-
- //获取体验合约记录
- public function get_tyhyorder(){
- $uid = userid();
- $list = M("tyhyorder")->where(array('uid'=>$uid))->field("id,num,coinname,status,buytime,selltime,buyprice,time")->order("id desc")->select();
- $data = array();
- foreach($list as $k=>$v){
- $data[$k]['coinanme'] = $v['coinname'];
- $data[$k]['num'] = $v['num'];
- if($v['status'] == 1){
- $data[$k]['statusstr'] = L("待结算");
- }elseif($v['status'] == 2){
- $data[$k]['statusstr'] = L("已结算");
- }elseif($v['status'] == 3){
- $data[$k]['statusstr'] = L("无效结算");
- }
- $data[$k]['buyprice'] = $v['buyprice'];
- $data[$k]['buytime'] = date("m-d H:i",strtotime($v['buytime']));
-
- $alltime = $v['time'] * 60;
- $endtime = strtotime($v['selltime']);
- $t = $endtime - time();
- if($t <= 0){
- $t = 0;
- }
- $bl = round($t / $alltime * 100);
- if($bl <= 0){
- $bl = 0;
- }
- $data[$k]['bl'] = $bl."%";
- $data[$k]['t'] = $t;
- $data[$k]['href'] = U('Contract/cbillinfo_ty') . "?bid=" . $v['id'];
- }
-
- $this->ajaxReturn(['code'=>1,'data'=>$data]);
- }
-
-
-
- //获取合约记录
- public function gethyorder(){
- $uid = userid();
- $list = M("hyorder")->where(array('uid'=>$uid))->field("id,num,coinname,status,buytime,selltime,buyprice,time")->order("id desc")->select();
- $data = array();
- foreach($list as $k=>$v){
- $data[$k]['coinanme'] = $v['coinname'];
- $data[$k]['num'] = $v['num'];
- if($v['status'] == 1){
- $data[$k]['statusstr'] = L("待结算");
- }elseif($v['status'] == 2){
- $data[$k]['statusstr'] = L("已结算");
- }elseif($v['status'] == 3){
- $data[$k]['statusstr'] = L("无效结算");
- }
- $data[$k]['buyprice'] = $v['buyprice'];
- $data[$k]['buytime'] = date("m-d H:i",strtotime($v['buytime']));
-
- $alltime = $v['time'] * 60;
- $endtime = strtotime($v['selltime']);
- $t = $endtime - time();
- if($t <= 0){
- $t = 0;
- }
- $bl = round($t / $alltime * 100);
- if($bl <= 0){
- $bl = 0;
- }
- $data[$k]['bl'] = $bl."%";
- $data[$k]['t'] = $t;
- $data[$k]['href'] = U('Contract/cbillinfo') . "?bid=" . $v['id'];
- }
-
- $this->ajaxReturn(['code'=>1,'data'=>$data]);
- }
-
- //购买合约详情
- public function cbillinfo(){
- $bid = trim(I('get.bid'));
- if($bid <= 0){
- $this->redirect('Contract/ctbill');
- }
- $uid = userid();
- $info = M("hyorder")->where(array('uid'=>$uid,'id'=>$bid))->find();
- $this->assign('info',$info);
- $this->display();
- }
-
- //体验合约详情
- public function cbillinfo_ty(){
- $bid = trim(I('get.bid'));
- if($bid <= 0){
- $this->redirect('Contract/ctbill');
- }
- $uid = userid();
- $info = M("tyhyorder")->where(array('uid'=>$uid,'id'=>$bid))->find();
- $this->assign('info',$info);
- $this->display();
- }
-
- //合约购买记录
- public function ctbill(){
- $uid = userid();
- if($uid <= 0){
- $this->redirect('Login/index');
- }
- $fields = "num,coinname,buytime,buyprice,time,status,id";
- $hylist = M("hyorder")->where(array('uid'=>$uid))->field($fields)->order("id desc")->limit(20)->select();
- $this->assign("list",$hylist);
- $this->display();
- }
-
- //获取行情数据
- public function get_maket_api($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;
- }
-
- //快速合约建仓
- public function ty_creatorder(){
- if($_POST){
- $uid = userid();
- $uinfo = M("user")->where(array('id'=>$uid))->field("id,username,money,rzstatus,invit_1,is_tax,taxstatus")->find();
- if(empty($uinfo)){
- $this->ajaxReturn(['code'=>0,'msg'=> L('请先登陆')]);
- }
-
- if($uinfo['rzstatus'] != 2){
- $this->ajaxReturn(['code'=>0,'msg'=>L('请先完成实名认证')]);
- }
-
- //检查该会员是不是需要缴纳税金,如有需要检查有没有缴纳
- // if($uinfo['is_tax'] == 1){
- // if($uinfo['taxstatus'] != 1){
- // $this->ajaxReturn(['code'=>3]);
- // }
- // }
-
-
- $puid = $uinfo['invit_1'];
- $puser = M("user")->where(array('id'=>$puid))->field("invit")->find();
-
-
- $ctime = trim(I('post.ctime'));
- if($ctime <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('请选择结算时间')]);
- }
- $ctzed = trim(I('post.ctzed'));
- if($ctzed <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('请选择投资额度')]);
- }
- $ccoinname = trim(I('post.ccoinname'));
- if($ccoinname == ''){
- $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
- }
- $ctzfx = trim(I('post.ctzfx'));
- if($ctzfx <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
- }
- $cykbl = trim(I('post.cykbl'));
- if($ctzfx <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
- }
-
- //获取会员资产
- //$minfo = M("user_coin")->where(array('userid'=>$uid))->find();
- $usdtnum = $uinfo['money'];
-
- //获取合约手续费比例
- $setting = M("hysetting")->where(array('id'=>1))->field("hy_sxf,hy_min")->find();
-
- $hymin = $setting['hy_min'];
- if($hymin > $ctzed){
- $this->ajaxReturn(['code'=>0,'msg'=> L('不能小于最低投资额度')]);
- }
-
- $sxf = $setting['hy_sxf'];
- $tmoney = $ctzed + $ctzed * $sxf / 100;
- if($tmoney > $usdtnum){
- $this->ajaxReturn(['code'=>0,'msg' => L('体验金余额不足')]);
- }
- $o = M("tyhyorder")->where([
- 'status'=>1,
- 'uid'=>$uid
- ])->find();
- /* if($o){
- $this->ajaxReturn(['code'=>0,'msg' => L('还有未完成订单')]);
- }*/
- //获取当前交易对的单价
- $coinarr = explode('/',$ccoinname);
- $symbol = strtolower($coinarr[0].$coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
- $result = $this->get_maket_api($coinapi);
- $price_arr = $result['data'][0];
- $close = $price_arr['close'];//现价
-
- //创建订单
- $odata['uid'] = $uid;
- $odata['username'] = $uinfo['username'];
- $odata['num'] = $ctzed;
- $odata['hybl'] = $cykbl;
- $odata['hyzd'] = $ctzfx;
- $odata['coinname'] = $ccoinname;
- $odata['status'] = 1;
- $odata['is_win'] = 0;
- $odata['buytime'] = date("Y-m-d H:i:s",time());
- $odata['selltime'] = date("Y-m-d H:i:s",(time()+$ctime*60));
- $odata['intselltime'] = time() + $ctime*60;
- $odata['buyprice'] = $close;
- $odata['sellprice'] = '';
- $odata['ploss'] = 0;
- $odata['time'] = $ctime;
- $odata['kongyk'] = 0;
- if($puser['invit'] == ''){
- $puser['invit'] = 0;
- }
- $odata['invit'] = $puser['invit'];
- $order = M("tyhyorder")->add($odata);
- //扣除体验金
- $decre = M("user")->where(array('id'=>$uid))->setDec('money',$tmoney);
-
-
- //创建财务日志
- // $bill['uid'] = $uid;
- // $bill['username'] = $uinfo['username'];
- // $bill['num'] = $ctzed;
- // $bill['coinname'] = "usdt";
- // $bill['afternum'] = $minfo['usdt'] - $ctzed;
- // $bill['type'] = 3;
- // $bill['addtime'] = date("Y-m-d H:i:s",time());
- // $bill['st'] = 2;
- // $bill['remark'] = L('购买').$ccoinname.L('快速合约');
- // $billre = M("bill")->add($bill);
- if($order && $decre){
-
- //这里应该是获取我下的体验合约的最后一单未结算的ID号
- $lastorder = M("tyhyorder")->where(array('status'=>1,'uid'=>$uid))->order("id desc")->field("id")->find();
- $lastid = $lastorder['id'];
- $orderinfo = M("tyhyorder")->where(array('id'=>$lastid))->field("id,hyzd,coinname,buyprice,time,num")->find();
-
- $ajax['code'] = 1;
- $ajax['id'] = $orderinfo['id'];
- if($orderinfo['hyzd'] == 1){
- $ajax['timer_type'] = '<span style="color:green;font-weight:bold;">'.L("买涨").'</span>';
- }else{
- $ajax['timer_type'] = '<span style="color:red;font-weight:bold;">'.L("买跌").'</span>';
- }
- $ajax['coinname'] = $orderinfo['coinname'];
- $ajax['buyprice'] = $orderinfo['buyprice'];
- $ajax['time'] = $orderinfo['time'] * 60;
- $ajax['timer_newprice'] = $close;
- $ajax['timer_buynum'] = $orderinfo['num'];
- $ajax['timer_buyprice'] = $orderinfo['buyprice'];
- $ajax['hyzd'] = $orderinfo['hyzd'];
-
- $this->ajaxReturn($ajax);
- }else{
- $this->ajaxReturn(['code'=>0,'msg' => L('体验订单建仓失败')]);
- }
-
- }else{
- $this->ajaxReturn(['code'=>0,'msg' => L('网络错误')]);
- }
- }
-
- //快速合约建仓
- public function creatorder(){
- if($_POST){
- $uid = userid();
- $uinfo = M("user")->where(array('id'=>$uid))->field("id,username,rzstatus,invit_1,is_tax,taxstatus")->find();
- if(empty($uinfo)){
- $this->ajaxReturn(['code'=>0,'msg'=> L('请先登陆')]);
- }
-
- if($uinfo['rzstatus'] != 2){
- $this->ajaxReturn(['code'=>0,'msg'=>L('请先完成实名认证')]);
- }
-
- //检查该会员是不是需要缴纳税金,如有需要检查有没有缴纳
- if($uinfo['is_tax'] == 1){
- if($uinfo['taxstatus'] != 1){
- $this->ajaxReturn(['code'=>3]);
- }
- }
-
- $puid = $uinfo['invit_1'];
- $puser = M("user")->where(array('id'=>$puid))->field("invit")->find();
-
-
- $ctime = trim(I('post.ctime'));
- if($ctime <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('请选择结算时间')]);
- }
- $ctzed = trim(I('post.ctzed'));
- if($ctzed <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('请选择投资额度')]);
- }
- $ccoinname = trim(I('post.ccoinname'));
- if($ccoinname == ''){
- $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
- }
- $ctzfx = trim(I('post.ctzfx'));
- if($ctzfx <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
- }
- $cykbl = trim(I('post.cykbl'));
- if($ctzfx <= 0){
- $this->ajaxReturn(['code'=>0,'msg'=> L('缺少重要参数')]);
- }
-
- //获取会员资产
- $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
- $usdtnum = $minfo['usdt'];
-
- //获取合约手续费比例
- $setting = M("hysetting")->where(array('id'=>1))->field("hy_sxf,hy_min")->find();
-
- $hymin = $setting['hy_min'];
- if($hymin > $ctzed){
- $this->ajaxReturn(['code'=>0,'msg'=> L('不能小于最低投资额度')]);
- }
-
- $sxf = $setting['hy_sxf'];
- $tmoney = $ctzed + $ctzed * $sxf / 100;
- if($tmoney > $usdtnum){
- $this->ajaxReturn(['code'=>0,'msg' => L('USDT余额不足')]);
- }
- $o = M("hyorder")->where([
- 'status'=>1,
- 'uid'=>$uid
- ])->find();
- /*if($o){
- $this->ajaxReturn(['code'=>0,'msg' => L('还有未完成订单')]);
- }*/
-
- //获取当前交易对的单价
- $coinarr = explode('/',$ccoinname);
- $symbol = strtolower($coinarr[0].$coinarr[1]);
- $coinapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
- $result = $this->get_maket_api($coinapi);
- $price_arr = $result['data'][0];
- $close = $price_arr['close'];//现价
-
- //创建订单
- $odata['uid'] = $uid;
- $odata['username'] = $uinfo['username'];
- $odata['num'] = $ctzed;
- $odata['hybl'] = $cykbl;
- $odata['hyzd'] = $ctzfx;
- $odata['coinname'] = $ccoinname;
- $odata['status'] = 1;
- $odata['is_win'] = 0;
- $odata['buytime'] = date("Y-m-d H:i:s",time());
- $odata['selltime'] = date("Y-m-d H:i:s",(time()+$ctime*60));
- $odata['intselltime'] = time() + $ctime*60;
- $odata['buyprice'] = $close;
- $odata['sellprice'] = '';
- $odata['ploss'] = 0;
- $odata['time'] = $ctime;
- $odata['kongyk'] = 0;
- if($puser['invit'] == ''){
- $puser['invit'] = 0;
- }
- $odata['invit'] = $puser['invit'];
- $order = M("hyorder")->add($odata);
- //扣除USDT额度
- $decre = M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$tmoney);
- //创建财务日志
- $bill['uid'] = $uid;
- $bill['username'] = $uinfo['username'];
- $bill['num'] = $ctzed;
- $bill['coinname'] = "usdt";
- $bill['afternum'] = $minfo['usdt'] - $ctzed;
- $bill['type'] = 3;
- $bill['addtime'] = date("Y-m-d H:i:s",time());
- $bill['st'] = 2;
- $bill['remark'] = L('购买').$ccoinname.L('快速合约');
- $billre = M("bill")->add($bill);
- if($order && $decre && $billre){
-
- $lastorder = M("hyorder")->where(array('status'=>1,'uid'=>$uid))->order("id desc")->field("id")->find();
- $lastid = $lastorder['id'];
- $orderinfo = M("hyorder")->where(array('id'=>$lastid))->field("id,hyzd,coinname,buyprice,time,num")->find();
-
- $ajax['code'] = 1;
- $ajax['id'] = $orderinfo['id'];
- if($orderinfo['hyzd'] == 1){
- $ajax['timer_type'] = '<span style="color:green;font-weight:bold;">'.L("买涨").'</span>';
- }else{
- $ajax['timer_type'] = '<span style="color:red;font-weight:bold;">'.L("买跌").'</span>';
- }
- $ajax['coinname'] = $orderinfo['coinname'];
- $ajax['buyprice'] = $orderinfo['buyprice'];
- $ajax['time'] = $orderinfo['time'] * 60;
- $ajax['timer_newprice'] = $close;
- $ajax['timer_buynum'] = $orderinfo['num'];
- $ajax['timer_buyprice'] = $orderinfo['buyprice'];
- $ajax['hyzd'] = $orderinfo['hyzd'];
- $this->ajaxReturn($ajax);
- }else{
- $this->ajaxReturn(['code'=>0,'msg' => L('建仓失败')]);
- }
-
- }else{
- $this->ajaxReturn(['code'=>0,'msg' => L('网络错误')]);
- }
- }
- //快速合约首页面
- public function index(){
- $uid = userid();
- if($uid <= 0 || $uid == ''){
- $uid = 0;
- }
- $this->assign('uid',$uid);
-
-
- $smybol = trim(I('get.coin'));
- if($smybol != ''){
- $map['status'] = 1;
- $map['coinname'] = array('like',"%$smybol%");
- $list = M("ctmarket")->where($map)->field("coinname")->find();
- if(!empty($list)){
- $smybol = $list['coinname'];
- }else{
- $smybol = "btc";
- }
- }else{
- $smybol = "btc";
- }
- $market = $smybol."usdt";
- $upmarket = strtoupper($smybol)."/USDT";
- $this->assign('upmarket',$upmarket);
- $this->assign('market',$market);
- $this->assign('smybol',$smybol);
-
- //获取合约设置项
- $hyset = M("hysetting")->where(array('id'=>1))->field("hy_time,hy_ykbl,hy_tzed")->find();
- $hy_time = $hyset['hy_time'];
- $hy_ykbl = $hyset['hy_ykbl'];
- $hy_tzed = $hyset['hy_tzed'];
- $time_arr = explode(',',$hy_time);
- $ykbl_arr = explode(',',$hy_ykbl);
- $tzed_arr = explode(',',$hy_tzed);
- $cd=array();
- $ed=array();
- $len = count($time_arr);
- for($i=0;$i<$len;$i++){
- $cd[$i]['sort'] = $i+1;
- $cd[$i]['time'] = $time_arr[$i];
- $cd[$i]['ykbl'] = $ykbl_arr[$i];
- $ed[$i]['sort'] = $i+1;
- $ed[$i]['tzed'] = $tzed_arr[$i];
- }
-
- $this->assign("cd",$cd);
- $this->assign('ed',$ed);
- //获取会员资产
- $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
- $usdt_blan = $minfo['usdt'];
- $this->assign('eusdt_blan',$usdt_blan);
-
- //体验金
- $uinfo = M("user")->where(array('id'=>$uid))->field("money")->find();
- $tj_money = $uinfo['money'];
- $this->assign('tj_money',$tj_money);
-
-
-
- $this->display();
- }
-
- }
- ?>
|