/**
* media.js - 媒体数据管理逻辑(列表+筛选+分页 / 新增编辑弹窗 / 导入导出 / 详情)
*/
$(function () {
renderShell('媒体数据', '数据管理 / 媒体数据');
renderPage();
initPage('media', function (user) {
if (!checkPagePermission('media')) return;
loadList(1);
});
var currentPage = 1;
/** 渲染页面内容(工具栏+表格+分页) */
function renderPage() {
$('#page-content').html(
'
' +
'
' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
'
' +
'
' +
' ' +
'
'
);
$('#check-all').on('change', function () { $('.row-check').prop('checked', this.checked); });
}
function loadList(page) {
currentPage = page;
var params = buildFilterParam();
params.page = page;
params.limit = PAGE_SIZE;
httpGet('media/list.php', params).then(function (data) {
var rows = data.list || [];
var dict = data.dict || {};
fillDict(dict);
var html = '';
rows.forEach(function (r) {
html += '' +
' | ' +
'' + r.id + ' | ' +
'' + escHtml(r.owner_name || r.owner_type + '#' + r.owner_id) + ' | ' +
'' + escHtml(r.platform || '-') + ' | ' +
'' + escHtml(r.account_id) + ' | ' +
'' + escHtml(r.certification_type || '-') + ' | ' +
'' + escHtml(r.account_level || '-') + ' | ' +
'' + (r.follower_count || 0) + ' | ' +
'' + (r.avg_read_count || 0) + ' | ' +
'' + escHtml(r.content_categories || '-') + ' | ' +
'' +
' 详情' +
' 编辑' +
' 删除' +
' |
';
});
if (!rows.length) {
html = '| 暂无数据 |
';
}
$('#media-tbody').html(html);
renderPagination($('#pagination'), data, loadList);
});
}
function fillDict(dict) {
if (dict.platforms && dict.platforms.length) {
var html = '';
dict.platforms.forEach(function (p) { html += ''; });
$('#filter-platform').html(html);
}
if (dict.certification_types && dict.certification_types.length) {
var h2 = '';
dict.certification_types.forEach(function (p) { h2 += ''; });
$('#filter-cert').html(h2);
}
if (dict.account_levels && dict.account_levels.length) {
var h3 = '';
dict.account_levels.forEach(function (p) { h3 += ''; });
$('#filter-level').html(h3);
}
}
$('#btn-search').on('click', function () { loadList(1); });
$('#btn-reset').on('click', function () {
$('.toolbar [data-filter]').val('');
loadList(1);
});
$('#btn-add').on('click', function () { openForm(null); });
window.editMedia = function (id) {
httpGet('media/detail.php', { id: id }).then(function (d) {
openForm(d.media);
});
};
function openForm(media) {
var isEdit = !!media;
var m = media || {};
var content =
'';
var idx = Dialog.open({
title: isEdit ? '编辑媒体账号' : '新增媒体账号',
area: ['720px', 'auto'],
content: content,
btn: false
});
$('#form-cancel').on('click', function () { Dialog.close(idx); });
$('#media-form').on('submit', function (e) {
e.preventDefault();
var data = {};
$(this).find('input, select, textarea').each(function () {
var name = $(this).attr('name');
if (!name) return;
if ($(this).attr('type') === 'number') {
data[name] = $(this).val() === '' ? '' : parseInt($(this).val(), 10);
} else {
data[name] = $(this).val();
}
});
if (!data.platform || !data.account_id) { Dialog.error('平台和账号为必填项'); return; }
if (!isEdit && (!data.owner_type || !data.owner_id)) { Dialog.error('归属类型和归属ID为必填项'); return; }
if (isEdit) data.id = m.id;
httpPost(isEdit ? 'media/update.php' : 'media/add.php', data).then(function () {
Dialog.success('保存成功', function () {
Dialog.close(idx);
loadList(currentPage);
});
});
});
}
function platformOpts(sel) {
var list = ['phone', 'email', 'wechat', 'douyin', 'linkedin', 'x', 'facebook', 'instagram', 'tiktok', 'other'];
var html = '';
list.forEach(function (p) {
html += '';
});
return html;
}
function levelOpts(sel) {
var list = ['头部', '腰部', '尾部', '达人', '素人', '媒体号'];
var html = '';
list.forEach(function (p) {
html += '';
});
return html;
}
function certOpts(sel) {
var list = ['个人认证', '企业认证', '媒体机构认证', '未认证'];
var html = '';
list.forEach(function (p) {
html += '';
});
return html;
}
window.delMedia = function (id) {
Dialog.confirm('确定删除该媒体账号吗?(软删除)', function () {
httpPost('media/delete.php', { id: id }).then(function () {
Dialog.success('删除成功', function () { loadList(currentPage); });
});
});
};
$('#btn-del-batch').on('click', function () {
var ids = [];
$('.row-check:checked').each(function () { ids.push($(this).val()); });
if (!ids.length) { Dialog.error('请先勾选记录'); return; }
Dialog.confirm('确定删除选中的 ' + ids.length + ' 条记录吗?', function () {
httpPost('media/delete.php', { ids: ids.join(',') }).then(function () {
Dialog.success('删除成功', function () { loadList(currentPage); });
});
});
});
$('#btn-import').on('click', function () {
triggerFileInput('.csv', function (file) {
var fd = new FormData();
fd.append('file', file);
httpPost('media/import.php', fd, true).then(function (d) {
Dialog.alert('导入结果:成功 ' + d.inserted + ' 条,失败 ' + d.failed + ' 条', function () {
loadList(1);
});
});
});
});
/* ---------- 导出(仅导出勾选的媒体数据,未勾选弹窗提醒) ---------- */
$('#btn-export').on('click', function () {
var ids = [];
$('.row-check:checked').each(function () { ids.push($(this).val()); });
if (!ids.length) {
layer.alert('请先选择需要导出的媒体数据', { icon: 2, title: '提示' });
return;
}
download('media/export.php', { ids: ids.join(',') });
});
window.viewMedia = function (id) {
httpGet('media/detail.php', { id: id }).then(function (d) {
var m = d.media;
var html =
'' +
'
' + escHtml(m.platform) + ':' + escHtml(m.account_id) + '
' +
'
' +
'
归属:' + escHtml(m.owner_type === 'person' ? '个人' : '企业') + ' #' + m.owner_id + '
' +
'
主页:' + (m.profile_url ? '
打开' : '-') + '
' +
'
等级:' + escHtml(m.account_level || '-') + '
' +
'
认证:' + escHtml(m.certification_type || '-') + '
' +
'
粉丝数:' + (m.follower_count || 0) + '
' +
'
平均阅读:' + (m.avg_read_count || 0) + '
' +
'
内容领域:' + escHtml(m.content_categories || '-') + '
' +
'
创建时间:' + fmtDate(m.created_at) + '
' +
'
' +
'
备注
' + escHtml(m.remark || '-') + '
' +
'
特殊要求
' + escHtml(m.special_requirements || '-') + '
' +
'
内部评估
' + escHtml(m.media_remark || '-') + '
' +
'
';
Dialog.open({ title: '媒体账号详情', area: ['680px', 'auto'], content: html, btn: ['关闭'] });
});
}
});