1
0
forked from bot/app

🐛 [docs]: 增加访客记录

This commit is contained in:
2024-09-02 00:13:37 +08:00
parent e493139d85
commit be59e241c6
3 changed files with 68 additions and 7 deletions

View File

@ -13,6 +13,7 @@ const i18nData = {
forks: 'Forks',
issues: 'Issues',
prs: 'Pull Requests',
visitors: 'Visitor',
size: 'Size',
plugins: 'Plugins',
resources: 'Resources',
@ -32,6 +33,7 @@ const i18nData = {
forks: '叉子',
issues: '开启议题',
prs: '合并请求',
visitors: '访客',
size: '大小',
plugins: '插件',
resources: '主题资源',

View File

@ -1,10 +1,11 @@
// URL
export const OWNER = "LiteyukiStudio"
export const REPO = "LiteyukiBot"
const githubAPIUrl = "https://api.github.com"
const onlineFetchUrl = "https://api.liteyuki.icu/online";
const totalFetchUrl = "https://api.liteyuki.icu/count";
const visitRecordUrl = "https://api.liteyuki.icu/visit";
const visitCountUrl = "https://api.liteyuki.icu/visit_count";
export const RepoUrl = `https://github.com/${OWNER}/${REPO}`
export const StarMapUrl = "https://starmap.liteyuki.icu"
@ -25,10 +26,11 @@ interface StatsApi {
getGithubStats: () => Promise<GithubStats>;
getPluginNum: () => Promise<number>;
getResourceNum: () => Promise<number>;
getVisitCount: () => Promise<number>;
}
export type { GithubStats };
export type {GithubStats};
// 实现接口
export const statsApi: StatsApi = {
@ -90,5 +92,48 @@ export const statsApi: StatsApi = {
} catch (e) {
return -1;
}
},
getVisitCount: async () => {
try {
const res = await fetch(visitCountUrl);
const data = await res.json();
return data.count;
} catch (e) {
return -1;
}
}
};
};
function getDeviceId(): string {
// 用户每次访问时生成一个唯一的设备ID储存在localStorage中用于统计用户数量
const deviceIdKey = 'deviceId';
let deviceId = localStorage.getItem(deviceIdKey);
if (!deviceId) {
deviceId = generateUUID();
localStorage.setItem(deviceIdKey, deviceId);
}
return deviceId;
}
export async function uploadVisitRecord() {
const deviceId = getDeviceId();
try {
await fetch(visitRecordUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({'device_id': deviceId}).toString(),
});
} catch (e) {
console.error('Failed to upload visit record:', e);
}
}
function generateUUID(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}