util_helper.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/12/21
  6. * Time: 下午4:06
  7. */
  8. class util
  9. {
  10. const min_memno = 500;
  11. const max_memno = 2000;
  12. public static function member_avatar($path)
  13. {
  14. if (is_null($path) || empty($path)) {
  15. $img = 'female.png';
  16. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  17. } else {
  18. if (strncasecmp($path, "http://", strlen("http://")) == 0) {
  19. $url = $path;
  20. } else {
  21. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  22. }
  23. return $url;
  24. }
  25. }
  26. public static function goods_thumb_image($path, $store_id)
  27. {
  28. return cthumb($path, 480, $store_id);
  29. }
  30. public static function goods_spec($item)
  31. {
  32. $keyval = unserialize($item);
  33. if($keyval == false) {
  34. return "";
  35. }
  36. foreach ($keyval as $key => $val) {
  37. return $val;
  38. }
  39. return "";
  40. }
  41. public static function nickname($nick, $mobile, $name)
  42. {
  43. if (isset($nick) && !empty($nick)) {
  44. return $nick;
  45. }
  46. if (isset($name) && !empty($name)) {
  47. return $name;
  48. }
  49. if (isset($mobile) && !empty($mobile)) {
  50. if (strlen($mobile) == 11) {
  51. return substr_replace($mobile, '****', 3, 4);
  52. }
  53. }
  54. return '';
  55. }
  56. public static function mem_no()
  57. {
  58. $number = self::mb_incr('member_no',1,self::min_memno);
  59. if($number > self::max_memno) {
  60. self::mb_del_incr('member_no');
  61. $start_number = mt_rand(self::min_memno,2 * self::min_memno);
  62. $number = self::mb_incr('member_no',1,$start_number);
  63. }
  64. return $number;
  65. }
  66. private static function mb_incr($key,$val,$def_value = 0)
  67. {
  68. return incrcache('global_incr',$key,$val,'mb_',$def_value);
  69. }
  70. private static function mb_del_incr($key)
  71. {
  72. return del_incr('global_incr',$key,'mb_');
  73. }
  74. public static function start_with($haystack, $needle)
  75. {
  76. $length = strlen($needle);
  77. return (substr($haystack, 0, $length) === $needle);
  78. }
  79. public static function end_with($haystack, $needle)
  80. {
  81. $length = strlen($needle);
  82. if ($length == 0) {
  83. return true;
  84. }
  85. return (substr($haystack, -$length) === $needle);
  86. }
  87. public static function imgsize($url)
  88. {
  89. if (util::start_with($url,BASE_SITE_URL) == false) {
  90. return @getimagesize($url);
  91. }
  92. else {
  93. $length = strlen(BASE_SITE_URL);
  94. $src = BASE_ROOT_PATH . substr($url, $length, strlen($url) - $length);
  95. $size = @getimagesize($src);
  96. if($size == false) {
  97. return @getimagesize($url);
  98. } else {
  99. return $size;
  100. }
  101. }
  102. }
  103. public static function http_add_params($url, $params)
  104. {
  105. if (!empty($params)) {
  106. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  107. }
  108. return $url;
  109. }
  110. public static function from_wechat()
  111. {
  112. $agent = request_helper::http_useragent();
  113. $pos = strpos($agent,"MicroMessenger");
  114. return ($pos >= 0);
  115. }
  116. }