CoinRate.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\common\library;
  3. use think\Cache;
  4. use think\Hook;
  5. /**
  6. * 根据汇率转换金额
  7. * Class CoinRate
  8. * @package app\common\library
  9. */
  10. class CoinRate
  11. {
  12. protected static $apiUrl = 'https://api.it120.cc/gooking/forex/rate?fromCode=%s&toCode=%s';
  13. protected static $apiUrlRp = 'https://webapi.huilv.cc/api/exchange?num=%s&chiyouhuobi=%s&duihuanhuobi=%s&type=1&_=%s';
  14. /**
  15. * 转换汇率金额
  16. * @param $amount
  17. * @param string $from
  18. * @param string $to
  19. * @return float|mixed
  20. */
  21. public static function transfer($amount, $from='CNY', $to='USD')
  22. {
  23. if(empty($from) || empty($to)){
  24. return $amount;
  25. }
  26. $cacheKey = "cache:rates:{$from}_{$to}";
  27. $data = Cache::get($cacheKey);
  28. $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
  29. $expired = isset($data['expired'])? intval($data['expired']) : 0;
  30. if($rate<=0 || $expired<time()){
  31. $result = curl_api_get(sprintf(self::$apiUrl, $to, $from));
  32. $result = $result? json_decode($result, true) : [];
  33. $data = isset($result['data'])? $result['data'] : [];
  34. $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
  35. if($rate > 0){
  36. $data['expired'] = time() + 300;
  37. Cache::set($cacheKey, $data, time() + 300);
  38. }
  39. }
  40. return $rate>0? floatval($rate * $amount) : $amount;
  41. }
  42. /**
  43. * 转换汇率金额(卢比)
  44. * @param $amount
  45. * @param string $from
  46. * @param string $to
  47. * @return float|mixed
  48. */
  49. public static function transferRp($amount, $from='CNY', $to='IDR')
  50. {
  51. if(empty($from) || empty($to)){
  52. return $amount;
  53. }
  54. $cacheKey = "cache:rates:{$from}_{$to}";
  55. $data = Cache::get($cacheKey);
  56. $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
  57. $expired = isset($data['expired'])? intval($data['expired']) : 0;
  58. if($rate<=0 || $expired<time()){
  59. $result = curl_api_get(sprintf(self::$apiUrlRp, 1, $from, $to, time()));
  60. $result = $result? json_decode($result, true) : [];
  61. $rate = isset($result['dangqianhuilv'])? floatval($result['dangqianhuilv']) : 0.00;
  62. if($rate > 0){
  63. $data['expired'] = time() + 300;
  64. Cache::set($cacheKey, $data, time() + 300);
  65. }
  66. }
  67. return $rate>0? floatval($rate * $amount) : $amount;
  68. }
  69. /**
  70. * 转换汇率金额
  71. * @param $amount
  72. * @param string $from
  73. * @param string $to
  74. * @return float|mixed
  75. */
  76. public static function getRate($from='CNY', $to='USD')
  77. {
  78. $cacheKey = "cache:rates:{$from}_{$to}";
  79. $data = Cache::get($cacheKey);
  80. $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
  81. $expired = isset($data['expired'])? intval($data['expired']) : 0;
  82. if($rate<=0 || $expired<time()){
  83. $result = curl_api_get(sprintf(self::$apiUrl, $to, $from));
  84. $result = $result? json_decode($result, true) : [];
  85. $data = isset($result['data'])? $result['data'] : [];
  86. $rate = isset($data['rate'])? floatval($data['rate']) : 0.00;
  87. if($rate > 0){
  88. $data['expired'] = time() + 300;
  89. Cache::set($cacheKey, $data, time() + 300);
  90. }
  91. }
  92. return $rate;
  93. }
  94. }
  95. ?>