/**
* media.js - 媒体数据管理逻辑(列表+筛选+分页 / 新增编辑弹窗 / 导入导出 / 详情)
*/
$(function () {
renderShell('媒体数据', '数据管理 / 媒体数据');
renderPage();
initPage('media', function (user) {
if (!checkPagePermission('media')) return;
loadList(1);
// 跳转定位:?id= 打开该媒体账号详情(重复校验跳转用)
var jumpId = parseInt(new URLSearchParams(location.search).get('id') || '0', 10);
if (jumpId > 0) window.viewMedia(jumpId);
});
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); });
// 手机号归属地自动识别(平台为 phone 时,录入号码自动识别归属地)
var $geo = $('#media-account-geo');
var bindGeo = function () {
if ($('#media-platform').val() !== 'phone') { $geo.text('').hide(); return; }
$('#media-account').off('blur.geo').on('blur.geo', function () {
var val = $.trim($(this).val());
if (!val) { $geo.text('').hide(); return; }
httpGet('common/phone_geo.php', { phone: val }).then(function (d) {
if (d && d.label && d.label !== '未知') {
$geo.text('归属地:' + d.label).show();
} else {
$geo.text('归属地:未能识别').show();
}
}).catch(function () { $geo.text('').hide(); });
});
};
$('#media-platform').on('change', bindGeo);
bindGeo();
// 录入自动清洗:平台为 phone 时账号去所有空格(含中间),为 email 时去首尾空格
var bindAccountClean = function () {
var plat = $('#media-platform').val();
if (plat === 'phone') {
$('#media-account').off('blur.clean').on('input.clean', function () {
var v = cleanPhone($(this).val());
if ($(this).val() !== v) $(this).val(v);
});
} else if (plat === 'email') {
$('#media-account').off('input.clean').on('blur.clean', function () {
$(this).val(cleanEmail($(this).val()));
});
} else {
$('#media-account').off('input.clean blur.clean');
}
};
$('#media-platform').on('change', bindAccountClean);
bindAccountClean();
$('#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 (data.platform === 'phone' && !validatePhoneCN(data.account_id)) { Dialog.error('手机号码格式不正确(应为国内 11 位手机号,如 13812345678)'); return; }
if (data.platform === 'email' && !validateEmail(data.account_id)) { Dialog.error('邮箱格式不正确'); return; }
if (!isEdit && (!data.owner_type || !data.owner_id)) { Dialog.error('归属类型和归属ID为必填项'); return; }
// 录入清洗:手机号去所有空格(含中间),邮箱去首尾空格
if (data.platform === 'phone') data.account_id = cleanPhone(data.account_id);
else if (data.platform === 'email') data.account_id = cleanEmail(data.account_id);
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;
}
$('#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: ['关闭'] });
});
}
});