Commit 9de92d26 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/optimize' into 'master'

fix See merge request !498
parents 1344840e 9fb3eb2d
...@@ -250,7 +250,7 @@ class CoinController extends BaseController ...@@ -250,7 +250,7 @@ class CoinController extends BaseController
foreach ($chains as $key => $value) { foreach ($chains as $key => $value) {
$chain_quotation[$value] = ExchangeBusiness::getquatation($value); $chain_quotation[$value] = ExchangeBusiness::getquatation($value);
} }
$last[] = 1; $last[] = $rate[] = 1;
$currency = CoinCurrency::find()->where(['pj_id' => $this->currency_id])->one(); $currency = CoinCurrency::find()->where(['pj_id' => $this->currency_id])->one();
if (false != $currency) { if (false != $currency) {
if (1111 == $this->currency_id) { if (1111 == $this->currency_id) {
...@@ -259,11 +259,12 @@ class CoinController extends BaseController ...@@ -259,11 +259,12 @@ class CoinController extends BaseController
if (Yii::$app->redis->hkeys('quotation_go_CNY_USD')) { if (Yii::$app->redis->hkeys('quotation_go_CNY_USD')) {
$last[0] = 1 / Yii::$app->redis->hmget('quotation_go_CNY_USD', 'last')[0]; $last[0] = 1 / Yii::$app->redis->hmget('quotation_go_CNY_USD', 'last')[0];
} else { } else {
$last = Yii::$app->redis_currency->hmget('quotation_boc_' . 'CNY_' . $currency->symbol, 'last'); $last = Yii::$app->redis_currency->hmget('quotation_currency_' . 'USD_' . $currency->symbol, 'last');
} }
} else { } else {
$last = Yii::$app->redis_currency->hmget('quotation_boc_' . 'CNY_' . $currency->symbol, 'last'); $last = Yii::$app->redis_currency->hmget('quotation_currency_' . 'USD_' . $currency->symbol, 'last');
} }
$rate = Yii::$app->redis_currency->hmget('quotation_currency_' . 'CNY_' . $currency->symbol, 'last');
} }
foreach ($result['data'] as $key => &$value) { foreach ($result['data'] as $key => &$value) {
...@@ -275,8 +276,8 @@ class CoinController extends BaseController ...@@ -275,8 +276,8 @@ class CoinController extends BaseController
$value['chain_quotation'] = $chain_quotation[$value['chain']] ?: null; $value['chain_quotation'] = $chain_quotation[$value['chain']] ?: null;
$value['chain_rmb'] = isset($value['chain_quotation']['rmb']) ? $value['chain_quotation']['rmb'] : 0; $value['chain_rmb'] = isset($value['chain_quotation']['rmb']) ? $value['chain_quotation']['rmb'] : 0;
$value['chain_usd'] = isset($value['chain_quotation']['usd']) ? $value['chain_quotation']['usd'] : 0; $value['chain_usd'] = isset($value['chain_quotation']['usd']) ? $value['chain_quotation']['usd'] : 0;
$value['chain_country_rate'] = (float)sprintf("%0.4f", $last[0] * $value['chain_rmb']); $value['chain_country_rate'] = (float)sprintf("%0.4f", $rate[0] * $value['chain_rmb']);
$value['country_rate'] = (false == $currency) ? $value['rmb'] : (float)sprintf("%0.4f", $last[0] * $value['rmb']); $value['country_rate'] = (false == $currency) ? $value['rmb'] : (float)sprintf("%0.4f", $last[0] * $value['last']);
} }
return $result; return $result;
} }
......
...@@ -432,7 +432,7 @@ class TickerController extends BaseController ...@@ -432,7 +432,7 @@ class TickerController extends BaseController
if (1111 == $val->currency->pj_id) { if (1111 == $val->currency->pj_id) {
$rate = (string)1; $rate = (string)1;
} else { } else {
list($rate) = Yii::$app->redis_currency->hmget('quotation_boc_' . 'CNY_' . $val->currency->symbol, 'last'); list($rate) = Yii::$app->redis_currency->hmget('quotation_currency_' . 'CNY_' . $val->currency->symbol, 'last');
} }
$val->pj_id = $val->currency->pj_id; $val->pj_id = $val->currency->pj_id;
$val->pj_name = $val->currency->pj_name; $val->pj_name = $val->currency->pj_name;
......
...@@ -79,14 +79,11 @@ class WalletCoinController extends BaseController ...@@ -79,14 +79,11 @@ class WalletCoinController extends BaseController
$chains_symbols = array_keys(array_flip($chains) + array_flip($symbol)); $chains_symbols = array_keys(array_flip($chains) + array_flip($symbol));
$coin_quotations = ExchangeBusiness::getQuatationByNames($chains_symbols); $coin_quotations = ExchangeBusiness::getQuatationByNames($chains_symbols);
$last[] = 1; $last[] = $rate[] = 1;
$currency = CoinCurrency::find()->where(['pj_id' => $this->currency_id])->one(); $currency = CoinCurrency::find()->where(['pj_id' => $this->currency_id])->one();
if (false != $currency) { if (false != $currency) {
if (1111 == $this->currency_id) { $rate = Yii::$app->redis_currency->hmget('quotation_currency_' . 'CNY_' . $currency->symbol, 'last');
$last[] = 1; $last = Yii::$app->redis_currency->hmget('quotation_currency_' . 'USD_' . $currency->symbol, 'last');
} else {
$last = Yii::$app->redis_currency->hmget('quotation_boc_' . 'CNY_' . $currency->symbol, 'last');
}
} }
$ticker = ['low' => 0, 'high' => 0, 'last' => 0, 'open' => 0, 'vol' => 0, 'rmb' => 0, 'usd' => 0]; $ticker = ['low' => 0, 'high' => 0, 'last' => 0, 'open' => 0, 'vol' => 0, 'rmb' => 0, 'usd' => 0];
...@@ -100,8 +97,8 @@ class WalletCoinController extends BaseController ...@@ -100,8 +97,8 @@ class WalletCoinController extends BaseController
$val['chain_rmb'] = isset($val['chain_quotation']['rmb']) ? $val['chain_quotation']['rmb'] : 0; $val['chain_rmb'] = isset($val['chain_quotation']['rmb']) ? $val['chain_quotation']['rmb'] : 0;
$val['chain_usd'] = isset($val['chain_quotation']['usd']) ? $val['chain_quotation']['usd'] : 0; $val['chain_usd'] = isset($val['chain_quotation']['usd']) ? $val['chain_quotation']['usd'] : 0;
$val['chain_country_rate'] = (float)sprintf("%0.4f", $last[0] * $val['chain_rmb']); $val['chain_country_rate'] = (float)sprintf("%0.4f", $rate[0] * $val['chain_rmb']);
$val['country_rate'] = (false == $currency) ? $val['rmb'] : (float)sprintf("%0.4f", $last[0] * $val['rmb']); $val['country_rate'] = (false == $currency) ? $val['rmb'] : (float)sprintf("%0.4f", $last[0] * $val['last']);
unset($val['chain_quotation']); unset($val['chain_quotation']);
} }
......
...@@ -38,38 +38,43 @@ class TickerController extends Controller ...@@ -38,38 +38,43 @@ class TickerController extends Controller
{ {
$ticker_builder = ExchangeFactory::createExchange($exchange); $ticker_builder = ExchangeFactory::createExchange($exchange);
$ticker_builder->setQuotation(); $ticker_builder->setQuotation();
echo date('Y-m-d H:i:s') . $exchange. '更新成功' . PHP_EOL; echo date('Y-m-d H:i:s') . $exchange . '更新成功' . PHP_EOL;
return 0; return 0;
} }
public function actionCurrency() public function actionCurrency()
{ {
$currency_model = CoinSupportedCurrency::find()->groupBy('currency_id')->all();
$curl = new Curl(); $curl = new Curl();
$curl->setHeaders([
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0',
'Host' => 'cc-api.oanda.com',
'Origin' => 'https://www.oanda.com',
'Sec-Fetch-Dest' => 'empty',
'Sec-Fetch-Mode' => 'cors',
'Sec-Fetch-Site' => 'same-site',
'TE' => 'trailers'
]);
$currency_model = CoinSupportedCurrency::find()->groupBy('currency_id')->all();
$start_date = date("Y-m-d", strtotime("-1 day"));
$end_data = date("Y-m-d");
foreach ($currency_model as $val) { foreach ($currency_model as $val) {
if (1111 == $val->currency->pj_id) continue; $url = 'https://cc-api.oanda.com/cc-api/v1/currencies?base=USD&quote=' . $val->currency->symbol . '&data_type=general_currency_pair&start_date=' . $start_date . '&end_date=' . $end_data;
go(function () use ($val, $curl) { $content = $curl->get($url, false);
\Co::sleep(0.5); if (is_array($content) && isset($content['response'])) {
$response = $curl->setPostParams([ $key = 'quotation_currency_' . 'USD_' . $val->currency->symbol;
'erectDate' => '', Yii::$app->redis_currency->hmset($key, 'low', $content['response'][0]['low_ask'], 'high', $content['response'][0]['high_ask'], 'last', $content['response'][0]['average_ask'], 'open', $content['response'][0]['average_ask']);
'nothing' => '', Yii::$app->redis_currency->sadd('supported_symbol_currency', $content['response'][0]['base_currency'] . '_' . $content['response'][0]['quote_currency']);
'pjname' => $val->currency->pj_name }
])->post('https://srh.bankofchina.com/search/whpj/search_cn.jsp');
$response = iconv('UTF-8', 'GBK//TRANSLIT', $response); $url = 'https://cc-api.oanda.com/cc-api/v1/currencies?base=CNY&quote=' . $val->currency->symbol . '&data_type=general_currency_pair&start_date=' . $start_date . '&end_date=' . $end_data;
$html = HtmlDomParser::str_get_html($response); $content = $curl->get($url, false);
$div = ($html->find('div.BOC_main')); if (is_array($content) && isset($content['response'])) {
if ($div->find('td')) { $key = 'quotation_currency_' . 'CNY_' . $val->currency->symbol;
foreach ($div->find('td') as $key => $e) { Yii::$app->redis_currency->hmset($key, 'low', $content['response'][0]['low_ask'], 'high', $content['response'][0]['high_ask'], 'last', $content['response'][0]['average_ask'], 'open', $content['response'][0]['average_ask']);
if ($key == 5) { Yii::$app->redis_currency->sadd('supported_symbol_currency', $content['response'][0]['base_currency'] . '_' . $content['response'][0]['quote_currency']);
$key = 'quotation_boc_' . 'CNY_' . $val->currency->symbol; }
$currency = rtrim(sprintf('%.6f', 1 / (str_replace("
", "", $e->innertext) / 100)), '0');
Yii::$app->redis_currency->hmset($key, 'low', $currency, 'high', $currency, 'last', $currency, 'open', $currency);
}
}
}
});
} }
echo date('Y-m-d H:i:s') . '计价货币更新成功' . PHP_EOL; echo date('Y-m-d H:i:s') . '计价货币更新成功' . PHP_EOL;
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment