123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- <?php
- /* 交易中心 */
- namespace Mobile\Controller;
- class TradeController extends MobileController
- {
- protected function _initialize()
- {
- parent::_initialize(); $allow_action=array("index","bauhinia","tradelist","ordinary","trans","transinfo","upbbbuy","getprice","upbbsell","clearorder","tradebill","billinfo");
- if(!in_array(ACTION_NAME,$allow_action)){
- $this->error(L("非法操作"));
- }
- }
- //成交详情
- public function billinfo($id = null){
- $uid = userid();
- if($uid <= 0){
- $this->ajaxReturn(['code' => 3, 'info' => L('未登录')]);
- }
- $info = M("bborder")->where(array('id'=>$id))->find();
- $this->ajaxReturn(['code' => 1, 'data' => $info]);
- // $this->assign('info',$info);
- // $this->display();
- }
- public function bauhinia()
- {
- $uid = userid();
- $name = I('get.name');
- $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
- $this->ajaxReturn(['code' => 1, 'data' => $minfo[strtolower($name)]]);
- }
- //全部委托订单及交易记录
- public function tradebill(){
- $uid = userid();
- if($uid <= 0){
- $this->ajaxReturn(['code' => 3, 'info' => L('未登录')]);
- }
- $type = I('get.type');
- if ($type == 1){
- //全总未交易委托
- $list = M("bborder")->where(array('uid'=>$uid,'ordertype'=>1,'status'=>1))->order('id desc')->select();
- }elseif ($type == 2){
- //全部交易记录
- $list = M("bborder")->where("uid = {$uid} and status != 1")->order('id desc')->select();
- }elseif ($type == 3){
- //全部成交记录
- $list = M("bborder")->where(array('uid'=>$uid,'status'=>2))->order('id desc')->select();
- }
- $this->ajaxReturn(['code' => 1, 'data' => $list]);
- }
- //取消委托订单
- public function clearorder($oid=null){
- if(checkstr($oid)){
- $this->ajaxReturn(['code'=>0,'info'=>L('缺少重要参数')]);
- }
- $oinfo = M("bborder")->where(array('id'=>$oid))->find();
- if(empty($oinfo)){
- $this->ajaxReturn(['code'=>0,'info'=>L('委托订单不存在')]);
- }
- $uid = $oinfo['uid'];
- $type = $oinfo['type'];
- //买入委托
- if($type == 1){
- $coin = "usdt";
- $num = $oinfo['usdtnum'];
- //出售委托
- }elseif($type == 2){
- $coin = strtolower($oinfo['coin']);
- $num = $oinfo['coinnum'];
- }
- M()->startTrans();
- $upre = M("bborder")->where(array('id'=>$oid))->lock(true)->save(array('status'=>3));
- $coind = $coin."d";
- //把冻结的资产转移到可用资产里
- $decre = M("user_coin")->where(array('userid'=>$uid))->setDec($coind,$num);
- $incre = M("user_coin")->where(array('userid'=>$uid))->setInc($coin,$num);
- if($upre && $decre && $incre){
- M()->commit();
- $this->ajaxReturn(['code'=>1,'info'=>"订单已撤消"]);
- }else{
- M()->rollback();
- $this->ajaxReturn(['code'=>0,'info'=>"订单撤消失败"]);
- }
- }
- //币币交易出售处理
- public function upbbsell($symbol=null,$mprice=null,$mnum=null,$selltype=null){
- if(checkstr($symbol)||checkstr($mprice)||checkstr($mnum)||checkstr($selltype)){
- $this->ajaxReturn(['code'=>0,'info'=>L('您输入的信息有误')]);
- }
- //判断是否开市
- $nowtime = date('H:i',time());
- $bbset = M("bbsetting")->where(array('id'=>1))->find();
- $kstime = explode("~",$bbset['bb_kstime']);
- $start = $kstime[0];
- $end = $kstime[1];
- if($nowtime < $start || $nowtime > $end){
- $this->ajaxReturn(['code' => 3, 'info' => L('未登录')]);
- }
- //判断登陆
- $uid = userid();
- $uinfo = M("user")->where(array('id'=>$uid))->field("id,username,rzstatus,is_tax,taxstatus")->find();
- if(empty($uinfo)){
- $this->ajaxReturn(['code'=>3,'info'=>L('请先登录')]);
- }
- if($uinfo['rzstatus'] != 2){
- $this->ajaxReturn(['code'=>0,'info'=>L('请先完成实名认证')]);
- }
- //检查该会员是不是需要缴纳税金,如有需要检查有没有缴纳
- if($uinfo['is_tax'] == 1){
- if($uinfo['taxstatus'] != 1){
- $this->ajaxReturn(['code'=>3]);
- }
- }
- if($symbol == ''){
- $this->ajaxReturn(['code'=>0,'info'=>L('缺少重要参数')]);
- }
- $arr = explode("/",$symbol);
- $coin = $arr[0];
- if($coin == "UKB"){
- $marketinfo = M("ctmarket")->where(array('coinname'=>"ukb"))->field("state")->find();
- if($marketinfo['state'] != 1){
- $this->ajaxReturn(['code'=>0,'info'=>L('禁止交易')]);
- }
- }
- if($coin == "GCB"){
- $marketinfo = M("ctmarket")->where(array('name'=>"gcb_usdt"))->field("state")->find();
- if($marketinfo['state'] != 1){
- $this->ajaxReturn(['code'=>0,'info'=>L('禁止交易')]);
- }
- }
- $lowercname = strtolower($arr[0]);
- $lowercoin = strtolower($arr[0]).strtolower($arr[1]);
- $coinname = strtoupper($coin);
- //查手续费比例
- $coininfo = M("coin")->where(array('name'=>$coinname))->field("bbsxf")->find();
- $sxf = $coininfo['bbsxf'];
- if($sxf < 0){
- $sxf = 0;
- }
- //查账户余额
- $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
- $coin_blance = $minfo[$lowercname];
- if($coin_blance < $mnum){
- $this->ajaxReturn(['code'=>0,'info'=>L($lowercname.'余额不足')]);
- }
- //限价必须有出售数量 限价单价
- if($selltype == 1){
- if($mnum <= 0){
- $this->ajaxReturn(['code'=>0,'info'=>L('请输入出售额度')]);
- }
- if($mprice <= 0){
- $this->ajaxReturn(['code'=>0,'info'=>L('请输入限价价格')]);
- }
- //写入交易记录
- $xjdata['uid'] = $uid;
- $xjdata['account'] = $uinfo['username'];
- $xjdata['type'] = 2;
- $xjdata['ordertype'] = 1;
- $xjdata['symbol'] = $symbol;
- $xjdata['coin'] = $coin;
- $xjdata['coinnum'] = $mnum;
- $xjdata['xjprice'] = $mprice;
- $xjdata['addtime'] = date("Y-m-d H:i:s",time());
- $xjdata['coin'] = $coin;
- $xjdata['status'] = 1;
- $xjdata['sxfbl'] = $sxf;
- $addre = M("bborder")->add($xjdata);
- //把资产转入冻结字段
- $decre = M("user_coin")->where(array('userid'=>$uid))->setDec($lowercname,$mnum);
- $incre = M("user_coin")->where(array('userid'=>$uid))->setInc($lowercname."d",$mnum);
- if($addre && $decre && $incre){
- $this->ajaxReturn(['code'=>1,'info'=> L('订单委托成功')]);
- }else{
- $this->ajaxReturn(['code'=>0,'info'=> L('订单委托失败')]);
- }
- //市价 出售数量,获取当前单价
- }elseif($selltype == 2){
- if($mnum <= 0){
- $this->ajaxReturn(['code'=>0,'info'=>L('请输入出售额度')]);
- }
- if($coin == "UKB"){
- $priceinfo = M("market")->where(array('name'=>"ukb_usdt"))->field("new_price")->find();
- $close = $priceinfo['new_price'];//现价
- }elseif ($coin == 'GCB'){
- $priceinfo = M("market")->where(array('name'=>"gcb_usdt"))->field("new_price")->find();
- $close = $priceinfo['new_price'];//现价
- }else{
- //获取当前交易对价格
- $oy_list = do_request(oy_url($lowercname, 'api/v5/market/ticker?instId='), []);
- $oy_list = $oy_list->data[0];
- $close = $oy_list->last;//现价
- }
- //求出卖出所得USDT
- $allusdt = $mnum * $close;
- //手续费
- $sxfnum = $allusdt * $sxf / 100;
- //实际到账USDT
- $tusdt = $allusdt - $sxfnum;
- //写入交易记录
- $sjdata['uid'] = $uid;
- $sjdata['account'] = $uinfo['username'];
- $sjdata['type'] = 2;
- $sjdata['ordertype'] = 2;
- $sjdata['symbol'] = $symbol;
- $sjdata['coin'] = $coin;
- $sjdata['coinnum'] = $mnum;
- $sjdata['usdtnum'] = $tusdt;
- $sjdata['price'] = $close;
- $sjdata['xjprice'] = $close;
- $sjdata['addtime'] = date("Y-m-d H:i:s",time());
- $sjdata['tradetime'] = date("Y-m-d H:i:s",time());
- $sjdata['fee'] = $sxfnum;
- $sjdata['sxfbl'] = $sxf;
- $sjdata['status'] = 2;
- $addre = M("bborder")->add($sjdata);
- //扣除卖出额度并写入日志
- $decre = M("user_coin")->where(array('userid'=>$uid))->setDec($lowercname,$mnum);
- $cbill['uid'] = $uid;
- $cbill['username'] = $uinfo['username'];
- $cbill['num'] = $mnum;
- $cbill['coinname'] = $lowercname;
- $cbill['afternum'] = $minfo[$lowercname] - $mnum;
- $cbill['type'] = 10;
- $cbill['addtime'] = date("Y-m-d H:i:s",time());
- $cbill['st'] = 2;
- $cbill['remark'] = L('币币交易出售').$coin;
- $cbillre = M("bill")->add($cbill);
- //增加USDT数量并写入日志
- $incre = M("user_coin")->where(array('userid'=>$uid))->setInc("usdt",$tusdt);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $tusdt;
- $ubill['coinname'] = "usdt";
- $ubill['afternum'] = $minfo['usdt'] + $tusdt;
- $ubill['type'] = 9;
- $ubill['addtime'] = date("Y-m-d H:i:s",time());
- $ubill['st'] = 1;
- $ubill['remark'] = L('币币交易出售').$coin;
- $ubillre = M("bill")->add($ubill);
- if($addre && $decre && $cbillre && $incre && $ubillre){
- $this->ajaxReturn(['code'=>1,"info"=>L('出售成功')]);
- }else{
- $this->ajaxReturn(['code'=>0,"info"=>L('出售失败')]);
- }
- }
- }
- //币币交易购买处理
- public function upbbbuy($symbol=null,$mprice=null,$musdt=null,$buytype=null){
- if(checkstr($symbol)||checkstr($mprice)||checkstr($musdt)||checkstr($buytype)){
- $this->ajaxReturn(['code'=>0,'info'=>L('您输入的信息有误')]);
- }
- if ($musdt <= 0) $this->ajaxReturn(['code'=>0,'info'=>L('請輸入購買數量')]);
- //判断是否开市
- $nowtime = date('H:i',time());
- $bbset = M("bbsetting")->where(array('id'=>1))->find();
- $kstime = explode("~",$bbset['bb_kstime']);
- $start = $kstime[0];
- $end = $kstime[1];
- if($nowtime < $start || $nowtime > $end){
- $this->ajaxReturn(['code'=>0,'info'=>L('未开市')]);
- }
- $uid = userid();
- $uinfo = M("user")->where(array('id'=>$uid))->field("id,username,rzstatus,is_tax,taxstatus")->find();
- if(empty($uinfo)){
- $this->ajaxReturn(['code'=>3,'info'=>L('请先登录')]);
- }
- if($uinfo['rzstatus'] != 2){
- $this->ajaxReturn(['code'=>0,'info'=>L('请先完成实名认证')]);
- }
- //检查该会员是不是需要缴纳税金,如有需要检查有没有缴纳
- if($uinfo['is_tax'] == 1){
- if($uinfo['taxstatus'] != 1){
- $this->ajaxReturn(['code'=>3]);
- }
- }
- if($symbol == ''){
- $this->ajaxReturn(['code'=>0,'info'=>L('缺少重要参数')]);
- }
- $arr = explode("/",$symbol);
- $coin = $arr[0];
- if($coin == "UKB"){
- $marketinfo = M("ctmarket")->where(array('coinname'=>"ukb"))->field("state")->find();
- if($marketinfo['state'] != 1){
- $this->ajaxReturn(['code'=>0,'info'=>L('禁止交易')]);
- }
- }
- if($coin == "GCB"){
- $marketinfo = M("ctmarket")->where(array('name'=>"gcb_usdt"))->field("state")->find();
- if($marketinfo['state'] != 1){
- $this->ajaxReturn(['code'=>0,'info'=>L('禁止交易')]);
- }
- }
- $lowercoin = strtolower($arr[0]).strtolower($arr[1]);
- $coinname = strtoupper($coin);
- $coininfo = M("coin")->where(array('name'=>$coinname))->field("bbsxf")->find();
- $sxf = $coininfo['bbsxf'];
- if($sxf < 0){
- $sxf = 0;
- }
- //需要查会员的账号USDT余额
- $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
- if($musdt > $minfo['usdt']){
- $this->ajaxReturn(['code'=>0,'info'=>L('USDT余额不足')]);
- }
- //限价必须有单价,USDT量
- //市价必须有USDT量,再获取当前最新单价
- if($buytype == 1){//限价
- if($mprice <= 0){
- $this->ajaxReturn(['code'=>0,'info'=>L('请输入限价价格')]);
- }
- $xjdata['uid'] = $uid;
- $xjdata['account'] = $uinfo['username'];
- $xjdata['type'] = 1;
- $xjdata['ordertype'] = 1;
- $xjdata['symbol'] = $symbol;
- $xjdata['coin'] = $coin;
- $xjdata['usdtnum'] = $musdt;
- $xjdata['xjprice'] = $mprice;
- $xjdata['addtime'] = date("Y-m-d H:i:s",time());
- $xjdata['coin'] = $coin;
- $xjdata['status'] = 1;
- $xjdata['sxfbl'] = $sxf;
- //添加限价委托记录
- $addre = M("bborder")->add($xjdata);
- //把USDT转入冻结字段
- $decre = M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$musdt);
- $incre = M("user_coin")->where(array('userid'=>$uid))->setInc('usdtd',$musdt);
- if($addre && $decre && $incre){
- $this->ajaxReturn(['code'=>1,'info'=> L('订单委托成功')]);
- }
- }elseif($buytype == 2){//市价
- //计算买入币的量
- //获取当前交易对价格
- //获取当前交易对价格
- if ($coin == 'GCB'){
- $priceinfo = M("market")->where(array('name'=>"gcb_usdt"))->field("new_price")->find();
- $close = $priceinfo['new_price'];//现价
- }else{
- //获取当前交易对价格
- $oy_list = do_request(oy_url($coinname, 'api/v5/market/ticker?instId='), []);
- $oy_list = $oy_list->data[0];
- $close = $oy_list->last;//现价
- }
- $coinnum = sprintf("%.8f",($musdt / $close));
- //计算手续费
- $sxfnum = $coinnum * $sxf / 100;
- //实际到账号的币量
- $tnum = $coinnum - $sxfnum;
- $sjdata['uid'] = $uid;
- $sjdata['account'] = $uinfo['username'];
- $sjdata['type'] = 1;
- $sjdata['ordertype'] = 2;
- $sjdata['symbol'] = $symbol;
- $sjdata['coin'] = $coin;
- $sjdata['coinnum'] = $coinnum;
- $sjdata['usdtnum'] = $musdt;
- $sjdata['price'] = $close;
- $sjdata['xjprice'] = $close;
- $sjdata['addtime'] = date("Y-m-d H:i:s",time());
- $sjdata['tradetime'] = date("Y-m-d H:i:s",time());
- $sjdata['fee'] = $sxfnum;
- $sjdata['sxfbl'] = $sxf;
- $sjdata['status'] = 2;
- $lowercoin = strtolower($coin);
- //生成交易记录
- $addre = M("bborder")->add($sjdata);
- //扣除USDT额度并写日志
- $decre = M("user_coin")->where(array('userid'=>$uid))->setDec('usdt',$musdt);
- $ubill['uid'] = $uid;
- $ubill['username'] = $uinfo['username'];
- $ubill['num'] = $musdt;
- $ubill['coinname'] = "usdt";
- $ubill['afternum'] = $minfo['usdt'] - $musdt;
- $ubill['type'] = 9;
- $ubill['addtime'] = date("Y-m-d H:i:s",time());
- $ubill['st'] = 2;
- $ubill['remark'] = L('币币交易购买').$coin;
- $ubillre = M("bill")->add($ubill);
- //增加币种额度并写日志
- $incre = M("user_coin")->where(array('userid'=>$uid))->setInc($lowercoin,$tnum);
- $cbill['uid'] = $uid;
- $cbill['username'] = $uinfo['username'];
- $cbill['num'] = $coinnum;
- $cbill['coinname'] = $lowercoin;
- $cbill['afternum'] = $minfo[$lowercoin] + $coinnum;
- $cbill['type'] = 10;
- $cbill['addtime'] = date("Y-m-d H:i:s",time());
- $cbill['st'] = 1;
- $cbill['remark'] = L('币币交易购买').$coin;
- $cbillre = M("bill")->add($cbill);
- if($addre && $decre && $ubillre && $incre && $cbillre){
- $this->ajaxReturn(['code'=>1,'info'=>L('交易成功')]);
- }else{
- $this->ajaxReturn(['code'=>0,'info'=>L('交易失败')]);
- }
- }
- }
- //获取行情数据
- public function getprice($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 transinfo(){
- $uid = userid();
- $symbol = trim(I('get.symbol'));
- $coin = strtolower($symbol);
- $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
- $usdt_blance = $minfo['usdt'];
- $coin_blance = $minfo[$coin];
- $this->assign('usdt_blance',$usdt_blance);
- $this->assign('coin_blance',$coin_blance);
- $this->assign('uid',$uid);
- $coinname = $symbol."/"."USDT";
- $this->assign('coinname',$coinname);
- $this->assign('symbol',$symbol);
- $where['ordertype'] = 1;
- $where['status'] = 1;
- $where['uid'] = $uid;
- $where['coin'] = array('eq',$symbol);
- $list = M("bborder")->where($where)->select();
- $this->assign('list',$list);
- $this->display();
- }
- //币币交易页面
- public function trans(){
- $uid = userid();
- $this->assign('uid',$uid);
- $sytx = trim(I('get.sytx'));
- $txarr = explode('/',$sytx);
- $symbol = $txarr[0];
- $market = strtolower($txarr[0].$txarr[1]);
- if($symbol == ''){
- $symbol = 'btc';
- }
- if($market == ''){
- $market = 'btcusdt';
- }
- $upmarket = strtoupper($symbol)."/USDT";
- $this->assign('upmarket',$upmarket);
- $this->assign('market', $market);
- $this->assign('smybol',$symbol);
- $lowercoin = strtolower($symbol);
- $cmarket = M("ctmarket")->where(array('coinname'=>$lowercoin))->field("state")->find();
- $state = $cmarket['state'];
- $this->assign('state',$state);
- $this->display();
- }
- //网站首页
- public function tradelist(){
- $uid = userid();
- $this->assign('uid',$uid);
- $clist = M("config")->where(array('id'=>1))->field("websildea,websildeb,websildec")->find();
- $this->assign('clist',$clist);
- $websildec = $clist['websildec'];
- foreach ($clist as &$item){
- $item = 'http://'.$_SERVER['HTTP_HOST'].'/Upload/public/'.$item;
- }
- // $this->assign('websildec',$websildec);
- $nlist = M("content")->where(array('status'=>1))->order("id desc")->field("title,id")->select();
- $this->assign('nlist',$nlist);
- if($uid > 0){
- $sum = M("notice")->where(array('uid'=>$uid,'status'=>1))->count();
- }else{
- $sum = 0;
- }
- $this->assign('sum',$sum);
- $list = M("ctmarket")->where(array('status'=>1))->field("coinname,id")->select();
- $this->assign("market",$list);
- $info = M("content")->where(array('status'=>1))->order("id desc")->find();
- $data = [
- 'uid' => $uid,
- 'clist' => $clist,
- 'websildec' => $websildec,
- 'sum' => $sum,
- 'ctmarket' => $list,
- 'info' => $info
- ];
- $this->ajaxReturn(['code' => 1, 'data' => $data, 'info' => '']);
- }
- //交易市场
- public function index(){
- $where['status'] = 1;
- //$where['coinname'] = array('eq','ukb');
- $list = M("ctmarket")->where($where)->field("coinname,id")->select();
- $this->assign("market",$list);
- $this->display();
- }
- /**
- * 普通K线图
- */
- public function ordinary($market = NULL)
- {
- $market = trim(I('get.market'));
- if($market == ''){
- $market = "btcusdt";
- }
- $this->assign('market', $market);
- $this->display();
- }
- /**
- * 专业K线图
- * @param [type] $market [description]
- * @return [type] [description]
- */
- public function specialty($market = NULL)
- {
- // 过滤非法字符----------------S
- if (checkstr($market)) {
- $this->error(L('您输入的信息有误'));
- }
- // 过滤非法字符----------------E
- if (!$market) {
- $market = C('market_mr');
- }
- $this->assign('market', $market);
- $this->display();
- }
- }
- ?>
|