Files
jyshd_mWtK2/application/api/controller/Cycle.php
T
li 1b24994e74 feat: 后端全量更新 - 含所有本次需求
- Contract.php: 返回合约账户余额(balance_contract)
- My.php: 地址管理增加BTC/ETH
- AppContract.php: 一键平仓(closeall)
- AppProxy.php: 代理专属注册链接 + 分级权限(L1/L2)
- site.php: 手续费减半(0.018→0.009)
- agent_permission_setup.sql: 代理权限SQL
- crypto_news_crawler.py: 新闻自动采集脚本
2026-03-30 20:16:32 +08:00

521 lines
20 KiB
PHP
Executable File

<?php
/**
* Created by PhpStorm.
* User: cchhyy
* Date: 2021/2/3
* Time: 4:35 PM
*/
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Config;
use app\common\library\Sms;
use fast\Random;
/**
* 秒合约
*/
class Cycle extends Api
{
protected $noNeedLogin = ['get_cycle_coin'];
protected $noNeedRight = '*';
//获取盈利订单
public function get_profit_order()
{
$params = $this->request->post();
$size = (isset($params['size']))?$params['size']:20;
$symbol = (!isset($params['symbol']))?'btcusdt':$params['symbol'];
$data = Db::name('his_order')
->field('createtime,symbol,trade_result_num,rise_or_fall,close_price as price')
->where('symbol',$symbol)
->where('trade_result','win')
->order('id','desc')
->paginate($size,false,['query' => request()->param()]);
foreach ($data as $key=>$value)
{
$value['trade_result_num'] = sprintf('%.2f',$value['trade_result_num']);
$value['createtime'] = date('m-d H:i',$value['createtime']);
$data[$key] = $value;
}
$this->success('success',$data);
}
//获取支持周期的币种
public function get_cycle_coin()
{
$params = $this->request->post();
if(isset($params['id']) && !empty($params['id']))
{
$data = Db::name('app_cycleset')
->where('id',$params['id'])
->find();
$hour = date('H');
$min = date('i');
$data['cyclejson'] = json_decode($data['cyclejson'], true);
$data['lostjson'] = json_decode($data['lostjson'], true);
$data['trade_time'] = explode(',', $data['trade_time']);
$winpl = 0;
$usdt = Db::name('app_currency_user')
->field('num')
->where('user_id',$this->auth->id)
->where('curr_id',1)->find();
$data['usdt'] = $usdt['num'];
$data['nowtime'] = time() - 1;
foreach ($data['cyclejson'] as $key=>$value)
{
$cycletime = explode('-', $key);
$cycletime1 = explode(":", $cycletime[0]);
$cycletime2 = explode(":", $cycletime[1]);
// var_dump($cycletime,$cycletime1,$cycletime2,$hour,$min);exit;
if($hour >= (int)$cycletime1[0] && $min >= (int)$cycletime1[1] && $hour< (int)$cycletime2[0])
{
$winpl = $value;
break;
}
}
$data['winpl'] = $winpl;
$data['exchange_rate'] = Config::get('site.exchange_rate');
}else {
$data = Db::name('app_cycleset a')
->field('a.*,b.high,b.low,b.open,b.close,b.increase,b.logo_image,b.amount,b.market_jd,b.price_jd,b.bb_type')
->join('app_rate b','a.curr_id=b.id','left')
->order('curr_id','asc')
->select();
foreach ($data as $key => $value) {
if($value['symbol'] == 'btcusdt')
{
$coindata = Db::name('hb_1s_btc')->order('ts','desc')
->find();
$value['close'] = $coindata['price'];
}else if($value['symbol'] == 'ethusdt')
{
$coindata = Db::name('hb_1s_eth')->order('ts','desc')
->find();
$value['close'] = $coindata['price'];
}
else if($value['symbol'] == 'bchusdt')
{
$coindata = Db::name('hb_1s_bch')->order('ts','desc')
->find();
$value['close'] = $coindata['price'];
}
else if($value['symbol'] == 'ltcusdt')
{
$coindata = Db::name('hb_1s_ltc')->order('ts','desc')
->find();
$value['close'] = $coindata['price'];
}
else if($value['symbol'] == 'eosusdt')
{
$coindata = Db::name('hb_1s_eos')->order('ts','desc')
->find();
$value['close'] = $coindata['price'];
}
$value['cyclejson'] = json_decode($value['cyclejson'], true);
$value['lostjson'] = json_decode($value['lostjson'], true);
$value['trade_time'] = explode(',', $value['trade_time']);
$value['logo_image'] = Config::get("site.image_url").$value['logo_image'];
$value['exchange_rate'] = Config::get("site.exchange_rate");
if($value['symbol'] == 'eosusdt') {
$value['high'] = sprintf('%.4f',$value['high']);
$value['low'] = sprintf('%.4f',$value['low']);
$value['open'] = sprintf('%.4f',$value['open']);
$value['close'] = sprintf('%.4f', $value['close']);
}else{
$value['high'] = sprintf('%.2f',$value['high']);
$value['low'] = sprintf('%.2f',$value['low']);
$value['open'] = sprintf('%.2f',$value['open']);
$value['close'] = sprintf('%.2f', $value['close']);
}
$hour = date('H');
$min = date('i');
$winpl = 0;
$usdt = Db::name('app_currency_user')
->field('num')
->where('user_id',$this->auth->id)
->where('curr_id',1)->find();
$value['usdt'] = $usdt['num'];
foreach ($value['cyclejson'] as $k=>$val)
{
$cycletime = explode('-', $k);
$cycletime1 = explode(":", $cycletime[0]);
$cycletime2 = explode(":", $cycletime[1]);
// var_dump($cycletime,$cycletime1,$cycletime2,$hour,$min);exit;
if($hour >= (int)$cycletime1[0] && $min >= (int)$cycletime1[1] && $hour< (int)$cycletime2[0])
{
$winpl = $val;
break;
}
}
$value['winpl'] = $winpl;
$value['exchange_rate'] = Config::get('site.exchange_rate');
$value['nowtime'] = time() - 1;
$data[$key] = $value;
}
}
$this->success('success',$data);
}
//获取持仓订单
public function get_order()
{
$symbol = $this->request->post("symbol");
$data = Db::name('order')
->where('user_id',$this->auth->id)
->where("symbol",$symbol)
->order('id','asc')
->select();
foreach ($data as $key=>$value)
{
$difftime = time() - $value['createtime'];
if($difftime >= $value['trade_cycle']){
unset($data[$key]);
}
$value['nowtime'] = time();
$value['current_price'] = sprintf("%.2f",$value['current_price']);
$value['trade_num'] = sprintf("%.2f",$value['trade_num']);
$value['show_name'] = strtoupper(str_replace('usdt','',$value['symbol'])).'/USDT';
$value['open_time'] = date('H:i:s',$value['open_time']);
$data[$key] = $value;
}
sort($data);
$this->success('success',$data);
}
//获取全部订单
public function get_all_order()
{
$params = $this->request->post();
$size = (isset($params['size']))?$params['size']:20;
$symbol = (isset($params['symbol']))?$params['symbol']:'btcusdt';
$data = Db::name('his_order')
->field('createtime,symbol,trade_num,trade_cycle,expect_percent,current_price,close_price,trade_result_num,
trade_result,rise_or_fall')
->where('user_id',$this->auth->id)
->where('symbol',$symbol)
->order('id','desc')
->paginate($size,false,['query' => request()->param()]);
foreach ($data as $key => $value)
{
$value['createtime'] = date('m-d H:i:s',$value['createtime']+$value['trade_cycle']);
$value['current_price'] = sprintf("%.2f",$value['current_price']);
$value['close_price'] = sprintf("%.2f",$value['close_price']);
$value['show_name'] = strtoupper(str_replace('usdt','',$value['symbol'])).'/USDT';
$value['current_price'] = sprintf("%.2f",$value['current_price']);
$value['trade_num'] = sprintf("%.2f",$value['trade_num']);
$data[$key] = $value;
}
$this->success('success',$data);
}
//开始下单
public function start_order()
{
$params = $this->request->post();
if(!isset($params['symbol']) || empty($params['symbol']))
{
$this->error(__('请选择交易对'));
}
if(!isset($params['num']) || empty($params['num']) || !is_numeric($params['num']) || $params['num']<=0)
{
$this->error(__('请输入正确的交易数量'));
}
if(!isset($params['cycle']) || empty($params['cycle']))
{
$this->error(__('请选择交易周期'));
}
if(!isset($params['position']) || empty($params['position']))
{
$this->error(__('请选择交易涨跌'));
}
$times = time(); //-691200
if(isset($params['times']) && ($times - $params['times']) < 5) $times = $params['times'];
//实名认证判断
if(!controller("Common")->get_auth()){
$this->error(__("请先完成实名认证"),['jump'=>1]);
}
//周期信息
$symbol = Db::name('app_cycleset')
->where('symbol',$params['symbol'])
->find();
if(!$symbol){
$this->error(__('交易对不存在'));
}
//限额判断
$numss = explode('-',$symbol['limit_num']);
if($params['num']< $numss[0] || $params['num'] > $numss[1])
{
$this->error(__('请输入正确的交易数量').$symbol['limit_num']);
}
$cycletime = explode(',',$symbol['trade_time']);
if(!in_array($params['cycle'],$cycletime)){
$this->error(__('交易周期错误'));
}
$currency = Db::name('app_currency_user')
->field('num,id')
->where('user_id',$this->auth->id)
->where('curr_id',1)->find();
if($currency['num'] < $params['num'])
{
$this->error(__('账号余额不足'));
}
//周期条件
// if($params['cycle'] >= 300 && $currency['num']<80000){
// $this->error("该周期需拥有资产80000USDT");
// }
// if($params['cycle'] >= 120 && $currency['num']<30000){
// $this->error("该周期需拥有资产30000USDT");
// }
$cyclejson = json_decode($symbol['cyclejson'],true);
$hour = date('H');
$min = date('i');
$winpl = 0;
foreach ($cyclejson as $key=>$value)
{
$cycletime = explode('-', $key);
$cycletime1 = explode(":", $cycletime[0]);
$cycletime2 = explode(":", $cycletime[1]);
// var_dump($cycletime,$cycletime1,$cycletime2,$hour,$min);exit;
if($hour >= (int)$cycletime1[0] && (($hour == (int)$cycletime1[0] && $min >= (int)$cycletime1[1]) || $hour > (int)$cycletime1[0]) &&
(($hour == (int)$cycletime2[0] && $min <= (int)$cycletime2[1]) || $hour < (int)$cycletime2[0] ))
{
$winpl = $value;
break;
}
}
$lostjson = json_decode($symbol['lostjson'],true);
$lostpl = 0;
foreach ($lostjson as $key=>$value)
{
$cycletime = explode('-', $key);
$cycletime1 = explode(":", $cycletime[0]);
$cycletime2 = explode(":", $cycletime[1]);
// var_dump($cycletime,$cycletime1,$cycletime2,$hour,$min);exit;
if($hour >= (int)$cycletime1[0] && (($hour == (int)$cycletime1[0] && $min >= (int)$cycletime1[1]) || $hour > (int)$cycletime1[0]) &&
(($hour == (int)$cycletime2[0] && $min <= (int)$cycletime2[1]) || $hour < (int)$cycletime2[0] ))
{
$lostpl = $value;
break;
}
}
// $order = [
// 'serial_no' => time().Random::build('numeric',8),
// 'user_id' => '8',
// 'createtime' => $times,
// 'symbol' => $params['symbol'],
// 'trade_num' => $params['num'],
// 'trade_cycle' => $params['cycle'],
// 'expect_percent' => 0.4,
// 'expect_income' => sprintf("%.2f",$params['num']*0.4),
// 'lose_percent' => 1,
// 'lose_income' => sprintf('%.2f',$params['num']*1),
// 'current_price' => 50000.00,
// 'rise_or_fall' => $params['position'],
// 'open_time' => $times+$params['cycle']
// ];
// $return = $order;
// $return['current_price'] = sprintf("%.2f",$return['current_price']);
// $return['trade_num'] = sprintf("%.2f",$return['trade_num']);
// $return['id'] = 100000;
// $return['nowtime'] = time();
// $return['show_name'] = strtoupper(str_replace('usdt','',$return['symbol'])).'/USDT';
// $this->success('提交成功',$return);exit;
$tablename = 'hb_1s_'.str_replace('usdt','',strtolower($params['symbol']));
for($i=1;$i<=10;$i++) {
$current_price = Db::name($tablename)
->where('ts', $times)
->order('ts', 'desc')->find();
if($current_price){
break;
}
}
if(!$current_price)
{
$current_price = Db::name($tablename)
->where('ts',"<=", $times)
->order('ts', 'desc')->find();
}
// var_dump($current_price,date('Y-m-d H:i:s',$current_price['ts']),date('Y-m-d H:i:s',$times),$i);exit;
//redis防重复点击
$symbol = "order" . $this->auth->id;
$submited = pushRedis($symbol);
if (!$submited) {
$this->error(__("操作频繁"));
}
//下单数据
$order = [
'serial_no' => time().Random::build('numeric',4),
'user_id' => $this->auth->id,
'createtime' => $times,
'symbol' => $params['symbol'],
'trade_num' => $params['num'],
'trade_cycle' => $params['cycle'],
'expect_percent' => $winpl,
'expect_income' => sprintf("%.2f",$params['num']*$winpl),
'lose_percent' => $lostpl,
'lose_income' => sprintf('%.2f',$params['num']*$lostpl),
'current_price' => $current_price['price'],
'rise_or_fall' => $params['position'],
'open_time' => $times+$params['cycle']
];
$lostnum = $currency['num'] - $params['num'];
//收支记录
$detailed = [
'user_id' => $this->auth->id,
'curr_id' => 1,
'price' => $params['num'],
'cart' => '2',
'type' => '3',
'serial_no' => $order['serial_no'],
'order_result' => 'process',
'description' => '周期持仓',
'createtime' => $times,
'notice' => '周期持仓',
'before_num' => $currency['num'],
'after_num' => $lostnum,
];
// $return = $order;
// $return['current_price'] = sprintf("%.2f",$return['current_price']);
// $return['trade_num'] = sprintf("%.2f",$return['trade_num']);
// $return['id'] = '';
// $return['nowtime'] = time();
// $return['show_name'] = strtoupper(str_replace('usdt','',$return['symbol'])).'/USDT';
// $this->success('提交成功',$return);
//
// Db::startTrans();
// try {
$ret = Db::name('app_currency_user')
->where('id',$currency['id'])->update(['num'=>$lostnum,'updatetime'=>time()]);
if(!$ret)
{
lopRedis($symbol);
// Db::rollback();
$this->error(__('系统繁忙'));
}
$ret1 = Db::name('order')->insertGetId($order);
if(!$ret1)
{
lopRedis($symbol);
// Db::rollback();
$this->error(__('系统繁忙'));
}
// $ret2 = Db::name('app_detailed')->insert($detailed);
// if(!$ret2)
// {
// lopRedis($symbol);
//// Db::rollback();
// $this->error('系统繁忙');
// }
lopRedis($symbol);
// Db::commit();
$return = $order;
$return['current_price'] = sprintf("%.2f",$return['current_price']);
$return['trade_num'] = sprintf("%.2f",$return['trade_num']);
$return['id'] = $ret1;
$return['nowtime'] = time();
$return['show_name'] = strtoupper(str_replace('usdt','',$return['symbol'])).'/USDT';
$this->success(__('提交成功'),$return);
// } catch (Exception $e) {
// Db::rollback();
// $this->error('系统繁忙');
// }
}
//5秒取消
public function cancel_order()
{
$params = $this->request->post();
if(!isset($params['id']) || empty($params['id']))
{
$this->error(__('请选择要取消的订单'));
}
$order = Db::name('order')
->where('user_id',$this->auth->id)
->where('serial_no',$params['id'])->find();
if(!$order){
$this->error(__('订单不存在'));
}
//订单取消5秒内
$difftime = time() - $order['createtime'];
if($difftime>6){
$this->error(__('订单超时,不能取消'));
}
$symbol = "order_cancel" . $this->auth->id;
$submited = pushRedis($symbol);
if (!$submited) {
$this->error(__("操作频繁"));
}
$currency = Db::name('app_currency_user')
->field('num,id')
->where('user_id',$this->auth->id)
->where('curr_id',1)->find();
$kcnum = sprintf("%.4f",$order['trade_num']*Config::get('site.cycle_cancle'));
$lostnum = $currency['num'] + $order['trade_num'] - $kcnum;
$detailed = [
'user_id' => $this->auth->id,
'curr_id' => 1,
'price' => $kcnum,
'cart' => '2',
'type' => '3',
'serial_no' => $order['serial_no'],
'order_result' => 'result',
'description' => '周期持仓取消',
'createtime' => time(),
'notice' => '周期持仓取消',
'before_num' => $currency['num']+$order['trade_num'],
'after_num' => $lostnum,
];
Db::startTrans();
try {
$ret = Db::name('app_currency_user')
->where('id',$currency['id'])->update(['num'=>$lostnum,'updatetime'=>time()]);
if(!$ret)
{
lopRedis($symbol);
Db::rollback();
$this->error(__('系统繁忙'));
}
$ret1 = Db::name('order')->where('id',$order['id'])->delete();
if(!$ret1)
{
lopRedis($symbol);
Db::rollback();
$this->error(__('系统繁忙'));
}
$ret2 = Db::name('app_detailed')->insert($detailed);
if(!$ret2)
{
lopRedis($symbol);
Db::rollback();
$this->error(__('系统繁忙'));
}
lopRedis($symbol);
Db::commit();
$this->success(__('提交成功'));
} catch (Exception $e) {
Db::rollback();
$this->error(__('系统繁忙'));
}
}
}