| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace common\helpers;
- use Yii;
- use yii\httpclient\Client;
- use yii\redis\Connection;
- use backend\models\Config;
- class RateHelper
- {
- const RATE_URL = 'http://web.juhe.cn:8080/finance/exchange/rmbquot?key=05806e7b15ebbab91c1babceed3b468a';
- const RATE_CACHE_KEY = 'usdRate';
- /**
- * 获取入金 出金汇率
- * @param bool $refresh
- * @return mixed
- */
- public static function getRate($refresh = false)
- {
-
- $cache = \Yii::$app->redis;
- $lastCode = $cache->get('exchangeRate');
- $time = $cache->get('exchangeRate_time');
- $exchangeRate = new RateHelper();
- if (!empty($lastCode) && ($exchangeRate->microtime_float() - $time <= 86400000)) {
- // 从缓存中获取我们的汇率
- $usdRate['exchangeRate'] = $lastCode;
- }else{
- // 从新获取汇率
- $exchangeRate = new RateHelper();
- $exchangeNum = $exchangeRate->exchangeRate();
- if(isset($exchangeNum)){
- $usdRate['exchangeRate'] = round(6.84/$exchangeNum,2);
- $config = Config::findOne(1);
- $config['exchangerate'] = $usdRate['exchangeRate'];
- $config->setAttributes($config, false);
-
- //更新数据库中的数据,并且更新数据库数据的 缓存
- if ($config->save()) {
- $cache = \Yii::$app->cache;
- $cache->set('config', Config::findOne(1));
- }
-
- //更新redis
- $cache = \Yii::$app->redis;
- $cache->set('exchangeRate', round(6.84/$exchangeNum,2));
- $cache->set('exchangeRate_time', $exchangeRate->microtime_float());
- }else{
- $config = Config::findOne(1);
- $usdRate['exchangeRate'] = $config['exchangerate'];
- }
-
- }
- $usdRate['sellRate'] = 7.00;
- $usdRate['buyRate'] = 6.70;
-
- /*
- $usdRate = Yii::$app->getCache()->get(self::RATE_CACHE_KEY);
- if ($usdRate == false || $refresh == true) {
- $client = new Client();
- $success = false;
- for ($i = 0; $i < 2; $i++) {
- $response = $client->get(self::RATE_URL, [], ['Referer' => 'web.juhe.cn'])->send();
- if ($response->getIsOk()) {
- $json = $response->getContent();
- $jsonArr = @json_decode($json, true);
- if ($jsonArr['error_code'] == 0 && !empty($jsonArr['result'][0])) {
- foreach ((array)$jsonArr['result'][0] as $dataKey => $dataRow) {
- if ($dataRow['name'] == '美元') {
- // $usdRate['buyRate'] = number_format(round($dataRow['fBuyPri'] / 100, 3), 3, '.', '');
- $usdRate['sellRate'] = number_format(round($dataRow['fSellPri'] / 100, 3), 3, '.', '');
- $usdRate['buyRate'] = number_format($usdRate['sellRate'] - 0.11, 3, '.', ''); // 出金汇率=入金汇率-0.11
- Yii::$app->getCache()->set(self::RATE_CACHE_KEY, $usdRate, 3600);
- $success = true;
- break;
- }
- }
- }
- }
- if ($success == true) {
- break;
- }
- usleep(50000); // sleep 50ms
- }
- }
- */
- return $usdRate;
- }
- //获取港币和人民币之间的汇率
- public function exchangeRate(){
- $WebServiceURL = "http://www.unionpayintl.com/cardholderServ/serviceCenter/rate/search";
- $headers = array("Content-Type: application/x-www-form-urlencoded; charset=utf-8");
- $day = date("w");
- $today = array('2','3','4','5','6');
- if(in_array($day,$today)){
- $time = date("Y-m-d",strtotime("-1 day"));
- }else if($day==1){
- $time = date("Y-m-d",strtotime("-3 day"));
- }else if($day==0){
- $time = date("Y-m-d",strtotime("-2 day"));
- }
- $parameters = array('curDate' => $time,'baseCurrency'=>'CNY','transactionCurrency'=>'HKD');
- $temp = $parameters;
- foreach ($temp as $key => $value) {
- if ($value == NULL) {
- unset($temp[$key]);
- }
- }
- $postData = $this->buildParametersString($temp, '&');
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $WebServiceURL);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
- $responseText = curl_exec($ch);
- $result = json_decode($responseText,true);
- return $result['exchangeRate'];
- }
-
- function buildParametersString($parameters, $jointer='') {
- ksort($parameters);
- $temp = array();
- foreach ($parameters as $key => $value) {
- array_push($temp, $key.'='.$value);
- }
- return implode($jointer, $temp);
- }
-
- /**
- * 获取毫秒级时间戳
- */
- public function microtime_float()
- {
- list($t1, $t2) = explode(' ', microtime());
- return number_format(((floatval($t1) + floatval($t2)) * 1000), 0, '', '');
- }
- /**
- * @return null|Connection
- */
- protected static function getRedis()
- {
- return Yii::$app->get('redis');
- }
- }
|