Utils.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. class Utils
  5. {
  6. public static function getTopDomain($hostInfo = '')
  7. {
  8. $hostInfo = $hostInfo ? $hostInfo : Yii::$app->getRequest()->getHostInfo();
  9. $host = parse_url($hostInfo, PHP_URL_HOST);
  10. $hostArr = explode('.', $host);
  11. $hostArr = array_reverse($hostArr);
  12. if (count($hostArr) >= 2) {
  13. return "{$hostArr[1]}.{$hostArr[0]}";
  14. } else {
  15. return $host;
  16. }
  17. }
  18. public static function formatFloatOrInt($number, $decimals = 2)
  19. {
  20. if (!is_numeric($number)) {
  21. return $number;
  22. }
  23. if (intval($number) == $number) {
  24. return intval($number);
  25. } else {
  26. return number_format($number, $decimals, '.', '');
  27. }
  28. }
  29. /**
  30. * 获取客户端IP地址
  31. * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  32. * @return mixed
  33. */
  34. public static function getClientIp($type = 0)
  35. {
  36. $type = $type ? 1 : 0;
  37. static $ip = NULL;
  38. if ($ip !== NULL) return $ip[$type];
  39. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  40. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  41. $pos = array_search('unknown', $arr);
  42. if (false !== $pos) unset($arr[$pos]);
  43. $ip = trim($arr[0]);
  44. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  45. $ip = $_SERVER['HTTP_CLIENT_IP'];
  46. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  47. $ip = $_SERVER['REMOTE_ADDR'];
  48. }
  49. // IP地址合法验证
  50. $long = sprintf("%u", ip2long($ip));
  51. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  52. return $ip[$type];
  53. }
  54. /**
  55. * 判断程序当前的运行环境,Env为ENVIRONMENT
  56. * @return string
  57. */
  58. public static function getEnv()
  59. {
  60. $env = 'prod';
  61. if (!isset(Yii::$app->params['environment']) || !is_array(Yii::$app->params['environment'])) {
  62. return $env;
  63. }
  64. if (isset($_SERVER['ENVIRONMENT']) && in_array($_SERVER['ENVIRONMENT'], Yii::$app->params['environment'])) {
  65. // 在web服务器定义的环境
  66. $env = $_SERVER['ENVIRONMENT'];
  67. } elseif (isset(Yii::$app->params['stbXcrmHost']) && static::getTopDomain() === static::getTopDomain(Yii::$app->params['stbXcrmHost'])) {
  68. // stb顶级域名跟线上的相同
  69. $hostInfo = Yii::$app->getRequest()->getHostInfo();
  70. $host = parse_url($hostInfo, PHP_URL_HOST);
  71. $hostArr = explode('.', $host);
  72. $hostArr = array_reverse($hostArr);
  73. if (count($hostArr) >= 4 && in_array($hostArr[2], Yii::$app->params['environment'])) {
  74. $env = $hostArr[2];
  75. }
  76. } elseif (isset(Yii::$app->params['rcXcrmHost']) && static::getTopDomain() === static::getTopDomain(Yii::$app->params['rcXcrmHost'])) {
  77. // rc顶级域名不同
  78. $env = 'rc';
  79. }
  80. return $env;
  81. }
  82. /**
  83. * 根据不同的环境返回不同的crm域名
  84. * @return string
  85. */
  86. public static function getCrmHostByEnv()
  87. {
  88. $host = Yii::$app->params['xcrmHost'];
  89. if (isset(Yii::$app->params['stbXcrmHost']) && static::getEnv() == 'stb') {
  90. $host = Yii::$app->params['stbXcrmHost'];
  91. } elseif (isset(Yii::$app->params['rcXcrmHost']) && static::getEnv() == 'rc') {
  92. $host = Yii::$app->params['rcXcrmHost'];
  93. }
  94. return $host;
  95. }
  96. }