| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Services\Common\ApiKeyService;
- use okv5\AccountApi;
- use okv5\FundingApi;
- use okv5\MarketDataAPI;
- use okv5\PublicDataAPI;
- use okv5\StatusApi;
- use okv5\TradeAPI;
- /**
- * OK平台交易管理-服务类
- * @package App\Services
- */
- class OkTradeService extends BaseService
- {
- protected $apiUrl = '';
- protected $apiKey = '';
- protected $userId = 0;
- protected $apiSecret = '';
- protected $passphrase = '';
- protected $debug = false;
- protected static $api = null; // 账户接口
- protected static $tradeApi = null; // 交易接口
- protected static $marketApi = null; // 行情数据接口
- protected static $fundingApi = null; // 币种和资金账户接口
- protected static $publicApi = null; // 公共接口
- protected static $statusApi = null; // 公告接口
- protected $config = [];
- protected $apiUrls = [
- // 币币交易
- 'trade' => '/api/v5/trade/order',
- // 批量下单
- 'batchTrade' => '/api/v5/trade/batch-orders',
- // 撤销订单
- 'cancelTrade' => '/api/v5/trade/cancel-order',
- // 批量取消订单
- 'cancelBatchTrade' => '/api/v5/trade/cancel-batch-orders',
- // 修改订单
- 'amendTrade' => '/api/v5/trade/amend-order',
- // 批量修改订单
- 'amendBatchTrade' => '/api/v5/trade/amend-batch-orders',
- // 获取订单信息
- 'orderDetail' => '/api/v5/trade/order?instId=%s&ordId=%s&clOrdId=%s',
- // 获取币币交易行情数据
- 'tickers'=> '/api/v5/market/tickers?instType=SPOT',
- // 指数价格
- 'indexTickers'=> '/api/v5/market/index-tickers?instId=%s"eCcy=USDT',
- // K线数据
- 'indexCandles'=> '/api/v5/market/index-candles?instId=%s&after=%s&before=%s%bar=%s%limit=%s',
- // 获取账户余额,ccy-币种
- 'balance'=>'/api/v5/account/balance?ccy=%s',
- // 币种列表
- 'currencies'=>'/api/v5/asset/currencies',
- ];
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- */
- public function __construct($userId=0, $type=0)
- {
- $this->config = ConfigService::make()->getConfigOptionByGroup(10);
- $this->apiUrl = isset($this->config['api_url'])? $this->config['api_url'] : '';
- if (empty($this->config)) {
- $this->error = '接口地址参数错误';
- return false;
- }
- if($type){
- $this->userId = $userId;
- $config = ApiKeyService::make()->getConfig($userId);
- $this->apiKey = isset($config['api_key'])? $config['api_key'] : '';
- $this->apiSecret = isset($config['api_secret'])? $config['api_secret'] : '';
- $this->passphrase = isset($config['passphrase'])? $config['passphrase'] : '';
- if(empty($this->apiKey) || empty($this->apiSecret) || empty($this->passphrase)){
- $this->error = '接口配置参数错误';
- return false;
- }
- if($type == 1){
- self::$api = new AccountApi([
- "apiKey"=> $this->apiKey,
- "apiSecret"=> $this->apiSecret,
- "passphrase"=> $this->passphrase,
- "api_url"=> $this->apiUrl,
- "debug"=> $this->debug,
- "paper"=> 0,
- ]);
- }else if ($type == 2){
- self::$tradeApi = new TradeAPI([
- "apiKey"=> $this->apiKey,
- "apiSecret"=> $this->apiSecret,
- "passphrase"=> $this->passphrase,
- "api_url"=> $this->apiUrl,
- "debug"=> $this->debug,
- "paper"=> 0,
- ]);
- }else if ($type == 3){
- self::$marketApi = new MarketDataAPI([
- "apiKey"=> $this->apiKey,
- "apiSecret"=> $this->apiSecret,
- "passphrase"=> $this->passphrase,
- "api_url"=> $this->apiUrl,
- "debug"=> $this->debug,
- "paper"=> 0,
- ]);
- }else if ($type == 4){
- self::$fundingApi = new FundingApi([
- "apiKey"=> $this->apiKey,
- "apiSecret"=> $this->apiSecret,
- "passphrase"=> $this->passphrase,
- "api_url"=> $this->apiUrl,
- "debug"=> $this->debug,
- "paper"=> 0,
- ]);
- }else if ($type == 5){
- self::$publicApi = new PublicDataAPI([
- "apiKey"=> $this->apiKey,
- "apiSecret"=> $this->apiSecret,
- "passphrase"=> $this->passphrase,
- "api_url"=> $this->apiUrl,
- "debug"=> $this->debug,
- "paper"=> 0,
- ]);
- }else if ($type == 6){
- self::$statusApi = new StatusApi([
- "apiKey"=> $this->apiKey,
- "apiSecret"=> $this->apiSecret,
- "passphrase"=> $this->passphrase,
- "api_url"=> $this->apiUrl,
- "debug"=> $this->debug,
- "paper"=> 0,
- ]);
- }
- }
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make($userId=0)
- {
- if (!self::$instance) {
- self::$instance = (new static($userId));
- }
- return self::$instance;
- }
- /**
- * 接口入口
- * @param $userId
- * @return AccountApi|null
- */
- public static function makeApi($userId=0)
- {
- if (!self::$api) {
- self::$instance = (new static($userId, 1));
- }
- return self::$api;
- }
- /**
- * 交易接口入口
- * @param $userId
- * @return TradeAPI|null
- */
- public static function makeTradeApi($userId=0)
- {
- if (!self::$tradeApi) {
- self::$instance = (new static($userId, 2));
- }
- return self::$tradeApi;
- }
- /**
- * 行情接口入口
- * @param $userId
- * @return TradeAPI|null
- */
- public static function makeMarketApi($userId=0)
- {
- if (!self::$marketApi) {
- self::$instance = (new static($userId, 3));
- }
- return self::$marketApi;
- }
- /**
- * 资金接口入口
- * @param $userId
- * @return TradeAPI|null
- */
- public static function makeFundingApi($userId=0)
- {
- if (!self::$fundingApi) {
- self::$instance = (new static($userId, 4));
- }
- return self::$fundingApi;
- }
- /**
- * 公共接口入口
- * @param $userId
- * @return TradeAPI|null
- */
- public static function makePublicApi($userId=0)
- {
- if (!self::$publicApi) {
- self::$instance = (new static($userId, 5));
- }
- return self::$publicApi;
- }
- /**
- * 公告接口入口
- * @param $userId
- * @return TradeAPI|null
- */
- public static function makeStatusApi($userId=0)
- {
- if (!self::$statusApi) {
- self::$instance = (new static($userId, 6));
- }
- return self::$statusApi;
- }
- /**
- * 接口签名
- * @param $str
- * @return false|string
- */
- public function makeSign($str)
- {
- $hash = hash_hmac('sha256', $str, $this->apiKey, true);
- return base64_encode($hash);
- }
- }
|