layui.define(['form', 'table', 'http', 'auth', 'laydate'], function (exports) { var $ = layui.jquery, form = layui.form, table = layui.table, layer = layui.layer, http = layui.http, laydate = layui.laydate, auth = layui.auth; var _savedPerms = (layui.data('backend').permission) || []; auth.init(_savedPerms); auth.setController('spider'); // 日期选择 laydate.render({ elem: '#dateInput' }); // 汇总 + 趋势(纯 CSS 迷你条形图,不依赖 echarts) http.get('stats').then(function (res) { if (res.code !== 0) { return; } var summary = (res.data && res.data.summary) || []; var trend = (res.data && res.data.trend) || { dates: [], series: [] }; var $body = $('#summaryBody').empty(); if (summary.length === 0) { $body.append('暂无蜘蛛来访记录'); } var seriesMap = {}; (trend.series || []).forEach(function (s) { seriesMap[s.name] = s.data; }); summary.forEach(function (row) { var line = seriesMap[row.label] || []; var max = Math.max.apply(null, line.concat([1])); var bars = line.map(function (v, i) { var h = v > 0 ? Math.max(2, Math.round(v / max * 28)) : 1; return ''; }).join(''); $body.append('' + '' + row.label + ' (' + row.spider + ')' + '' + row.today + '' + '' + row.week + '' + '' + row.month + '' + '' + row.total + '' + '
' + bars + '
' + ''); $('#spiderSelect').append(''); }); form.render('select'); }); // 明细表 table.render({ elem: '#dataTable', url: 'index', toolbar: '#tableBar', defaultToolbar: ['filter', 'exports', 'print'], parseData: function (res) { return { code: res.code === 0 ? 0 : 1, msg: res.message || '', count: res.count || (res.data ? res.data.length : 0), data: res.data || [] }; }, cols: [[ { field: 'id', width: 90, title: 'ID', sort: true }, { field: 'spider_text', width: 110, title: '蜘蛛' }, { field: 'url', title: '抓取URL', minWidth: 240 }, { field: 'ip', width: 140, title: '来源IP' }, { field: 'app', width: 100, title: '应用' }, { field: 'http_code', width: 90, title: '状态码' }, { field: 'time_text', width: 170, title: '抓取时间' } ]], limits: [15, 30, 50, 100], limit: 15, page: true, skin: 'line', done: function () { var view = $('#dataTable').next('.layui-table-view'); view.find('[data-perm]').each(function () { var perm = $(this).attr('data-perm'); if (perm && !auth.has(perm)) { $(this).remove(); } }); } }); // 搜索 form.on('submit(data-search-btn)', function (data) { table.reload('dataTable', { page: { curr: 1 }, where: data.field }); return false; }); $('#btn-reset').on('click', function () { $('input[name="ip"],input[name="url"],input[name="date"]').val(''); $('#spiderSelect').val(''); form.render('select'); table.reload('dataTable', { page: { curr: 1 }, where: { spider: '', ip: '', url: '', date: '' } }); }); // 工具条:清理旧日志 table.on('toolbar(dataTable)', function (elem) { if (elem.event === 'dataClear') { layer.prompt({ title: '保留最近多少天的明细?(聚合报表不受影响)', value: '30' }, function (value, index) { layer.close(index); var days = parseInt(value, 10) || 30; http.post('clear', { days: days }).then(function (res) { if (res.code === 0) { layer.msg(res.message || '清理完成'); table.reload('dataTable', {}, true); } else { layer.msg(res.msg || res.message || '清理失败'); } }); }); } }); exports('spider', {}); });