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.
 
 
 
 
 
 

249 lines
8.5 KiB

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