define(['jquery', 'bootstrap', 'backend', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Echarts) { var Controller = { index: function () { var trend = (typeof Config !== 'undefined' && Config.trend) ? Config.trend : null; // 刷新当前看板 $(document).on('click', '.fc-dash__refresh', function () { $(this).find('.fa').addClass('fa-spin'); window.location.reload(); }); // 没有趋势数据(或 echarts 未加载)时静默降级:下方明细表已展示同样数据 if (!trend || !trend.dates || !trend.dates.length || !Echarts) { return; } var dates = $.map(trend.dates, function (day) { return day.slice(5); // 2026-10-09 => 10-09 }); var total = function (list) { var sum = 0; $.each(list || [], function (i, n) { sum += Number(n) || 0; }); return sum; }; var formatNum = function (n) { var value = Number(n) || 0; var options = value % 1 === 0 ? {} : {minimumFractionDigits: 2, maximumFractionDigits: 2}; try { return value.toLocaleString('zh-CN', options); } catch (e) { return value.toFixed(value % 1 === 0 ? 0 : 2); } }; var METRICS = { posts: { label: __('发布量'), color: '#44507A', fill: 'rgba(68, 80, 122, .18)', axis: '{value}', money: false, data: trend.posts || [] }, orders: { label: __('订单额'), color: '#B7793A', fill: 'rgba(183, 121, 58, .18)', axis: '¥{value}', money: true, data: trend.orders || [] }, members: { label: __('新增注册用户'), color: '#2E8A66', fill: 'rgba(46, 138, 102, .16)', axis: '{value}', money: false, data: trend.members || [] } }; var reduceMotion = !!(window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches); var $chart = $('#fc-trend-chart'); var chart = null; var buildOption = function (metric) { return { color: [metric.color], grid: {left: 6, right: 16, top: 18, bottom: 4, containLabel: true}, tooltip: { trigger: 'axis', backgroundColor: '#FFFFFF', borderColor: '#E7EBF1', borderWidth: 1, padding: [8, 12], textStyle: {color: '#232A38', fontSize: 12}, extraCssText: 'border-radius:8px;box-shadow:0 6px 18px rgba(23,35,61,.10);', axisPointer: {type: 'line', lineStyle: {color: '#C9D2DF', type: 'dashed'}}, formatter: function (params) { var item = params[0]; var prefix = metric.money ? '¥' : ''; var dot = ''; return item.axisValue + '
' + dot + metric.label + ':' + prefix + formatNum(item.data) + ''; } }, xAxis: { type: 'category', boundaryGap: false, data: dates, axisLine: {lineStyle: {color: '#E7EBF1'}}, axisTick: {show: false}, axisLabel: {color: '#6B7684', fontSize: 11, margin: 10} }, yAxis: { type: 'value', minInterval: metric.money ? 0 : 1, axisLine: {show: false}, axisTick: {show: false}, axisLabel: {color: '#6B7684', fontSize: 11, formatter: metric.axis}, splitLine: {lineStyle: {color: '#EFF2F6', type: 'dashed'}} }, series: [{ name: metric.label, type: 'line', smooth: true, symbol: 'circle', symbolSize: 6, showSymbol: false, lineStyle: {width: 2}, itemStyle: {color: metric.color, borderColor: '#FFFFFF', borderWidth: 2}, emphasis: {focus: 'series'}, areaStyle: { color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [ {offset: 0, color: metric.fill}, {offset: 1, color: 'rgba(255, 255, 255, 0)'} ]) }, data: metric.data }], animation: !reduceMotion, animationDuration: 500 }; }; var applyMetric = function (key) { var metric = METRICS[key]; $('.fc-seg__btn') .removeClass('is-active') .attr('aria-pressed', 'false') .filter('[data-metric="' + key + '"]') .addClass('is-active') .attr('aria-pressed', 'true'); $chart.attr('aria-label', __('近 14 天') + metric.label + __('趋势图')); $('#fc-trend-total').text((metric.money ? '¥' : '') + formatNum(total(metric.data))); if (chart) { chart.setOption(buildOption(metric), true); } }; if ($chart.length) { chart = Echarts.init($chart[0], 'walden'); applyMetric('posts'); $(window).on('resize', function () { chart.resize(); }); $(document).on('click', '.fc-seg__btn', function () { applyMetric($(this).data('metric')); }); } } }; return Controller; });