You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
7.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Common;
  3. class Helper
  4. {
  5. //保留两位小数,最后一位会四舍五入
  6. public static function formatPrice($price)
  7. {
  8. return sprintf("%.2f",$price);
  9. }
  10. //验证是否是合法的手机号码
  11. public static function isValidMobile($mobile)
  12. {
  13. return preg_match('/^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/', $mobile);
  14. }
  15. //验证是否是合法中文
  16. public static function isValidChinese($word, $length = 16)
  17. {
  18. $pattern = "/(^[\x{4e00}-\x{9fa5}]+)/u";
  19. preg_match($pattern, $word, $match);
  20. if (!$match)
  21. {
  22. return false;
  23. }
  24. if (mb_strlen($match[1]) > $length)
  25. {
  26. return false;
  27. }
  28. return $match[1];
  29. }
  30. //验证是否是合法的身份证号,简单验证
  31. public static function isValidIdCardNo($idcard)
  32. {
  33. $length = strlen($idcard);
  34. //15位老身份证
  35. if ($length == 15)
  36. {
  37. if (checkdate(substr($idcard, 8, 2), substr($idcard, 10, 2), '19' . substr($idcard, 6, 2)))
  38. {
  39. return true;
  40. }
  41. }
  42. //18位二代身份证号
  43. if ($length == 18)
  44. {
  45. if (!checkdate(substr($idcard, 10, 2), substr($idcard, 12, 2), substr($idcard, 6, 4)))
  46. {
  47. return false;
  48. }
  49. $idcard = str_split($idcard);
  50. if (strtolower($idcard[17]) == 'x')
  51. {
  52. $idcard[17] = '10';
  53. }
  54. //加权求和
  55. $sum = 0;
  56. //加权因子
  57. $wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
  58. for ($i = 0; $i < 17; $i++)
  59. {
  60. $sum += $wi[$i] * $idcard[$i];
  61. }
  62. //得到验证码所位置
  63. $position = $sum % 11;
  64. //身份证验证位值 10代表X
  65. $code = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
  66. if ($idcard[17] == $code[$position])
  67. {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. //验证是否是合法的银行卡,不包含信用卡
  74. public static function isValidBankCard($card)
  75. {
  76. if (!is_numeric($card))
  77. {
  78. return false;
  79. }
  80. if (strlen($card) < 16 || strlen($card) > 19)
  81. {
  82. return false;
  83. }
  84. $cardHeader = [10, 18, 30, 35, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 60, 62, 65, 68, 69, 84, 87, 88, 94, 95, 98, 99];
  85. if (!in_array(substr($card, 0, 2), $cardHeader))
  86. {
  87. return false;
  88. }
  89. $numShouldCheck = str_split(substr($card, 0, -1));
  90. krsort($numShouldCheck);
  91. $odd = $odd['gt9'] = $odd['gt9']['tens'] = $odd['gt9']['unit'] = $odd['lt9'] = $even = [];
  92. array_walk($numShouldCheck, function ($item, $key) use (&$odd, &$even, $card){
  93. if ((strlen($card) == 16) && (substr($card, 0, 2) == '62'))
  94. {
  95. $key += 1;
  96. }
  97. if (($key & 1))
  98. {
  99. $t = $item * 2;
  100. if ($t > 9)
  101. {
  102. $odd['gt9']['unit'][] = intval($t % 10);
  103. $odd['gt9']['tens'][] = intval($t / 10);
  104. }
  105. else
  106. {
  107. $odd['lt9'][] = $t;
  108. }
  109. }
  110. else
  111. {
  112. $even[] = $item;
  113. }
  114. });
  115. $total = array_sum($even);
  116. array_walk_recursive($odd, function ($item, $key) use (&$total) {
  117. $total += $item;
  118. });
  119. $luhm = 10 - ($total % 10 == 0 ? 10 : $total % 10);
  120. $lastNumOfCard = substr($card, -1, 1);
  121. if ($luhm != $lastNumOfCard)
  122. {
  123. return false;
  124. }
  125. return true;
  126. }
  127. //随机字母
  128. public static function randLetter($len)
  129. {
  130. $letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  131. $result = '';
  132. for ($i = 0; $i < $len; $i++)
  133. {
  134. $result .= $letter[array_rand($letter, 1)];
  135. }
  136. return $result;
  137. }
  138. //生成二维码
  139. public static function qrcode($url,$size=150)
  140. {
  141. return 'data:image/png;base64,'.base64_encode(\QrCode::format('png')->encoding('UTF-8')->size($size)->margin(0)->errorCorrection('H')->generate($url));
  142. }
  143. //获取浏览器信息
  144. public static function getBrowser()
  145. {
  146. $browser = array('name'=>'unknown', 'version'=>'unknown');
  147. if(empty($_SERVER['HTTP_USER_AGENT'])) return $browser;
  148. $agent = $_SERVER["HTTP_USER_AGENT"];
  149. // Chrome should checked before safari
  150. if(strpos($agent, 'Firefox') !== false) $browser['name'] = "firefox";
  151. if(strpos($agent, 'Opera') !== false) $browser['name'] = 'opera';
  152. if(strpos($agent, 'Safari') !== false) $browser['name'] = 'safari';
  153. if(strpos($agent, 'Chrome') !== false) $browser['name'] = "chrome";
  154. // Check the name of browser
  155. if(strpos($agent, 'MSIE') !== false || strpos($agent, 'rv:11.0')) $browser['name'] = 'ie';
  156. if(strpos($agent, 'Edge') !== false) $browser['name'] = 'edge';
  157. // Check the version of browser
  158. if(preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  159. if(preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  160. if(preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  161. if(preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  162. if((strpos($agent, 'Chrome') == false) && preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs)) $browser['version'] = $regs[1];
  163. if(preg_match('/rv:(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  164. if(preg_match('/Edge\/(\d+)\..*/i', $agent, $regs)) $browser['version'] = $regs[1];
  165. return $browser;
  166. }
  167. /**
  168. * 检查是否是AJAX请求。
  169. * Check is ajax request.
  170. *
  171. * @static
  172. * @access public
  173. * @return bool
  174. */
  175. public static function isAjaxRequest()
  176. {
  177. if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
  178. if(isset($_GET['HTTP_X_REQUESTED_WITH']) && $_GET['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
  179. return false;
  180. }
  181. /**
  182. * 301跳转。
  183. * Header 301 Moved Permanently.
  184. *
  185. * @param string $locate
  186. * @access public
  187. * @return void
  188. */
  189. public static function header301($locate)
  190. {
  191. header('HTTP/1.1 301 Moved Permanently');
  192. die(header('Location:' . $locate));
  193. }
  194. /**
  195. * 获取远程IP。
  196. * Get remote ip.
  197. *
  198. * @access public
  199. * @return string
  200. */
  201. public static function getRemoteIp()
  202. {
  203. $ip = '';
  204. if(!empty($_SERVER["REMOTE_ADDR"])) $ip = $_SERVER["REMOTE_ADDR"];
  205. if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  206. if(!empty($_SERVER['HTTP_CLIENT_IP'])) $ip = $_SERVER['HTTP_CLIENT_IP'];
  207. return $ip;
  208. }
  209. /**
  210. * 建立文件夹
  211. *
  212. * @param string $aimUrl
  213. * @return viod
  214. */
  215. public static function createDir($aimUrl)
  216. {
  217. $aimUrl = str_replace('', '/', $aimUrl);
  218. $aimDir = '';
  219. $arr = explode('/', $aimUrl);
  220. $result = true;
  221. foreach ($arr as $str)
  222. {
  223. $aimDir .= $str . '/';
  224. if (!file_exists($aimDir))
  225. {
  226. $result = mkdir($aimDir);
  227. }
  228. }
  229. return $result;
  230. }
  231. //判断访问终端是否是微信浏览器
  232. public static function isWechatBrowser()
  233. {
  234. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false)
  235. {
  236. return true;
  237. }
  238. return false;
  239. }
  240. }