define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'backend/format'], function ($, undefined, Backend, Table, Form, Format) { var Controller = { /** * 信息审核列表页面逻辑 * * 专职负责同城待审信息的核对、单条审核、一键通过/驳回与批量审核处理 */ index: function () { // 初始化表格参数配置 Table.api.init({ extend: { index_url: 'fc/postaudit/index' + location.search, audit_url: 'fc/postaudit/audit', table: 'fc_post', } }); var table = $("#table"); var esc = Controller.api.esc; var canAudit = table.data('operate-audit'); /** * 打开审核工作台 * * @param {Object} row 行数据 */ function openAudit(row) { Fast.api.open('fc/postaudit/detail?ids=' + row.id + '&mode=audit', '审核:' + esc(row.title), {area: ['860px', '88%']}); } /** * 打开只读详情 * * @param {Object} row 行数据 */ function openDetail(row) { Fast.api.open('fc/postaudit/detail?ids=' + row.id + '&mode=view', '详情:' + esc(row.title), {area: ['860px', '88%']}); } // 初始化表格 table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', sortName: 'id', sortOrder: 'desc', escape: false, clickToSelect: false, commonSearch: true, searchFormVisible: false, fixedColumns: true, fixedRightNumber: 1, columns: [ [ {checkbox: true}, { field: 'id', title: '序号', operate: false, sortable: false, align: 'center', width: '60px', class: 'fc-serial', table: table, formatter: Format.serial }, { field: 'cover', title: '封面', operate: false, align: 'center', width: '65px', events: Table.api.events.image, formatter: function (value, row) { var img = value; if (!img && row.media) { try { var raw = typeof row.media === 'string' ? row.media.trim() : ''; if (raw.indexOf('[') === 0) { var parsed = JSON.parse(raw); img = parsed && parsed.length > 0 ? parsed[0] : ''; } else if (raw) { img = raw.split(',')[0]; } } catch (e) { img = ''; } } if (img) { var fullUrl = Fast.api.cdnurl(img, true); return ''; } return '无图'; } }, { field: 'title', title: '标题', operate: 'LIKE', align: 'left', events: { 'click .btn-view-audit': function (e, value, row) { e.stopPropagation(); e.preventDefault(); if (canAudit) { openAudit(row); } else { openDetail(row); } } }, formatter: function (value, row) { return '' + esc(value) + ''; } }, { field: 'category_id', title: '分类', addClass: 'selectpage', extend: "data-source='fc/category/index' data-field='name'", formatter: function (value, row) { return row.category_name ? '' + esc(row.category_name) + '' : '-'; } }, { field: 'identity_type', title: '发布身份', align: 'center', width: '85px', searchList: {1: '个人', 2: '商家'}, formatter: function (value, row) { var val = Number(value || (row && row.identity_type) || 1); if (val === 2) { return ' 商家'; } return ' 个人'; } }, { field: 'price', title: '价格', operate: 'BETWEEN', align: 'right', formatter: function (value, row) { var unit = row.price_unit ? esc(row.price_unit) : ''; if (value === null || value === '' || value === undefined || Number(value) === 0) { return unit === '面议' ? '面议' : '免费'; } var num = Number(value).toLocaleString('zh-CN', {minimumFractionDigits: 2, maximumFractionDigits: 2}); var suffix = (unit && unit !== '元') ? ' ' + unit + '' : ''; return '¥' + num + '' + suffix; } }, { field: 'area', title: '区域', operate: 'LIKE', formatter: function (value) { return value ? esc(value) : '-'; } }, { field: 'user_id', title: '发布用户', addClass: 'selectpage', extend: "data-source='user/user/index' data-field='nickname'", formatter: function (value, row) { return row.user_name ? ' ' + esc(row.user_name) : '-'; } }, { field: 'phone', title: '联系电话', operate: 'LIKE', formatter: function (value, row) { var contact = row.contact ? esc(row.contact) + ' ' : ''; return value ? contact + esc(value) : '-'; } }, { field: 'audit_status', title: '审核状态', searchList: {0: '待审核', 1: '已通过', 2: '已驳回'}, formatter: function (value) { var map = { 0: '待审核', 1: '已通过', 2: '已驳回' }; return map[value] || '未知'; } }, { field: 'createtime', title: '提交时间', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime }, { field: 'operate', title: '操作', align: 'center', width: '100px', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [ { name: 'audit', text: '审核', title: '进入审核工作台', icon: '', classname: 'btn btn-xs btn-success btn-click', visible: function (row) { return Number(row.audit_status) === 0 && canAudit; }, click: function (options, row) { openAudit(row); } }, { name: 'detail', text: '详情', title: '查看详情', icon: '', classname: 'btn btn-xs btn-info btn-click', click: function (options, row) { openDetail(row); } } ] } ] ] }); // 批量通过操作 $('.toolbar').on('click', '.btn-batch-pass', function () { var ids = Table.api.selectedids(table); if (ids.length === 0) { Toastr.warning('请先勾选要审核的信息'); return; } Layer.confirm('确认批量通过选中的 ' + ids.length + ' 条信息并上架?', {title: '批量通过', btn: ['确认通过', '取消']}, function (index) { Fast.api.ajax({ url: 'fc/postaudit/pass', data: {ids: ids.join(',')} }, function () { table.bootstrapTable('refresh'); return false; }); Layer.close(index); }); }); // 批量驳回操作 $('.toolbar').on('click', '.btn-batch-reject', function () { var ids = Table.api.selectedids(table); if (ids.length === 0) { Toastr.warning('请先勾选要驳回的信息'); return; } Controller.api.askReason({ title: '批量驳回', tip: '请选择或填写驳回原因,所选 ' + ids.length + ' 条信息发布者均会收到该通知。', presets: ['内容不合规', '虚假信息', '违规联系方式', '重复发布', '类目选择错误'], okText: '确认批量驳回' }, function (reason) { Fast.api.ajax({ url: 'fc/postaudit/reject', data: {ids: ids.join(','), reason: reason} }, function () { table.bootstrapTable('refresh'); return false; }); }); }); // 为表格绑定通用事件 Table.api.bindevent(table); // 双击行直接打开审核工作台 table.on('dbl-click-row.bs.table', function (e, row) { if (canAudit) { openAudit(row); } else { openDetail(row); } }); }, /** * 审核工作台与详情弹窗交互逻辑 * * 负责通过/驳回操作、快捷键监听(A/R/左右方向键)、上下条切换及父窗口列表刷新 * * @return void */ detail: function () { var postId = Fast.api.query('ids') || Fast.api.query('id'); var queue = window.postQueue || {index: 0, total: 0, prev: 0, next: 0}; var esc = Controller.api.esc; /** * 刷新父页面表格 */ function refreshParent() { try { if (parent.$ && parent.$('.btn-refresh').length) { parent.$('.btn-refresh').trigger('click'); } } catch (e) { } } /** * 关闭当前弹窗 */ function closeDialog() { refreshParent(); Fast.api.close(); } /** * 切换到上一条/下一条待审信息 * * @param {string} dir 'prev' | 'next' */ function go(dir) { var id = dir === 'prev' ? queue.prev : queue.next; if (!id) { Toastr.info(dir === 'prev' ? '已经是第一条了' : '已经是最后一条了'); return; } location.href = Fast.api.fixurl('fc/postaudit/detail') + '?ids=' + id + '&mode=audit&dialog=1'; } /** * 审核完成后更新队列:若存在下一条则直接切入,否则提示完成并关闭窗口 */ function afterAudit() { refreshParent(); Fast.api.ajax({ url: 'fc/postaudit/queue', type: 'GET', loading: false, data: {id: postId} }, function (data, ret) { queue = ret.data || queue; if (queue.next) { go('next'); } else { Toastr.success('待审核队列已处理完'); Fast.api.close(); } return false; }); } /** * 提交审核结果 * * @param {number} status 1=通过, 2=驳回 * @param {string} reason 驳回原因 */ function submitAudit(status, reason) { Fast.api.ajax({ url: 'fc/postaudit/audit', data: { ids: postId, auditStatus: status, reason: reason || '' } }, function (data, ret) { Toastr.success(ret.msg || '操作成功'); afterAudit(); return false; }, function (data, ret) { Toastr.error(ret.msg || '操作失败'); return false; }); } // 纯查看模式时仅绑定关闭事件 if (!$('#btn-pass').length) { $('#btn-close').off('click').on('click', function () { closeDialog(); }); return; } /** * 弹出通过二次确认弹层 */ function askPass() { Layer.confirm('确认该信息内容及资质合规,通过审核并正式上架?', { title: '审核通过确认', btn: ['确认通过', '取消'], icon: 3 }, function (index) { Layer.close(index); submitAudit(1, ''); }); } /** * 弹出驳回理由选择/输入弹层 */ function askReject() { Controller.api.askReason({ title: '驳回原因', tip: '请选择或填写驳回原因,原因会通过站内信通知发布用户。', presets: ['虚假或欺诈信息', '联系方式违规', '重复发布', '类目选择错误', '内容含违规词', '图片不合规'], okText: '确认驳回' }, function (reason) { submitAudit(2, reason); }); } /** * 全局快捷键监听:A 通过 · R 驳回 · ← → 切换 · Esc 关闭 */ $(document).off('keydown.postAudit').on('keydown.postAudit', function (e) { if ($(e.target).is('input, textarea, select')) { return; } if ($('.layui-layer').length > 0) { return; } var key = (e.key || '').toLowerCase(); if (key === 'a') { e.preventDefault(); askPass(); } else if (key === 'r') { e.preventDefault(); askReject(); } else if (key === 'arrowleft') { e.preventDefault(); go('prev'); } else if (key === 'arrowright') { e.preventDefault(); go('next'); } else if (key === 'escape') { e.preventDefault(); closeDialog(); } }); // 绑定工作台底部按钮事件 $('#btn-pass').off('click').on('click', function () { askPass(); }); $('#btn-reject').off('click').on('click', function () { askReject(); }); $('#btn-close').off('click').on('click', function () { closeDialog(); }); $('#btn-prev').off('click').on('click', function () { go('prev'); }); $('#btn-next').off('click').on('click', function () { go('next'); }); // 送审图片相册画廊大图预览 $('.fc-detail-container').on('click', '.fc-media-preview', function (e) { e.preventDefault(); var photos = []; var currentSrc = $(this).attr('href'); var startIndex = 0; $('.fc-media-preview').each(function (i) { var src = $(this).attr('href'); if (src) { photos.push({src: src, thumb: src}); if (src === currentSrc) { startIndex = i; } } }); if (photos.length > 0) { Layer.photos({ photos: { title: '送审图片质检', start: startIndex, data: photos }, anim: 5 }); } }); // 页面加载后自动聚焦主窗口,保证快捷键开箱即用 try { document.body.focus(); } catch (e) { } }, api: { /** * HTML 转义防注入 * * @param {string} value * @return {string} */ esc: function (value) { return String(value === null || value === undefined ? '' : value).replace(/[&<>"']/g, function (c) { return {'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}[c]; }); }, /** * 填写原因弹窗组件(驳回共用) * * @param {Object} options {title, tip, presets, okText} * @param {Function} callback function(reason) */ askReason: function (options, callback) { var presets = options.presets || []; var esc = Controller.api.esc; var html = '
' + '

' + esc(options.tip || '请选择或填写原因') + '

' + '
' + presets.map(function (item) { return ''; }).join('') + '
' + '' + '
'; Layer.open({ type: 1, title: options.title || '填写原因', area: ['460px', 'auto'], content: html, btn: [options.okText || '确定', '取消'], yes: function (index, layero) { var preset = $.trim(layero.find('.fc-reason__item.active').data('reason') || ''); var extra = $.trim(layero.find('.fc-reason__text').val()); var reason = preset ? (extra ? preset + ':' + extra : preset) : extra; if (!reason) { Toastr.warning('请选择或填写原因'); return; } Layer.close(index); callback(reason); }, success: function (layero) { layero.find('.fc-reason__item').on('click', function () { layero.find('.fc-reason__item').removeClass('active btn-success').addClass('btn-default'); $(this).addClass('active btn-success').removeClass('btn-default'); layero.find('.fc-reason__text').focus(); }); } }); } } }; return Controller; });