完善资源模块

This commit is contained in:
云泽网
2025-05-16 23:56:21 +08:00
parent 02b21dcc6d
commit f6f8f6ac52
21 changed files with 3622 additions and 285 deletions
+102 -62
View File
@@ -114,7 +114,7 @@ class Index extends Base{
// 保存附件信息
$fileName = $files->getOriginalName();
$fileSize = $files->getSize();
$attachmentId = $this->saveAttachment($fileName, 1, $fileSize, $img);
$attachmentId = $this->saveAttachment($fileName, 1, $fileSize, $img); // 1: 图片
// 返回成功信息
return json([
@@ -132,67 +132,6 @@ class Index extends Base{
return json(['code'=>1, 'msg'=>'上传失败:'.$e->getMessage()])->send();
}
}
# 富文本图片上传
public function upload_imgs(){
$file = request()->file();
$files = request()->file('file');
if(empty($file)){
return json(['code'=>1, 'msg'=>'没有文件上传'])->send();
}
try {
validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check($file);
$info = \think\facade\Filesystem::disk('public')->putFile('uploads', $files);
// 处理文件路径
$info = str_replace("\\", "/", $info);
$img = '/storage/'.$info;
// 保存附件信息
$fileName = $files->getOriginalName();
$fileSize = $files->getSize();
$attachmentId = $this->saveAttachment($fileName, 2, $fileSize, $img);
return json([
'code' => 0,
'data' => [
'src' => $img,
'attachment_id' => $attachmentId
]
])->send();
} catch (\think\exception\ValidateException $e) {
return json(['code'=>1, 'msg'=>$e->getMessage()])->send();
}
}
# 富文本图片上传
public function upload_imgs_kin(){
$file = request()->file();
$files = request()->file('imgFile');
if(empty($file)){
return json(['error'=>1, 'message'=>'没有文件上传'])->send();
}
try {
validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check($file);
$info = \think\facade\Filesystem::disk('public')->putFile('uploads', $files);
// 处理文件路径
$info = str_replace("\\", "/", $info);
$img = '/storage/'.$info;
// 保存附件信息
$fileName = $files->getOriginalName();
$fileSize = $files->getSize();
$attachmentId = $this->saveAttachment($fileName, 3, $fileSize, $img);
return json([
'error' => 0,
'url' => $img,
'attachment_id' => $attachmentId
])->send();
} catch (\think\exception\ValidateException $e) {
return json(['error'=>1, 'message'=>$e->getMessage()])->send();
}
}
# 清除缓存
public function clear(){
$a = delete_dir_file(Env::get('runtime_path').'cache/');
@@ -203,4 +142,105 @@ class Index extends Base{
$this->returnCode(1, '清除缓存失败');
}
}
# 文件上传
public function upload_file(){
$file = request()->file();
$files = request()->file('file');
if(empty($file)){
return json(['code'=>1, 'msg'=>'没有文件上传'])->send();
}
try {
// 只验证文件扩展名,不验证大小
validate([
'file'=>'fileExt:doc,docx,xls,xlsx,ppt,pptx,pdf,txt,zip,rar,7z'
])->check($file);
$info = \think\facade\Filesystem::disk('public')->putFile('uploads/files', $files);
// 处理文件路径
$info = str_replace("\\", "/", $info);
$filePath = '/storage/'.$info;
// 保存附件信息
$fileName = $files->getOriginalName();
$fileSize = $files->getSize();
$attachmentId = $this->saveAttachment($fileName, 2, $fileSize, $filePath); // 2: 文件
return json([
'code' => 0,
'data' => [
'src' => $filePath,
'attachment_id' => $attachmentId
]
])->send();
} catch (\think\exception\ValidateException $e) {
return json(['code'=>1, 'msg'=>$e->getMessage()])->send();
} catch (\Exception $e) {
return json(['code'=>1, 'msg'=>'上传失败:'.$e->getMessage()])->send();
}
}
# 视频上传
public function upload_video(){
$file = request()->file();
$files = request()->file('file');
if(empty($file)){
return json(['code'=>1, 'msg'=>'没有文件上传'])->send();
}
try {
validate(['video'=>'filesize:102400|fileExt:mp4,avi,mov,wmv,flv'])->check($file);
$info = \think\facade\Filesystem::disk('public')->putFile('uploads/videos', $files);
// 处理文件路径
$info = str_replace("\\", "/", $info);
$videoPath = '/storage/'.$info;
// 保存附件信息
$fileName = $files->getOriginalName();
$fileSize = $files->getSize();
$attachmentId = $this->saveAttachment($fileName, 3, $fileSize, $videoPath); // 3: 视频
return json([
'code' => 0,
'data' => [
'src' => $videoPath,
'attachment_id' => $attachmentId
]
])->send();
} catch (\think\exception\ValidateException $e) {
return json(['code'=>1, 'msg'=>$e->getMessage()])->send();
}
}
# 音频上传
public function upload_audio(){
$file = request()->file();
$files = request()->file('file');
if(empty($file)){
return json(['code'=>1, 'msg'=>'没有文件上传'])->send();
}
try {
validate(['audio'=>'filesize:51200|fileExt:mp3,wav,ogg,m4a'])->check($file);
$info = \think\facade\Filesystem::disk('public')->putFile('uploads/audios', $files);
// 处理文件路径
$info = str_replace("\\", "/", $info);
$audioPath = '/storage/'.$info;
// 保存附件信息
$fileName = $files->getOriginalName();
$fileSize = $files->getSize();
$attachmentId = $this->saveAttachment($fileName, 4, $fileSize, $audioPath); // 4: 音频
return json([
'code' => 0,
'data' => [
'src' => $audioPath,
'attachment_id' => $attachmentId
]
])->send();
} catch (\think\exception\ValidateException $e) {
return json(['code'=>1, 'msg'=>$e->getMessage()])->send();
}
}
}
+84 -50
View File
@@ -72,7 +72,6 @@ class Resources extends Base
// 格式化时间
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
$item['upload_time'] = $item['upload_time'] ? date('Y-m-d H:i:s', $item['upload_time']) : '';
return $item;
});
@@ -125,7 +124,6 @@ class Resources extends Base
'uploader' => input('post.uploader'),
'desc' => input('post.desc'),
'status' => input('post.status', 2),
'upload_time' => time(),
'create_time' => time()
];
@@ -149,54 +147,6 @@ class Resources extends Base
}
}
// 编辑资源
public function edit()
{
if (Request::isPost()) {
$id = input('get.id');
$data = [
'name' => input('post.name'),
'cate' => input('post.cate'),
'icon' => input('post.icon'),
'url' => input('post.url'),
'code' => input('post.code'),
'uploader' => input('post.uploader'),
'desc' => input('post.desc'),
'status' => input('post.status', 2),
'update_time' => time()
];
$update = Db::table('yz_resources')->where('id', $id)->update($data);
return json(['code' => $update === false ? 1 : 0, 'msg' => $update === false ? '更新失败' : '更新成功', 'data' => []]);
} else {
$id = input('get.id');
$info = Db::table('yz_resources')->where('id', $id)->find();
if ($info === null) {
return json(['code' => 1, 'msg' => '资源不存在', 'data' => []]);
}
$cates = Db::table('yz_resources_category')
->where('delete_time', null)
->where('status', 1)
->order('sort asc, id asc')
->select()
->toArray();
$currentCate = Db::table('yz_resources_category')
->where('id', $info['cate'])
->where('delete_time', null)
->where('status', 1)
->find();
$info['cate_name'] = $currentCate ? $currentCate['name'] : '';
View::assign([
'info' => $info,
'cates' => $cates
]);
return View::fetch();
}
}
// 删除资源
public function delete()
{
@@ -411,4 +361,88 @@ class Resources extends Base
}
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
}
/**
* 获取资源详情
*/
public function get()
{
$id = input('id/d', 0);
if (!$id) {
return json(['code' => 1, 'msg' => '参数错误']);
}
$resource = Db::table('yz_resources')
->where('id', $id)
->where('delete_time', null)
->find();
if (!$resource) {
return json(['code' => 1, 'msg' => '资源不存在']);
}
// 获取分类信息
$cateInfo = Db::table('yz_resources_category')
->where('id', $resource['cate'])
->field('name')
->find();
if ($cateInfo) {
$resource['cate_name'] = $cateInfo['name'];
}
// 调试输出
trace('Resource data: ' . json_encode($resource), 'debug');
return json(['code' => 0, 'msg' => '获取成功', 'data' => $resource]);
}
/**
* 编辑资源
*/
public function edit()
{
if (Request::isPost()) {
$data = input('post.');
$id = input('id/d', 0);
if (!$id) {
return json(['code' => 1, 'msg' => '参数错误']);
}
// 更新数据
$result = Db::table('yz_resources')->where('id', $id)->update([
'title' => $data['title'],
'cate' => $data['cate'],
'desc' => $data['desc'],
'uploader' => $data['uploader'],
'icon' => $data['icon'],
'fileurl' => $data['fileurl'],
'url' => $data['url'],
'code' => $data['code'],
'sort' => $data['sort'],
'update_time' => time()
]);
if ($result !== false) {
return json(['code' => 0, 'msg' => '编辑成功']);
} else {
return json(['code' => 1, 'msg' => '编辑失败']);
}
}
$id = input('id/d', 0);
if (!$id) {
$this->error('参数错误');
}
$resource = Db::table('yz_resources')->where('id', $id)->find();
if (!$resource) {
$this->error('资源不存在');
}
View::assign('resource', $resource);
return View::fetch();
}
}