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.

277 lines
9.9 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/10/8
  6. * Time: 17:02
  7. */
  8. class zzcsUtils
  9. {
  10. static function writeLog($log)
  11. {
  12. $dir = __DIR__ . "/../Log/";
  13. if (!is_dir($dir)) {
  14. mkdir($dir);
  15. }
  16. $filename = $dir . date("Y-m-d") . ".log";
  17. $need_chmod = file_exists($filename);
  18. $log_str = '[' . date("Y-m-d H:i:s") . ']';
  19. file_put_contents($filename, $log_str . PHP_EOL, FILE_APPEND);
  20. if ($need_chmod == false) {
  21. @chmod($filename, 0777);
  22. }
  23. }
  24. static function httpRequest($url, $data = null)
  25. {
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_URL, $url);
  28. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  29. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  30. if (!empty($data)) {
  31. curl_setopt($ch, CURLOPT_POST, 1);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  33. }
  34. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  35. $output = curl_exec($ch);
  36. curl_close($ch);
  37. return $output;
  38. }
  39. /**
  40. * 发送HTTP请求
  41. * @param $url
  42. * @param array $param
  43. * @return mixed
  44. * @throws Exception
  45. */
  46. static function httpsPost($url, $param = array())
  47. {
  48. $ch = curl_init(); // 初始化一个 cURL 对象
  49. curl_setopt($ch, CURLOPT_URL, $url); // 设置需要抓取的URL
  50. curl_setopt($ch, CURLOPT_HEADER, 0); // // 设置header
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
  52. // 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。
  53. curl_setopt($ch, CURLOPT_POST, 1);
  54. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); // 传递一个作为HTTP “POST”操作的所有数据的字符串。//http_build_query:生成 URL-encode 之后的请求字符串
  55. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  57. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  58. 'Content-type:application/x-www-form-urlencoded;charset=utf-8'
  59. ));
  60. $rtn = curl_exec($ch); // 运行cURL,请求网页
  61. if ($errno = curl_errno($ch)) {
  62. throw new Exception ('Curl Error(' . $errno . '):' . curl_error($ch));
  63. }
  64. curl_close($ch); // 关闭URL请求
  65. return $rtn; // 返回获取的数据
  66. }
  67. /*
  68. * 字段验证
  69. * @params String 字段名,支持格式'attr1,attr2,attr3'
  70. * @post Array 被验证的数组,默认为$_POST
  71. */
  72. static function validateParams($params, $post = '')
  73. {
  74. if ($post == '')
  75. $post = $_POST;
  76. $result = array(
  77. 'status' => true,
  78. 'info' => ''
  79. );
  80. foreach ($params as $attr => $type) {
  81. $list = explode(',', $attr);
  82. foreach ($list as $item) {
  83. $item = trim($item);
  84. if($item != ''){
  85. if (!isset($post[$item]) || $post[$item] == '') {
  86. $result['status'] = false;
  87. $result['info'] = '请填写所有必填项';
  88. return $result;
  89. } elseif (zzcsUtils::validate($post[$item], $type)) {
  90. continue;
  91. } else {
  92. $result['status'] = false;
  93. $result['info'] = ' 数据格式不正确';
  94. return $result;
  95. }
  96. }
  97. }
  98. }
  99. return $result;
  100. }
  101. /*
  102. * 验证字段是否合法,URL必须带http(s),合法return true,非法return false
  103. * @input mixed需要验证的参数
  104. * @type 需要的方法,Number, Float, Integer, ID, Email, Phone, URL, ShenFenZheng,
  105. */
  106. static function validate($input, $type = '')
  107. {
  108. $typeArr = explode(',', $type);
  109. foreach ($typeArr as $type) {
  110. switch (strtolower($type)) {
  111. case 'amount':
  112. $reg = '/^([1-9]\d*\.?\d{0,2}$)|0\.?\d{0,2}$/';
  113. $message = '金额';
  114. break;
  115. case 'number':
  116. $reg = '/^-?[0-9]*$/';
  117. $message = '数字';
  118. break;
  119. case 'email':
  120. $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])?$/';
  121. $message = '邮箱';
  122. break;
  123. case 'telorphone': //正则不正确,需再完善
  124. $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})$/';
  125. $message = '电话';
  126. break;
  127. case 'tel':
  128. $reg = '/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/';
  129. $message = '手机';
  130. break;
  131. case 'phone':
  132. $reg = '/^d{3}-\d{8}|\d{4}-\d{7}$/';
  133. $message = '电话';
  134. break;
  135. case 'url':
  136. $reg = '/^https?:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])$/i';
  137. $message = '链接';
  138. break;
  139. case 'card':
  140. $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])$)$/';
  141. $message = '身份证';
  142. break;
  143. case 'dcode':
  144. $reg = '/^[1-9]\d{5}(?!\d)$/';
  145. $message = '邮编';
  146. break;
  147. case 'time':
  148. $reg = '/^(([0-1][0-9])|(2[0-3])):([0-5][0-9])$/';
  149. $message = '时间';
  150. break;
  151. case 'empty':
  152. default:
  153. $reg = '/^[\s\S]*$/';
  154. }
  155. $result = preg_match($reg, $input);
  156. if ($result == 0)
  157. return false;
  158. }
  159. return true;
  160. }
  161. /*
  162. * 上传多张图片,不确定图片张数,
  163. * 图片路径以逗号分隔,
  164. *
  165. */
  166. static function uploadMultiple($name, $dirPath = '../coding/resource/upload/car/', $realPath = '../../resource/upload/car/')
  167. {
  168. $maxSize = 2000000;
  169. $allow = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg');
  170. $path = '';
  171. $type = '';
  172. if (is_array($_FILES[$name]['name'])) { //多个文件
  173. foreach ($_FILES[$name]['name'] as $key => $item) {
  174. //图片不为空,且图片对应的类型也不为空
  175. if ($_POST['bus_img_type'][$key] != '' && $item != '' && in_array($_FILES[$name]['type'][$key], $allow) && $_FILES[$name]['size'][$key] <= $maxSize) {
  176. $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$name]['name'][$key]));
  177. move_uploaded_file($_FILES[$name]["tmp_name"][$key],
  178. __DIR__ . DIRECTORY_SEPARATOR . $dirPath . $newName);
  179. if ($path == '') {
  180. $path .= $realPath . $newName;
  181. $type .= $_POST['bus_img_type'][$key];
  182. } else {
  183. $path .= ',' . $realPath . $newName;
  184. $type .= ',' . $_POST['bus_img_type'][$key];
  185. }
  186. }
  187. }
  188. } else { //单个文件
  189. }
  190. if ($path != '')
  191. return array('path' => $path, 'type' => $type);
  192. else
  193. return false;
  194. }
  195. /*
  196. * 上传文件
  197. * @type 上传类型,主要是图片,还有附件rar, word, excel,还没有需求
  198. */
  199. static function upload($type, $file)
  200. {
  201. if ((($_FILES[$file]["type"] == "image/gif")
  202. || ($_FILES[$file]["type"] == "image/jpeg")
  203. || ($_FILES[$file]["type"] == "image/png")
  204. || ($_FILES[$file]["type"] == "image/pjpeg"))
  205. && ($_FILES[$file]["size"] < 200000)
  206. ) {
  207. if ($_FILES[$file]["error"] > 0) {
  208. return '';
  209. } else {
  210. $path = '../upload/car/';
  211. $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$file]["name"]));
  212. move_uploaded_file($_FILES[$file]["tmp_name"],
  213. __DIR__ . DIRECTORY_SEPARATOR . $path . $newName);
  214. return $path . $newName;
  215. }
  216. } else {
  217. return '';
  218. }
  219. }
  220. /*
  221. * 在upload目录上传文件时,初始化一个不存在的文件名
  222. */
  223. static function initFileName($path, $suffix)
  224. {
  225. $flag = true;
  226. while ($flag) {
  227. $name = time() . rand(10000, 99999);
  228. if (!file_exists("$path$name$suffix")) {
  229. $flag = false;
  230. }
  231. }
  232. return $name . $suffix;
  233. }
  234. static function getSuffix($imgName)//获取图像文件类型
  235. {
  236. if (preg_match("/\.(jpg|jpeg|gif|png|bmp)$/i", $imgName, $matches)) {
  237. $type = strtolower($matches[0]);
  238. } else {
  239. $type = "string";
  240. }
  241. return $type;
  242. }
  243. /*
  244. * 删除车辆之前存放的图片
  245. */
  246. static function deleteFiles($oldPath, $newPath)
  247. {
  248. if (is_string($oldPath))
  249. $oldPath = explode(',', $oldPath);
  250. if (is_string($newPath))
  251. $newPath = explode(',', $newPath);
  252. foreach ($oldPath as $item) {
  253. if (!in_array($item, $newPath)) {
  254. if (file_exists($item) && is_file($item))
  255. unlink($item);
  256. }
  257. }
  258. }
  259. }