feat: 为主题配置器插件新增文件上传功能及相关路由配置
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
namespace addon\theme_configurator\controller;
|
||||
|
||||
use app\event\controller\PluginAdminBaseController;
|
||||
use think\Response;
|
||||
use think\facade\Filesystem;
|
||||
|
||||
/**
|
||||
* 后台上传控制器
|
||||
*
|
||||
* 为主题配置插件提供图片上传功能
|
||||
*/
|
||||
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'),
|
||||
]);
|
||||
}
|
||||
|
||||
// 验证文件
|
||||
$validate = [
|
||||
'size' => 10 * 1024 * 1024, // 10MB
|
||||
'ext' => 'jpg,jpeg,png,gif,webp,svg',
|
||||
];
|
||||
|
||||
try {
|
||||
validate($validate)->check(['file' => $file]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'status' => 400,
|
||||
'msg' => lang_plugins('upload_validate_failed') . ': ' . $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
// 上传到本地服务器
|
||||
$saveName = Filesystem::disk('public')->putFile('theme', $file);
|
||||
|
||||
if (!$saveName) {
|
||||
return json([
|
||||
'status' => 400,
|
||||
'msg' => lang_plugins('upload_save_failed'),
|
||||
]);
|
||||
}
|
||||
|
||||
// 构建完整的URL路径
|
||||
$url = '/upload/' . str_replace('\\', '/', $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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user