| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\ActionLogModel;
- use App\Models\CurrencyModel;
- use App\Models\TickerModel;
- use App\Services\BaseService;
- use App\Services\OkTradeService;
- use App\Services\RedisService;
- /**
- * 币种价格行情管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class TickerService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new TickerModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 更新同步最新价格
- * @param $ccy
- * @return false
- */
- public function updatePrice($ccy)
- {
- $cacheKey = "caches:tickers:locks:{$ccy}";
- if(RedisService::get($cacheKey)){
- $this->error = "价格更新失败,频繁请求";
- return false;
- }
- RedisService::set($cacheKey, ['ccy'=> $ccy ,'date'=>date('Y-m-d H:i:s')], rand(1,2));
- $result = OkTradeService::makeMarketApi()->getTicker("{$ccy}-USDT");
- $result = $result? json_decode($result, true) : [];
- $data = isset($result['data'])? $result['data'] : [];
- $ticker = isset($data[0])? $data[0] : [];
- $msg = $result['msg']? $result['msg'] : '';
- if($ticker){
- $this->errorData = $ticker;
- $ts = isset($ticker['ts'])? $ticker['ts'] : 0;
- $cacheKey = "caches:tickers:info_{$ccy}_{$ts}";
- $cacheTicker = RedisService::get($cacheKey);
- if(empty($cacheTicker)){
- $cacheTicker = $this->model->where(['ccy'=> $ccy,'ts'=> $ts])->first();
- $cacheTicker = $cacheTicker? $cacheTicker->toArray() : [];
- if($cacheTicker){
- RedisService::set($cacheKey, $cacheTicker, rand(30, 60));
- }
- }
- if($cacheTicker){
- $this->error = "价格更新失败,已更新过,{$ts}-".date('Y-m-d H:i:s');
- return false;
- }
- $price = isset($ticker['last'])? $ticker['last'] : 0;
- if($price<=0){
- $this->error = "价格更新失败,价格为0";
- return false;
- }
- $data = [
- 'instId'=> "{$ccy}-USDT",
- 'ccy'=> $ccy,
- "instType"=>'SPOT',
- "price"=> $price,
- "lastSz"=> isset($ticker['lastSz'])? $ticker['lastSz'] : 0,
- "askPx"=> isset($ticker['askPx'])? $ticker['askPx'] : 0,
- "askSz"=> isset($ticker['askSz'])? $ticker['askSz'] : 0,
- "bidPx"=> isset($ticker['bidPx'])? $ticker['bidPx'] : 0,
- "bidSz"=> isset($ticker['bidSz'])? $ticker['bidSz'] : 0,
- "open24h"=> isset($ticker['open24h'])? $ticker['open24h'] : 0,
- "high24h"=> isset($ticker['high24h'])? $ticker['high24h'] : 0,
- "low24h"=> isset($ticker['low24h'])? $ticker['low24h'] : 0,
- "volCcy24h"=> isset($ticker['volCcy24h'])? $ticker['volCcy24h'] : '',
- "sodUtc0"=> isset($ticker['sodUtc0'])? $ticker['sodUtc0'] : '',
- "sodUtc8"=> isset($ticker['sodUtc8'])? $ticker['sodUtc8'] : '',
- "ts"=> isset($ticker['ts'])? $ticker['ts'] : time(),
- "create_time"=> $ts? intval($ts/1000) : time(),
- "update_time"=> time(),
- "update_at"=> date('Y-m-d H:i:s'),
- "status"=> 1,
- "mark"=> 1,
- ];
- if(!$this->model->insert($data)){
- $this->error = "价格更新失败,存表失败";
- return false;
- }
- $data = ['ccy'=>$ccy,'price'=> $price, 'update_at'=>date('Y-m-d H:i:s'), 'ts'=> $ts];
- RedisService::set("caches:tickers:pricesLast:{$ccy}-USDT", $data, 300);
- return $data;
- }else{
- $this->error = $msg? $msg : "价格更新失败";
- return false;
- }
- }
- /**
- * 获取币种最新价格
- * @param $ccy 币种
- * @return false|int|mixed
- */
- public function getLastPrice($ccy)
- {
- $cacheKey = "caches:tickers:pricesLast:{$ccy}-USDT";
- $data = RedisService::get($cacheKey);
- $price = isset($data['price'])? $data['price'] : 0;
- if($price<=0){
- $data = $this->model->where(['ccy'=> $ccy, 'status'=>1,'mark'=>1])
- ->select(['id','ccy','price','update_at','ts'])
- ->orderBy('ts','desc')
- ->first();
- $data = $data? $data->toArray() : [];
- $price = isset($data['price'])? $data['price'] : 0;
- if($price<=0){
- $this->error = "获取最新价格失败";
- return false;
- }
- RedisService::set($cacheKey, $data, 300);
- }
- return $price;
- }
- /**
- * 定期清理价格记录
- * @param $ccy 币种
- * @return false
- */
- public function clearPriceLog($ccy)
- {
- if($this->model->where(['ccy'=> $ccy])->where('create_time','>', time() - 30)->value('id')){
- $priceExpired = \App\Services\ConfigService::make()->getConfigByCode('ticker_price_expired', 24);
- $priceExpired = $priceExpired>0 && $priceExpired<=4320? $priceExpired : 24;
- $this->model->where(['ccy'=> $ccy])->where('create_time','<=', time() - $priceExpired * 3600)->delete();
- $this->error = '价格数据太少无需清理';
- return ['ccy'=>$ccy,'expired'=>$priceExpired];
- }else{
- $this->error = '价格数据太少无需清理';
- return false;
- }
- }
- /**
- * 添加会编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- // 请求参数
- $data = request()->all();
- ActionLogModel::setTitle("修改币种信息");
- ActionLogModel::record();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- }
|