request->get('page', 1); $device_code = Yii::$app->request->get('device_code', ''); $platform_id = Yii::$app->request->get('device_code', 0); $exchange = Yii::$app->request->get('exchange', 'zhaobi'); $data_value = Yii::$app->request->get('data_value', ''); $sort_value = Yii::$app->request->get('sort_value', ''); $exchange_arr = ['huobi', 'binance', 'zhaobi', 'bitnasdaq']; if (!in_array($exchange, $exchange_arr)) { $msg = '不存在的交易平台'; $code = -1; $data = []; goto doEnd; } $condition = []; if (false != $data_value && false != $sort_value) { $condition = [ 'data_value' => $data_value, 'sort_value' => $sort_value ]; } $builder = ExchangeBuilderFactory::create($exchange); $result = $builder->getTickerFromCache($page, $condition); $code = $result['code']; $data = $result['data']; if (false != $device_code) { if (false != $platform_id) { $coin_optional = CoinOptional::find()->select('symbol')->where(['platform' => $exchange, 'device_code' => $device_code])->asArray()->all(); } else { $coin_optional = CoinOptional::find()->select('symbol')->where(['platform' => $exchange, 'device_code' => $device_code, 'platform_id' => $platform_id])->asArray()->all(); } $coin_optional = ArrayHelper::getColumn($coin_optional, 'symbol'); foreach ($data['ticker'] as &$val) { if (in_array($val['symbol'], $coin_optional)) { $val['optional'] = true; } } } $msg = 'success'; doEnd : return ['code' => $code, 'msg' => $msg, 'data' => $data]; } public function actionOptional() { $curl = new Curl(); $data = [ "names" => [ "eth,ethereum", "btc,btc", "usdt,ethereum", "bty,bty", ] ]; $params = json_encode($data); $curl->setHeader('Content-Type', 'application/json'); $curl->setRawPostData($params); $res = $curl->post(Yii::$app->params['biqianbao'] . '/interface/coin/coin-index', true); $res = json_decode($res, true); foreach ($res['data'] as $val) { $this->basic_price[$val['name']]['rmb'] = $val['rmb']; $this->basic_price[$val['name']]['usd'] = $val['usd']; } $code = -1; $msg = 'fail'; $data = null; $request = Yii::$app->request; if ($request->isPost) { $symbol = $request->post('symbol', ''); $platform = $request->post('platform', ''); $device_code = $request->post('device_code', ''); $platform_id = $request->post('platform_id', 0); if (empty($symbol) || empty($platform) || empty($device_code) || empty($platform_id)) { $msg = '参数错误'; goto doEnd; } $model = CoinOptional::find()->where(['device_code' => $device_code, 'symbol' => $symbol, 'platform_id' => (int)$platform_id])->one(); if ($model) { $msg = '数据已存在!'; goto doEnd; } $model = new CoinOptional(); $model->setScenario(CoinOptional::SCENARIOS_CREATE); if ($model->load(Yii::$app->request->post(), '') && $model->save()) { $msg = 'success'; $code = 0; } else { $msg = current($model->firstErrors); } goto doEnd; } if ($request->isGet) { $device_code = $request->get('device_code', ''); $platform_id = $request->get('platform_id', 0); $page = Yii::$app->request->get('page', 1); if (false == $device_code || false == $platform_id) { $msg = '参数错误'; goto doEnd; } $data_value = Yii::$app->request->get('data_value', ''); $sort_value = Yii::$app->request->get('sort_value', ''); $condition = []; if (false != $data_value && false != $sort_value) { $condition = [ 'data_value' => $data_value, 'sort_value' => $sort_value ]; } $temp = []; $data = CoinOptional::find()->select('symbol, platform') ->where(['device_code' => $device_code, 'platform_id' => (int)$platform_id]) ->orderBy('update_time desc') ->asArray() ->all(); if (false == $data) { $msg = 'success'; $code = 0; $data = [ 'ticker' => [], 'page' => [ 'pageSize' => 50, 'currentPage' => (int)$page, ] ]; goto doEnd; } $ticker = []; foreach ($data as $val) { if ('huobi' == $val['platform']) { $exchange = 'HuoBi'; } else if ('binance' == $val['platform']) { $exchange = 'Binance'; } else if ('zhaobi' == $val['platform']) { $exchange = 'Zhaobi'; } else { } $symbol = explode('/', $val['symbol']); $tag_first = $currency = $symbol[0]; $tag_second = $symbol[1]; if ('BCH' == strtoupper($tag_first)){ $tag_first = 'BCC'; } $exchange = ExchangeFactory::createExchange($exchange); $quotation = $exchange->getTicker(strtolower($tag_first), strtolower($tag_second)); if (empty($quotation)) continue; $temp['symbol'] = $val['symbol']; $temp['currency'] = strtoupper($currency); $temp['base_currency'] = strtoupper($tag_second); $temp['close'] = rtrim(sprintf('%.8f', floatval($quotation['last'])), '0'); $temp['close_usd'] = rtrim(sprintf('%.6f', floatval($quotation['last'] * $this->basic_price[$tag_second]['usd'])), '0'); $temp['close_rmb'] = (float)sprintf("%0.4f", $quotation['last'] * $this->basic_price[$tag_second]['rmb']); $temp['change'] = (0 == $quotation['open']) ? 0 : (float)sprintf("%0.2f", ($quotation['last'] - $quotation['open']) / $quotation['open'] * 100); $temp['high_usd'] = (float)sprintf("%0.4f", $quotation['high'] * $this->basic_price[$tag_second]['usd']); $temp['low_usd'] = (float)sprintf("%0.4f", $quotation['low'] * $this->basic_price[$tag_second]['usd']); $temp['high_rmb'] = (float)sprintf("%0.4f", $quotation['high'] * $this->basic_price[$tag_second]['rmb']); $temp['low_rmb'] = (float)sprintf("%0.4f", $quotation['low'] * $this->basic_price[$tag_second]['rmb']); $temp['vol'] = (float)sprintf("%0.4f", $quotation['vol']); $temp['platform_us'] = $val['platform']; if ('ZHAOBI' == strtoupper($val['platform'])) { $temp['platform_zh'] = '找币'; $temp['platform_us'] = 'zhaobi'; } if ('HUOBI' == strtoupper($val['platform'])) { $temp['platform_zh'] = '火币'; $temp['platform_us'] = 'huobi'; } if ('BINANCE' == strtoupper($val['platform'])) { $temp['platform_zh'] = '币安'; $temp['platform_us'] = 'binance'; } array_push($ticker, $temp); } if (!empty($condition)) { if ('price' == $condition['data_value']) { if ('price_asc' == $condition['sort_value']) { $ticker = Tools::arraySort($ticker, 'close', SORT_ASC); } if ('price_desc' == $condition['sort_value']) { $ticker = Tools::arraySort($ticker, 'close', SORT_DESC); } } if ('change' == $condition['data_value']) { if ('change_asc' == $condition['sort_value']) { $ticker = Tools::arraySort($ticker, 'change', SORT_ASC); } if ('change_desc' == $condition['sort_value']) { $ticker = Tools::arraySort($ticker, 'change', SORT_DESC); } } } } $data = [ 'ticker' => $ticker, 'page' => [ 'pageSize' => 50, 'currentPage' => (int)$page, ] ]; $msg = 'success'; $code = 0; doEnd : return ['code' => $code, 'msg' => $msg, 'data' => $data]; } public function actionRemoveOptional() { $code = -1; $msg = 'fail'; $data = null; $request = Yii::$app->request; if (!$request->isPost) { $msg = '请求错误!'; goto doEnd; } $symbol = $request->post('symbol', ''); $platform = $request->post('platform', ''); $device_code = $request->post('device_code', ''); $platform_id = $request->post('platform_id', 0); if (empty($symbol) || empty($device_code) || empty($platform) || empty($platform_id)) { $msg = '请求参数错误!'; goto doEnd; } $model = CoinOptional::find()->where(['symbol' => $symbol, 'platform' => $platform, 'device_code' => $device_code, 'platform_id' => (int)$platform_id])->one(); if (empty($model)) { $msg = '数据不存在!'; goto doEnd; } if (!$model->delete()) { $msg = '删除失败!'; goto doEnd; } $code = 0; $msg = 'success'; doEnd : return ['code' => $code, 'msg' => $msg, 'data' => $data]; } public function actionHotTicker() { $builder = ExchangeBuilderFactory::create('huobi'); $result = $builder->getHotTicker(); $code = $result['code']; $data = $result['ticker']; $msg = 'success'; doEnd : return ['code' => $code, 'msg' => $msg, 'data' => $data]; } }