common.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. use think\Db;
  3. function IntToChr($index, $start = 65) {
  4. $str = '';
  5. if (floor($index / 26) > 0) {
  6. $str .= IntToChr(floor($index / 26)-1);
  7. }
  8. return $str . chr($index % 26 + $start);
  9. }
  10. /********************php验证身份证号码是否正确函数*********************/
  11. function is_idcard($id)
  12. {
  13. $id = strtoupper($id);
  14. $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
  15. $arr_split = array();
  16. if(!preg_match($regx, $id))
  17. {
  18. return FALSE;
  19. }
  20. if(15==strlen($id)) //检查15位
  21. {
  22. $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
  23. @preg_match($regx, $id, $arr_split);
  24. //检查生日日期是否正确
  25. $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  26. if(!strtotime($dtm_birth))
  27. {
  28. return FALSE;
  29. } else {
  30. return TRUE;
  31. }
  32. }
  33. else //检查18位
  34. {
  35. $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
  36. @preg_match($regx, $id, $arr_split);
  37. $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  38. if(!strtotime($dtm_birth)) //检查生日日期是否正确
  39. {
  40. return FALSE;
  41. }
  42. else
  43. {
  44. //检验18位身份证的校验码是否正确。
  45. //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  46. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  47. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  48. $sign = 0;
  49. for ( $i = 0; $i < 17; $i++ )
  50. {
  51. $b = (int) $id[$i];
  52. $w = $arr_int[$i];
  53. $sign += $b * $w;
  54. }
  55. $n = $sign % 11;
  56. $val_num = $arr_ch[$n];
  57. if ($val_num != substr($id,17, 1))
  58. {
  59. return FALSE;
  60. } //phpfensi.com
  61. else
  62. {
  63. return TRUE;
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * 下载文件到服务器
  70. * addtime 2020年8月28日 18:38:43
  71. */
  72. function getFile($url, $save_dir = '', $filename = '', $type = 0)
  73. {
  74. if (trim($url) == '') {
  75. return false;
  76. }
  77. if (trim($save_dir) == '') {
  78. $save_dir = './';
  79. }
  80. if (0 !== strrpos($save_dir, '/')) {
  81. $save_dir.= '/';
  82. }
  83. //创建保存目录
  84. if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
  85. return false;
  86. }
  87. //获取远程文件所采用的方法
  88. if ($type) {
  89. $ch = curl_init();
  90. $timeout = 5;
  91. curl_setopt($ch, CURLOPT_URL, $url);
  92. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  93. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  94. $content = curl_exec($ch);
  95. curl_close($ch);
  96. } else {
  97. ob_start();
  98. readfile($url);
  99. $content = ob_get_contents();
  100. ob_end_clean();
  101. }
  102. $size = strlen($content);
  103. //文件大小
  104. $fp2 = @fopen($save_dir . $filename, 'a');
  105. fwrite($fp2, $content);
  106. fclose($fp2);
  107. unset($content, $url);
  108. return $filename;
  109. }
  110. /**
  111. * 获取单个系统配置信息
  112. */
  113. function getConfigValue($name){
  114. return Db::name('system_config')->where('name',$name)->value('value');
  115. }
  116. /**
  117. * 调试函数
  118. */
  119. function debug($str,$name='debug'){
  120. $str = var_export($str,true).PHP_EOL;
  121. $str.= date('Y-m-d H:i:s').' ';
  122. $str.= $_SERVER['REMOTE_ADDR'].' ';
  123. $str.= $_SERVER['REQUEST_METHOD'].': ';
  124. // $str.=$_SERVER['HTTP_HOST'];
  125. $str.= $_SERVER['REQUEST_URI'].''.PHP_EOL.PHP_EOL;
  126. $str='<?php exit();?>'.PHP_EOL.$str;
  127. error_log($str,3,$name.".log");
  128. }
  129. /**
  130. * 获取前12个月
  131. */
  132. function monthpast(){
  133. $str = array();
  134. for ($i=0;$i<12;$i++){
  135. $str[$i] = date('Y-m',strtotime('+'.$i.'month',strtotime(date('2022-09'))));
  136. }
  137. return $str;
  138. }
  139. //计算概率
  140. function get_rand($proArr) {
  141. $result = '';
  142. //概率数组的总概率精度
  143. $proSum = array_sum($proArr);
  144. // var_dump($proSum);
  145. //概率数组循环
  146. foreach ($proArr as $key => $proCur) {
  147. if($proSum<1){
  148. continue;
  149. }
  150. $randNum = mt_rand(1, $proSum); //返回随机整数
  151. if ($randNum <= $proCur) {
  152. $result = $key;
  153. break;
  154. } else {
  155. $proSum -= $proCur;
  156. }
  157. }
  158. unset ($proArr);
  159. return $result;
  160. }