chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace ywxapp\controller;
|
||||
|
||||
use ywxapp\model\Attachment;
|
||||
use ywxapp\service\FileStorageService;
|
||||
|
||||
/**
|
||||
* UeditorPlus 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class UeditorPlus
|
||||
{
|
||||
|
||||
private $user;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (app('auth')) {
|
||||
$this->user = app()->auth->info;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置 config
|
||||
* GET action config 请求动作名称
|
||||
*/
|
||||
public function config()
|
||||
{
|
||||
return json(config('ueditor-plus'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片 image
|
||||
* GET action image 请求动作名称,可通过 imageActionName 配置修改名称
|
||||
* FILE file file 上传的文件,文件表单名称可通过 imageFieldName 配置修改名称
|
||||
*return { "state":"SUCCESS", "url":"upload/demo.jpg", "title":"demo.jpg", "original":"demo.jpg"; }
|
||||
*/
|
||||
public function image()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
// return json(['data'=> app()->auth->info]);
|
||||
try {
|
||||
$storage = new FileStorageService();
|
||||
$data = $storage->upload($file, 'images');
|
||||
$info = $this->toSaveData($data);
|
||||
return json([
|
||||
'mime' => $data['mime'],
|
||||
"state" => "SUCCESS",
|
||||
"url" => $info->url,
|
||||
"title" => $info->name,
|
||||
"original" => $info->original,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json(['code' => 1, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片抓取 catch
|
||||
* GET action catch 请求动作名称,可通过 catcherActionName 配置修改名称
|
||||
* POST source url 抓取的图片地址,文件表单名称可通过 catcherFieldName 配置修改名称
|
||||
*/
|
||||
public function catch ()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
try {
|
||||
// 实例化上传服务
|
||||
$storage = new FileStorageService();
|
||||
|
||||
// 执行上传
|
||||
// 这里的逻辑不需要关心具体是上传到哪里,由配置决定
|
||||
$result = $storage->upload($file, 'avatar/' . date('Ymd'));
|
||||
return json(['code' => 0, 'msg' => '上传成功', 'data' => $result]);
|
||||
} catch (\Exception $e) {
|
||||
return json(['code' => 1, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 视频上传 video
|
||||
* GET action video 请求动作名称,可通过 videoActionName 配置修改名称
|
||||
* FILE file file 上传的文件,文件表单名称可通过 videoFieldName 配置修改名称
|
||||
*/
|
||||
public function video()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
|
||||
$data = [
|
||||
'size' => $file->getSize(),
|
||||
'Mime' => $file->getOriginalMime(),
|
||||
];
|
||||
return json(['code' => 0, 'msg' => '上传成功', 'data' => $data]);
|
||||
try {
|
||||
// 实例化上传服务
|
||||
$storage = new FileStorageService();
|
||||
|
||||
// 执行上传
|
||||
// 这里的逻辑不需要关心具体是上传到哪里,由配置决定
|
||||
$result = $storage->upload($file, 'avatar/' . date('Ymd'));
|
||||
return json(['code' => 0, 'msg' => '上传成功', 'data' => $result]);
|
||||
} catch (\Exception $e) {
|
||||
return json(['code' => 1, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传 file
|
||||
* GET action file 请求动作名称,可通过 fileActionName 配置修改名称
|
||||
* FILE file file 上传的文件,文件表单名称可通过 fileFieldName 配置修改名称
|
||||
*/
|
||||
public function file()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
try {
|
||||
// 实例化上传服务
|
||||
$storage = new FileStorageService();
|
||||
|
||||
// 执行上传
|
||||
// 这里的逻辑不需要关心具体是上传到哪里,由配置决定
|
||||
$result = $storage->upload($file, 'avatar/' . date('Ymd'));
|
||||
return json(['code' => 0, 'msg' => '上传成功', 'data' => $result]);
|
||||
} catch (\Exception $e) {
|
||||
return json(['code' => 1, 'msg' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片列表 listImage
|
||||
* GET action listImage 请求动作名称,可通过 imageManagerActionName 配置修改名称
|
||||
*/
|
||||
public function listImage()
|
||||
{
|
||||
try {
|
||||
$user = app()->auth->info;
|
||||
$datas = Attachment::where('uid', $user->uid)->where('mime', 'like', 'image%')->field('name,url')->select();
|
||||
return json([
|
||||
"state" => "SUCCESS",
|
||||
"list" => $datas,
|
||||
"start" => 0,
|
||||
"total" => count($datas),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录日志,但不要因为入库失败而中断上传流程(或者根据需求决定)
|
||||
\think\facade\Log::error( $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件列表 listFile
|
||||
* GET action listFile 请求动作名称,可通过 fileManagerActionName 配置修改名称
|
||||
*/
|
||||
public function listFile()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件信息保存到数据库
|
||||
*/
|
||||
protected function toSaveData(array $data, array $options = [])
|
||||
{
|
||||
try {
|
||||
$user = app()->auth->info;
|
||||
$data['uid'] = $user->uid; // 根据你的登录逻辑获取用户ID
|
||||
$info = Attachment::create($data);
|
||||
return $info;
|
||||
} catch (\Exception $e) {
|
||||
// 记录日志,但不要因为入库失败而中断上传流程(或者根据需求决定)
|
||||
\think\facade\Log::error('文件记录入库失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user