From ba0904069cacedebf5043b9ff77f23e8317e8971 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Thu, 5 Jun 2025 09:51:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=90=8E=E9=9D=A2=E6=A8=AA?= =?UTF-8?q?=E5=B9=85=E5=88=86=E9=A1=B5=E6=8E=A5=E5=8F=A3=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E4=BF=AE=E6=94=B9=E4=B8=BA=E7=BC=A9=E7=95=A5?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/v1/BannerItem.php | 3 ++- app/common.php | 30 +++++++++++++++++++++++ app/index/common.php | 33 ++------------------------ 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/app/admin/controller/v1/BannerItem.php b/app/admin/controller/v1/BannerItem.php index d949bcd9..17243489 100644 --- a/app/admin/controller/v1/BannerItem.php +++ b/app/admin/controller/v1/BannerItem.php @@ -60,7 +60,8 @@ class BannerItem ->paginate([ 'list_rows' => $param['size'], 'page' => $param['page'] - ]); + ]) + ->each(fn($item) => $item->image = thumb($item->image)); return success('获取成功', $banner_items); } diff --git a/app/common.php b/app/common.php index ae91895a..37309be1 100644 --- a/app/common.php +++ b/app/common.php @@ -102,4 +102,34 @@ if (!function_exists('array_to_tree')) { } return $ret; } +} + +if (!function_exists('thumb')) { + /** + * 获取缩略图 + * @param string $url 图片地址 + * @return string + */ + function thumb(?string $url): string + { + if (empty($url)) { + return ''; + } + if ( + str_contains($url, '_thumb') || + \think\helper\Str::startsWith($url, ['http://', 'https://']) || + !\think\helper\Str::endsWith($url, ['.png', '.jpg', '.jpeg', '.gif', '.tif', '.svg', '.webp', '.bmp']) + ) { + return $url; + } + + $idx = mb_strripos($url, '.', 0, 'utf-8'); + if ($idx === false) { + return $url; + } + + $len = mb_strlen($url, 'utf-8'); + + return mb_substr($url, 0, $idx, 'utf-8') . '_thumb' . mb_substr($url, $idx, $len - $idx, 'utf-8'); + } } \ No newline at end of file diff --git a/app/index/common.php b/app/index/common.php index 35395eec..7e901805 100644 --- a/app/index/common.php +++ b/app/index/common.php @@ -71,36 +71,6 @@ if (!function_exists('style')) { } } -if (!function_exists('thumb')) { - /** - * 获取缩略图 - * @param string $url 图片地址 - * @return string - */ - function thumb(string $url): string - { - if (empty($url)) { - return ''; - } - if ( - str_contains($url, '_thumb') || - \think\helper\Str::startsWith($url, ['http://', 'https://']) || - !\think\helper\Str::endsWith($url, ['.png', '.jpg', '.jpeg', '.gif', '.tif', '.svg', '.webp', '.bmp']) - ) { - return $url; - } - - $idx = mb_strripos($url, '.', 0, 'utf-8'); - if ($idx === false) { - return $url; - } - - $len = mb_strlen($url, 'utf-8'); - - return mb_substr($url, 0, $idx, 'utf-8') . '_thumb' . mb_substr($url, $idx, $len - $idx, 'utf-8'); - } -} - if (!function_exists('get_path_from_img_tag')) { /** * 从img标签中获取图片路径 @@ -128,7 +98,8 @@ if (!function_exists('lang_i18n')) { * @param string $lang 语言标识 * @return string */ - function lang_i18n(string $name, array $vars = [], string $lang = '') { + function lang_i18n(string $name, array $vars = [], string $lang = ''): string + { if (is_null($name)) { return ''; }