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.
 
 
 
 

244 lines
8.3 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/10/8
  6. * Time: 17:02
  7. */
  8. require_once ROOT_PATH . '/ZZBS/Utils/util.class.php';
  9. class zzcsUtils extends utils
  10. {
  11. static function writeLog($log)
  12. {
  13. $backtrace = debug_backtrace();
  14. array_shift($backtrace);
  15. $dir = __DIR__ . "/../Log/";
  16. if (!is_dir($dir)) {
  17. mkdir($dir);
  18. }
  19. $filename = $dir . date("Y-m-d") . ".log";
  20. $need_chmod = file_exists($filename);
  21. $log_str = '[' . date("Y-m-d H:i:s") . '] [' . $backtrace[0]['class'] . '.class.php] [' . $backtrace[0]['class'] . '] [' . $backtrace[0]['function'] . '] [' . $log . ']';
  22. file_put_contents($filename, $log_str . PHP_EOL, FILE_APPEND);
  23. if ($need_chmod == false) {
  24. @chmod($filename, 0777);
  25. }
  26. }
  27. /*
  28. * 字段验证
  29. * @params String 字段名,支持格式'attr1,attr2,attr3'
  30. * @post Array 被验证的数组,默认为$_POST
  31. */
  32. static function validateParams($params, $post = '')
  33. {
  34. if ($post == '')
  35. $post = $_POST;
  36. $result = array(
  37. 'status' => true,
  38. 'info' => ''
  39. );
  40. foreach ($params as $attr => $type) {
  41. $list = explode(',', $attr);
  42. foreach ($list as $item) {
  43. $item = trim($item);
  44. if($item != ''){
  45. if (!isset($post[$item]) || $post[$item] == '') {
  46. $result['status'] = false;
  47. $result['info'] = '请填写所有必填项';
  48. return $result;
  49. } elseif (zzcsUtils::validate($post[$item], $type)) {
  50. continue;
  51. } else {
  52. $result['status'] = false;
  53. $result['info'] = ' 数据格式不正确';
  54. return $result;
  55. }
  56. }
  57. }
  58. }
  59. return $result;
  60. }
  61. /*
  62. * 验证字段是否合法,URL必须带http(s),合法return true,非法return false
  63. * @input mixed需要验证的参数
  64. * @type 需要的方法,Number, Float, Integer, ID, Email, Phone, URL, ShenFenZheng,
  65. */
  66. static function validate($input, $type = '')
  67. {
  68. $typeArr = explode(',', $type);
  69. foreach ($typeArr as $type) {
  70. switch (strtolower($type)) {
  71. case 'amount':
  72. $reg = '/^([1-9]\d*\.?\d{0,2}$)|0\.?\d{0,2}$/';
  73. $message = '金额';
  74. break;
  75. case 'number':
  76. $reg = '/^-?[0-9]*$/';
  77. $message = '数字';
  78. break;
  79. case 'email':
  80. $reg = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';
  81. $message = '邮箱';
  82. break;
  83. case 'telorphone': //正则不正确,需再完善
  84. $reg = '/^(d{3}-\d{8}|\d{4}-\d{7})|((0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8})$/';
  85. $message = '电话';
  86. break;
  87. case 'tel':
  88. $reg = '/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/';
  89. $message = '手机';
  90. break;
  91. case 'phone':
  92. $reg = '/^d{3}-\d{8}|\d{4}-\d{7}$/';
  93. $message = '电话';
  94. break;
  95. case 'url':
  96. $reg = '/^https?:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])$/i';
  97. $message = '链接';
  98. break;
  99. case 'card':
  100. $reg = '/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/';
  101. $message = '身份证';
  102. break;
  103. case 'dcode':
  104. $reg = '/^[1-9]\d{5}(?!\d)$/';
  105. $message = '邮编';
  106. break;
  107. case 'time':
  108. $reg = '/^(([0-1][0-9])|(2[0-3])):([0-5][0-9])$/';
  109. $message = '时间';
  110. break;
  111. case 'empty':
  112. default:
  113. $reg = '/^[\s\S]*$/';
  114. }
  115. $result = preg_match($reg, $input);
  116. if ($result == 0)
  117. return false;
  118. }
  119. return true;
  120. }
  121. /*
  122. * 上传多张图片,不确定图片张数,
  123. * 图片路径以逗号分隔,
  124. *
  125. */
  126. static function uploadMultiple($name, $dirPath = '../coding/resource/upload/car/', $realPath = '../../resource/upload/car/')
  127. {
  128. $maxSize = 2000000;
  129. $allow = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg');
  130. $path = '';
  131. $type = '';
  132. if (is_array($_FILES[$name]['name'])) { //多个文件
  133. foreach ($_FILES[$name]['name'] as $key => $item) {
  134. //图片不为空,且图片对应的类型也不为空
  135. if ($_POST['bus_img_type'][$key] != '' && $item != '' && in_array($_FILES[$name]['type'][$key], $allow) && $_FILES[$name]['size'][$key] <= $maxSize) {
  136. $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$name]['name'][$key]));
  137. move_uploaded_file($_FILES[$name]["tmp_name"][$key],
  138. __DIR__ . DIRECTORY_SEPARATOR . $dirPath . $newName);
  139. if ($path == '') {
  140. $path .= $realPath . $newName;
  141. $type .= $_POST['bus_img_type'][$key];
  142. } else {
  143. $path .= ',' . $realPath . $newName;
  144. $type .= ',' . $_POST['bus_img_type'][$key];
  145. }
  146. }
  147. }
  148. } else { //单个文件
  149. }
  150. if ($path != '')
  151. return array('path' => $path, 'type' => $type);
  152. else
  153. return false;
  154. }
  155. /*
  156. * 上传单个
  157. */
  158. static function uploadSingle()
  159. {
  160. }
  161. /*
  162. * 上传文件
  163. * @type 上传类型,主要是图片,还有附件rar, word, excel,还没有需求
  164. */
  165. static function upload($type, $file)
  166. {
  167. if ((($_FILES[$file]["type"] == "image/gif")
  168. || ($_FILES[$file]["type"] == "image/jpeg")
  169. || ($_FILES[$file]["type"] == "image/png")
  170. || ($_FILES[$file]["type"] == "image/pjpeg"))
  171. && ($_FILES[$file]["size"] < 200000)
  172. ) {
  173. if ($_FILES[$file]["error"] > 0) {
  174. return '';
  175. } else {
  176. $path = '../upload/car/';
  177. $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$file]["name"]));
  178. move_uploaded_file($_FILES[$file]["tmp_name"],
  179. __DIR__ . DIRECTORY_SEPARATOR . $path . $newName);
  180. return $path . $newName;
  181. }
  182. } else {
  183. return '';
  184. }
  185. }
  186. /*
  187. * 在upload目录上传文件时,初始化一个不存在的文件名
  188. */
  189. static function initFileName($path, $suffix)
  190. {
  191. $flag = true;
  192. while ($flag) {
  193. $name = time() . rand(10000, 99999);
  194. if (!file_exists("$path$name$suffix")) {
  195. $flag = false;
  196. }
  197. }
  198. return $name . $suffix;
  199. }
  200. static function getSuffix($imgName)//获取图像文件类型
  201. {
  202. if (preg_match("/\.(jpg|jpeg|gif|png|bmp)$/i", $imgName, $matches)) {
  203. $type = strtolower($matches[0]);
  204. } else {
  205. $type = "string";
  206. }
  207. return $type;
  208. }
  209. /*
  210. * 删除车辆之前存放的图片
  211. */
  212. static function deleteFiles($oldPath, $newPath)
  213. {
  214. if (is_string($oldPath))
  215. $oldPath = explode(',', $oldPath);
  216. if (is_string($newPath))
  217. $newPath = explode(',', $newPath);
  218. foreach ($oldPath as $item) {
  219. if (!in_array($item, $newPath)) {
  220. if (file_exists($item) && is_file($item))
  221. unlink($item);
  222. }
  223. }
  224. }
  225. }