AjaxtradeController.class.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. <?php
  2. namespace Mobile\Controller;
  3. class AjaxtradeController extends MobileController
  4. {
  5. //获取当前最新价格
  6. public function getcoinprice($symbol = null){
  7. if($symbol == '' || $symbol == null){
  8. $this->ajaxReturn(['code'=>0]);
  9. }
  10. if($symbol == "UKB/USDT"){
  11. $symbol = "ukb_usdt";
  12. $mlist = M("market")->where(array('name'=>$symbol))->field("new_price,min_price,max_price,faxingjia,volume")->find();
  13. $num = 0.001 * rand(1,9);
  14. $open = $mlist['min_price'];//开盘价
  15. $close = $mlist['new_price'] + $num;//现价
  16. $lowhig = $close - $open; //涨跌
  17. $change = round(($lowhig / $open * 100),2); //涨跌幅
  18. if($change >= 0){
  19. $change = '<span class="green" style="font-size:18px;font-weight: 500;">+' .$change. '%</span>';
  20. }else{
  21. $change = '<span class="red" style="font-size:18px;font-weight: 500;">' .$change. '%</span>';
  22. }
  23. if($close >= $open){
  24. $close = '<span class="green" style="font-size:18px;font-weight: 500;">'.$close.'</span>';
  25. }else{
  26. $close = '<span class="red" style="font-size:18px;font-weight: 500;">'.$close.'</span>';
  27. }
  28. $data['code']=1;
  29. $data['price'] = $close;
  30. $data['change']= $change;
  31. $this->ajaxReturn($data);
  32. }else{
  33. $arr = explode('/',$symbol);
  34. $coinname = strtolower($arr[0]).strtolower($arr[1]);
  35. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$coinname;
  36. $result = $this->get_maket_api($url);
  37. $pdata = $result['data'][0];
  38. $open = $pdata['open'];//开盘价
  39. $close = $pdata['close'];//现价
  40. $lowhig = $close - $open; //涨跌
  41. $change = round(($lowhig / $open * 100),2); //涨跌幅
  42. if($change >= 0){
  43. $change = '<span class="green" style="font-size:18px;font-weight: 500;">+' .$change. '%</span>';
  44. }else{
  45. $change = '<span class="red" style="font-size:18px;font-weight: 500;">' .$change. '%</span>';
  46. }
  47. if($close >= $open){
  48. $close = '<span class="green" style="font-size:18px;font-weight: 500;">'.$close.'</span>';
  49. }else{
  50. $close = '<span class="red" style="font-size:18px;font-weight: 500;">'.$close.'</span>';
  51. }
  52. $data['code']=1;
  53. $data['price'] = $close;
  54. $data['change']= $change;
  55. $data['ot_price'] = $pdata['close'];
  56. $this->ajaxReturn($data);
  57. }
  58. }
  59. //获取当前最新价格
  60. public function getnewprice($symbol = null){
  61. if($symbol == '' || $symbol == null){
  62. $this->ajaxReturn(['code'=>0]);
  63. }
  64. if($symbol == "UKB/USDT"){
  65. $symbol = "ukb_usdt";
  66. $mlist = M("market")->where(array('name'=>$symbol))->field("new_price")->find();
  67. $num = 0.001 * rand(1,9);
  68. $close = $mlist['new_price'] + $num;//现价
  69. $data['code']=1;
  70. $data['price']=$close;
  71. $this->ajaxReturn($data);
  72. }else{
  73. $arr = explode('/',$symbol);
  74. $coinname = strtolower($arr[0]).strtolower($arr[1]);
  75. $url = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$coinname;
  76. $result = $this->get_maket_api($url);
  77. $pdata = $result['data'][0];
  78. $price = $pdata['close'];
  79. $data['code']=1;
  80. $data['price']=$price;
  81. $this->ajaxReturn($data);
  82. }
  83. }
  84. //获取5条卖出记录
  85. public function gettradbuy($symbol = null){
  86. if($symbol == "UKB/USDT"){
  87. $market = "ukb_usdt";
  88. $list = M("trade")->where(array('market'=>$market))->order("id desc")->limit(20)->select();
  89. foreach($list as $key=>$vo){
  90. if($vo['type'] == 1){
  91. $ajdata[$key]['amount'] = sprintf("%.4f",$vo['num']);
  92. $ajdata[$key]['price'] = sprintf("%.4f",$vo['price']);
  93. }
  94. }
  95. $this->ajaxReturn(['code'=>1,'data'=>$ajdata]);
  96. }else{
  97. $arr = explode('/',$symbol);
  98. $coinname = strtolower($arr[0]).strtolower($arr[1]);
  99. $url = "https://api.huobi.pro/market/history/trade?symbol=".$coinname."&size=20";
  100. $result = $this->get_maket_api($url);
  101. $data = $result['data'];
  102. $ajdata = array();
  103. foreach($data as $key=>$vo){
  104. $direction = $vo['data'][0]['direction'];
  105. if($direction == "buy"){
  106. $ajdata[$key]['amount'] = sprintf("%.4f",$vo['data'][0]['amount']);
  107. $ajdata[$key]['price'] = sprintf("%.4f",$vo['data'][0]['price']);
  108. }
  109. }
  110. $this->ajaxReturn(['code'=>1,'data'=>$ajdata]);
  111. }
  112. }
  113. //获取5条购买记录
  114. public function gettradsell($symbol = null){
  115. if($symbol == "UKB/USDT"){
  116. $market = "ukb_usdt";
  117. $list = M("trade")->where(array('market'=>$market))->order("id desc")->limit(20)->select();
  118. foreach($list as $key=>$vo){
  119. if($vo['type'] == 2){
  120. $ajdata[$key]['amount'] = sprintf("%.4f",$vo['num']);
  121. $ajdata[$key]['price'] = sprintf("%.4f",$vo['price']);
  122. }
  123. }
  124. $this->ajaxReturn(['code'=>1,'data'=>$ajdata]);
  125. }else{
  126. $arr = explode('/',$symbol);
  127. $coinname = strtolower($arr[0]).strtolower($arr[1]);
  128. $url = "https://api.huobi.pro/market/history/trade?symbol=".$coinname."&size=40";
  129. $result = $this->get_maket_api($url);
  130. $data = $result['data'];
  131. $ajdata = array();
  132. foreach($data as $key=>$vo){
  133. $direction = $vo['data'][0]['direction'];
  134. if($direction == "sell"){
  135. $ajdata[$key]['amount'] = sprintf("%.4f",$vo['data'][0]['amount']);
  136. $ajdata[$key]['price'] = sprintf("%.4f",$vo['data'][0]['price']);
  137. }
  138. }
  139. $this->ajaxReturn(['code'=>1,'data'=>$ajdata]);
  140. }
  141. }
  142. //获取最新买卖记录
  143. public function gettradlist(){
  144. $coinname = trim(I('post.coinname'));
  145. if($coinname == "UKB"){
  146. $symbol = "ukb_usdt";
  147. $tlist = M("trade")->where(array("market"=>$symbol))->order("id desc")->limit(20)->select();
  148. $ajdata = array();
  149. foreach($tlist as $key=>$vo){
  150. if($vo['type'] == 1){
  151. $str = L('买入');
  152. $ajdata[$key]['strtype'] = '<span class="fzmm green">'. $str .'</span>';
  153. $ajdata[$key]['amount'] = '<span class="fzmm green">'. sprintf("%.4f",$vo['num']) .'</span>';
  154. $ajdata[$key]['price'] = '<span class="fzmm green">'. sprintf("%.4f",$vo['price']) .'</span>';
  155. }elseif($vo['type'] == 2){
  156. $str = L('卖出');
  157. $ajdata[$key]['strtype'] = '<span class="fzmm red">'. $str.'</span>';
  158. $ajdata[$key]['amount'] = '<span class="fzmm red">'. sprintf("%.4f",$vo['num']) .'</span>';
  159. $ajdata[$key]['price'] = '<span class="fzmm red">'. sprintf("%.4f",$vo['price']) .'</span>';
  160. }
  161. }
  162. $this->ajaxReturn(['code'=>1,'data'=>$ajdata]);
  163. }else{
  164. $symbol = strtolower($coinname).'usdt';
  165. $url = "https://api.huobi.pro/market/history/trade?symbol=".$symbol."&size=20";
  166. $result = $this->get_maket_api($url);
  167. $data = $result['data'];
  168. $ajdata = array();
  169. foreach($data as $key=>$vo){
  170. $direction = $vo['data'][0]['direction'];
  171. if($direction == "sell"){
  172. $str = L('卖出');
  173. $ajdata[$key]['strtype'] = '<span class="fzmm red">'. $str.'</span>';
  174. $ajdata[$key]['amount'] = '<span class="fzmm red">'. sprintf("%.4f",$vo['data'][0]['amount']) .'</span>';
  175. $ajdata[$key]['price'] = '<span class="fzmm red">'. sprintf("%.4f",$vo['data'][0]['price']) .'</span>';
  176. }elseif($direction == "buy"){
  177. $str = L('买入');
  178. $ajdata[$key]['strtype'] = '<span class="fzmm green">'. $str .'</span>';
  179. $ajdata[$key]['amount'] = '<span class="fzmm green">'. sprintf("%.4f",$vo['data'][0]['amount']) .'</span>';
  180. $ajdata[$key]['price'] = '<span class="fzmm green">'. sprintf("%.4f",$vo['data'][0]['price']) .'</span>';
  181. }
  182. }
  183. $this->ajaxReturn(['code'=>1,'data'=>$ajdata]);
  184. }
  185. }
  186. //获取主流货币详情
  187. public function get_market_one(){
  188. $btcapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=btcusdt";
  189. $ethapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=ethusdt";
  190. $bchapi = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=bchusdt";
  191. $btcresult = $this->get_maket_api($btcapi);
  192. $ethresult = $this->get_maket_api($ethapi);
  193. $bchresult = $this->get_maket_api($bchapi);
  194. $btcdata = $this->processing_onedata($btcresult);
  195. $ethdata = $this->processing_onedata($ethresult);
  196. $bchdata = $this->processing_onedata($bchresult);
  197. $market['btccoin'] = "BTC/USDT";
  198. $market['btcnewprice'] = $btcdata['open'];
  199. $market['btcchange'] = $btcdata['change'];
  200. $market['ethcoin'] = "ETH/USDT";
  201. $market['ethnewprice'] = $ethdata['open'];
  202. $market['ethchange'] = $ethdata['change'];
  203. $market['bchcoin'] = "BCH/USDT";
  204. $market['bchnewprice'] = $bchdata['open'];
  205. $market['bchchange'] = $bchdata['change'];
  206. $market['code'] = 1;
  207. $this->ajaxReturn($market);
  208. }
  209. //处理单个行情数理
  210. public function processing_onedata($array){
  211. $price_arr = $array['data'][0];
  212. $open = $price_arr['open'];//开盘价
  213. $close = $price_arr['close'];//现价
  214. $lowhig = $close - $open; //涨跌
  215. $change = round(($lowhig / $open * 100),2); //涨跌幅
  216. if($change >= 0){
  217. $change = '<span class="green" style="font-size:14px;font-weight: 500;">+' .$change. '%</span>';
  218. }else{
  219. $change = '<span class="red" style="font-size:14px;font-weight: 500;">' .$change. '%</span>';
  220. }
  221. if($close >= $open){
  222. $close = '<span class="green" style="font-size:16px;font-weight: 500;">'.$close.'</span>';
  223. }else{
  224. $close = '<span class="red" style="font-size:16px;font-weight: 500;">'.$close.'</span>';
  225. }
  226. $pdata['open'] = $close;
  227. $pdata['change'] = $change;
  228. return $pdata;
  229. }
  230. //获取行情单个行情数据
  231. public function obtain_btc($coin){
  232. $symbol = $coin."usdt";
  233. $cname = strtoupper($coin)."/USDT";
  234. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  235. $data = $this->get_maket_api($api);
  236. if($data['status'] == 'ok'){
  237. $price_arr = $data['data'][0];
  238. $open = $price_arr['open'];//开盘价
  239. $close = $price_arr['close'];//现价
  240. $lowhig = $close - $open; //涨跌
  241. $change = round(($lowhig / $open * 100),2); //涨跌幅
  242. if($change >= 0){
  243. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  244. }else{
  245. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  246. }
  247. if($close >= $open){
  248. $close = "<span class='fzmmm green'>".$close."</span>";
  249. }else{
  250. $close = "<span class='fzmmm red'>".$close."</span>";
  251. }
  252. $alldata['code'] = 1;
  253. $alldata['cname'] = $cname;
  254. $alldata['open'] = $close;
  255. $alldata['change'] = $changestr;
  256. $this->ajaxReturn($alldata);
  257. }else{
  258. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  259. }
  260. }
  261. //获取行情单个行情数据
  262. public function obtain_eth($coin){
  263. $symbol = $coin."usdt";
  264. $cname = strtoupper($coin)."/USDT";
  265. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  266. $data = $this->get_maket_api($api);
  267. if($data['status'] == 'ok'){
  268. $price_arr = $data['data'][0];
  269. $open = $price_arr['open'];//开盘价
  270. $close = $price_arr['close'];//现价
  271. $lowhig = $close - $open; //涨跌
  272. $change = round(($lowhig / $open * 100),2); //涨跌幅
  273. if($change >= 0){
  274. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  275. }else{
  276. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  277. }
  278. if($close >= $open){
  279. $close = "<span class='fzmmm green'>".$close."</span>";
  280. }else{
  281. $close = "<span class='fzmmm red'>".$close."</span>";
  282. }
  283. $alldata['code'] = 1;
  284. $alldata['cname'] = $cname;
  285. $alldata['open'] = $close;
  286. $alldata['change'] = $changestr;
  287. $this->ajaxReturn($alldata);
  288. }else{
  289. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  290. }
  291. }
  292. //获取行情单个行情数据
  293. public function obtain_eos($coin){
  294. $symbol = $coin."usdt";
  295. $cname = strtoupper($coin)."/USDT";
  296. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  297. $data = $this->get_maket_api($api);
  298. if($data['status'] == 'ok'){
  299. $price_arr = $data['data'][0];
  300. $open = $price_arr['open'];//开盘价
  301. $close = $price_arr['close'];//现价
  302. $lowhig = $close - $open; //涨跌
  303. $change = round(($lowhig / $open * 100),2); //涨跌幅
  304. if($change >= 0){
  305. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  306. }else{
  307. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  308. }
  309. if($close >= $open){
  310. $close = "<span class='fzmmm green'>".$close."</span>";
  311. }else{
  312. $close = "<span class='fzmmm red'>".$close."</span>";
  313. }
  314. $alldata['code'] = 1;
  315. $alldata['cname'] = $cname;
  316. $alldata['open'] = $close;
  317. $alldata['change'] = $changestr;
  318. $this->ajaxReturn($alldata);
  319. }else{
  320. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  321. }
  322. }
  323. //获取行情单个行情数据
  324. public function obtain_doge($coin){
  325. $symbol = $coin."usdt";
  326. $cname = strtoupper($coin)."/USDT";
  327. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  328. $data = $this->get_maket_api($api);
  329. if($data['status'] == 'ok'){
  330. $price_arr = $data['data'][0];
  331. $open = $price_arr['open'];//开盘价
  332. $close = $price_arr['close'];//现价
  333. $lowhig = $close - $open; //涨跌
  334. $change = round(($lowhig / $open * 100),2); //涨跌幅
  335. if($change >= 0){
  336. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  337. }else{
  338. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  339. }
  340. if($close >= $open){
  341. $close = "<span class='fzmmm green'>".$close."</span>";
  342. }else{
  343. $close = "<span class='fzmmm red'>".$close."</span>";
  344. }
  345. $alldata['code'] = 1;
  346. $alldata['cname'] = $cname;
  347. $alldata['open'] = $close;
  348. $alldata['change'] = $changestr;
  349. $this->ajaxReturn($alldata);
  350. }else{
  351. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  352. }
  353. }
  354. //获取行情单个行情数据
  355. public function obtain_bch($coin){
  356. $symbol = $coin."usdt";
  357. $cname = strtoupper($coin)."/USDT";
  358. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  359. $data = $this->get_maket_api($api);
  360. if($data['status'] == 'ok'){
  361. $price_arr = $data['data'][0];
  362. $open = $price_arr['open'];//开盘价
  363. $close = $price_arr['close'];//现价
  364. $lowhig = $close - $open; //涨跌
  365. $change = round(($lowhig / $open * 100),2); //涨跌幅
  366. if($change >= 0){
  367. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  368. }else{
  369. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  370. }
  371. if($close >= $open){
  372. $close = "<span class='fzmmm green'>".$close."</span>";
  373. }else{
  374. $close = "<span class='fzmmm red'>".$close."</span>";
  375. }
  376. $alldata['code'] = 1;
  377. $alldata['cname'] = $cname;
  378. $alldata['open'] = $close;
  379. $alldata['change'] = $changestr;
  380. $this->ajaxReturn($alldata);
  381. }else{
  382. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  383. }
  384. }
  385. //获取行情单个行情数据
  386. public function obtain_ltc($coin){
  387. $symbol = $coin."usdt";
  388. $cname = strtoupper($coin)."/USDT";
  389. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  390. $data = $this->get_maket_api($api);
  391. if($data['status'] == 'ok'){
  392. $price_arr = $data['data'][0];
  393. $open = $price_arr['open'];//开盘价
  394. $close = $price_arr['close'];//现价
  395. $lowhig = $close - $open; //涨跌
  396. $change = round(($lowhig / $open * 100),2); //涨跌幅
  397. if($change >= 0){
  398. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  399. }else{
  400. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  401. }
  402. if($close >= $open){
  403. $close = "<span class='fzmmm green'>".$close."</span>";
  404. }else{
  405. $close = "<span class='fzmmm red'>".$close."</span>";
  406. }
  407. $alldata['code'] = 1;
  408. $alldata['cname'] = $cname;
  409. $alldata['open'] = $close;
  410. $alldata['change'] = $changestr;
  411. $this->ajaxReturn($alldata);
  412. }else{
  413. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  414. }
  415. }
  416. //获取行情单个行情数据
  417. public function obtain_iota($coin){
  418. $symbol = $coin."usdt";
  419. $cname = strtoupper($coin)."/USDT";
  420. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  421. $data = $this->get_maket_api($api);
  422. if($data['status'] == 'ok'){
  423. $price_arr = $data['data'][0];
  424. $open = $price_arr['open'];//开盘价
  425. $close = $price_arr['close'];//现价
  426. $lowhig = $close - $open; //涨跌
  427. $change = round(($lowhig / $open * 100),2); //涨跌幅
  428. if($change >= 0){
  429. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  430. }else{
  431. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  432. }
  433. if($close >= $open){
  434. $close = "<span class='fzmmm green'>".$close."</span>";
  435. }else{
  436. $close = "<span class='fzmmm red'>".$close."</span>";
  437. }
  438. $alldata['code'] = 1;
  439. $alldata['cname'] = $cname;
  440. $alldata['open'] = $close;
  441. $alldata['change'] = $changestr;
  442. $this->ajaxReturn($alldata);
  443. }else{
  444. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  445. }
  446. }
  447. //获取行情单个行情数据
  448. public function obtain_fil($coin){
  449. $symbol = $coin."usdt";
  450. $cname = strtoupper($coin)."/USDT";
  451. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  452. $data = $this->get_maket_api($api);
  453. if($data['status'] == 'ok'){
  454. $price_arr = $data['data'][0];
  455. $open = $price_arr['open'];//开盘价
  456. $close = $price_arr['close'];//现价
  457. $lowhig = $close - $open; //涨跌
  458. $change = round(($lowhig / $open * 100),2); //涨跌幅
  459. if($change >= 0){
  460. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  461. }else{
  462. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  463. }
  464. if($close >= $open){
  465. $close = "<span class='fzmmm green'>".$close."</span>";
  466. }else{
  467. $close = "<span class='fzmmm red'>".$close."</span>";
  468. }
  469. $alldata['code'] = 1;
  470. $alldata['cname'] = $cname;
  471. $alldata['open'] = $close;
  472. $alldata['change'] = $changestr;
  473. $this->ajaxReturn($alldata);
  474. }else{
  475. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  476. }
  477. }
  478. //获取行情单个行情数据
  479. public function obtain_flow($coin){
  480. $symbol = $coin."usdt";
  481. $cname = strtoupper($coin)."/USDT";
  482. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  483. $data = $this->get_maket_api($api);
  484. if($data['status'] == 'ok'){
  485. $price_arr = $data['data'][0];
  486. $open = $price_arr['open'];//开盘价
  487. $close = $price_arr['close'];//现价
  488. $lowhig = $close - $open; //涨跌
  489. $change = round(($lowhig / $open * 100),2); //涨跌幅
  490. if($change >= 0){
  491. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  492. }else{
  493. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  494. }
  495. if($close >= $open){
  496. $close = "<span class='fzmmm green'>".$close."</span>";
  497. }else{
  498. $close = "<span class='fzmmm red'>".$close."</span>";
  499. }
  500. $alldata['code'] = 1;
  501. $alldata['cname'] = $cname;
  502. $alldata['open'] = $close;
  503. $alldata['change'] = $changestr;
  504. $this->ajaxReturn($alldata);
  505. }else{
  506. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  507. }
  508. }
  509. //获取行情单个行情数据
  510. public function obtain_jst($coin){
  511. $symbol = $coin."usdt";
  512. $cname = strtoupper($coin)."/USDT";
  513. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  514. $data = $this->get_maket_api($api);
  515. if($data['status'] == 'ok'){
  516. $price_arr = $data['data'][0];
  517. $open = $price_arr['open'];//开盘价
  518. $close = $price_arr['close'];//现价
  519. $lowhig = $close - $open; //涨跌
  520. $change = round(($lowhig / $open * 100),2); //涨跌幅
  521. if($change >= 0){
  522. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  523. }else{
  524. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  525. }
  526. if($close >= $open){
  527. $close = "<span class='fzmmm green'>".$close."</span>";
  528. }else{
  529. $close = "<span class='fzmmm red'>".$close."</span>";
  530. }
  531. $alldata['code'] = 1;
  532. $alldata['cname'] = $cname;
  533. $alldata['open'] = $close;
  534. $alldata['change'] = $changestr;
  535. $this->ajaxReturn($alldata);
  536. }else{
  537. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  538. }
  539. }
  540. //获取行情单个行情数据
  541. public function obtain_itc($coin){
  542. $symbol = $coin."usdt";
  543. $cname = strtoupper($coin)."/USDT";
  544. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  545. $data = $this->get_maket_api($api);
  546. if($data['status'] == 'ok'){
  547. $price_arr = $data['data'][0];
  548. $open = $price_arr['open'];//开盘价
  549. $close = $price_arr['close'];//现价
  550. $lowhig = $close - $open; //涨跌
  551. $change = round(($lowhig / $open * 100),2); //涨跌幅
  552. if($change >= 0){
  553. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  554. }else{
  555. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  556. }
  557. if($close >= $open){
  558. $close = "<span class='fzmmm green'>".$close."</span>";
  559. }else{
  560. $close = "<span class='fzmmm red'>".$close."</span>";
  561. }
  562. $alldata['code'] = 1;
  563. $alldata['cname'] = $cname;
  564. $alldata['open'] = $close;
  565. $alldata['change'] = $changestr;
  566. $this->ajaxReturn($alldata);
  567. }else{
  568. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  569. }
  570. }
  571. //获取行情单个行情数据
  572. public function obtain_ht($coin){
  573. $symbol = $coin."usdt";
  574. $cname = strtoupper($coin)."/USDT";
  575. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  576. $data = $this->get_maket_api($api);
  577. if($data['status'] == 'ok'){
  578. $price_arr = $data['data'][0];
  579. $open = $price_arr['open'];//开盘价
  580. $close = $price_arr['close'];//现价
  581. $lowhig = $close - $open; //涨跌
  582. $change = round(($lowhig / $open * 100),2); //涨跌幅
  583. if($change >= 0){
  584. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  585. }else{
  586. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  587. }
  588. if($close >= $open){
  589. $close = "<span class='fzmmm green'>".$close."</span>";
  590. }else{
  591. $close = "<span class='fzmmm red'>".$close."</span>";
  592. }
  593. $alldata['code'] = 1;
  594. $alldata['cname'] = $cname;
  595. $alldata['open'] = $close;
  596. $alldata['change'] = $changestr;
  597. $this->ajaxReturn($alldata);
  598. }else{
  599. $this->ajaxReturn(['code'=>0,'info'=>"error"]);
  600. }
  601. }
  602. //获取行情单个行情数据
  603. public function obtain_usdz($coin){
  604. $symbol = "ukb_usdt";
  605. $mlist = M("market")->where(array('name'=>$symbol))->field("new_price,min_price,max_price,faxingjia,volume")->find();
  606. //$num = 0.001 * rand(1,9);
  607. $open = $mlist['min_price'];//开盘价
  608. $close = $mlist['new_price']; //+ $num;//现价
  609. $lowhig = $close - $open; //涨跌
  610. $change = round(($lowhig / $open * 100),2); //涨跌幅
  611. if($change >= 0){
  612. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  613. }else{
  614. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  615. }
  616. if($close >= $open){
  617. $close = "<span class='fzmmm green'>".$close."</span>";
  618. }else{
  619. $close = "<span class='fzmmm red'>".$close."</span>";
  620. }
  621. $alldata['code'] = 1;
  622. $alldata['cname'] = $cname;
  623. $alldata['open'] = $close;
  624. $alldata['change'] = $changestr;
  625. $this->ajaxReturn($alldata);
  626. }
  627. //获取行情数据
  628. public function get_maket_api($api){
  629. $ch = curl_init();
  630. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  631. curl_setopt ($ch, CURLOPT_URL, $api );
  632. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  633. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
  634. $result = json_decode(curl_exec($ch),true);
  635. return $result;
  636. }
  637. //处理行情数理
  638. public function processing_data($array,$cname){
  639. $price_arr = $array['data'][0];
  640. $open = $price_arr['open'];//开盘价
  641. $close = $price_arr['close'];//现价
  642. $lowhig = $close - $open; //涨跌
  643. $change = round(($lowhig / $open * 100),2); //涨跌幅
  644. if($change >= 0){
  645. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  646. }else{
  647. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  648. }
  649. if($close >= $open){
  650. $close = "<span class='fzmmm green'>".$close."</span>";
  651. }else{
  652. $close = "<span class='fzmmm red'>".$close."</span>";
  653. }
  654. $pdata['open'] = $close;
  655. $pdata['cname'] = $cname;
  656. $pdata['change'] = $changestr;
  657. return $pdata;
  658. }
  659. //获取市场行情
  660. public function getallsymbol(){
  661. $list = M("ctmarket")->where(array('status'=>1))->field("coinname,id")->select();
  662. if(!empty($list)){
  663. foreach($list as $k=>$v){
  664. $symbol = $v['coinname']."usdt";
  665. $cname = strtoupper($v['coinname'])."/USDT";
  666. $sid = $v['id'];
  667. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  668. $data = $this->get_maket_api($api);
  669. // print_r($data);die;
  670. $price_arr = $data['data'][0];
  671. $open = $price_arr['open'];//开盘价
  672. $close = $price_arr['close'];//现价
  673. $lowhig = $close - $open; //涨跌
  674. $change = round(($lowhig / $open * 100),2); //涨跌幅
  675. if($change >= 0){
  676. $changestr = "<span class='fzmm bgreen' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>+". $change ."%</span>";
  677. }else{
  678. $changestr = "<span class='fzmm bred' style='color:#fff;border-radius: 2px;width:70px;height:35px;line-height:35px;text-align:center;display:inline-block;'>". $change ."%</span>";
  679. }
  680. if($close >= $open){
  681. $close = "<span class='fzmmm green'>".$close."</span>";
  682. }else{
  683. $close = "<span class='fzmmm red'>".$close."</span>";
  684. }
  685. $alldata[$k]['sid'] = $sid;
  686. $alldata[$k]['cname'] = $cname;
  687. $alldata[$k]['open'] = $close;
  688. $alldata[$k]['change'] = $changestr;
  689. }
  690. $this->ajaxReturn(['code'=>1,'data'=>$alldata]);
  691. }else{
  692. $this->ajaxReturn(['code'=>0]);
  693. }
  694. }
  695. //处理合约页面交易对数据
  696. public function hydata($array,$cname){
  697. $price_arr = $array['data'][0];
  698. $open = $price_arr['open'];//开盘价
  699. $close = $price_arr['close'];//现价
  700. $lowhig = $close - $open; //涨跌
  701. $change = round(($lowhig / $open * 100),2); //涨跌幅
  702. if($change >= 0){
  703. $changestr = "<span class='fzmm green'>+".$change."%</span>";
  704. }else{
  705. $changestr = "<span class='fzmm red'>".$change."%</span>";
  706. }
  707. if($close >= $open){
  708. $close = "<span class='fzmm green'>".$close."</span>";
  709. }else{
  710. $close = "<span class='fzmm red'>".$close."</span>";
  711. }
  712. $pdata['open'] = $close;
  713. $pdata['cname'] = $cname;
  714. $pdata['change'] = $changestr;
  715. return $pdata;
  716. }
  717. //合约页面获取所有交易对
  718. public function getallcoin(){
  719. $where['status'] = 1;
  720. //$where['coinname'] = array('neq','ukb');
  721. $list = M("ctmarket")->where($where)->field("coinname,id")->select();
  722. if(!empty($list)){
  723. foreach($list as $k=>$v){
  724. if($v['coinname'] == "ukb"){
  725. $cname = "UKB/USDT";
  726. $sid = $v['id'];
  727. $symbol = "ukb_usdt";
  728. $mlist = M("market")->where(array('name'=>$symbol))->field("new_price,min_price,max_price,faxingjia,volume")->find();
  729. //$num = 0.001 * rand(1,9);
  730. $open = $mlist['min_price'];//开盘价
  731. $close = $mlist['new_price']; //+ $num;//现价
  732. $lowhig = $close - $open; //涨跌
  733. $change = round(($lowhig / $open * 100),2); //涨跌幅
  734. if($change >= 0){
  735. $changestr = "<span class='fzmm green'>+".$change."%</span>";
  736. }else{
  737. $changestr = "<span class='fzmm red'>".$change."%</span>";
  738. }
  739. if($close >= $open){
  740. $close = "<span class='fzmm green'>".$close."</span>";
  741. }else{
  742. $close = "<span class='fzmm red'>".$close."</span>";
  743. }
  744. $alldata[$k]['sid'] = $sid;
  745. $alldata[$k]['coin'] = strtoupper($v['coinname']);
  746. $alldata[$k]['cname'] = $cname;
  747. $alldata[$k]['symbol'] = $v['coinname'];
  748. $alldata[$k]['open'] = $close;
  749. $alldata[$k]['change'] = $changestr;
  750. }else{
  751. $symbol = $v['coinname']."usdt";
  752. $cname = strtoupper($v['coinname'])."/USDT";
  753. $sid = $v['id'];
  754. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  755. $data = $this->get_maket_api($api);
  756. $result = $this->hydata($data,$cname);
  757. $alldata[$k]['sid'] = $sid;
  758. $alldata[$k]['coin'] = strtoupper($v['coinname']);
  759. $alldata[$k]['cname'] = $result['cname'];
  760. $alldata[$k]['symbol'] = $v['coinname'];
  761. $alldata[$k]['open'] = $result['open'];
  762. $alldata[$k]['change'] = $result['change'];
  763. }
  764. }
  765. $this->ajaxReturn(['code'=>1,'data'=>$alldata]);
  766. }else{
  767. $this->ajaxReturn(['code'=>0]);
  768. }
  769. }
  770. //获取交易对数据
  771. public function getcoin_data(){
  772. $coinname = strtolower(trim($_POST['coinname']));
  773. if($coinname == "ukb"){
  774. $symbol = "ukb_usdt";
  775. $mlist = M("market")->where(array('name'=>$symbol))->field("new_price,min_price,max_price,faxingjia,volume")->find();
  776. $num = 0.001 * rand(1,9);
  777. $open = $mlist['min_price'];//开盘价
  778. $close = $mlist['new_price'] + $num;//现价
  779. $lowhig = $close - $open; //涨跌
  780. $change = round(($lowhig / $open * 100),2); //涨跌幅
  781. $_SESSION['close'] = $close;
  782. if($change >= 0){
  783. $changestr = "<span class='green' style='font-size:16px;'>+". $change ."%</span>";
  784. }else{
  785. $changestr = "<span class='red' style='font-size:16px;'>". $change ."%</span>";
  786. }
  787. if($close >= $open){
  788. $close = "<span class='green' style='font-size:22px;'>".$close."</span>";
  789. }else{
  790. $close = "<span class='red' style='font-size:22px;'>".$close."</span>";
  791. }
  792. $high = $mlist['max_price'];
  793. $low = $mlist['min_price'];
  794. $amount = $mlist['volume'];
  795. $result['close'] = $close;
  796. $result['change'] = $changestr;
  797. $result['high'] = $high;
  798. $result['low'] = $low;
  799. $result['amount'] = sprintf("%.6f",$amount);
  800. $result['code'] = 1;
  801. $result['aa'] = $_SESSION['close'];
  802. $this->ajaxReturn($result);
  803. }else{
  804. $symbol = strtolower($coinname)."usdt";
  805. $cname = strtoupper($coinname)."/USDT";
  806. $api = "https://api.huobi.pro/market/history/kline?period=1day&size=1&symbol=".$symbol;
  807. $data = $this->get_maket_api($api);
  808. $price_arr = $data['data'][0];
  809. $open = $price_arr['open'];//开盘价
  810. $close = $price_arr['close'];//现价
  811. $lowhig = $close - $open; //涨跌
  812. $change = round(($lowhig / $open * 100),2); //涨跌幅
  813. if($change >= 0){
  814. $changestr = "<span class='green' style='font-size:16px;'>+". $change ."%</span>";
  815. }else{
  816. $changestr = "<span class='red' style='font-size:16px;'>". $change ."%</span>";
  817. }
  818. if($close >= $open){
  819. $close = "<span class='green' style='font-size:22px;'>".$close."</span>";
  820. }else{
  821. $close = "<span class='red' style='font-size:22px;'>".$close."</span>";
  822. }
  823. $high = $price_arr['high'];
  824. $low = $price_arr['low'];
  825. $amount = $price_arr['amount'];
  826. $result['close'] = $close;
  827. $result['change'] = $changestr;
  828. $result['high'] = $high;
  829. $result['low'] = $low;
  830. $result['amount'] = sprintf("%.6f",$amount);
  831. $result['code'] = 1;
  832. $this->ajaxReturn($result);
  833. }
  834. }
  835. public function ajax_data(){
  836. $url = "https://api.huobi.pro/market/history";
  837. $url .= $_POST['url'].'?';
  838. foreach ($_POST as $k=>$v){
  839. if($k != 'url'){
  840. $url .= $k.'='.$v.'&';
  841. }
  842. }
  843. $data = $this->get_maket_api($url);
  844. echo json_encode($data);die();
  845. }
  846. }
  847. ?>