| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace app\common\library;
- use think\Cache;
- use think\Hook;
- /**
- * 根据汇率转换金额
- * Class CoinRate
- * @package app\common\library
- */
- class CoinRate
- {
- protected static $apiUrl = 'https://api.it120.cc/gooking/forex/rate?fromCode=%s&toCode=%s';
- protected static $apiUrlRp = 'https://webapi.huilv.cc/api/exchange?num=%s&chiyouhuobi=%s&duihuanhuobi=%s&type=1&_=%s';
- /**
- * 转换汇率金额
- * @param $amount
- * @param string $from
- * @param string $to
- * @return float|mixed
- */
- public static function transfer($amount, $from='CNY', $to='USD')
- {
- if(empty($from) || empty($to)){
- return $amount;
- }
- $cacheKey = "cache:rates:{$from}_{$to}";
- $data = Cache::get($cacheKey);
- $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
- $expired = isset($data['expired'])? intval($data['expired']) : 0;
- if($rate<=0 || $expired<time()){
- $result = curl_api_get(sprintf(self::$apiUrl, $to, $from));
- $result = $result? json_decode($result, true) : [];
- $data = isset($result['data'])? $result['data'] : [];
- $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
- if($rate > 0){
- $data['expired'] = time() + 300;
- Cache::set($cacheKey, $data, time() + 300);
- }
- }
- return $rate>0? floatval($rate * $amount) : $amount;
- }
- /**
- * 转换汇率金额(卢比)
- * @param $amount
- * @param string $from
- * @param string $to
- * @return float|mixed
- */
- public static function transferRp($amount, $from='CNY', $to='IDR')
- {
- if(empty($from) || empty($to)){
- return $amount;
- }
- $cacheKey = "cache:rates:{$from}_{$to}";
- $data = Cache::get($cacheKey);
- $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
- $expired = isset($data['expired'])? intval($data['expired']) : 0;
- if($rate<=0 || $expired<time()){
- $result = curl_api_get(sprintf(self::$apiUrlRp, 1, $from, $to, time()));
- $result = $result? json_decode($result, true) : [];
- $rate = isset($result['dangqianhuilv'])? floatval($result['dangqianhuilv']) : 0.00;
- if($rate > 0){
- $data['expired'] = time() + 300;
- Cache::set($cacheKey, $data, time() + 300);
- }
- }
- return $rate>0? floatval($rate * $amount) : $amount;
- }
- /**
- * 转换汇率金额
- * @param $amount
- * @param string $from
- * @param string $to
- * @return float|mixed
- */
- public static function getRate($from='CNY', $to='USD')
- {
- $cacheKey = "cache:rates:{$from}_{$to}";
- $data = Cache::get($cacheKey);
- $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
- $expired = isset($data['expired'])? intval($data['expired']) : 0;
- if($rate<=0 || $expired<time()){
- $result = curl_api_get(sprintf(self::$apiUrl, $to, $from));
- $result = $result? json_decode($result, true) : [];
- $data = isset($result['data'])? $result['data'] : [];
- $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
- if($rate > 0){
- $data['expired'] = time() + 300;
- Cache::set($cacheKey, $data, time() + 300);
- }
- }
- return $rate;
- }
- }
- ?>
|