This commit is contained in:
@@ -3,17 +3,17 @@
|
|||||||
此插件演示如何通过后台插件的方式为 BlackFruit-UI 主题提供可配置能力,支持设置导航、页脚、站点信息、SEO、首页轮播、友情链接、荣誉资质、反馈类型以及右侧浮窗,并提供 `/console/v1/theme/config` 接口与前端联动。
|
此插件演示如何通过后台插件的方式为 BlackFruit-UI 主题提供可配置能力,支持设置导航、页脚、站点信息、SEO、首页轮播、友情链接、荣誉资质、反馈类型以及右侧浮窗,并提供 `/console/v1/theme/config` 接口与前端联动。
|
||||||
|
|
||||||
## 功能
|
## 功能
|
||||||
- **图片上传**: 插件自带独立的上传接口 `POST /{DIR_ADMIN}/v1/upload`,支持jpg、png、gif、webp、svg等格式,最大10MB
|
- **图片上传**: 依赖业务系统的全局上传接口 `/{DIR_ADMIN}/v1/upload` 和 `/console/v1/upload`,与官方示例插件保持一致
|
||||||
- 后台界面(`template/admin/index.html`)通过表单 + JSON 的方式维护主题参数:
|
- 后台界面(`template/admin/index.html`)通过表单 + JSON 的方式维护主题参数:
|
||||||
- SEO、站点基础信息(企业名称、电话、备案、协议链接、产品链接等);
|
- SEO、站点基础信息(企业名称、电话、备案、协议链接、产品链接等);
|
||||||
- 首页轮播 Banner(支持图片上传);
|
- 首页轮播 Banner(支持图片上传);
|
||||||
- 友情链接(friendly_link);
|
- 友情链接(friendly_link);
|
||||||
- 企业荣誉(honor,支持图片上传);
|
- 企业荣誉(honor,支持图片上传);
|
||||||
- 反馈类型(feedback_type);
|
- 反馈类型(feedback_type);
|
||||||
- 右侧浮窗(side / side_floating_window,支持图标上传);
|
- 右侧浮窗(side / side_floating_window,支持图标上传);
|
||||||
- 复杂导航结构(header_nav/footer_nav)可在"高级配置 (JSON)"中维护,子菜单支持图标上传。
|
- 复杂导航结构(header_nav/footer_nav)可在"高级配置 (JSON)"中维护,子菜单支持图标上传。
|
||||||
- 接口 `GET/POST /{DIR_ADMIN}/v1/theme/config` 提供配置读取与保存;
|
- 接口 `GET/POST /{DIR_ADMIN}/v1/theme/config` 提供配置读取与保存;
|
||||||
- 前台接口 `GET /console/v1/theme/config` 输出与 `/console/v1/common` 同结构的数据,BlackFruit-UI 可以直接接入;
|
- 前台接口 `GET /console/v1/theme/config` 输出与 `/console/v1/common` 同结构的数据,BlackFruit-UI 可以直接接入;
|
||||||
- 插件安装时创建 `addon_theme_configurator` 表并写入默认配置。
|
- 插件安装时创建 `addon_theme_configurator` 表并写入默认配置。
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|||||||
@@ -1,101 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace addon\theme_configurator\controller;
|
|
||||||
|
|
||||||
use app\event\controller\PluginAdminBaseController;
|
|
||||||
use think\Response;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 后台上传控制器
|
|
||||||
*
|
|
||||||
* 为主题配置插件提供图片上传功能
|
|
||||||
*/
|
|
||||||
class UploadController extends PluginAdminBaseController
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* 上传文件
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function upload(): Response
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
// 获取上传的文件
|
|
||||||
$file = $this->request->file('file');
|
|
||||||
|
|
||||||
if (!$file) {
|
|
||||||
return json([
|
|
||||||
'status' => 400,
|
|
||||||
'msg' => lang_plugins('upload_no_file'),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证文件类型和大小
|
|
||||||
$fileExt = strtolower($file->extension());
|
|
||||||
$fileSize = $file->getSize();
|
|
||||||
|
|
||||||
$allowedExts = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
|
|
||||||
$maxSize = 10 * 1024 * 1024; // 10MB
|
|
||||||
|
|
||||||
if (!in_array($fileExt, $allowedExts)) {
|
|
||||||
return json([
|
|
||||||
'status' => 400,
|
|
||||||
'msg' => lang_plugins('upload_validate_failed') . ': ' . '仅支持 jpg, jpeg, png, gif, webp, svg 格式',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($fileSize > $maxSize) {
|
|
||||||
return json([
|
|
||||||
'status' => 400,
|
|
||||||
'msg' => lang_plugins('upload_validate_failed') . ': ' . '文件大小不能超过 10MB',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建保存路径
|
|
||||||
$uploadPath = root_path('public') . 'upload' . DIRECTORY_SEPARATOR . 'theme';
|
|
||||||
$dateDir = date('Ymd');
|
|
||||||
$savePath = $uploadPath . DIRECTORY_SEPARATOR . $dateDir;
|
|
||||||
|
|
||||||
// 创建目录
|
|
||||||
if (!is_dir($savePath)) {
|
|
||||||
if (!mkdir($savePath, 0755, true)) {
|
|
||||||
return json([
|
|
||||||
'status' => 400,
|
|
||||||
'msg' => lang_plugins('upload_save_failed') . ': 无法创建上传目录',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成唯一文件名
|
|
||||||
$fileName = md5(uniqid() . microtime()) . '.' . $fileExt;
|
|
||||||
$fullPath = $savePath . DIRECTORY_SEPARATOR . $fileName;
|
|
||||||
|
|
||||||
// 移动文件
|
|
||||||
if (!$file->move($savePath, $fileName)) {
|
|
||||||
return json([
|
|
||||||
'status' => 400,
|
|
||||||
'msg' => lang_plugins('upload_save_failed'),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建URL路径
|
|
||||||
$saveName = 'theme/' . $dateDir . '/' . $fileName;
|
|
||||||
$url = '/upload/' . $saveName;
|
|
||||||
|
|
||||||
return json([
|
|
||||||
'status' => 200,
|
|
||||||
'msg' => lang_plugins('upload_success'),
|
|
||||||
'data' => [
|
|
||||||
'save_name' => $saveName,
|
|
||||||
'url' => $url,
|
|
||||||
'image_url' => $url,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return json([
|
|
||||||
'status' => 500,
|
|
||||||
'msg' => lang_plugins('upload_failed') . ': ' . $e->getMessage(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
use think\facade\Route;
|
use think\facade\Route;
|
||||||
|
|
||||||
// 前台读取接口(对外提供主题配置,允许未登录访问)
|
// 前台读取接口(对外提供主题配置,允许未登录访问)
|
||||||
Route::group('console/v1', function () {
|
Route::group('console/v1', function () {
|
||||||
Route::get('theme/config', "\\addon\\theme_configurator\\controller\\clientarea\\ThemeController@config")
|
Route::get('theme/config', "\\addon\\theme_configurator\\controller\\clientarea\\ThemeController@config")
|
||||||
->append([
|
->append([
|
||||||
@@ -12,16 +12,9 @@ Route::group('console/v1', function () {
|
|||||||
})->middleware(\app\http\middleware\ParamFilter::class);
|
})->middleware(\app\http\middleware\ParamFilter::class);
|
||||||
|
|
||||||
// 后台配置接口
|
// 后台配置接口
|
||||||
|
// 注意:上传功能使用业务系统的全局上传接口 /{DIR_ADMIN}/v1/upload
|
||||||
|
// 不在插件内实现,参考官方示例插件 idcsmart_ticket 的做法
|
||||||
Route::group(DIR_ADMIN . '/v1', function () {
|
Route::group(DIR_ADMIN . '/v1', function () {
|
||||||
// 文件上传接口
|
|
||||||
Route::post('upload', "\\addon\\theme_configurator\\controller\\UploadController@upload")
|
|
||||||
->append([
|
|
||||||
'_plugin' => 'theme_configurator',
|
|
||||||
'_controller' => 'upload',
|
|
||||||
'_action' => 'upload',
|
|
||||||
])
|
|
||||||
->middleware(\app\http\middleware\CheckAdmin::class);
|
|
||||||
|
|
||||||
Route::get('theme/config', "\\addon\\theme_configurator\\controller\\ThemeController@config")
|
Route::get('theme/config', "\\addon\\theme_configurator\\controller\\ThemeController@config")
|
||||||
->append([
|
->append([
|
||||||
'_plugin' => 'theme_configurator',
|
'_plugin' => 'theme_configurator',
|
||||||
|
|||||||
Reference in New Issue
Block a user