= $storeBrokeragePrice; } } } if (!function_exists('time_tran')) { /** * 时间戳人性化转化 * @param $time * @return string */ function time_tran($time) { $t = time() - $time; $f = array( '31536000' => '年', '2592000' => '个月', '604800' => '星期', '86400' => '天', '3600' => '小时', '60' => '分钟', '1' => '秒' ); foreach ($f as $k => $v) { if (0 != $c = floor($t / (int)$k)) { return $c . $v . '前'; } } } } if (!function_exists('url_to_path')) { /** * url转换路径 * @param $url * @return string */ function url_to_path($url) { $path = trim(str_replace('/', DS, $url), DS); if (0 !== strripos($path, 'public')) $path = 'public' . DS . $path; return app()->getRootPath() . $path; } } if (!function_exists('path_to_url')) { /** * 路径转url路径 * @param $path * @return string */ function path_to_url($path) { return trim(str_replace(DS, '/', $path), '.'); } } if (!function_exists('get_image_thumb')) { /** * 获取缩略图 * @param $filePath * @param string $type all|big|mid|small * @param bool $is_remote_down * @return mixed|string|string[] */ function get_image_thumb($filePath, string $type = 'all', bool $is_remote_down = false) { if (!$filePath || !is_string($filePath) || strpos($filePath, '?') !== false) return $filePath; try { $arr = explode('.', $filePath); $ext_name = trim($arr[count($arr) - 1]); if (!in_array($ext_name, ['png', 'jpg', 'jpeg'])) { return $filePath; } $upload = UploadService::getOssInit($filePath, $is_remote_down); $data = $upload->thumb('', $type); $image = $type == 'all' ? $data : $data[$type] ?? $filePath; } catch (\Throwable $e) { $image = $filePath; // throw new ValidateException($e->getMessage()); \think\facade\Log::error('获取缩略图失败,原因:' . $e->getMessage() . '----' . $e->getFile() . '----' . $e->getLine() . '----' . $filePath); } $data = parse_url($image); if (!isset($data['host']) && (substr($image, 0, 2) == './' || substr($image, 0, 1) == '/')) {//不是完整地址 $image = sys_config('site_url') . $image; } //请求是https 图片是http 需要改变图片地址 if (strpos(request()->domain(), 'https:') !== false && strpos($image, 'https:') === false) { $image = str_replace('http:', 'https:', $image); } return $image; } } if (!function_exists('get_thumb_water')) { /** * 处理数组获取缩略图、水印 * @param $list * @param string $type * @param array|string[] $field 1、['image','images'] type 取值参数:type 2、['small'=>'image','mid'=>'images'] type 取field数组的key * @param bool $is_remote_down * @return array|mixed|string|string[] */ function get_thumb_water($list, string $type = 'small', array $field = ['image'], bool $is_remote_down = false) { if (!$list || !$field) return $list; $baseType = $type; $data = $list; if (is_string($list)) { $field = [$type => 'image']; $data = ['image' => $list]; } if (is_array($data)) { foreach ($field as $type => $key) { if (is_integer($type)) {//索引数组,默认type $type = $baseType; } //一维数组 if (isset($data[$key])) { if (is_array($data[$key])) { $path_data = []; foreach ($data[$key] as $k => $path) { $path_data[] = get_image_thumb($path, $type, $is_remote_down); } $data[$key] = $path_data; } else { $data[$key] = get_image_thumb($data[$key], $type, $is_remote_down); } } else { foreach ($data as &$item) { if (!isset($item[$key])) continue; if (is_array($item[$key])) { $path_data = []; foreach ($item[$key] as $k => $path) { $path_data[] = get_image_thumb($path, $type, $is_remote_down); } $item[$key] = $path_data; } else { $item[$key] = get_image_thumb($item[$key], $type, $is_remote_down); } } } } } return is_string($list) ? ($data['image'] ?? '') : $data; } } if (!function_exists('put_image')) { /** * 获取图片转为base64 * @param string $avatar * @return bool|string */ function put_image($url, $filename = '') { if ($url == '') { return false; } try { if ($filename == '') { $ext = pathinfo($url); if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") { return false; } $filename = time() . "." . $ext['extension']; } $pathArr = parse_url($url); $path = $pathArr['path'] ?? ''; if ($path && file_exists(public_path() . trim($path, '/'))) { return $path; } else { //文件保存路径 ob_start(); $url = str_replace('phar://', '', $url); readfile($url); $img = ob_get_contents(); ob_end_clean(); $path = 'uploads/qrcode'; $fp2 = fopen(public_path() . $path . '/' . $filename, 'a'); fwrite($fp2, $img); fclose($fp2); return $path . '/' . $filename; } } catch (\Exception $e) { return false; } } } if (!function_exists('make_path')) { /** * 上传路径转化,默认路径 * @param $path * @param int $type * @param bool $force * @return string */ function make_path($path, int $type = 2, bool $force = false) { $path = DS . ltrim(rtrim($path)); switch ($type) { case 1: $path .= DS . date('Y'); break; case 2: $path .= DS . date('Y') . DS . date('m'); break; case 3: $path .= DS . date('Y') . DS . date('m') . DS . date('d'); break; } try { if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) { return trim(str_replace(DS, '/', $path), '.'); } else return ''; } catch (\Exception $e) { if ($force) throw new \Exception($e->getMessage()); return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS; } } } if (!function_exists('check_phone')) { /** * 手机号验证 * @param $phone * @return false|int */ function check_phone($phone) { return preg_match("/^1[3456789]\d{9}$/", $phone); } } if (!function_exists('check_mail')) { /** * 邮箱验证 * @param $mail * @return false|int */ function check_mail($mail) { if (filter_var($mail, FILTER_VALIDATE_EMAIL)) { return true; } else { return false; } } } if (!function_exists('aj_captcha_check_one')) { /** * 验证滑块1次验证 * @param string $token * @param string $pointJson * @return bool */ function aj_captcha_check_one(string $captchaType, string $token, string $pointJson) { aj_get_serevice($captchaType)->check($token, $pointJson); return true; } } if (!function_exists('aj_captcha_check_two')) { /** * 验证滑块2次验证 * @param string $token * @param string $pointJson * @return bool */ function aj_captcha_check_two(string $captchaType, string $captchaVerification ) { aj_get_serevice($captchaType)->verificationByEncryptCode($captchaVerification); return true; } } if (!function_exists('aj_captcha_create')) { /** * 创建验证码 * @return array */ function aj_captcha_create(string $captchaType) { return aj_get_serevice($captchaType)->get(); } } if (!function_exists('aj_get_serevice')) { /** * @param string $captchaType * @return ClickWordCaptchaService|BlockPuzzleCaptchaService */ function aj_get_serevice(string $captchaType) { $config = Config::get('ajcaptcha'); switch ($captchaType) { case "clickWord": $service = new ClickWordCaptchaService($config); break; case "blockPuzzle": $service = new BlockPuzzleCaptchaService($config); break; default: throw new ValidateException('captchaType参数不正确!'); } return $service; } } if (!function_exists('mb_substr_str')) { /** * 截取制定长度,并使用填充 * @param string $value * @param int $length * @param string $str * @return string * @author 等风来 * @email 136327134@qq.com * @date 2022/12/1 */ function mb_substr_str(string $value, int $length, string $str = '...', int $type = 0) { if (mb_strlen($value) > $length) { $value = mb_substr($value, 0, $length - mb_strlen($str)) . $str; } //等于1时去掉数组 if ($type === 1) { $value = preg_replace('/[0-9]/', '', $value); } return $value; } } if (!function_exists('response_log_write')) { /** * 日志写入 * @param array $data * @author 等风来 * @email 136327134@qq.com * @date 2022/12/2 */ function response_log_write(array $data, string $type = \think\Log::ERROR) { try { $id = 0; foreach (['adminId', 'kefuId', 'uid', 'supplierId'] as $value) { if (request()->hasMacro($value)) { $id = request()->{$value}(); } } //日志内容 $log = [ $id, //管理员ID request()->ip(), //客户ip ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒) request()->method(true), //请求类型 str_replace("/", "", request()->rootUrl()), //应用 request()->baseUrl(), //路由 json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//请求参数 json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据 ]; Log::write(implode("|", $log), $type); } catch (\Throwable $e) { } } } if (!function_exists('supplier_config')) { /** * @param int $supplierId * @param string $name * @param null $default * @return array|string|null */ function supplier_config(int $supplierId, string $name, $default = null) { if (empty($name)) { return $default; } /** @var SystemConfigService $configService */ $configService = app('sysConfig'); $configService->setSupplier($supplierId); $sysConfig = $configService->get($name); if (is_array($sysConfig)) { foreach ($sysConfig as &$item) { if (strpos($item, '/uploads/system/') !== false) { $item = set_file_url($item); } } } else { if (strpos($sysConfig, '/uploads/system/') !== false) { $sysConfig = set_file_url($sysConfig); } } $config = is_array($sysConfig) ? $sysConfig : trim($sysConfig); if ($config === '' || $config === false) { return $default; } else { return $config; } } } if (!function_exists('stringToArray')) { /** * 处理ids等并过滤参数 * @param $string * @param string $separator * @return array */ function stringToArray($string, string $separator = ',') { $res = []; if ($string) { $string = is_string($string) ? explode($separator, $string) : $string; $ids = []; foreach ($string as $item) { $ids[] = (int)$item; } $res = array_unique(array_diff($ids, [0])); } return $res; } } if (!function_exists('getFileHeaders')) { /** * 获取文件大小头部信息 * @param string $url * @param $isData * @return array */ function getFileHeaders(string $url, $isData = true) { stream_context_set_default(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]); $header['size'] = 0; $header['type'] = 'image/jpeg'; if (!$isData) { return $header; } try { $headerArray = get_headers(str_replace('\\', '/', $url), true); if (!isset($headerArray['Content-Length'])) { $header['size'] = 0; } else { if (is_array($headerArray['Content-Length']) && count($headerArray['Content-Length']) == 2) { $header['size'] = $headerArray['Content-Length'][1]; } else { $header['size'] = $headerArray['Content-Length'] ?? 0; } } if (!isset($headerArray['Content-Type'])) { $header['type'] = 'image/jpeg'; } else { if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Type']) == 2) { $header['type'] = $headerArray['Content-Type'][1]; } else { $header['type'] = $headerArray['Content-Type'] ?? 'image/jpeg'; } } } catch (\Exception $e) { } return $header; } } if (!function_exists('formatFileSize')) { /** * 格式化文件大小 * @param $size * @return mixed|string|null */ function formatFileSize($size) { if (!$size) { return '0KB'; } try { $toKb = 1024; $toMb = $toKb * 1024; $toGb = $toMb * 1024; if ($size >= $toGb) { return round($size / $toGb, 2) . 'GB'; } elseif ($size >= $toMb) { return round($size / $toMb, 2) . 'MB'; } elseif ($size >= $toKb) { return round($size / $toKb, 2) . 'KB'; } else { return $size . 'B'; } } catch (\Exception $e) { return '0KB'; } } } if (!function_exists('get_group_user')) { //所有下级 function get_group_user($id, $init = true, $members = null) { if ($init) { $us = \app\model\user\User::column('spread_uid', 'uid'); $members = []; foreach ($us as $k => $v) { if ($v > 0) $members[$v][] = $k; } $id = [$id]; } $arr = array(); foreach ($id as $v) { $child = $members[$v] ?? []; $arr = array_merge($arr, $child); } if (count($arr)) { return array_merge($arr, get_group_user($arr, false, $members)); } else { return $arr; } } } if (!function_exists('stringToIntArray')) { /** * 处理ids等并过滤参数 * @param string $string * @param string $separator * @return array */ function stringToIntArray(string $string, string $separator = ',') { return !empty($string) ? array_unique(array_diff(array_map('intval', explode($separator, $string)), [0])) : []; } } if (!function_exists('filter_str')) { /** * 过滤字符串敏感字符 * @param $str * @return array|mixed|string|string[]|null */ function filter_str($str) { $rules = [ '/\.\./', // 禁用包含 ../ 的参数 '/\<\?/', // 禁止 php 脚本出现 '/\bor\b.*=.*/i', // 匹配 'or 1=1',防止 SQL 注入(注意边界词 \b 和不区分大小写 i 修饰符) '/(select[\s\S]*?)(from|limit)/i', // 防止 SQL 注入 '/(union[\s\S]*?select)/i', // 防止 SQL 注入 '/(having|updatexml|extractvalue)/i', // 防止 SQL 注入 '/sleep\((\s*)(\d*)(\s*)\)/i', // 防止 SQL 盲注 '/benchmark\((.*)\,(.*)\)/i', // 防止 SQL 盲注 '/base64_decode\(/i', // 防止 SQL 变种注入 '/(?:from\W+information_schema\W)/i', // 注意这里的 (?:...) 是不合法的,应该是 (?:...) 表示非捕获组,但通常我们不需要这个 '/(?:current_|user|database|schema|connection_id)\s*\(/i', // 防止 SQL 注入(注意去掉了不必要的 (?:...)) '/(?:etc\/\W*passwd)/i', // 防止窥探 Linux 用户信息 '/into(\s+)(?:dump|out)file\s*/i', // 禁用 MySQL 导出函数 '/group\s+by.+\(/i', // 防止 SQL 注入 '/(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(/i', // 禁用 webshell 相关某些函数 '/(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/\//i', // 防止一些协议攻击(注意协议后的三个斜杠) '/\$_(GET|POST|COOKIE|FILES|SESSION|ENV|GLOBALS|SERVER)\[/i', // 禁用一些内置变量,注意 PHP 变量名通常是大写的 '/<(iframe|script|body|img|layer|div|meta|style|base|object|input)/i', // 防止 XSS 标签植入 '/(onmouseover|onerror|onload|onclick)\=/i', // 防止 XSS 事件植入 '/\|\|.*?(?:ls|pwd|whoami|ll|ifconfig|ipconfig|&&|chmod|cd|mkdir|rmdir|cp|mv)/i', // 防止执行 shell(注意去掉了不合适的 ifconfog) '/\sand\s+.*=.*/i' // 匹配 and 1=1 ]; if (filter_var($str, FILTER_VALIDATE_URL)) { $url = parse_url($str); if (!isset($url['scheme'])) return $str; $host = $url['scheme'] . '://' . $url['host']; $str = $host . preg_replace($rules, '', str_replace($host, '', $str)); } else { $str = preg_replace($rules, '', $str); } return $str; } }