refactor(rest-api):调整文件接口数据

This commit is contained in:
zhengxinonly
2021-07-26 04:21:13 +08:00
parent 64748e513d
commit 2b7304e615
2 changed files with 48 additions and 53 deletions
+27 -30
View File
@@ -15,8 +15,18 @@ from applications.models import Photo
file_api = Api(api_bp, prefix='/file')
@file_api.resource('/upload')
class Upload(Resource):
@file_api.resource('/photos')
class FilePhotos(Resource):
@authorize("admin:file:main", log=True)
def get(self):
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, code=0)
@authorize("admin:file:add", log=True)
def post(self):
if 'file' in request.files:
@@ -36,34 +46,8 @@ class Upload(Resource):
@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_api.resource('/table')
class FileTable(Resource):
"""图片数据"""
@authorize("admin:file:main", log=True)
def get(self):
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, code=0)
@file_api.resource('/batch_remove')
class FileBatchRemove(Resource):
"""图片批量删除"""
@authorize("admin:file:delete", log=True)
def post(self):
"""图片批量删除"""
# TODO bugs 图片删除失败
ids = request.form.getlist('ids[]')
photo_name = Photo.query.filter(Photo.id.in_(ids)).all()
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
@@ -75,3 +59,16 @@ class FileBatchRemove(Resource):
return success_api(msg="删除成功")
else:
return fail_api(msg="删除失败")
@file_api.resource('/photo/<int:photo_id>')
class FileTable(Resource):
"""图片数据"""
@authorize("admin:file:delete", log=True)
def delete(self, photo_id):
res = delete_photo_by_id(photo_id)
if res:
return success_api(msg="删除成功")
else:
return fail_api(msg="删除失败")
+21 -23
View File
@@ -35,26 +35,20 @@
</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 %}
<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="add">
<i class="layui-icon layui-icon-add-1"></i>
新增
</button>
<button class="pear-btn pear-btn-sm" lay-event="batchRemove">
<i class="layui-icon layui-icon-delete"></i>
删除
</button>
</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 %}
<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
<i class="layui-icon layui-icon-delete"></i>
</button>
</script>
<script type="text/html" id="file-uploadTime">
{{ ' {{layui.util.toDateString(d.create_time, "yyyy-MM-dd HH:mm:ss")}' |safe }}}
@@ -67,8 +61,12 @@
let form = layui.form
let $ = layui.jquery
let upload = layui.upload
let MODULE_PATH = '/api/v1/file/'
let MODULE_PATH = '/api/v1/file'
let FILE_API = {
'PHOTOS': () => `${MODULE_PATH}/photos`,
'PHOTO': (photo_id) => `${MODULE_PATH}/photo/${photo_id}`,
}
let cols = [
[
@@ -130,7 +128,7 @@
table.render({
elem: '#photo_table',
url: MODULE_PATH + 'table',
url: FILE_API.PHOTOS(),
page: true,
cols: cols,
skin: 'line',
@@ -189,7 +187,7 @@
layer.close(index)
let loading = layer.load()
$.ajax({
url: MODULE_PATH + 'upload',
url: FILE_API.PHOTO(obj.data['id']),
data: { id: obj.data['id'] },
dataType: 'json',
type: 'delete',
@@ -237,7 +235,7 @@
layer.close(index)
let loading = layer.load()
$.ajax({
url: MODULE_PATH + "batch_remove",
url: FILE_API.PHOTOS(),
data: { ids: ids },
dataType: 'json',
type: 'POST',
@@ -297,7 +295,7 @@
// 选完文件后不自动上传
upload.render({
elem: '#logo-img'
, url: MODULE_PATH + "upload"
, url: FILE_API.PHOTOS()
, auto: false
, exts: 'jpg|png|gif|bmp|jpeg'
, size: 1000