ApiController.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /* API平台接口数据 */
  3. namespace Home\Controller;
  4. class ApiController extends HomeController
  5. {
  6. protected function _initialize()
  7. {
  8. parent::_initialize();
  9. $allow_action = array("ticker","ticker2","depth","trades");
  10. if (!in_array(ACTION_NAME,$allow_action)) {
  11. $this->error("非法操作!");
  12. }
  13. }
  14. // 市场行情
  15. public function ticker($market = NULL, $ajax = 'json')
  16. {
  17. // 过滤非法字符----------------S
  18. if (checkstr($market)) {
  19. $this->error('您输入的信息有误!');
  20. }
  21. // 过滤非法字符----------------E
  22. $data = (APP_DEBUG ? null : S('getJsonTop' . $market));
  23. if (!$data) {
  24. if ($market) {
  25. $xnb = explode('_', $market)[0];
  26. $rmb = explode('_', $market)[1];
  27. // foreach (C('market') as $k => $v) {
  28. // $v['xnb'] = explode('_', $v['name'])[0];
  29. // $v['rmb'] = explode('_', $v['name'])[1];
  30. // $data['list'][$k]['name'] = $v['name'];
  31. // $data['list'][$k]['img'] = $v['xnbimg'];
  32. // $data['list'][$k]['title'] = $v['title'];
  33. // $data['list'][$k]['new_price'] = $v['new_price'];
  34. // }
  35. // 24小时 交易量
  36. $volume_24h = round(M('TradeLog')->where(array(
  37. 'market' => $market,
  38. 'addtime' => array('gt', time() - (60 * 60 * 24))
  39. ))->sum('num'), 4);
  40. $data['date'] = time();
  41. //最新成交价
  42. $data['ticker']['last'] = C('market')[$market]['new_price'];
  43. //最高价
  44. $data['ticker']['high'] = C('market')[$market]['max_price'];
  45. //最低价
  46. $data['ticker']['low'] = C('market')[$market]['min_price'];
  47. //买一价
  48. $data['ticker']['buy'] = C('market')[$market]['buy_price'];
  49. //卖一价
  50. $data['ticker']['sell'] = C('market')[$market]['sell_price'];
  51. //成交量
  52. $data['ticker']['vol'] = isset($volume_24h) ? $volume_24h : 0;//C('market')[$market]['volume'];
  53. //涨跌幅
  54. $data['ticker']['change'] = C('market')[$market]['change'];
  55. S('getJsonTop' . $market, $data);
  56. }
  57. }
  58. if ($ajax) {
  59. exit(json_encode($data));
  60. } else {
  61. return $data;
  62. }
  63. }
  64. public function ticker2($market = NULL, $ajax = 'json')
  65. {
  66. /* // 过滤非法字符----------------S
  67. if (checkstr($market)) {
  68. $this->error('您输入的信息有误!');
  69. }
  70. // 过滤非法字符----------------E
  71. $data = (APP_DEBUG ? null : S('getJsonTop' . $market));
  72. if (!$data) {
  73. if ($market) {
  74. $xnb = explode('_', $market)[0];
  75. $rmb = explode('_', $market)[1];
  76. $data['LastDealPrize-'.strtoupper($xnb)]=C('market')[$market]['new_price'];
  77. S('getJsonTop' . $market, $data);
  78. }
  79. }
  80. if ($ajax) {
  81. exit(json_encode($data));
  82. } else {
  83. return $data;
  84. }*/
  85. }
  86. public function depth($market = NULL, $trade_moshi = 1, $ajax = 'json')
  87. {
  88. /* //dump($market);
  89. // 过滤非法字符----------------S
  90. if (checkstr($market) || checkstr($trade_moshi)) {
  91. $this->error('您输入的信息有误!');
  92. }
  93. // 过滤非法字符----------------E
  94. if (!C('market')[$market]) {
  95. return null;
  96. }
  97. $data_getDepth = (APP_DEBUG ? null : S('getDepth'));
  98. if (!$data_getDepth[$market][$trade_moshi]) {
  99. if ($trade_moshi == 1) {
  100. $limt = 12;
  101. }
  102. if (($trade_moshi == 3) || ($trade_moshi == 4)) {
  103. $limt = 25;
  104. }
  105. $mo = M();
  106. if ($trade_moshi == 1) {
  107. $buy = $mo->query('select id,price,sum(num-deal)as nums from tw_trade where status=0 and type=1 and market =\'' . $market . '\' group by price order by price desc limit ' . $limt . ';');
  108. $sell = array_reverse($mo->query('select id,price,sum(num-deal)as nums from tw_trade where status=0 and type=2 and market =\'' . $market . '\' group by price order by price asc limit ' . $limt . ';'));
  109. }
  110. if ($trade_moshi == 3) {
  111. $buy = $mo->query('select id,price,sum(num-deal)as nums from tw_trade where status=0 and type=1 and market =\'' . $market . '\' group by price order by price desc limit ' . $limt . ';');
  112. $sell = null;
  113. }
  114. if ($trade_moshi == 4) {
  115. $buy = null;
  116. $sell = array_reverse($mo->query('select id,price,sum(num-deal)as nums from tw_trade where status=0 and type=2 and market =\'' . $market . '\' group by price order by price asc limit ' . $limt . ';'));
  117. }
  118. if ($sell) {
  119. foreach ($sell as $k => $v) {
  120. $data['asks'][$k] = array(round(floatval($v['nums'] * 1),3),round(floatval($v['price'] * 1),3));
  121. }
  122. } else {
  123. $data['asks'] = '';
  124. }
  125. if ($buy) {
  126. foreach ($buy as $k => $v) {
  127. $data['bids'][$k] = array(round(floatval($v['nums'] * 1),3),round(floatval($v['price'] * 1),3));
  128. }
  129. } else {
  130. $data['bids'] = '';
  131. }
  132. $data_getDepth[$market][$trade_moshi] = $data;
  133. S('getDepth', $data_getDepth);
  134. } else {
  135. $data = $data_getDepth[$market][$trade_moshi];
  136. }
  137. if ($ajax) {
  138. exit(json_encode($data));
  139. } else {
  140. return $data;
  141. }*/
  142. }
  143. public function trades()
  144. {
  145. /* $input = I('get.');
  146. if (!$input['since']) {
  147. $tradeLog = M('TradeLog')->where(array('market' => $input['market']))->order('id desc')->find();
  148. // foreach ($tradeLog as $k => $v) {
  149. // $json_data[] = array('tid' => $v['id'], 'date' => $v['addtime'], 'price' => $v['price'], 'amount' => $v['num'], 'trade_type' => $v['type'] == 1 ? 'bid' : 'ask');
  150. // }
  151. $json_data[] = array('date' => $tradeLog['addtime'], 'date_ms' => $tradeLog['addtime']*1000, 'price' => $tradeLog['price'], 'amount' => $tradeLog['num'], 'tid' => $tradeLog['id'], 'trade_type' => $tradeLog['type'] == 1 ? 'buy' : 'sell');
  152. header("Content-type:application/json");
  153. header('X-Frame-Options: SAMEORIGIN');
  154. exit(json_encode($json_data));
  155. } else {
  156. $tradeLog = M('TradeLog')->where(array(
  157. 'market' => $input['market'],
  158. 'id' => array('gt', $input['since'])
  159. ))->order('id desc')->select();
  160. foreach ($tradeLog as $k => $v) {
  161. $json_data[] = array('date' => $v['addtime'], 'date_ms' => $v['addtime']*1000, 'price' => $v['price'], 'amount' => $v['num'], 'tid' => $v['id'], 'trade_type' => $v['type'] == 1 ? 'buy' : 'sell');
  162. }
  163. if (!empty($json_data)) {
  164. header("Content-type:application/json");
  165. header('X-Frame-Options: SAMEORIGIN');
  166. exit(json_encode($json_data));
  167. }
  168. }*/
  169. }
  170. }
  171. ?>