util_helper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/12/21
  6. * Time: 下午4:06
  7. */
  8. require_once (BASE_DATA_PATH . '/api/phpqrcode/phpqrcode.php');
  9. class util
  10. {
  11. const min_memno = 500;
  12. const max_memno = 2000;
  13. const passwd = 'xmmz@lrlz';
  14. public static function member_avatar($path)
  15. {
  16. if (is_null($path) || empty($path)) {
  17. $img = 'female.png';
  18. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  19. } else {
  20. if (strncasecmp($path, "http://", strlen("http://")) == 0) {
  21. $url = $path;
  22. } else {
  23. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  24. }
  25. return $url;
  26. }
  27. }
  28. public static function goods_thumb_image($path, $store_id)
  29. {
  30. return cthumb($path, 480, $store_id);
  31. }
  32. public static function goods_spec($item)
  33. {
  34. $keyval = unserialize($item);
  35. if($keyval == false) {
  36. return "";
  37. }
  38. foreach ($keyval as $key => $val) {
  39. return $val;
  40. }
  41. return "";
  42. }
  43. public static function nickname($nick, $mobile, $name)
  44. {
  45. if (isset($nick) && !empty($nick)) {
  46. return $nick;
  47. }
  48. if (isset($name) && !empty($name)) {
  49. return $name;
  50. }
  51. if (isset($mobile) && !empty($mobile)) {
  52. if (strlen($mobile) == 11) {
  53. return substr_replace($mobile, '****', 3, 4);
  54. }
  55. }
  56. return '';
  57. }
  58. public static function mem_no()
  59. {
  60. $number = self::mb_incr('member_no',1,self::min_memno);
  61. if($number > self::max_memno) {
  62. self::mb_del_incr('member_no');
  63. $start_number = mt_rand(self::min_memno,2 * self::min_memno);
  64. $number = self::mb_incr('member_no',1,$start_number);
  65. }
  66. return $number;
  67. }
  68. private static function mb_incr($key,$val,$def_value = 0)
  69. {
  70. return incrcache('global_incr',$key,$val,'mb_',$def_value);
  71. }
  72. private static function mb_del_incr($key)
  73. {
  74. return del_incr('global_incr',$key,'mb_');
  75. }
  76. public static function start_with($haystack, $needle)
  77. {
  78. $length = strlen($needle);
  79. return (substr($haystack, 0, $length) === $needle);
  80. }
  81. public static function end_with($haystack, $needle)
  82. {
  83. $length = strlen($needle);
  84. if ($length == 0) {
  85. return true;
  86. }
  87. return (substr($haystack, -$length) === $needle);
  88. }
  89. public static function imgsize($url)
  90. {
  91. if (util::start_with($url,BASE_SITE_URL) == false) {
  92. return @getimagesize($url);
  93. }
  94. else {
  95. $length = strlen(BASE_SITE_URL);
  96. $src = BASE_ROOT_PATH . substr($url, $length, strlen($url) - $length);
  97. $size = @getimagesize($src);
  98. if($size == false) {
  99. return @getimagesize($url);
  100. } else {
  101. return $size;
  102. }
  103. }
  104. }
  105. public static function http_add_params($url, $params)
  106. {
  107. if (!empty($params)) {
  108. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  109. }
  110. return $url;
  111. }
  112. public static function from_wechat()
  113. {
  114. $agent = request_helper::http_useragent();
  115. $pos = strpos($agent,"MicroMessenger");
  116. if($pos === false) {
  117. return false;
  118. } else {
  119. return ($pos >= 0);
  120. }
  121. }
  122. public static function encrypt_data($plaintext)
  123. {
  124. return encrypt("{$plaintext}",util::passwd);
  125. }
  126. public static function decrypt_data($ciphertext)
  127. {
  128. return decrypt($ciphertext,util::passwd);
  129. }
  130. public static function qrcode($url,$name)
  131. {
  132. if(empty($url) || empty($name)) return false;
  133. $save_path = BASE_UPLOAD_PATH . DS.ATTACH_MQRCODE.DS . $name;
  134. QRcode::png($url,$save_path,QR_ECLEVEL_H);
  135. return $save_path;
  136. }
  137. public static function qrcode_path($url,$save_path)
  138. {
  139. if(empty($url) || empty($save_path)) return false;
  140. QRcode::png($url,$save_path,QR_ECLEVEL_H);
  141. return $save_path;
  142. }
  143. public static function base64url_encode($plainText)
  144. {
  145. $base64 = base64_encode($plainText);
  146. $base64url = strtr($base64, ['+' => '-','/' => '_','=' => ',']);
  147. return $base64url;
  148. }
  149. public static function base64url_decode($plainText)
  150. {
  151. $base64url = strtr($plainText, ['-' => '+','_' => '/',',' => '=']);
  152. $base64 = base64_decode($base64url);
  153. return $base64;
  154. }
  155. public static function last_day_secs($input)
  156. {
  157. return strtotime(date('Y-m-d',$input)) + 86399;
  158. }
  159. }