OkTradeService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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;
  12. use App\Services\Common\ApiKeyService;
  13. use okv5\AccountApi;
  14. use okv5\FundingApi;
  15. use okv5\MarketDataAPI;
  16. use okv5\PublicDataAPI;
  17. use okv5\StatusApi;
  18. use okv5\TradeAPI;
  19. /**
  20. * OK平台交易管理-服务类
  21. * @package App\Services
  22. */
  23. class OkTradeService extends BaseService
  24. {
  25. protected $apiUrl = '';
  26. protected $apiKey = '';
  27. protected $userId = 0;
  28. protected $apiSecret = '';
  29. protected $passphrase = '';
  30. protected $debug = false;
  31. protected static $api = null; // 账户接口
  32. protected static $tradeApi = null; // 交易接口
  33. protected static $marketApi = null; // 行情数据接口
  34. protected static $fundingApi = null; // 币种和资金账户接口
  35. protected static $publicApi = null; // 公共接口
  36. protected static $statusApi = null; // 公告接口
  37. protected $config = [];
  38. protected $apiUrls = [
  39. // 币币交易
  40. 'trade' => '/api/v5/trade/order',
  41. // 批量下单
  42. 'batchTrade' => '/api/v5/trade/batch-orders',
  43. // 撤销订单
  44. 'cancelTrade' => '/api/v5/trade/cancel-order',
  45. // 批量取消订单
  46. 'cancelBatchTrade' => '/api/v5/trade/cancel-batch-orders',
  47. // 修改订单
  48. 'amendTrade' => '/api/v5/trade/amend-order',
  49. // 批量修改订单
  50. 'amendBatchTrade' => '/api/v5/trade/amend-batch-orders',
  51. // 获取订单信息
  52. 'orderDetail' => '/api/v5/trade/order?instId=%s&ordId=%s&clOrdId=%s',
  53. // 获取币币交易行情数据
  54. 'tickers'=> '/api/v5/market/tickers?instType=SPOT',
  55. // 指数价格
  56. 'indexTickers'=> '/api/v5/market/index-tickers?instId=%s&quoteCcy=USDT',
  57. // K线数据
  58. 'indexCandles'=> '/api/v5/market/index-candles?instId=%s&after=%s&before=%s%bar=%s%limit=%s',
  59. // 获取账户余额,ccy-币种
  60. 'balance'=>'/api/v5/account/balance?ccy=%s',
  61. // 币种列表
  62. 'currencies'=>'/api/v5/asset/currencies',
  63. ];
  64. // 静态对象
  65. protected static $instance = null;
  66. /**
  67. * 构造函数
  68. */
  69. public function __construct($userId=0, $type=0)
  70. {
  71. $this->config = ConfigService::make()->getConfigOptionByGroup(10);
  72. $this->apiUrl = isset($this->config['api_url'])? $this->config['api_url'] : '';
  73. if (empty($this->config)) {
  74. $this->error = '接口地址参数错误';
  75. return false;
  76. }
  77. if($type){
  78. $this->userId = $userId;
  79. $config = ApiKeyService::make()->getConfig($userId);
  80. $this->apiKey = isset($config['api_key'])? $config['api_key'] : '';
  81. $this->apiSecret = isset($config['api_secret'])? $config['api_secret'] : '';
  82. $this->passphrase = isset($config['passphrase'])? $config['passphrase'] : '';
  83. if(empty($this->apiKey) || empty($this->apiSecret) || empty($this->passphrase)){
  84. $this->error = '接口配置参数错误';
  85. return false;
  86. }
  87. if($type == 1){
  88. self::$api = new AccountApi([
  89. "apiKey"=> $this->apiKey,
  90. "apiSecret"=> $this->apiSecret,
  91. "passphrase"=> $this->passphrase,
  92. "api_url"=> $this->apiUrl,
  93. "debug"=> $this->debug,
  94. "paper"=> 0,
  95. ]);
  96. }else if ($type == 2){
  97. self::$tradeApi = new TradeAPI([
  98. "apiKey"=> $this->apiKey,
  99. "apiSecret"=> $this->apiSecret,
  100. "passphrase"=> $this->passphrase,
  101. "api_url"=> $this->apiUrl,
  102. "debug"=> $this->debug,
  103. "paper"=> 0,
  104. ]);
  105. }else if ($type == 3){
  106. self::$marketApi = new MarketDataAPI([
  107. "apiKey"=> $this->apiKey,
  108. "apiSecret"=> $this->apiSecret,
  109. "passphrase"=> $this->passphrase,
  110. "api_url"=> $this->apiUrl,
  111. "debug"=> $this->debug,
  112. "paper"=> 0,
  113. ]);
  114. }else if ($type == 4){
  115. self::$fundingApi = new FundingApi([
  116. "apiKey"=> $this->apiKey,
  117. "apiSecret"=> $this->apiSecret,
  118. "passphrase"=> $this->passphrase,
  119. "api_url"=> $this->apiUrl,
  120. "debug"=> $this->debug,
  121. "paper"=> 0,
  122. ]);
  123. }else if ($type == 5){
  124. self::$publicApi = new PublicDataAPI([
  125. "apiKey"=> $this->apiKey,
  126. "apiSecret"=> $this->apiSecret,
  127. "passphrase"=> $this->passphrase,
  128. "api_url"=> $this->apiUrl,
  129. "debug"=> $this->debug,
  130. "paper"=> 0,
  131. ]);
  132. }else if ($type == 6){
  133. self::$statusApi = new StatusApi([
  134. "apiKey"=> $this->apiKey,
  135. "apiSecret"=> $this->apiSecret,
  136. "passphrase"=> $this->passphrase,
  137. "api_url"=> $this->apiUrl,
  138. "debug"=> $this->debug,
  139. "paper"=> 0,
  140. ]);
  141. }
  142. }
  143. }
  144. /**
  145. * 静态入口
  146. * @return static|null
  147. */
  148. public static function make($userId=0)
  149. {
  150. if (!self::$instance) {
  151. self::$instance = (new static($userId));
  152. }
  153. return self::$instance;
  154. }
  155. /**
  156. * 接口入口
  157. * @param $userId
  158. * @return AccountApi|null
  159. */
  160. public static function makeApi($userId=0)
  161. {
  162. if (!self::$api) {
  163. self::$instance = (new static($userId, 1));
  164. }
  165. return self::$api;
  166. }
  167. /**
  168. * 交易接口入口
  169. * @param $userId
  170. * @return TradeAPI|null
  171. */
  172. public static function makeTradeApi($userId=0)
  173. {
  174. if (!self::$tradeApi) {
  175. self::$instance = (new static($userId, 2));
  176. }
  177. return self::$tradeApi;
  178. }
  179. /**
  180. * 行情接口入口
  181. * @param $userId
  182. * @return TradeAPI|null
  183. */
  184. public static function makeMarketApi($userId=0)
  185. {
  186. if (!self::$marketApi) {
  187. self::$instance = (new static($userId, 3));
  188. }
  189. return self::$marketApi;
  190. }
  191. /**
  192. * 资金接口入口
  193. * @param $userId
  194. * @return TradeAPI|null
  195. */
  196. public static function makeFundingApi($userId=0)
  197. {
  198. if (!self::$fundingApi) {
  199. self::$instance = (new static($userId, 4));
  200. }
  201. return self::$fundingApi;
  202. }
  203. /**
  204. * 公共接口入口
  205. * @param $userId
  206. * @return TradeAPI|null
  207. */
  208. public static function makePublicApi($userId=0)
  209. {
  210. if (!self::$publicApi) {
  211. self::$instance = (new static($userId, 5));
  212. }
  213. return self::$publicApi;
  214. }
  215. /**
  216. * 公告接口入口
  217. * @param $userId
  218. * @return TradeAPI|null
  219. */
  220. public static function makeStatusApi($userId=0)
  221. {
  222. if (!self::$statusApi) {
  223. self::$instance = (new static($userId, 6));
  224. }
  225. return self::$statusApi;
  226. }
  227. /**
  228. * 接口签名
  229. * @param $str
  230. * @return false|string
  231. */
  232. public function makeSign($str)
  233. {
  234. $hash = hash_hmac('sha256', $str, $this->apiKey, true);
  235. return base64_encode($hash);
  236. }
  237. }