TickerService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\ActionLogModel;
  13. use App\Models\CurrencyModel;
  14. use App\Models\TickerModel;
  15. use App\Services\BaseService;
  16. use App\Services\OkTradeService;
  17. use App\Services\RedisService;
  18. /**
  19. * 币种价格行情管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * @package App\Services\Common
  23. */
  24. class TickerService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new TickerModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 更新同步最新价格
  50. * @param $ccy
  51. * @return false
  52. */
  53. public function updatePrice($ccy)
  54. {
  55. $cacheKey = "caches:tickers:locks:{$ccy}";
  56. if(RedisService::get($cacheKey)){
  57. $this->error = "价格更新失败,频繁请求";
  58. return false;
  59. }
  60. RedisService::set($cacheKey, ['ccy'=> $ccy ,'date'=>date('Y-m-d H:i:s')], rand(1,2));
  61. $result = OkTradeService::makeMarketApi()->getTicker("{$ccy}-USDT");
  62. $result = $result? json_decode($result, true) : [];
  63. $data = isset($result['data'])? $result['data'] : [];
  64. $ticker = isset($data[0])? $data[0] : [];
  65. $msg = $result['msg']? $result['msg'] : '';
  66. if($ticker){
  67. $this->errorData = $ticker;
  68. $ts = isset($ticker['ts'])? $ticker['ts'] : 0;
  69. $cacheKey = "caches:tickers:info_{$ccy}_{$ts}";
  70. $cacheTicker = RedisService::get($cacheKey);
  71. if(empty($cacheTicker)){
  72. $cacheTicker = $this->model->where(['ccy'=> $ccy,'ts'=> $ts])->first();
  73. $cacheTicker = $cacheTicker? $cacheTicker->toArray() : [];
  74. if($cacheTicker){
  75. RedisService::set($cacheKey, $cacheTicker, rand(30, 60));
  76. }
  77. }
  78. if($cacheTicker){
  79. $this->error = "价格更新失败,已更新过,{$ts}-".date('Y-m-d H:i:s');
  80. return false;
  81. }
  82. $price = isset($ticker['last'])? $ticker['last'] : 0;
  83. if($price<=0){
  84. $this->error = "价格更新失败,价格为0";
  85. return false;
  86. }
  87. $data = [
  88. 'instId'=> "{$ccy}-USDT",
  89. 'ccy'=> $ccy,
  90. "instType"=>'SPOT',
  91. "price"=> $price,
  92. "lastSz"=> isset($ticker['lastSz'])? $ticker['lastSz'] : 0,
  93. "askPx"=> isset($ticker['askPx'])? $ticker['askPx'] : 0,
  94. "askSz"=> isset($ticker['askSz'])? $ticker['askSz'] : 0,
  95. "bidPx"=> isset($ticker['bidPx'])? $ticker['bidPx'] : 0,
  96. "bidSz"=> isset($ticker['bidSz'])? $ticker['bidSz'] : 0,
  97. "open24h"=> isset($ticker['open24h'])? $ticker['open24h'] : 0,
  98. "high24h"=> isset($ticker['high24h'])? $ticker['high24h'] : 0,
  99. "low24h"=> isset($ticker['low24h'])? $ticker['low24h'] : 0,
  100. "volCcy24h"=> isset($ticker['volCcy24h'])? $ticker['volCcy24h'] : '',
  101. "sodUtc0"=> isset($ticker['sodUtc0'])? $ticker['sodUtc0'] : '',
  102. "sodUtc8"=> isset($ticker['sodUtc8'])? $ticker['sodUtc8'] : '',
  103. "ts"=> isset($ticker['ts'])? $ticker['ts'] : time(),
  104. "create_time"=> $ts? intval($ts/1000) : time(),
  105. "update_time"=> time(),
  106. "update_at"=> date('Y-m-d H:i:s'),
  107. "status"=> 1,
  108. "mark"=> 1,
  109. ];
  110. if(!$this->model->insert($data)){
  111. $this->error = "价格更新失败,存表失败";
  112. return false;
  113. }
  114. $data = ['ccy'=>$ccy,'price'=> $price, 'update_at'=>date('Y-m-d H:i:s'), 'ts'=> $ts];
  115. RedisService::set("caches:tickers:pricesLast:{$ccy}-USDT", $data, 300);
  116. return $data;
  117. }else{
  118. $this->error = $msg? $msg : "价格更新失败";
  119. return false;
  120. }
  121. }
  122. /**
  123. * 获取币种最新价格
  124. * @param $ccy 币种
  125. * @return false|int|mixed
  126. */
  127. public function getLastPrice($ccy)
  128. {
  129. $cacheKey = "caches:tickers:pricesLast:{$ccy}-USDT";
  130. $data = RedisService::get($cacheKey);
  131. $price = isset($data['price'])? $data['price'] : 0;
  132. if($price<=0){
  133. $data = $this->model->where(['ccy'=> $ccy, 'status'=>1,'mark'=>1])
  134. ->select(['id','ccy','price','update_at','ts'])
  135. ->orderBy('ts','desc')
  136. ->first();
  137. $data = $data? $data->toArray() : [];
  138. $price = isset($data['price'])? $data['price'] : 0;
  139. if($price<=0){
  140. $this->error = "获取最新价格失败";
  141. return false;
  142. }
  143. RedisService::set($cacheKey, $data, 300);
  144. }
  145. return $price;
  146. }
  147. /**
  148. * 定期清理价格记录
  149. * @param $ccy 币种
  150. * @return false
  151. */
  152. public function clearPriceLog($ccy)
  153. {
  154. if($this->model->where(['ccy'=> $ccy])->where('create_time','>', time() - 30)->value('id')){
  155. $priceExpired = \App\Services\ConfigService::make()->getConfigByCode('ticker_price_expired', 24);
  156. $priceExpired = $priceExpired>0 && $priceExpired<=4320? $priceExpired : 24;
  157. $this->model->where(['ccy'=> $ccy])->where('create_time','<=', time() - $priceExpired * 3600)->delete();
  158. $this->error = '价格数据太少无需清理';
  159. return ['ccy'=>$ccy,'expired'=>$priceExpired];
  160. }else{
  161. $this->error = '价格数据太少无需清理';
  162. return false;
  163. }
  164. }
  165. /**
  166. * 添加会编辑
  167. * @return array
  168. * @since 2020/11/11
  169. * @author laravel开发员
  170. */
  171. public function edit()
  172. {
  173. // 请求参数
  174. $data = request()->all();
  175. ActionLogModel::setTitle("修改币种信息");
  176. ActionLogModel::record();
  177. return parent::edit($data); // TODO: Change the autogenerated stub
  178. }
  179. }