/**
* 附件统一管理(common_attachment)
*
* 删除走后台 Attachment::delete():先经 FileStorageService 触发 FileDelete 事件
* (OSS / FTP 等自定义存储在此接管物理删除),再软删记录。
*/
layui.define(['table', 'layer', 'form', 'auth', 'http', 'jquery'], function (exports) {
var table = layui.table,
layer = layui.layer,
form = layui.form,
auth = layui.auth,
http = layui.http,
$ = layui.jquery;
// URL 由浏览器按当前页面目录解析(/admin/attachment/),只写动作名
var FUNC = {
index: 'index',
delete: 'delete'
};
function parse(res) {
return {
code: res.code,
msg: res.message,
count: res.count || 0,
data: res.data || []
};
}
function post(url, data, done) {
var loading = layer.load(2);
http.post(url, data).then(function (res) {
layer.close(loading);
if (res && res.code === 0) {
layer.msg(res.message || '操作成功', {icon: 1});
if (done) { done(res.data); }
} else {
layer.msg((res && res.message) || '操作失败', {icon: 2});
}
}).catch(function (err) {
layer.close(loading);
layer.msg('请求失败:' + (err && err.message ? err.message : '网络异常'), {icon: 2});
});
}
function checkedIds() {
var rows = table.checkStatus('dataTable').data || [];
var ids = [];
for (var i = 0; i < rows.length; i++) { ids.push(rows[i].id); }
return ids;
}
var obj = {
render: function () {
table.render({
elem: '#dataTable',
url: FUNC.index,
where: {},
toolbar: '#tableBar',
defaultToolbar: ['filter'],
method: 'get',
cols: [[
{type: 'checkbox', width: 50},
{field: 'id', title: 'ID', width: 70, sort: true},
{title: '预览', width: 70, templet: '#thumbTpl', align: 'center'},
{field: 'original', title: '文件名', minWidth: 220},
{field: 'module', title: '模块', width: 90},
{field: 'size_text', title: '大小', width: 100},
{field: 'storage', title: '存储', width: 90},
{field: 'uid', title: '上传者', width: 90},
{field: 'create_at_text', title: '时间', width: 170, sort: true},
{title: '操作', width: 130, toolbar: '#dataBar', fixed: 'right'}
]],
page: true,
limit: 20,
limits: [20, 50, 100],
response: {statusCode: 0},
parseData: parse,
done: function () {
auth.filterDom($('#dataTable').next('.layui-table-view'));
auth.filterDom('.layui-btn[data-perm]');
}
});
},
events: function () {
table.on('toolbar(dataTable)', function (o) {
if (o.event === 'deleteSelected') {
var ids = checkedIds();
if (ids.length === 0) {
return layer.msg('请先勾选要删除的附件', {icon: 2});
}
layer.confirm(
'确定删除选中的 ' + ids.length + ' 个附件?
会同时删除存储上的文件',
{title: '提示'},
function (index) {
layer.close(index);
post(FUNC.delete, {ids: ids}, function () {
table.reload('dataTable');
setTimeout(function () { location.reload(); }, 800);
});
}
);
}
});
table.on('tool(dataTable)', function (o) {
if (o.event === 'open') {
window.open(o.data.url, '_blank');
} else if (o.event === 'delete') {
layer.confirm('确定删除该附件?会同时删除存储上的文件', {title: '提示'}, function (index) {
layer.close(index);
post(FUNC.delete, {ids: [o.data.id]}, function () {
table.reload('dataTable');
setTimeout(function () { location.reload(); }, 800);
});
});
}
});
form.on('submit(data-search-btn)', function (o) {
table.reload('dataTable', {
where: {
keyword: o.field.keyword,
module: o.field.module,
only_image: o.field.only_image ? 1 : 0
},
page: {curr: 1}
});
return false;
});
$('#btn-reset').on('click', function () {
setTimeout(function () {
table.reload('dataTable', {
where: {keyword: '', module: '', only_image: 0},
page: {curr: 1}
});
}, 10);
});
}
};
obj.render();
obj.events();
exports('attachment', obj);
});