save
This commit is contained in:
parent
c1f0e532b3
commit
113b2fe7a6
@ -4,15 +4,65 @@ namespace plugin\admin\app\controller;
|
||||
|
||||
use Exception;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
use plugin\admin\app\controller\Base;
|
||||
use plugin\admin\app\controller\Crud;
|
||||
use plugin\admin\app\model\Upload;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
/**
|
||||
* 上传
|
||||
* 附件管理
|
||||
*/
|
||||
class UploadController extends Base
|
||||
class UploadController extends Crud
|
||||
{
|
||||
/**
|
||||
* 不需要鉴权的方法
|
||||
*/
|
||||
public $noNeedAuth = ['file', 'image'];
|
||||
|
||||
/**
|
||||
* @var Upload
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new Upload;
|
||||
}
|
||||
|
||||
/**
|
||||
* 浏览
|
||||
* @return Response
|
||||
*/
|
||||
public function index(): Response
|
||||
{
|
||||
return view("upload/index");
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function insert(Request $request): Response
|
||||
{
|
||||
return view("upload/insert");
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
return not_found();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
@ -37,9 +87,44 @@ class UploadController extends Base
|
||||
}
|
||||
$data = $this->base($request, '/upload/files/'.date('Ymd'));
|
||||
return $this->json(0, '上传成功', [
|
||||
'path' => $data['data']['path'],
|
||||
'name' => $data['data']['name'],
|
||||
'size' => $data['data']['size'],
|
||||
'path' => $data['path'],
|
||||
'name' => $data['name'],
|
||||
'size' => $data['size'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传附件
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function attachment(Request $request): Response
|
||||
{
|
||||
$file = current($request->file());
|
||||
if (!$file || !$file->isValid()) {
|
||||
return $this->json(1, '未找到文件');
|
||||
}
|
||||
$data = $this->base($request, '/upload/files/'.date('Ymd'));
|
||||
$upload = new Upload;
|
||||
$upload->admin_id = admin_id();
|
||||
$upload->name = $data['name'];
|
||||
[
|
||||
$upload->url,
|
||||
$upload->name,
|
||||
$_,
|
||||
$upload->file_size,
|
||||
$upload->mime_type,
|
||||
$upload->image_width,
|
||||
$upload->image_height,
|
||||
$upload->ext
|
||||
] = array_values($data);
|
||||
$upload->category = $request->post('category');
|
||||
$upload->save();
|
||||
return $this->json(0, '上传成功', [
|
||||
'path' => $data['path'],
|
||||
'name' => $data['name'],
|
||||
'size' => $data['size'],
|
||||
]);
|
||||
}
|
||||
|
||||
@ -105,7 +190,7 @@ class UploadController extends Base
|
||||
public function image(Request $request): Response
|
||||
{
|
||||
$data = $this->base($request, '/upload/img/'.date('Ymd'));
|
||||
$realpath = $data['data']['realpath'];
|
||||
$realpath = $data['realpath'];
|
||||
try {
|
||||
$img = Image::make($realpath);
|
||||
$max_height = 1170;
|
||||
@ -128,9 +213,9 @@ class UploadController extends Base
|
||||
'code' => 0,
|
||||
'msg' => '上传成功',
|
||||
'data' => [
|
||||
'path' => $data['data']['path'],
|
||||
'name' => $data['data']['name'],
|
||||
'size' => $data['data']['size'],
|
||||
'path' => $data['path'],
|
||||
'name' => $data['name'],
|
||||
'size' => $data['size'],
|
||||
]
|
||||
]);
|
||||
}
|
||||
@ -166,16 +251,22 @@ class UploadController extends Base
|
||||
$full_path = $base_dir . $relative_path;
|
||||
$file_size = $file->getSize();
|
||||
$file_name = $file->getUploadName();
|
||||
$mime_type = $file->getUploadMimeType();
|
||||
$file->move($full_path);
|
||||
$image_with = $image_height = 0;
|
||||
if ($img_info = getimagesize($full_path)) {
|
||||
[$image_with, $image_height] = $img_info;
|
||||
$mime_type = $img_info['mime'];
|
||||
}
|
||||
return [
|
||||
'code' => 0,
|
||||
'msg' => '上传成功',
|
||||
'data' => [
|
||||
'path' => "/app/admin/$relative_path",
|
||||
'name' => $file_name,
|
||||
'realpath' => $full_path,
|
||||
'size' => $this->formatSize($file_size)
|
||||
]
|
||||
'path' => "/app/admin/$relative_path",
|
||||
'name' => $file_name,
|
||||
'realpath' => $full_path,
|
||||
'size' => $file_size,
|
||||
'mime_type' => $mime_type,
|
||||
'image_with' => $image_with,
|
||||
'image_height' => $image_height,
|
||||
'ext' => $ext,
|
||||
];
|
||||
}
|
||||
|
||||
@ -184,7 +275,8 @@ class UploadController extends Base
|
||||
* @param $file_size
|
||||
* @return string
|
||||
*/
|
||||
protected function formatSize($file_size) {
|
||||
protected function formatSize($file_size): string
|
||||
{
|
||||
$size = sprintf("%u", $file_size);
|
||||
if($size == 0) {
|
||||
return("0 Bytes");
|
||||
|
46
src/plugin/admin/app/model/Upload.php
Normal file
46
src/plugin/admin/app/model/Upload.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\admin\app\model;
|
||||
|
||||
use plugin\admin\app\model\Base;
|
||||
|
||||
/**
|
||||
* @property integer $id 主键(主键)
|
||||
* @property string $name 名字
|
||||
* @property string $url url
|
||||
* @property integer $admin_id 管理员id
|
||||
* @property integer $user_id 用户id
|
||||
* @property integer $file_size 文件大小
|
||||
* @property integer $mime_type mime类型
|
||||
* @property integer $image_width 图片宽度
|
||||
* @property integer $image_height 图片高度
|
||||
* @property string $ext 扩展名
|
||||
* @property string $storage 存储位置
|
||||
* @property string $created_at 上传时间
|
||||
* @property string $category 类别
|
||||
*/
|
||||
class Upload extends Base
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'upload';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
|
||||
}
|
328
src/plugin/admin/app/view/upload/index.html
Normal file
328
src/plugin/admin/app/view/upload/index.html
Normal file
@ -0,0 +1,328 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>浏览页面</title>
|
||||
<link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
|
||||
<link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
|
||||
</head>
|
||||
<body class="pear-container">
|
||||
|
||||
<!-- 顶部查询表单 -->
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<form class="layui-form top-search-from">
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">主键</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="number" name="id" value="" class="layui-input">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item layui-inline">
|
||||
<label class="layui-form-label"></label>
|
||||
<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
|
||||
<i class="layui-icon layui-icon-search"></i>查询
|
||||
</button>
|
||||
<button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
|
||||
<i class="layui-icon layui-icon-refresh"></i>重置
|
||||
</button>
|
||||
</div>
|
||||
<div class="toggle-btn">
|
||||
<a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
|
||||
<a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 数据表格 -->
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<table id="data-table" lay-filter="data-table"></table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 表格顶部工具栏 -->
|
||||
<script type="text/html" id="table-toolbar">
|
||||
<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
|
||||
<i class="layui-icon layui-icon-add-1"></i>新增
|
||||
</button>
|
||||
<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
|
||||
<i class="layui-icon layui-icon-delete"></i>删除
|
||||
</button>
|
||||
</script>
|
||||
|
||||
<!-- 表格行工具栏 -->
|
||||
<script type="text/html" id="table-bar">
|
||||
<button class="pear-btn pear-btn-xs tool-btn" lay-event="edit">编辑</button>
|
||||
<button class="pear-btn pear-btn-xs tool-btn" lay-event="remove">删除</button>
|
||||
</script>
|
||||
|
||||
<script src="/app/admin/component/layui/layui.js"></script>
|
||||
<script src="/app/admin/component/pear/pear.js"></script>
|
||||
<script src="/app/admin/admin/js/index.js"></script>
|
||||
<script>
|
||||
|
||||
// 相关接口
|
||||
const PRIMARY_KEY = 'id';
|
||||
const SELECT_API = "/app/admin/upload/select";
|
||||
const UPDATE_API = "/app/admin/upload/update";
|
||||
const DELETE_API = "/app/admin/upload/delete";
|
||||
const INSERT_URL = "/app/admin/upload/insert";
|
||||
const UPDATE_URL = "/app/admin/upload/update";
|
||||
|
||||
// 表格渲染
|
||||
layui.use(['table', 'form', 'jquery', 'common', 'popup', 'util'], function() {
|
||||
let table = layui.table;
|
||||
let form = layui.form;
|
||||
let $ = layui.jquery;
|
||||
let common = layui.common;
|
||||
let util = layui.util;
|
||||
|
||||
// 表头参数
|
||||
let cols = [
|
||||
{
|
||||
type: "checkbox"
|
||||
},{
|
||||
title: "主键",
|
||||
field: "id",
|
||||
sort: true,
|
||||
},{
|
||||
title: "名字",
|
||||
field: "name",
|
||||
},{
|
||||
title: "url",
|
||||
field: "url",
|
||||
templet: function (d) {
|
||||
return '<a href="' + encodeURI(d['url']) + '" target="_blank">' + util.escape(d['url']) + '</a>';
|
||||
}
|
||||
},{
|
||||
title: "管理员id",
|
||||
field: "admin_id",
|
||||
hide: true,
|
||||
},{
|
||||
title: "用户id",
|
||||
field: "user_id",
|
||||
hide: true,
|
||||
},{
|
||||
title: "文件大小",
|
||||
field: "file_size",
|
||||
},{
|
||||
title: "mime类型",
|
||||
field: "mime_type",
|
||||
},{
|
||||
title: "图片宽度",
|
||||
field: "image_width",
|
||||
},{
|
||||
title: "图片高度",
|
||||
field: "image_height",
|
||||
},{
|
||||
title: "扩展名",
|
||||
field: "ext",
|
||||
},{
|
||||
title: "存储位置",
|
||||
field: "storage",
|
||||
hide: true,
|
||||
},{
|
||||
title: "上传时间",
|
||||
field: "created_at",
|
||||
hide: true,
|
||||
},{
|
||||
title: "类别",
|
||||
field: "category",
|
||||
templet: function (d) {
|
||||
let field = "category";
|
||||
if (typeof d[field] == "undefined") return "";
|
||||
let items = [];
|
||||
layui.each((d[field] + "").split(","), function (k , v) {
|
||||
items.push(apiResults[field][v] || v);
|
||||
});
|
||||
return util.escape(items.join(","));
|
||||
}
|
||||
},{
|
||||
title: "操作",
|
||||
toolbar: "#table-bar",
|
||||
align: "center",
|
||||
fixed: "right",
|
||||
}
|
||||
];
|
||||
|
||||
// 渲染表格
|
||||
function render()
|
||||
{
|
||||
table.render({
|
||||
elem: "#data-table",
|
||||
url: SELECT_API,
|
||||
page: true,
|
||||
cols: [cols],
|
||||
skin: "line",
|
||||
size: "lg",
|
||||
toolbar: "#table-toolbar",
|
||||
autoSort: false,
|
||||
defaultToolbar: [{
|
||||
title: "刷新",
|
||||
layEvent: "refresh",
|
||||
icon: "layui-icon-refresh",
|
||||
}, "filter", "print", "exports"]
|
||||
});
|
||||
}
|
||||
|
||||
// 获取表格中下拉或树形组件数据
|
||||
let apis = [];
|
||||
apis.push(['category', '/app/admin/dict/get/upload']);
|
||||
let apiResults = {};
|
||||
apiResults['category'] = [];
|
||||
let count = apis.length;
|
||||
layui.each(apis, function (k, item) {
|
||||
let [field, url] = item;
|
||||
$.ajax({
|
||||
url: url,
|
||||
dateType: "json",
|
||||
success: function (res) {
|
||||
function travel(items) {
|
||||
for (let k in items) {
|
||||
let item = items[k];
|
||||
apiResults[field][item.value] = item.name;
|
||||
if (item.children) {
|
||||
travel(item.children);
|
||||
}
|
||||
}
|
||||
}
|
||||
travel(res.data);
|
||||
},
|
||||
complete: function () {
|
||||
if (--count === 0) {
|
||||
render();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
if (!count) {
|
||||
render();
|
||||
}
|
||||
|
||||
// 编辑或删除行事件
|
||||
table.on('tool(data-table)', function(obj) {
|
||||
if (obj.event === 'remove') {
|
||||
remove(obj);
|
||||
} else if (obj.event === 'edit') {
|
||||
edit(obj);
|
||||
}
|
||||
});
|
||||
|
||||
// 表格顶部工具栏事件
|
||||
table.on('toolbar(data-table)', function(obj) {
|
||||
if (obj.event === 'add') {
|
||||
add();
|
||||
} else if (obj.event === 'refresh') {
|
||||
refreshTable();
|
||||
} else if (obj.event === 'batchRemove') {
|
||||
batchRemove(obj);
|
||||
}
|
||||
});
|
||||
|
||||
// 表格顶部搜索事件
|
||||
form.on('submit(table-query)', function(data) {
|
||||
table.reload('data-table', {
|
||||
where: data.field
|
||||
})
|
||||
return false;
|
||||
});
|
||||
|
||||
// 表格顶部搜索重置事件
|
||||
form.on('submit(table-reset)', function(data) {
|
||||
table.reload('data-table', {
|
||||
where: []
|
||||
})
|
||||
});
|
||||
|
||||
// 表格排序事件
|
||||
table.on('sort(data-table)', function(obj){
|
||||
table.reload('data-table', {
|
||||
initSort: obj,
|
||||
scrollPos: 'fixed',
|
||||
where: {
|
||||
field: obj.field,
|
||||
order: obj.type
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 表格新增数据
|
||||
let add = function() {
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: '新增',
|
||||
shade: 0.1,
|
||||
area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'450px'],
|
||||
content: INSERT_URL
|
||||
});
|
||||
}
|
||||
|
||||
// 表格编辑数据
|
||||
let edit = function(obj) {
|
||||
let value = obj.data[PRIMARY_KEY];
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: '修改',
|
||||
shade: 0.1,
|
||||
area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'450px'],
|
||||
content: UPDATE_URL + '?' + PRIMARY_KEY + '=' + value
|
||||
});
|
||||
}
|
||||
|
||||
// 删除一行
|
||||
let remove = function(obj) {
|
||||
return doRemove(obj.data[PRIMARY_KEY]);
|
||||
}
|
||||
|
||||
// 删除多行
|
||||
let batchRemove = function(obj) {
|
||||
let checkIds = common.checkField(obj, PRIMARY_KEY);
|
||||
if (checkIds === "") {
|
||||
layui.popup.warning('未选中数据');
|
||||
return false;
|
||||
}
|
||||
doRemove(checkIds.split(','));
|
||||
}
|
||||
|
||||
// 执行删除
|
||||
let doRemove = function (ids) {
|
||||
let data = {};
|
||||
data[PRIMARY_KEY] = ids;
|
||||
layer.confirm('确定删除?', {
|
||||
icon: 3,
|
||||
title: '提示'
|
||||
}, function(index) {
|
||||
layer.close(index);
|
||||
let loading = layer.load();
|
||||
$.ajax({
|
||||
url: DELETE_API,
|
||||
data: data,
|
||||
dataType: 'json',
|
||||
type: 'post',
|
||||
success: function(res) {
|
||||
layer.close(loading);
|
||||
if (res.code) {
|
||||
return layui.popup.failure(res.msg);
|
||||
}
|
||||
return layui.popup.success('操作成功', refreshTable);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// 刷新表格数据
|
||||
window.refreshTable = function(param) {
|
||||
table.reloadData('data-table', {
|
||||
scrollPos: 'fixed'
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
87
src/plugin/admin/app/view/upload/insert.html
Normal file
87
src/plugin/admin/app/view/upload/insert.html
Normal file
@ -0,0 +1,87 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>新增页面</title>
|
||||
<link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
|
||||
<link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form class="layui-form" action="">
|
||||
|
||||
<div class="mainBox">
|
||||
<div class="main-container">
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">类别</label>
|
||||
<div class="layui-input-block">
|
||||
<div name="category" id="category" value="" ></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label required">url</label>
|
||||
<div class="layui-input-block">
|
||||
<span></span>
|
||||
<input type="text" style="display:none" name="url" value="" />
|
||||
<button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="url">
|
||||
<i class="layui-icon"></i>上传文件
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
|
||||
<script src="/app/admin/component/layui/layui.js"></script>
|
||||
<script src="/app/admin/component/pear/pear.js"></script>
|
||||
<script>
|
||||
|
||||
// 字段 url url
|
||||
layui.use(['upload', 'layer', 'jquery', 'popup', 'util'], function() {
|
||||
let input = layui.jquery('#url').prev();
|
||||
input.prev().html(layui.util.escape(input.val()));
|
||||
layui.upload.render({
|
||||
elem: "#url",
|
||||
accept: 'file',
|
||||
data: {category: function () {
|
||||
return layui.jquery('input[name="category"]').val();
|
||||
}},
|
||||
url: '/app/admin/upload/attachment',
|
||||
field: '__file__',
|
||||
done: function (res) {
|
||||
if (res.code) return layui.popup.failure(res.msg);
|
||||
parent.refreshTable();
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 字段 类别 category
|
||||
layui.use(["jquery", "xmSelect"], function() {
|
||||
layui.jquery.ajax({
|
||||
url: "/app/admin/dict/get/upload",
|
||||
dataType: "json",
|
||||
success: function (e) {
|
||||
let value = layui.jquery("#category").attr("value");
|
||||
let initValue = value ? value.split(",") : [];
|
||||
layui.xmSelect.render({
|
||||
el: "#category",
|
||||
name: "category",
|
||||
initValue: initValue,
|
||||
data: e.data,
|
||||
model: {"icon":"hidden","label":{"type":"text"}},
|
||||
clickClose: 'true',
|
||||
radio: 'true',
|
||||
})
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user