|
- <?php
- /**
- * Created by PhpStorm.
- * User: Steven
- * Date: 2016/10/8
- * Time: 17:02
- */
- namespace common\models;
- //use common\models\Utils;
-
- class zzcsUtils extends Utils
- {
- static function writeLog($log)
- {
- $backtrace = debug_backtrace();
- array_shift($backtrace);
- $dir = __DIR__ . "/../Log/";
- if (!is_dir($dir)) {
- mkdir($dir);
- }
- $filename = $dir . date("Y-m-d") . ".log";
- $need_chmod = file_exists($filename);
- $log_str = '[' . date("Y-m-d H:i:s") . '] [' . $_SERVER['SERVER_ADDR'] . '] [' . $backtrace[0]['class'] . '.class.php] [' . $backtrace[0]['class'] . '] [' . $backtrace[0]['function'] . '] [' . $log . ']';
- file_put_contents($filename, $log_str . PHP_EOL, FILE_APPEND);
- if ($need_chmod == false) {
- @chmod($filename, 0777);
- }
- }
-
- /*
- * 字段验证
- * @params String 字段名,支持格式'attr1,attr2,attr3'
- * @post Array 被验证的数组,默认为$_POST
- */
- static function validateParams($params, $post = '')
- {
- if ($post == '')
- $post = $_POST;
- $result = array(
- 'status' => true,
- 'info' => ''
- );
- foreach ($params as $attr => $type) {
- $list = explode(',', $attr);
- foreach ($list as $item) {
- $item = trim($item);
- if ($item != '') {
- if (!isset($post[$item]) || $post[$item] == '') {
- $result['status'] = false;
- $result['info'] = '请填写所有必填项';
- return $result;
- } elseif (zzcsUtils::validate($post[$item], $type)) {
- continue;
-
- } else {
- $result['status'] = false;
- $result['info'] = ' 数据格式不正确';
- return $result;
- }
- }
-
- }
- }
- return $result;
- }
-
- /*
- * 验证字段是否合法,URL必须带http(s),合法return true,非法return false
- * @input mixed需要验证的参数
- * @type 需要的方法,Number, Float, Integer, ID, Email, Phone, URL, ShenFenZheng,
- */
- static function validate($input, $type = '')
- {
- $typeArr = explode(',', $type);
- foreach ($typeArr as $type) {
- switch (strtolower($type)) {
- case 'amount':
- $reg = '/^([1-9]\d*\.?\d{0,2}$)|0\.?\d{0,2}$/';
- $message = '金额';
- break;
- case 'number':
- $reg = '/^-?[0-9]*$/';
- $message = '数字';
- break;
- case 'email':
- $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])?$/';
- $message = '邮箱';
- break;
- case 'telorphone': //正则不正确,需再完善
- $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})$/';
- $message = '电话';
- break;
- case 'tel':
- $reg = '/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/';
- $message = '手机';
- break;
- case 'phone':
- $reg = '/^d{3}-\d{8}|\d{4}-\d{7}$/';
- $message = '电话';
- break;
- case 'url':
- $reg = '/^https?:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])$/i';
- $message = '链接';
- break;
- case 'card':
- $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])$)$/';
- $message = '身份证';
- break;
- case 'dcode':
- $reg = '/^[1-9]\d{5}(?!\d)$/';
- $message = '邮编';
- break;
- case 'time':
- $reg = '/^(([0-1][0-9])|(2[0-3])):([0-5][0-9])$/';
- $message = '时间';
- break;
- case 'empty':
- default:
- $reg = '/^[\s\S]*$/';
-
- }
-
- $result = preg_match($reg, $input);
- if ($result == 0)
- return false;
- }
-
- return true;
- }
-
- /*
- * 上传多张图片,不确定图片张数,
- * 图片路径以逗号分隔,
- *
- */
- static function uploadMultiple($name, $dirPath = '', $realPath = '')
- {
- $dirPath = $_SERVER['DOCUMENT_ROOT'] . '/resource/zzcs/upload/car/';
- $realPath = '/resource/zzcs/upload/car/';
- $maxSize = 2000000;
- $allow = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg');
- $path = '';
- $type = '';
- if (isset($_FILES[$name]) && is_array($_FILES[$name]['name'])) { //多个文件
- foreach ($_FILES[$name]['name'] as $key => $item) {
- //图片不为空,且图片对应的类型也不为空
- if ($item != '' && $_POST['bus_img_type'][$key] != '' && in_array($_FILES[$name]['type'][$key], $allow) && $_FILES[$name]['size'][$key] <= $maxSize) {
- $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$name]['name'][$key]));
- /*move_uploaded_file($_FILES[$name]["tmp_name"][$key],
- __DIR__ . DIRECTORY_SEPARATOR . $dirPath . $newName);*/
- move_uploaded_file($_FILES[$name]["tmp_name"][$key],
- $dirPath . $newName);
-
- if ($path == '') {
- $path .= $realPath . $newName;
- $type .= $_POST['bus_img_type'][$key];
- } else {
- $path .= ',' . $realPath . $newName;
- $type .= ',' . $_POST['bus_img_type'][$key];
-
- }
- }
- }
- } else { //单个文件
-
- }
- if ($path != '')
- return array('path' => $path, 'type' => $type);
- else
- return false;
- }
-
- /*
- * 上传单个
- */
- static function uploadSingle()
- {
-
- }
-
- /*
- * 上传文件
- * @type 上传类型,主要是图片,还有附件rar, word, excel,还没有需求
- */
- static function upload($type, $file)
- {
- if ((($_FILES[$file]["type"] == "image/gif")
- || ($_FILES[$file]["type"] == "image/jpeg")
- || ($_FILES[$file]["type"] == "image/png")
- || ($_FILES[$file]["type"] == "image/pjpeg"))
- && ($_FILES[$file]["size"] < 200000)
- ) {
- if ($_FILES[$file]["error"] > 0) {
- return '';
- } else {
- $path = '../upload/car/';
- $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$file]["name"]));
- move_uploaded_file($_FILES[$file]["tmp_name"],
- __DIR__ . DIRECTORY_SEPARATOR . $path . $newName);
- return $path . $newName;
- }
- } else {
- return '';
- }
- }
-
- /*
- * 在upload目录上传文件时,初始化一个不存在的文件名
- */
- static function initFileName($path, $suffix)
- {
- $flag = true;
-
- while ($flag) {
- $name = time() . rand(10000, 99999);
- if (!file_exists("$path$name$suffix")) {
- $flag = false;
- }
- }
- return $name . $suffix;
- }
-
- static function getSuffix($imgName)//获取图像文件类型
- {
- if (preg_match("/\.(jpg|jpeg|gif|png|bmp)$/i", $imgName, $matches)) {
- $type = strtolower($matches[0]);
- } else {
- $type = "string";
- }
- return $type;
- }
-
- /*
- * 删除车辆之前存放的图片
- */
- static function deleteFiles($oldPath, $newPath)
- {
- if (is_string($oldPath))
- $oldPath = explode(',', $oldPath);
- if (is_string($newPath))
- $newPath = explode(',', $newPath);
- foreach ($oldPath as $item) {
- if (!in_array($item, $newPath)) {
- if (file_exists($item) && is_file($item))
- unlink($item);
- }
- }
- }
- }
|