公告栏下添加访问数据

效果展示

前置umami搭建

umami搭建可参考heo写的一篇文章

添加js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 等待DOM完全加载
document.addEventListener('DOMContentLoaded', () => {
// 确保容器存在后再执行
const container = document.getElementById('aside-content');
if (container) {
initUmamiStats();
}
});

// 初始化Umami统计
function initUmamiStats() {
// 创建统计容器
const statsContainer = document.createElement('div');
statsContainer.id = 'umami-stats-container';
statsContainer.style.cssText = `
width: 100%;
padding: 20px;
background: linear-gradient(60deg,#ffd7e4 0,#c8f1ff 83%) ;
border-radius: 8px;
margin: 15px 0;
display: block;
`;

// 创建统计项模板
const statsItems = [
{ id: 'today-views', title: '今日浏览量' },
{ id: 'today-visitors', title: '今日访客数' },
{ id: 'yesterday-views', title: '昨日浏览量' },
{ id: 'yesterday-visitors', title: '昨日访客数' },
{ id: 'last-month-views', title: '上月浏览量' },
{ id: 'last-year-views', title: '去年浏览量' }
];

// 创建每行两个统计项的布局
const row1 = document.createElement('div');
row1.style.cssText = `
display: flex;
justify-content: space-between;
margin-bottom: 15px;
`;
const row2 = document.createElement('div');
row2.style.cssText = `
display: flex;
justify-content: space-between;
margin-bottom: 15px;
`;
const row3 = document.createElement('div');
row3.style.cssText = `
display: flex;
justify-content: space-between;
`;

// 插入统计项到对应的行中
statsItems.forEach((item, index) => {
const div = document.createElement('div');
div.className = 'umami-stat-item';
div.innerHTML = `
<div class="stat-title">${item.title}</div>
<div class="stat-value" id="${item.id}">加载中...</div>
`;
div.style.cssText = `
width: 48%;
margin: 0;
padding: 12px;
box-shadow: rgb(162 159 201 / 61%) 0px 2px 4px;
border-radius: 6px;
`;
if (index < 2) {
row1.appendChild(div);
} else if (index < 4) {
row2.appendChild(div);
} else {
row3.appendChild(div);
}
});

// 将行添加到统计容器中
statsContainer.appendChild(row1);
statsContainer.appendChild(row2);
statsContainer.appendChild(row3);

// 查找目标元素并在其前面插入统计容器
const target = document.querySelector('.sticky_layout');
if (target && target.parentElement) {
target.parentElement.insertBefore(statsContainer, target);
} else {
console.error('找不到目标插入位置');
return;
}

// 获取并更新统计数据
fetchUmamiStats();
}

// 获取并更新Umami统计数据
async function fetchUmamiStats() {
try {
const response = await fetch('https://api.qcxz.ink/api/umami-api.php');
if (!response.ok) {
throw new Error(`HTTP错误! 状态码: ${response.status}`);
}
const data = await response.json();
console.log('获取到的统计数据:', data);

// 更新统计项
document.getElementById('today-views').textContent = data.today_pv.toLocaleString();
document.getElementById('today-visitors').textContent = data.today_uv.toLocaleString();
document.getElementById('yesterday-views').textContent = data.yesterday_pv.toLocaleString();
document.getElementById('yesterday-visitors').textContent = data.yesterday_uv.toLocaleString();
document.getElementById('last-month-views').textContent = data.last_month_pv.toLocaleString();
document.getElementById('last-year-views').textContent = data.last_year_pv.toLocaleString();

// 更新样式
document.querySelectorAll('.stat-value').forEach(el => {
el.style.color = '#333';
});
} catch (error) {
console.error('获取统计数据失败:', error);
// 更新所有统计项显示获取失败
const elements = document.querySelectorAll('.stat-value');
elements.forEach(el => {
el.textContent = '获取失败';
el.style.color = '#ff4444';
});
}
}

添加引用

再在 _config.anzhiyu.yml 中,添加对应的js文件就好了

1
2
3
bottom:
# 自定义js
# - <script src="/js/xxx"></script>

尾声

如果需要修改颜色的话可以自己在js文件中进行修改