KdataController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 25276
  5. * Date: 2018/4/26
  6. * Time: 9:58
  7. */
  8. namespace backend\controllers;
  9. use backend\forms\KdataForm;
  10. use components\Exception;
  11. use components\service\Redis;
  12. use yii\helpers\ArrayHelper;
  13. use Yii;
  14. class KdataController extends BaseController
  15. {
  16. /**
  17. * behaviors
  18. * @return array
  19. */
  20. public function behaviors()
  21. {
  22. return ArrayHelper::merge(parent::behaviors(), [
  23. 'verbs' => [
  24. 'class' => \yii\filters\VerbFilter::className(),
  25. 'actions' => [
  26. 'history'=>['GET']
  27. ],
  28. ],
  29. ]);
  30. }
  31. public function actionHistory(){
  32. header("Access-Control-Allow-Origin: *");
  33. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  34. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  35. $params = Yii::$app->request->queryParams;
  36. $symbolArray = array('BTCUSDT','BCCUSDT','BTYUSDT','ETHUSDT','ZECUSDT','LTCUSDT','DCRUSDT','BTSUSDT','BCCBTC','ETHBTC','ETCBTC','ZECBTC','LTCBTC');
  37. $periodArray = array('m1','m3','m5','m15','m30','h1','h2','h4','h6','h12','d1','d3','d5','d7','d15','mo');
  38. //参数为空的异常处理:
  39. if (!isset($params['symbol']))
  40. throw new Exception(1001,'交易对不能为空!');
  41. if (!isset($params['period']))
  42. throw new Exception(1002,'k线级别不能为空!');
  43. if ((!isset($params['from']))&&(!isset($params['to'])))
  44. throw new Exception(1003,'起点终点时间戳 至少有一个要有值!');
  45. if((!isset($params['from'])||!isset($params['to']))&&!isset($params['count']))
  46. throw new Exception(1004,'起点终点时间戳只有一个有值时 count不能为空!');
  47. //参数类型异常处理:
  48. if (!is_string($params['symbol']) || !is_string($params['period']))
  49. throw new Exception(2001,'交易对和k线级别参数类型只能是字符串!');
  50. if(isset($params['count'])&&!is_int($params['count']+0))
  51. throw new Exception(2002,'count必须是整数!');
  52. //参数值异常处理:
  53. // if(isset($params['symbol'])&&!in_array($params['symbol'],$symbolArray))
  54. // throw new Exception(3001,'无此交易对!');
  55. if(isset($params['period'])&&!in_array($params['period'],$periodArray))
  56. throw new Exception(3002,'不支持此级别k线!');
  57. if((isset($params['from'])&&isset($params['to']))&&$params['to']<$params['from'])
  58. throw new Exception(3003,'终点时间戳必须大于起点时间戳!');
  59. $symbol = $params['symbol'];
  60. $period = $params['period'];
  61. $from = isset($params['from'])?$params['from']:-1;
  62. $to = isset($params['to'])?$params['to']:-1;
  63. $count = isset($params['count'])?$params['count']:-1;
  64. $model = new KdataForm();
  65. $data = $model->getData($symbol,$period,$from,$to,$count);
  66. return $data;
  67. }
  68. }