79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?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(),
|
|
]);
|
|
}
|
|
}
|
|
}
|