[ "eth,ethereum", "btc,btc", "usdt,ethereum", "bty,bty", ] ]; $params = json_encode($data); $curl->setHeader('Content-Type', 'application/json'); $curl->setRawPostData($params); $res = $curl->post('https://b.biqianbao.net/interface/coin/coin-index', true); $res = json_decode($res, true); foreach ($res['data'] as $val) { $this->basic_price[$val['name']] = $val['rmb']; } } public function actionIndex() { $device_code = Yii::$app->request->get('device_code', ''); $exchange = Yii::$app->request->get('exchange', 'zhaobi'); $exchange_arr = ['huobi', 'binance', 'okex', 'zhaobi']; if (!in_array($exchange, $exchange_arr)) { $msg = '不存在的交易平台'; $code = -1; $data = []; goto doEnd; } $builder = ExchangeBuilderFactory::create($exchange); $result = $builder->getTickerFromCache(); $code = $result['code']; $data = $result['ticker']; if (false != $device_code) { $coin_optional = CoinOptional::find()->select('symbol')->where(['platform' => $exchange, 'device_code' => $device_code])->asArray()->all(); $coin_optional = ArrayHelper::getColumn($coin_optional, 'symbol'); foreach ($data 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() { $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', ''); if (empty($symbol) || empty($platform) || empty($device_code)) { $msg = '参数错误'; goto doEnd; } $model = CoinOptional::find()->where(['device_code' => $device_code, 'symbol' => $symbol])->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', ''); if (empty($device_code)) { $msg = '参数错误'; goto doEnd; } $data = $temp = []; $model = CoinOptional::find()->select('symbol, platform')->where(['device_code' => $device_code])->asArray()->all(); foreach ($model 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 = $symbol[0]; $tag_second = $symbol[1]; $exchange = ExchangeFactory::createExchange($exchange); $quotation = $exchange->getTicker(strtolower($tag_first), strtolower($tag_second)); $temp['symbol'] = $val['symbol']; $temp['currency'] = strtoupper($tag_first); $temp['base_currency'] = strtoupper($tag_second); $temp['close'] = (float)sprintf("%0.6f", $quotation['last']); $temp['close_usd'] = (float)sprintf("%0.6f", $quotation['last'] * $this->basic_price[$tag_second]['usd']); $temp['close_rmb'] = (float)sprintf("%0.4f", $quotation['last'] * $this->basic_price[$tag_second]['rmb']); $temp['change'] = (float)sprintf("%0.4f", ($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['platform_us'] = $val['platform']; if ('ZHAOBI' == strtoupper($val['platform'])) { $temp['platform_zh'] = '找币'; } if ('HUOBI' == strtoupper($val['platform'])) { $temp['platform_zh'] = '火币'; } if ('BINANCE' == strtoupper($val['platform'])) { $temp['platform_zh'] = '币安'; } array_push($data, $temp); } $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', ''); if (empty($symbol) || empty($device_code) || empty($platform)) { $msg = '请求参数错误!'; goto doEnd; } $model = CoinOptional::find()->where(['symbol' => $symbol, 'platform' => $platform, 'device_code' => $device_code])->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]; } }