RateHelper.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. use yii\httpclient\Client;
  5. use yii\redis\Connection;
  6. use backend\models\Config;
  7. class RateHelper
  8. {
  9. const RATE_URL = 'http://web.juhe.cn:8080/finance/exchange/rmbquot?key=05806e7b15ebbab91c1babceed3b468a';
  10. const RATE_CACHE_KEY = 'usdRate';
  11. /**
  12. * 获取入金 出金汇率
  13. * @param bool $refresh
  14. * @return mixed
  15. */
  16. public static function getRate($refresh = false)
  17. {
  18. $cache = \Yii::$app->redis;
  19. $lastCode = $cache->get('exchangeRate');
  20. $time = $cache->get('exchangeRate_time');
  21. $exchangeRate = new RateHelper();
  22. if (!empty($lastCode) && ($exchangeRate->microtime_float() - $time <= 86400000)) {
  23. // 从缓存中获取我们的汇率
  24. $usdRate['exchangeRate'] = $lastCode;
  25. }else{
  26. // 从新获取汇率
  27. $exchangeRate = new RateHelper();
  28. $exchangeNum = $exchangeRate->exchangeRate();
  29. if(isset($exchangeNum)){
  30. $usdRate['exchangeRate'] = round(6.84/$exchangeNum,2);
  31. $config = Config::findOne(1);
  32. $config['exchangerate'] = $usdRate['exchangeRate'];
  33. $config->setAttributes($config, false);
  34. //更新数据库中的数据,并且更新数据库数据的 缓存
  35. if ($config->save()) {
  36. $cache = \Yii::$app->cache;
  37. $cache->set('config', Config::findOne(1));
  38. }
  39. //更新redis
  40. $cache = \Yii::$app->redis;
  41. $cache->set('exchangeRate', round(6.84/$exchangeNum,2));
  42. $cache->set('exchangeRate_time', $exchangeRate->microtime_float());
  43. }else{
  44. $config = Config::findOne(1);
  45. $usdRate['exchangeRate'] = $config['exchangerate'];
  46. }
  47. }
  48. $usdRate['sellRate'] = 7.00;
  49. $usdRate['buyRate'] = 6.70;
  50. /*
  51. $usdRate = Yii::$app->getCache()->get(self::RATE_CACHE_KEY);
  52. if ($usdRate == false || $refresh == true) {
  53. $client = new Client();
  54. $success = false;
  55. for ($i = 0; $i < 2; $i++) {
  56. $response = $client->get(self::RATE_URL, [], ['Referer' => 'web.juhe.cn'])->send();
  57. if ($response->getIsOk()) {
  58. $json = $response->getContent();
  59. $jsonArr = @json_decode($json, true);
  60. if ($jsonArr['error_code'] == 0 && !empty($jsonArr['result'][0])) {
  61. foreach ((array)$jsonArr['result'][0] as $dataKey => $dataRow) {
  62. if ($dataRow['name'] == '美元') {
  63. // $usdRate['buyRate'] = number_format(round($dataRow['fBuyPri'] / 100, 3), 3, '.', '');
  64. $usdRate['sellRate'] = number_format(round($dataRow['fSellPri'] / 100, 3), 3, '.', '');
  65. $usdRate['buyRate'] = number_format($usdRate['sellRate'] - 0.11, 3, '.', ''); // 出金汇率=入金汇率-0.11
  66. Yii::$app->getCache()->set(self::RATE_CACHE_KEY, $usdRate, 3600);
  67. $success = true;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. if ($success == true) {
  74. break;
  75. }
  76. usleep(50000); // sleep 50ms
  77. }
  78. }
  79. */
  80. return $usdRate;
  81. }
  82. //获取港币和人民币之间的汇率
  83. public function exchangeRate(){
  84. $WebServiceURL = "http://www.unionpayintl.com/cardholderServ/serviceCenter/rate/search";
  85. $headers = array("Content-Type: application/x-www-form-urlencoded; charset=utf-8");
  86. $day = date("w");
  87. $today = array('2','3','4','5','6');
  88. if(in_array($day,$today)){
  89. $time = date("Y-m-d",strtotime("-1 day"));
  90. }else if($day==1){
  91. $time = date("Y-m-d",strtotime("-3 day"));
  92. }else if($day==0){
  93. $time = date("Y-m-d",strtotime("-2 day"));
  94. }
  95. $parameters = array('curDate' => $time,'baseCurrency'=>'CNY','transactionCurrency'=>'HKD');
  96. $temp = $parameters;
  97. foreach ($temp as $key => $value) {
  98. if ($value == NULL) {
  99. unset($temp[$key]);
  100. }
  101. }
  102. $postData = $this->buildParametersString($temp, '&');
  103. $ch = curl_init();
  104. curl_setopt($ch, CURLOPT_URL, $WebServiceURL);
  105. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  106. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  107. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  108. curl_setopt($ch, CURLOPT_POST, 1);
  109. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  110. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
  111. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  112. $responseText = curl_exec($ch);
  113. $result = json_decode($responseText,true);
  114. return $result['exchangeRate'];
  115. }
  116. function buildParametersString($parameters, $jointer='') {
  117. ksort($parameters);
  118. $temp = array();
  119. foreach ($parameters as $key => $value) {
  120. array_push($temp, $key.'='.$value);
  121. }
  122. return implode($jointer, $temp);
  123. }
  124. /**
  125. * 获取毫秒级时间戳
  126. */
  127. public function microtime_float()
  128. {
  129. list($t1, $t2) = explode(' ', microtime());
  130. return number_format(((floatval($t1) + floatval($t2)) * 1000), 0, '', '');
  131. }
  132. /**
  133. * @return null|Connection
  134. */
  135. protected static function getRedis()
  136. {
  137. return Yii::$app->get('redis');
  138. }
  139. }