修改说明文档
This commit is contained in:
@@ -86,7 +86,7 @@ Pear Admin Flask
|
||||
git clone https://gitee.com/pear-admin/pear-admin-flask
|
||||
|
||||
# 切换分支
|
||||
git checkout v2
|
||||
git checkout mini
|
||||
```
|
||||
|
||||
##### 2. 安装依赖
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>图片上传</title>
|
||||
{% include 'admin/common/header.html' %}
|
||||
</head>
|
||||
<body class="pear-container">
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<table id="dataTable" lay-filter="dataTable"></table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="text/html" id="toolbar">
|
||||
{% if authorize("admin:file:add") %}
|
||||
<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="add">
|
||||
<i class="layui-icon layui-icon-add-1"></i>
|
||||
新增
|
||||
</button>
|
||||
{% endif %}
|
||||
{% if authorize("admin:file:delete") %}
|
||||
<button class="pear-btn pear-btn-sm" lay-event="batchRemove">
|
||||
<i class="layui-icon layui-icon-delete"></i>
|
||||
删除
|
||||
</button>
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="user-bar">
|
||||
{% if authorize("admin:file:delete") %}
|
||||
<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
|
||||
class="layui-icon layui-icon-delete"></i></button>
|
||||
{% endif %}
|
||||
</script>
|
||||
<script type="text/html" id="file-uploadTime">
|
||||
{{ ' {{layui.util.toDateString(d.create_time, "yyyy-MM-dd HH:mm:ss")}' |safe }}}
|
||||
</script>
|
||||
{% include 'admin/common/footer.html' %}
|
||||
<script>
|
||||
layui.use(['table', 'form', 'jquery'], function () {
|
||||
let table = layui.table
|
||||
let form = layui.form
|
||||
let $ = layui.jquery
|
||||
let cols = [
|
||||
[
|
||||
{
|
||||
type: 'checkbox'
|
||||
},
|
||||
{
|
||||
field: 'id',
|
||||
title: 'ID',
|
||||
sort: true,
|
||||
align: 'center',
|
||||
unresize: true,
|
||||
width: 80
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '文件名称',
|
||||
unresize: true,
|
||||
align: 'center'
|
||||
},
|
||||
{
|
||||
field: 'href',
|
||||
title: '图片',
|
||||
unresize: true,
|
||||
align: 'center',
|
||||
templet: function (d) {
|
||||
return '<img class="photo" style="max-width: 100%;\n' +
|
||||
' height: 100%;\n' +
|
||||
' cursor: pointer;" lay-event="photo" src=" ' + d.href + '"></i>'
|
||||
}
|
||||
|
||||
},
|
||||
{
|
||||
field: 'mime',
|
||||
title: 'mime类型',
|
||||
unresize: true,
|
||||
align: 'center'
|
||||
},
|
||||
{
|
||||
field: 'size',
|
||||
title: '文件大小',
|
||||
unresize: true,
|
||||
align: 'center'
|
||||
},
|
||||
{
|
||||
field: 'create_time',
|
||||
title: '创建时间',
|
||||
templet: '#file-uploadTime',
|
||||
unresize: true,
|
||||
align: 'center'
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
toolbar: '#user-bar',
|
||||
align: 'center',
|
||||
unresize: true,
|
||||
width: 200
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
table.render({
|
||||
elem: '#dataTable',
|
||||
url: 'table',
|
||||
page: true,
|
||||
cols: cols,
|
||||
skin: 'line',
|
||||
toolbar: '#toolbar',
|
||||
defaultToolbar: [{
|
||||
layEvent: 'refresh',
|
||||
icon: 'layui-icon-refresh',
|
||||
}, 'filter', 'print', 'exports']
|
||||
})
|
||||
|
||||
table.on('tool(dataTable)', function (obj) {
|
||||
if (obj.event === 'remove') {
|
||||
window.remove(obj)
|
||||
} else if (obj.event === 'photo') {
|
||||
window.photo(obj)
|
||||
}
|
||||
})
|
||||
|
||||
table.on('toolbar(dataTable)', function (obj) {
|
||||
if (obj.event === 'add') {
|
||||
window.add()
|
||||
} else if (obj.event === 'refresh') {
|
||||
window.refresh()
|
||||
} else if (obj.event === 'batchRemove') {
|
||||
window.batchRemove(obj)
|
||||
}
|
||||
})
|
||||
|
||||
//弹出窗设置 自己设置弹出百分比
|
||||
function screen () {
|
||||
if (typeof width !== 'number' || width === 0) {
|
||||
width = $(window).width() * 0.8
|
||||
}
|
||||
if (typeof height !== 'number' || height === 0) {
|
||||
height = $(window).height() - 20
|
||||
}
|
||||
return [width + 'px', height + 'px']
|
||||
}
|
||||
|
||||
window.add = function () {
|
||||
layer.open({
|
||||
type: 2,
|
||||
maxmin: true,
|
||||
title: '新增图片',
|
||||
shade: 0.1,
|
||||
area: screen(),
|
||||
content: 'upload'
|
||||
})
|
||||
}
|
||||
|
||||
window.remove = function (obj) {
|
||||
layer.confirm('确定要删除该图片', {
|
||||
icon: 3,
|
||||
title: '提示'
|
||||
}, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: 'upload',
|
||||
data: { id: obj.data['id'] },
|
||||
dataType: 'json',
|
||||
type: 'delete',
|
||||
success: function (res) {
|
||||
layer.close(loading)
|
||||
if (res.success) {
|
||||
layer.msg(res.msg, {
|
||||
icon: 1,
|
||||
time: 1000
|
||||
}, function () {
|
||||
obj.del()
|
||||
})
|
||||
} else {
|
||||
layer.msg(res.msg, {
|
||||
icon: 2,
|
||||
time: 1000
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
window.batchRemove = function (obj) {
|
||||
let data = table.checkStatus(obj.config.id).data
|
||||
if (data.length === 0) {
|
||||
layer.msg('未选中数据', {
|
||||
icon: 3,
|
||||
time: 1000
|
||||
})
|
||||
return false
|
||||
}
|
||||
var ids = []
|
||||
var hasCheck = table.checkStatus('dataTable')
|
||||
var hasCheckData = hasCheck.data
|
||||
if (hasCheckData.length > 0) {
|
||||
$.each(hasCheckData, function (index, element) {
|
||||
ids.push(element.id)
|
||||
})
|
||||
}
|
||||
layer.confirm('确定要删除这些图片', {
|
||||
icon: 3,
|
||||
title: '提示'
|
||||
}, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: "{{ url_for('file.batch_remove') }}",
|
||||
data: { ids: ids },
|
||||
dataType: 'json',
|
||||
type: 'POST',
|
||||
success: function (res) {
|
||||
layer.close(loading)
|
||||
if (res.success) {
|
||||
layer.msg(res.msg, {
|
||||
icon: 1,
|
||||
time: 1000
|
||||
}, function () {
|
||||
table.reload('dataTable')
|
||||
})
|
||||
} else {
|
||||
layer.msg(res.msg, {
|
||||
icon: 2,
|
||||
time: 1000
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
window.refresh = function () {
|
||||
table.reload('dataTable')
|
||||
}
|
||||
// 查看大图
|
||||
window.photo = function (obj) {
|
||||
if (!obj.data.href || obj.data.href === '') {
|
||||
layer.msg('图片地址错误!')
|
||||
return
|
||||
}
|
||||
var auto_img = {}
|
||||
var img = new Image()
|
||||
img.src = obj.data.href
|
||||
img.onload = function () {
|
||||
var max_height = $(window).height() - 100
|
||||
var max_width = $(window).width()
|
||||
var rate1 = max_height / img.height
|
||||
var rate2 = max_width / img.width
|
||||
var rate3 = 1
|
||||
var rate = Math.min(rate1, rate2, rate3)
|
||||
auto_img.height = img.height * rate
|
||||
auto_img.width = img.width * rate
|
||||
layer.open({
|
||||
type: 1,
|
||||
title: false,
|
||||
area: ['auto'],
|
||||
skin: 'layui-layer-nobg', //没有背景色
|
||||
shadeClose: true,
|
||||
content: '<img src=\'' + obj.data['href'] + '\' width=\'' + auto_img.width + 'px\' height=\'' + auto_img.height + 'px\'>'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
</script>
|
||||
@@ -0,0 +1,65 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
{% include 'admin/common/header.html' %}
|
||||
<style>
|
||||
.pear-container {
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="layui-row layui-col-space15">
|
||||
<div class="layui-col-md12">
|
||||
<div class="layui-card">
|
||||
<div class="layui-tab-content">
|
||||
<fieldset class="layui-elem-field layui-field-title">
|
||||
<legend>新增图片</legend>
|
||||
</fieldset>
|
||||
<form class="layui-form edit-form">
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">
|
||||
新增图片
|
||||
</label>
|
||||
<button type="button" class="layui-btn layui-btn-normal" id="logo-img">选择文件</button>
|
||||
<button type="button" class="layui-btn" id="logo-upload-button">开始上传</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% include 'admin/common/footer.html' %}
|
||||
<script>
|
||||
layui.use(['jquery', 'element', 'form', 'upload'], function () {
|
||||
var $ = layui.jquery;
|
||||
var element = layui.element;
|
||||
var form = layui.form;
|
||||
var upload = layui.upload;
|
||||
//选完文件后不自动上传
|
||||
upload.render({
|
||||
elem: '#logo-img'
|
||||
, url: "{{ url_for('file.upload') }}"
|
||||
, auto: false
|
||||
, exts: 'jpg|png|gif|bmp|jpeg'
|
||||
, size: 1000
|
||||
, bindAction: '#logo-upload-button'
|
||||
, done: function (res) {
|
||||
if (res.success) {
|
||||
layer.msg(res.msg, {icon: 1, time: 500}, function () {
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
|
||||
window.parent.location.reload();
|
||||
});
|
||||
} else {
|
||||
layer.msg(res.msg, {icon: 2});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,26 @@
|
||||
import os
|
||||
from flask import current_app
|
||||
from applications.common.utils.upload import photos
|
||||
from applications.extensions import db
|
||||
from applications.models import Photo
|
||||
|
||||
|
||||
def upload_one(photo, mime):
|
||||
filename = photos.save(photo)
|
||||
file_url = photos.url(filename)
|
||||
|
||||
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
|
||||
size = os.path.getsize(upload_url + '/' + filename)
|
||||
photo = Photo(name=filename, href=file_url, mime=mime, size=size)
|
||||
db.session.add(photo)
|
||||
db.session.commit()
|
||||
return file_url
|
||||
|
||||
|
||||
def delete_photo_by_id(_id):
|
||||
photo_name = Photo.query.filter_by(id=_id).first().name
|
||||
photo = Photo.query.filter_by(id=_id).delete()
|
||||
db.session.commit()
|
||||
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
|
||||
os.remove(upload_url + '/' + photo_name)
|
||||
return photo
|
||||
@@ -0,0 +1,83 @@
|
||||
import os
|
||||
from flask import Blueprint, request, render_template, jsonify, current_app, make_response
|
||||
from flask_restful import Api, Resource, marshal
|
||||
from sqlalchemy import desc
|
||||
|
||||
from applications.common.serialization import photo_fields
|
||||
from applications.common.utils.http import fail_api, success_api, table_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.extensions import db
|
||||
from applications.models import Photo
|
||||
|
||||
from applications.view.file._utils import delete_photo_by_id, upload_one
|
||||
|
||||
file_bp = Blueprint('file', __name__, url_prefix='/file')
|
||||
file_api = Api(file_bp)
|
||||
|
||||
|
||||
# 图片管理
|
||||
@file_bp.get('/')
|
||||
@authorize("admin:file:main", log=True)
|
||||
def index():
|
||||
return render_template('admin/file/photo.html')
|
||||
|
||||
|
||||
# 图片数据
|
||||
@file_bp.get('/table')
|
||||
@authorize("admin:file:main", log=True)
|
||||
def table():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
photo_paginate = Photo.query.order_by(desc(Photo.create_time)).paginate(page=page, per_page=limit, error_out=False)
|
||||
data = marshal(photo_paginate.items, photo_fields)
|
||||
return table_api(data=data, count=photo_paginate.total)
|
||||
|
||||
|
||||
@file_api.resource('/upload')
|
||||
class Upload(Resource):
|
||||
@authorize("admin:file:add", log=True)
|
||||
def get(self):
|
||||
return make_response(render_template('admin/file/photo_add.html'))
|
||||
|
||||
@authorize("admin:file:add", log=True)
|
||||
def post(self):
|
||||
if 'file' in request.files:
|
||||
photo = request.files['file']
|
||||
mime = request.files['file'].content_type
|
||||
file_url = upload_one(photo=photo, mime=mime)
|
||||
|
||||
res = {
|
||||
"msg": "上传成功",
|
||||
"code": 0,
|
||||
"success": True,
|
||||
"data":
|
||||
{"src": file_url}
|
||||
}
|
||||
return jsonify(res)
|
||||
return fail_api()
|
||||
|
||||
@authorize("admin:file:delete", log=True)
|
||||
def delete(self):
|
||||
_id = request.form.get('id')
|
||||
res = delete_photo_by_id(_id)
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
|
||||
|
||||
# 图片批量删除
|
||||
@file_bp.route('/batchRemove', methods=['GET', 'POST'])
|
||||
@authorize("admin:file:delete", log=True)
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
photo_name = Photo.query.filter(Photo.id.in_(ids)).all()
|
||||
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
|
||||
for p in photo_name:
|
||||
os.remove(upload_url + '/' + p.name)
|
||||
photo = Photo.query.filter(Photo.id.in_(ids)).delete(synchronize_session=False)
|
||||
db.session.commit()
|
||||
if photo:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
@@ -2,7 +2,7 @@ from flask import Flask
|
||||
|
||||
from applications.view.system.logs import admin_log
|
||||
from applications.view.system.index import admin_bp
|
||||
from applications.view.upload.file import file_bp
|
||||
from applications.view.file.file import file_bp
|
||||
from applications.view.roles.role import role_bp
|
||||
from applications.view.users import users_bp
|
||||
|
||||
|
||||
Reference in New Issue
Block a user