mirror of https://github.com/1099438829/apeblog
修正友链和图片上传相关的bug
This commit is contained in:
parent
d832049d77
commit
50114953fb
|
|
@ -6,7 +6,9 @@ namespace app\admin\controller;
|
|||
use app\admin\model\Attachment;
|
||||
use app\admin\services\storage\QcloudCoService;
|
||||
use app\admin\services\UtilService as Util;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Filesystem;
|
||||
use think\Request;
|
||||
|
||||
class Files extends AuthController
|
||||
{
|
||||
|
|
@ -66,131 +68,48 @@ class Files extends AuthController
|
|||
return json_encode(['location'=>"/uploads/".$savename]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传多图片
|
||||
* @return mixed
|
||||
*/
|
||||
public function images()
|
||||
{
|
||||
return Filesystem::putFile( 'image', request()->file('file')) ? app("json")->code()->success("上传成功") : app("json")->fail("上传失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 证书上传
|
||||
* @return mixed
|
||||
*/
|
||||
public function cert()
|
||||
{
|
||||
$file = $this->request->file("file");
|
||||
$savename = Filesystem::putFile( 'file', $file);
|
||||
$filePath = "/uploads/".$savename;
|
||||
return $savename ? app("json")->code()->success("上传成功",['filePath'=>$filePath,"name"=>$savename]) : app("json")->fail("上传失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到cid:0,
|
||||
* 图片 视频 音频
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function file()
|
||||
{
|
||||
$file = $this->request->file("file");
|
||||
try {
|
||||
$savename = Filesystem::putFile( 'file', $file);
|
||||
$filePath = "/uploads/".$savename;
|
||||
return $savename ? app("json")->code()->success("上传成功",['filePath'=>$filePath,"name"=>$savename]) : app("json")->fail("上传失败");
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return app("json")->fail($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @文档上传
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function document()
|
||||
{
|
||||
// 获取表单上传文件 例如上传了001.jpg
|
||||
$file = request()->file('file');//根据表单name替换imgFile
|
||||
try {
|
||||
// 使用验证器验证上传的文件
|
||||
validate(['file' => [
|
||||
// 限制文件大小(单位b),这里限制为5M
|
||||
'fileSize' => 5 * 1024 * 1024,
|
||||
// 限制文件后缀,多个后缀以英文逗号分割
|
||||
'fileExt' => 'pdf,doc,docx'
|
||||
// 更多规则请看“上传验证”的规则,文档地址https://www.kancloud.cn/manual/thinkphp6_0/1037629#_444
|
||||
]])->check(['file' => $file]);
|
||||
$savename = Filesystem::putFile( 'file', $file);
|
||||
$filePath = "/uploads/".$savename;
|
||||
return $savename ? app("json")->code()->success("上传成功",['filePath'=>$filePath,"name"=>$savename]) : app("json")->fail("上传失败");
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return app("json")->fail($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @单文件上传
|
||||
* @param string $type 类型 files images documents banners
|
||||
* @return mixed
|
||||
*/
|
||||
public function upload()
|
||||
public function upload(Request $request)
|
||||
{
|
||||
if(Request::isPost()) {
|
||||
if($request->isPost()) {
|
||||
// 获取表单上传文件 例如上传了001.jpg
|
||||
$file = request()->file('file');//根据表单name替换imgFile
|
||||
$file = $request->file('file');//根据表单name替换imgFile
|
||||
$type = $this->request->post("type");
|
||||
$type = $type ?:'files';
|
||||
switch (system_config("storage_type"))
|
||||
{
|
||||
case 1:
|
||||
switch ($type) {
|
||||
case 'files':
|
||||
$fileSize = 10 * 1024 * 1024;
|
||||
$fileExt = 'pdf,doc,docx';
|
||||
$fileExt = 'pdf,doc,docx,png,jpeg,jpg,text,mp4';
|
||||
break;
|
||||
case 'banner':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
case 'slides':
|
||||
$fileSize = 10 * 1024 * 1024;
|
||||
$fileExt = 'png,jpeg,jpg';
|
||||
break;
|
||||
case 'documents':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
$fileExt = 'pdf,doc,docx';
|
||||
break;
|
||||
case 'image':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
$fileExt = 'png,jpeg,jpg';
|
||||
break;
|
||||
case 'images':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
$fileExt = 'png,jpeg,jpg';
|
||||
break;
|
||||
case 'template':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
$fileExt = 'pdf,doc,docx';
|
||||
break;
|
||||
case 'talentpool':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
$fileExt = 'pdf,doc,docx,png,jpeg,jpg';
|
||||
break;
|
||||
case 'offer':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
$fileExt = 'pdf,doc,docx,png,jpeg,jpg';
|
||||
break;
|
||||
case 'contract':
|
||||
$fileSize = 5 * 1024 * 1024;
|
||||
$fileExt = 'pdf,doc,docx,png,jpeg,jpg';
|
||||
case 'avatar':
|
||||
$fileSize = 4 * 1024 * 1024;
|
||||
$fileExt = 'png,jpeg,jpg';
|
||||
break;
|
||||
default:
|
||||
$fileSize = 0 * 1024 * 1024;
|
||||
$fileExt = 'pdf,doc,docx';
|
||||
break;
|
||||
}
|
||||
// Log::record('yangzailu::');
|
||||
// Log::record($fileExt);
|
||||
try {
|
||||
// 使用验证器验证上传的文件
|
||||
validate(['file' => [
|
||||
|
|
@ -202,10 +121,22 @@ class Files extends AuthController
|
|||
]])->check(['file' => $file]);
|
||||
$savename = Filesystem::putFile( $type, $file);
|
||||
$filePath = "/uploads/".$savename;
|
||||
return $savename ? app("json")->code()->success("上传成功",['filePath'=>$filePath,"name"=>basename($savename)]) : app("json")->fail("上传失败");
|
||||
$res = Attachment::addAttachment($this->request->param("cid",0),basename($savename),$filePath,$type,$file->getMime(),$file->getSize(),system_config("storage_type"));
|
||||
return $res? app("json")->code()->success("上传成功",['filePath'=>$filePath,"name"=>basename($savename)]) : app("json")->fail("上传失败");
|
||||
} catch (ValidateException $e) {
|
||||
return app("json")->fail($e->getMessage());
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
return app("json")->fail('上传失败');
|
||||
$savename = Filesystem::putFile( 'image', $file);
|
||||
$ext = $file->getOriginalExtension();
|
||||
$key = '/image/'.date('Ymd')."/".substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
|
||||
$filePath = QcloudCoService::put($key, $file->getRealPath());
|
||||
$res = Attachment::addAttachment($this->request->param("cid",0),basename($savename),$filePath,$type,$file->getMime(),$file->getSize(),system_config("storage_type"));
|
||||
return $res? app("json")->code()->success("上传成功",['filePath'=>$filePath,"name"=>basename($savename)]) : app("json")->fail("上传失败");
|
||||
break;
|
||||
}
|
||||
}else{
|
||||
return app("json")->fail('上传失败');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,11 @@
|
|||
namespace app\admin\controller;
|
||||
|
||||
use app\admin\model\FriendLink as aModel;
|
||||
use app\admin\services\FormBuilderService as Form;
|
||||
use app\Request;
|
||||
use app\admin\services\UtilService as Util;
|
||||
use FormBuilder\Factory\Elm;
|
||||
use think\facade\Route as Url;
|
||||
|
||||
/**
|
||||
* Class Message
|
||||
|
|
@ -48,16 +51,53 @@ class FriendLink extends AuthController
|
|||
}
|
||||
|
||||
/**
|
||||
* 更新
|
||||
* 添加账号
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 李玉坤
|
||||
* @date 2021-02-16 21:15
|
||||
* @return string
|
||||
* @throws \FormBuilder\Exception\FormBuilderException
|
||||
*/
|
||||
public function update(Request $request)
|
||||
public function add(Request $request)
|
||||
{
|
||||
$form = array();
|
||||
$form[] = Elm::input('title','链接名称')->col(10);
|
||||
$form[] = Elm::input('url','链接地址')->col(10);
|
||||
$form[] = Elm::frameImage('image','网站图标',Url::buildUrl('admin/images/index',array('fodder'=>'image','limit'=>1)))->icon("ios-image")->width('96%')->height('440px')->col(10);
|
||||
$form[] = Elm::input('sort','排序')->col(10);
|
||||
$form[] = Elm::textarea('description','描述')->col(10);
|
||||
$form[] = Elm::radio('status','状态',1)->options([['label'=>'启用','value'=>1],['label'=>'冻结','value'=>0]])->col(10);
|
||||
$form = Form::make_post_form($form, url('save')->build());
|
||||
$this->assign(compact('form'));
|
||||
return $this->fetch("public/form-builder");
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改账号
|
||||
* @return string
|
||||
* @throws \FormBuilder\Exception\FormBuilderException
|
||||
*/
|
||||
public function edit($id="")
|
||||
{
|
||||
if (!$id) return app("json")->fail("数据id不能为空");
|
||||
$ainfo = aModel::get($id);
|
||||
if (!$ainfo) return app("json")->fail("没有该数据");
|
||||
$form = array();
|
||||
$form[] = Elm::input('title','链接名称',$ainfo['title'])->col(10);
|
||||
$form[] = Elm::input('url','链接地址',$ainfo['url'])->col(10);
|
||||
$form[] = Elm::frameImage('image','网站图标',Url::buildUrl('admin/images/index',array('fodder'=>'image','limit'=>1)),$ainfo['avatar'])->icon("ios-image")->width('96%')->height('440px')->col(10);
|
||||
$form[] = Elm::input('sort','排序',$ainfo['sort'])->col(10);
|
||||
$form[] = Elm::textarea('description','描述',$ainfo['description'])->col(10);
|
||||
$form[] = Elm::radio('status','状态',$ainfo['status'])->options([['label'=>'启用','value'=>1],['label'=>'冻结','value'=>0]])->col(10);
|
||||
$form = Form::make_post_form($form, url('save',['id'=>$id])->build());
|
||||
$this->assign(compact('form'));
|
||||
return $this->fetch("public/form-builder");
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存修改
|
||||
* @param string $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function save($id="")
|
||||
{
|
||||
$data = Util::postMore([
|
||||
['id',''],
|
||||
|
|
@ -68,16 +108,20 @@ class FriendLink extends AuthController
|
|||
['sort',''],
|
||||
['status',1],
|
||||
]);
|
||||
if ($data['id'] == '') return app("json")->fail("参数有误,Id为空!");
|
||||
$id = $data['id'];
|
||||
unset($data['id']);
|
||||
$saveData = [];
|
||||
foreach ($data as $key=>$value){
|
||||
if ($value !== ''){
|
||||
$saveData[$key] = $value;
|
||||
if ($data['title'] == "") return app("json")->fail("链接名称不能为空");
|
||||
if ($data['url'] == "") return app("json")->fail("链接地址不能为空");
|
||||
if (is_array($data['image'])) $data['image'] = $data['avatar'][0];
|
||||
if ($id=="") {
|
||||
//判断下用户是否存在
|
||||
$info = aModel::where('url',$data['url'])->find();
|
||||
if ($info){
|
||||
return app("json")->fail("链接已存在");
|
||||
}
|
||||
$res = aModel::create($data);
|
||||
}else {
|
||||
$res = aModel::update($data,['id'=>$id]);
|
||||
}
|
||||
return aModel::update($saveData,['id'=>$id]) ? app("json")->success("操作成功") : app("json")->fail("操作失败");
|
||||
return $res ? app("json")->success("操作成功",'code') : app("json")->fail("操作失败");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class Images extends AuthController
|
|||
*/
|
||||
public function category()
|
||||
{
|
||||
return app("json")->success(AttachmentCategory::buildNodes("image",0,$this->request->param("title","")));
|
||||
return app("json")->success(AttachmentCategory::buildNodes("images",0,$this->request->param("title","")));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -103,7 +103,7 @@ class Images extends AuthController
|
|||
if ($id == "")
|
||||
{
|
||||
$data['create_user'] = $this->adminId;
|
||||
$res = AttachmentCategory::save($data);
|
||||
$res = AttachmentCategory::create($data);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -33,9 +33,8 @@ class Attachment extends BaseModel
|
|||
'mime' => $mime,
|
||||
'size' => $size,
|
||||
'storage' => $storage,
|
||||
'upload_time' => time()
|
||||
];
|
||||
return self::insert($data);
|
||||
return self::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ class AttachmentCategory extends BaseModel
|
|||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public static function buildNodes(string $type = "image", int $pid = 0, string $title = "")
|
||||
public static function buildNodes(string $type = "images", int $pid = 0, string $title = "")
|
||||
{
|
||||
$model = new self;
|
||||
$model = $model->where("type",$type);
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@
|
|||
<div class="card">
|
||||
<div class="card-toolbar clearfix">
|
||||
<div class="toolbar-btn-action">
|
||||
<button id="btn_add" type="button" class="btn btn-primary m-r-5" onclick="iframe.createIframe('添加用户','/admin/admin/add')">
|
||||
<button id="btn_add" type="button" class="btn btn-primary m-r-5" onclick="iframe.createIframe('添加友链','/admin/friendlink/add')">
|
||||
<span class="mdi mdi-plus" aria-hidden="true"></span>新增
|
||||
</button>
|
||||
<a class="btn btn-warning" href="#!" onclick="delSelect()"><i class="mdi mdi-window-close"></i> 删除</a>
|
||||
|
|
@ -133,12 +133,17 @@
|
|||
let html = '<a type="button" class="btn-edit btn btn-xs btn-default m-r-5" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a> \n' +
|
||||
'<a class="btn btn-xs btn-default btn-del" href="#!" title="删除" data-toggle="tooltip" onclick="delOne('+row.id+')"><i class="mdi mdi-window-close"></i></a>';
|
||||
return html;
|
||||
},
|
||||
events: {
|
||||
'click .btn-edit': function (event, value, row, index) {
|
||||
iframe.createIframe('修改友链','/admin/friendlink/edit?id='+row.id)
|
||||
},
|
||||
}
|
||||
}],
|
||||
onEditableSave: function (field, row) {
|
||||
$.ajax({
|
||||
type: "post",
|
||||
url: "/admin/friendlink/update",
|
||||
url: "/admin/friendlink/save",
|
||||
data: row,
|
||||
success: function (res) {
|
||||
if (res.code == 200 || res.status == 200) {
|
||||
|
|
@ -195,31 +200,7 @@
|
|||
text: '确认',
|
||||
btnClass: 'btn-danger',
|
||||
action: function () {
|
||||
$.post("/admin/admin_log/del",data={id:id},function (res) {
|
||||
if (res.status == 200 || res.code == 200) lightyear.notify(res.msg, 'success', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
|
||||
else lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-neutral', 'top', 'center');
|
||||
location.reload();
|
||||
})
|
||||
}
|
||||
},
|
||||
cancel: {
|
||||
text: '取消',
|
||||
btnClass: 'btn-primary'
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
function delAll() {
|
||||
$.confirm({
|
||||
title: '重要提醒!',
|
||||
content: '清空后将不可恢复,请谨慎操作!',
|
||||
backgroundDismiss: true,
|
||||
buttons: {
|
||||
ok: {
|
||||
text: '确认',
|
||||
btnClass: 'btn-danger',
|
||||
action: function () {
|
||||
$.post("/admin/admin_log/empty",data={},function (res) {
|
||||
$.post("/admin/friendlink/del",data={id:id},function (res) {
|
||||
if (res.status == 200 || res.code == 200) lightyear.notify(res.msg, 'success', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
|
||||
else lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-neutral', 'top', 'center');
|
||||
location.reload();
|
||||
|
|
@ -252,7 +233,7 @@
|
|||
text: '确认',
|
||||
btnClass: 'btn-danger',
|
||||
action: function () {
|
||||
$.post("/admin/admin_log/del",data={id:checkID},function (res) {
|
||||
$.post("/admin/friendlink/del",data={id:checkID},function (res) {
|
||||
if (res.status == 200 || res.code == 200) { lightyear.notify(res.msg, 'success', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');location.reload();}
|
||||
else lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-neutral', 'top', 'center');
|
||||
})
|
||||
|
|
|
|||
|
|
@ -229,7 +229,6 @@
|
|||
});
|
||||
// 选中节点
|
||||
$('#tree').on('nodeSelected', function (event, data) {
|
||||
console.log(data);
|
||||
id = data.id;
|
||||
pid = data.pid;
|
||||
that.getImageList();
|
||||
|
|
@ -300,10 +299,11 @@
|
|||
if (!$("#fileUpload").val()) return;
|
||||
var formData = new FormData();
|
||||
formData.append("file", $("#fileUpload")[0].files[0]);
|
||||
formData.append("type",'images');
|
||||
formData.append("cid",id);
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "/admin/files/image",
|
||||
url: "/admin/files/upload",
|
||||
enctype: 'multipart/form-data',
|
||||
data: formData,
|
||||
processData: false,
|
||||
|
|
|
|||
Loading…
Reference in New Issue