<?php
/* */

namespace app\chainex\logic;

use fast\Random;
use think\Cache;
use think\Config;
use think\Db;
use think\Exception;

/**
* Token接口
*/
class Tokens
{

private static $server = array();

//查询所有启用的交易对
public function getTokenQueue($field = 'switch') {
return Db::name('token_queue')->where($field,'=','1')->select();
}

//写入行情
public function addMarket($token_id,$symbol,$ts,$data) {
if ($data['open'] <= 0) {
return false;
}
try {

// $data = $this->getZBMarketPrice($data,$symbol);

//获取全局浮动
$fd_price = BaseConfig::getInstance()->getBaseConfig('kline_add_point');

// $this->setMarketToRedis($symbol,[
// 'amount' => $data['amount'],
// 'open' => $data['open'] + $fd_price,
// 'close' => $data['close'] + $fd_price,
// 'high' => $data['high'] + $fd_price,
// 'count' => $data['count'],
// 'low' => $data['low'] + $fd_price,
// 'vol' => $data['vol'],
// ]);

// $key = json_encode(['market_id' => $data['id'],'symbol' => $symbol]);
// $key = sprintf('new1_token_%s',md5($key));

$key = sprintf('websocket_token_market_%s_%s',md5($symbol),$ts);

$redis = Cache::init()->handler();
if ($redis->exists($key)) {
//更新
// Db::name('token_market')->where(['symbol' => $symbol,'market_id' => $data['id']])->update([
// 'amount' => $data['amount'],
// 'open' => $data['open'] + $fd_price,
// 'close' => $data['close'] + $fd_price,
// 'high' => $data['high'] + $fd_price,
// 'count' => $data['count'],
// 'low' => $data['low'] + $fd_price,
// 'vol' => $data['vol'],
// 'update_time' => time()
// ]);

return false;
}

//作弊
$data = Zuobi::getInstance()->check($symbol,$fd_price,$data);

Db::name('token_market')->insert([
'amount' => $data['amount'],
'open' => $data['open'],
'close' => $data['close'],
'high' => $data['high'],
'count' => $data['count'],
'low' => $data['low'],
'vol' => $data['vol'],
'token_id' => $token_id,
'create_time' => $ts,
'symbol' => $symbol,
'market_id' => $data['id'],
'update_time' => time()
]);
//更新价格
Db::name('tokens')->where('token_id','=',$token_id)->update(['price' => $data['close']]);

$redis->set($key,1);
$redis->expire($key,86400);

return true;
} catch (Exception $e) {
$redis->set($key,1);
$redis->expire($key,86400);
dump($e->getMessage());
}
}

//获取作弊k线价格
public function getZBMarketPrice($data,$symbol) {
$config = Db::name('token_no_market_time')->where('symbol','=',$symbol)->find();
if (!$config) {
return $data;
}
if ($config['start_time'] > time() || $config['end_time'] < time()) {
return $data;
}
//作弊
$type = $config['type'];
$num = $config['num'];
if ($type == 1) {
//模式一
$data['open'] = $data['open'] + $num;
$data['close'] = $data['close'] + $num;
$data['high'] = $data['high'] + $num;
$data['low'] = $data['low'] + $num;
} else {
//模式二
//查询上一个价格数据
$last_data = Db::name('token_market')
->where('symbol','=',$symbol)
->order('create_time dsc')
->limit(0,1)
->find();
if ($last_data) {
$data['open'] = $last_data['open'] + $num;
$data['close'] = $last_data['close'] + $num;
$data['high'] = $last_data['high'] + $num;
$data['low'] = $last_data['low'] + $num;
}
}
return $data;
}

//根据名称查询tokenid
public function getTokenIdByName($name) {
return Db::name('tokens')->where('name','=',$name)->value('token_id');
}

//写入k线队列
public function addKline($symbol,$period,$data) {

$check = Zuobi::getInstance()->checkKline($symbol,$data);
$is = $check['is'];
$data = $check['data'];

try {
$key = json_encode(['period_time' => $data['id'],'period' => $period,'symbol' => $symbol]);
$key = sprintf('new1_token_%s',md5($key));

$redis = Cache::init()->handler();
if ($redis->exists($key)) {
// dump('更新k线');
//
// //更新
Db::name('token_kline')->where(['symbol' => $symbol,'period' => $period,'period_time' => $data['id']])->where('is','=',0)->update([
'open' => $data['open'],
'close' => $data['close'],
// 'low' => $data['low'],
// 'high' => $data['high'],
'amount' => $data['amount'],
'vol' => $data['vol'],
'count' => $data['count'],
'symbol' => $symbol,
'period' => $period,
'period_time' => $data['id'],
'create_time' => time(),
]);

return false;
}

// $data = $this->getZBKlinePrice($data,$symbol,$period);

Db::name('token_kline')->insert([
'open' => $data['open'],
'close' => $data['close'],
'low' => $data['low'],
'high' => $data['high'],
'amount' => $data['amount'],
'vol' => $data['vol'],
'count' => $data['count'],
'symbol' => $symbol,
'period' => $period,
'period_time' => $data['id'],
'create_time' => time(),
'is' => $is
]);

$redis->set($key,1);
$redis->expire($key,86400);
} catch (Exception $e) {
$redis->set($key,1);
$redis->expire($key,86400);
dump($e->getMessage());
}
}

//获取作弊k线价格
public function getZBKlinePrice($data,$symbol,$period) {

$kline_add_point = BaseConfig::getInstance()->getBaseConfig('kline_add_point');
$data['open'] = $data['open'] + $kline_add_point;
$data['close'] = $data['close'] + $kline_add_point;
$data['high'] = $data['high'] + $kline_add_point;
$data['low'] = $data['low'] + $kline_add_point;

if (!in_array($period,['1min','5min'])) {
return $data;
}
$config = Db::name('token_no_market_time')->where('symbol','=',$symbol)->find();
if (!$config) {
return $data;
}
if ($config['start_time'] > time() || $config['end_time'] < time()) {
return $data;
}
//作弊
$type = $config['type'];
$num = $config['num'];
if ($type == 1) {
//模式一
$data['open'] = $data['open'] + $num;
$data['close'] = $data['close'] + $num;
$data['high'] = $data['high'] + $num;
$data['low'] = $data['low'] + $num;
} else {
//模式二
//查询上一个价格数据
$last_data = Db::name('token_kline')
->where('symbol','=',$symbol)
->where('period','=',$period)
->order('period_time dsc')
->limit(0,1)
->find();
if ($last_data) {
$data['open'] = $last_data['open'] + $num;
$data['close'] = $last_data['close'] + $num;
$data['high'] = $last_data['high'] + $num;
$data['low'] = $last_data['low'] + $num;
}
}
return $data;
}

//作弊,根据行情数据,生成新的k线价格
public function getNewKlinePrice($price,$symbol,$period) {
$periods = [
'1min' => 60,
'5min' => 300,
'15min' => 900,
'30min' => 1800,
'60min' => 3600,
'4hour' => 4 * 60 * 60,
'1day' => 86400,
'1mon' => 30 * 86400
];
$time = $periods[$period];
//查询数据
$open = Db::name('token_market')->where('symbol','=',$symbol)->where('create_time','>=',time() - $time)->order('id asc')->limit(0,1)->value('open');
$close = Db::name('token_market')->where('symbol','=',$symbol)->where('create_time','<=',time())->order('id desc')->limit(0,1)->value('close');
$high = Db::name('token_market')->where('symbol','=',$symbol)->where('create_time','>=',time() - $time)->where('create_time','<=',time())->max('high');
$low = Db::name('token_market')->where('symbol','=',$symbol)->where('create_time','>=',time() - $time)->where('create_time','<=',time())->max('low');

$price_add = BaseConfig::getInstance()->getBaseConfig('kline_add_point');
return [
'open' => $open == 0 ? $price['open'] : $open + $price_add,
'close' => $close == 0 ? $price['close'] : $close + $price_add,
'high' => $high == 0 ? $price['high'] : $high + $price_add,
'low' => $low == 0 ? $price['low'] : $low + $price_add
];
}

//查询行情
public function getMarket($token_id) {
$query = Db::name('token_queue');
if ($token_id > 0) {
$query->where('token_id','=',$token_id);
}
$data = Db::name('token_queue')->field('symbol')->select();
foreach ($data as $k => &$v) {
$v['price'] = (new Tran())->getPriceBySymbol($v['symbol']);
//计算涨跌幅
$v['scope'] = $this->getScope($v['symbol'],$v['price']);
$v['price'] = sprintf('%.2f',$v['price']);
$v['cny_price'] = sprintf('%.2f',$v['price'] * BaseConfig::getInstance()->getBaseConfig('cn_price'));
$tran_amount = $this->getTranAmount($v['symbol']);
$v['high'] = $tran_amount['high'];
$v['low'] = $tran_amount['low'];
$v['amount'] = $tran_amount['amount'];
}
return $data;
}

//计算涨跌幅
public function getScope($symbol,$price) {
$start_time = time() - 86400;
$data = Db::name('token_market')
->where('symbol','=',$symbol)
->where('create_time','>=',$start_time)
->order('id asc')
->limit(0,1)
->find();
if (!$data) {
return '0%';
}
$close = $data['close'];

Db::name('token_market')
->where('symbol','=',$symbol)
->where('create_time','<=',$start_time)
->delete();

return sprintf("%.4f",($price - $close) / $close) * 100 . '%';
}

//查询主流币信息
public function getMainCoinInfo() {
$data = Db::name('token_queue')->field('symbol')->where('symbol','in',['BTC/USDT','ETH/USDT','LTC/USDT'])->select();
foreach ($data as $k => &$v) {
$v['price'] = Db::name('token_market')->where('symbol','=',$v['symbol'])->order('id desc')
->limit(0,1)
->value('close');
//计算涨跌幅
$v['scope'] = $this->getScope($v['symbol'],$v['price']);
$v['price'] = sprintf('%.2f',$v['price']);
$v['cny_price'] = sprintf('%.2f',$v['price'] * BaseConfig::getInstance()->getBaseConfig('cn_price'));
}
return $data;
}

//查询k线图
public function getKline($symbol,$period,$from,$to) {
if ($period == '1s') {
return $this->getFromMarket($symbol,$from,$to);
}
return Db::name('token_kline')->where(['symbol' => $symbol,'period' => $period])
->where('period_time','>',$from)
->where('period_time','<=',$to)
->order('period_time desc')
->select();
}

public function getKlineNew($symbol,$period) {
return Db::name('token_kline')->where(['symbol' => $symbol,'period' => $period])
->order('period_time desc')
->limit(0,1)
->find();
}

public function getFromMarket($symbol,$from,$to) {
$data = Db::name('token_market')->where('symbol','=',$symbol)
->where('create_time','>',$from)
->where('create_time','<=',$to)
->field('amount,open,close,high,low,vol,count,symbol,create_time period_time,create_time')
->select();
for ($i = 0;$i < count($data); $i++) {
if ($i == 0) {
continue;
}
$data[$i]['open'] = $data[$i - 1]['close'];
}
unset($data[0]);
return array_values($data);
}

//写入最新交易
public function addNewTran($data) {
if (Db::name('token_tran')->where($data)->count() > 0) {
return false;
}
$data['create_time'] = time();
return Db::name('token_tran')->insert($data);
}

//写入深度图
public function addDepth($symbol,$type,$data) {

$key = sprintf('depth_token_%s_%s',$symbol,$type);

$redis = Cache::init()->handler();

unset($data['type']);
unset($data['symbol']);

$redis->set($key,json_encode($data));
}

//获取深度数据
public function getDepth($symbol,$type) {
try {
$key = sprintf('depth_token_%s_%s',$symbol,$type);

$redis = Cache::init()->handler();

$data = $redis->get($key);
$data = json_decode($data,true);
foreach ($data as $k => &$v) {
unset($v['type']);
unset($v['symbol']);
$v['count'] = (string)$v['count'];
$v['price'] = (string)$v['price'];
}
return array_values($data);
} catch (Exception $e) {
dump($e->getMessage());
return false;
}
}

//获取币种最新价格
public function getNewPrice($symbol) {
$data = Db::name('token_market')->where('symbol','=',$symbol)->order('create_time desc')
->limit(0,1)->find();
$new_data = [];
$new_data['price'] = sprintf('%.2f',$data['close']);
$new_data['cny_price'] = sprintf('%.2f',$data['close'] * BaseConfig::getInstance()->getBaseConfig('cn_price'));
$new_data['scope'] = $this->getScope($data['symbol'],$data['close']);
return $new_data;
}

//获取所有支持的主币
public function getMainToken() {
return Db::name('tokens')->field('token_id,name')->where('is_main_token','=','1')->select();
}

//查询作弊时间间隔
public function getNoMarketTime($symbol) {
return Db::name('token_no_market_time')->where('id','=',1)->where('symbol','=',$symbol)->find();
}

//查询交易量
public function getTranAmount($symbol) {
$data['price'] = Db::name('token_market')->where('symbol','=',$symbol)->order('id desc')
->limit(0,1)
->value('close');
//计算涨跌幅
$data['scope'] = $this->getScope($symbol,$data['price']);
$data['price'] = sprintf('%.2f',$data['price']);
$data['cny_price'] = sprintf('%.2f',$data['price'] * BaseConfig::getInstance()->getBaseConfig('cn_price'));
$data['high'] = Db::name('token_market')->where('symbol','=',$symbol)->order('create_time desc')->limit(0,1)->value('high');
$data['low'] = Db::name('token_market')->where('symbol','=',$symbol)->order('create_time desc')->limit(0,1)->value('low');
$data['amount'] = Db::name('token_market')->where('symbol','=',$symbol)->order('create_time desc')->limit(0,1)->value('amount');
return $data;
}

 

 

 

 

 

 

 

 

 

 

 

 

//market to redis
public function setMarketToRedis($symbol,$data) {
$key = sprintf('market_%s',$symbol);
$redis = Cache::init()->handler();
$redis->set($key,json_encode($data));
}

//get from redis
public function getMarketToRedis($symbol) {
$key = sprintf('market_%s',$symbol);
$redis = Cache::init()->handler();
$data = $redis->get($key);
$data = json_decode($data,true);
return $data;
}

 

 

 

 

 

 

 

 

 

/**
* 单例入口
* @return mixed
*/
public static function getInstance() {
$className = get_called_class();
if(!isset(self::$server[$className])){
self::$server[$className] = new $className();
}
return self::$server[$className];
}
}

本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或联络我们。

对于会员专享、整站源码、程序插件、网站模板、网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理

源码素材属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源 1.本站提供的一切源码、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途! 2.本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容! 3.本站提供的源码,模板,插件等其他资源都不包含技术服务,敬请谅解! 4.本站资源售价只是赞助,收取费用仅维持本站日常运营所需! 5.如无备注,本站不保证所提供下载的资源准确性安全性和完整性,源码仅供下载学习之用! 6.如用于商业或者非法用途,与本站无关。一切后果请自行负责! 7.如果遇到加密压缩包,默认解压密码为:bori99.com如遇到无法解压请联系站长!