12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 25276
- * Date: 2018/4/26
- * Time: 9:58
- */
- namespace backend\controllers;
- use backend\forms\KdataForm;
- use components\Exception;
- use components\service\Redis;
- use yii\helpers\ArrayHelper;
- use Yii;
- class KdataController extends BaseController
- {
- /**
- * behaviors
- * @return array
- */
- public function behaviors()
- {
- return ArrayHelper::merge(parent::behaviors(), [
- 'verbs' => [
- 'class' => \yii\filters\VerbFilter::className(),
- 'actions' => [
- 'history'=>['GET']
- ],
- ],
- ]);
- }
- public function actionHistory(){
- header("Access-Control-Allow-Origin: *");
- header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
- header('Access-Control-Allow-Headers:x-requested-with,content-type');
- $params = Yii::$app->request->queryParams;
- $symbolArray = array('BTCUSDT','BCCUSDT','BTYUSDT','ETHUSDT','ZECUSDT','LTCUSDT','DCRUSDT','BTSUSDT','BCCBTC','ETHBTC','ETCBTC','ZECBTC','LTCBTC');
- $periodArray = array('m1','m3','m5','m15','m30','h1','h2','h4','h6','h12','d1','d3','d5','d7','d15','mo');
- //参数为空的异常处理:
- if (!isset($params['symbol']))
- throw new Exception(1001,'交易对不能为空!');
- if (!isset($params['period']))
- throw new Exception(1002,'k线级别不能为空!');
- if ((!isset($params['from']))&&(!isset($params['to'])))
- throw new Exception(1003,'起点终点时间戳 至少有一个要有值!');
- if((!isset($params['from'])||!isset($params['to']))&&!isset($params['count']))
- throw new Exception(1004,'起点终点时间戳只有一个有值时 count不能为空!');
- //参数类型异常处理:
- if (!is_string($params['symbol']) || !is_string($params['period']))
- throw new Exception(2001,'交易对和k线级别参数类型只能是字符串!');
- if(isset($params['count'])&&!is_int($params['count']+0))
- throw new Exception(2002,'count必须是整数!');
- //参数值异常处理:
- // if(isset($params['symbol'])&&!in_array($params['symbol'],$symbolArray))
- // throw new Exception(3001,'无此交易对!');
- if(isset($params['period'])&&!in_array($params['period'],$periodArray))
- throw new Exception(3002,'不支持此级别k线!');
- if((isset($params['from'])&&isset($params['to']))&&$params['to']<$params['from'])
- throw new Exception(3003,'终点时间戳必须大于起点时间戳!');
- $symbol = $params['symbol'];
- $period = $params['period'];
- $from = isset($params['from'])?$params['from']:-1;
- $to = isset($params['to'])?$params['to']:-1;
- $count = isset($params['count'])?$params['count']:-1;
- $model = new KdataForm();
- $data = $model->getData($symbol,$period,$from,$to,$count);
- return $data;
- }
- }
|