feat: 实现存储抽象、任务队列与区块链/归属地 Mock 适配
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
export interface DeviceInfo {
|
||||
deviceType: string; // Mobile | Tablet | PC
|
||||
os: string;
|
||||
}
|
||||
|
||||
// 轻量 User-Agent 解析,用于查询日志风控统计。
|
||||
export function parseUserAgent(ua?: string): DeviceInfo {
|
||||
if (!ua) return { deviceType: 'Unknown', os: 'Unknown' };
|
||||
const s = ua.toLowerCase();
|
||||
let deviceType = 'PC';
|
||||
if (/ipad|tablet/.test(s)) deviceType = 'Tablet';
|
||||
else if (/mobile|android|iphone|ipod|micromessenger/.test(s)) deviceType = 'Mobile';
|
||||
|
||||
let os = 'Unknown';
|
||||
if (/windows/.test(s)) os = 'Windows';
|
||||
else if (/iphone|ipad|ipod|ios/.test(s)) os = 'iOS';
|
||||
else if (/android/.test(s)) os = 'Android';
|
||||
else if (/mac os|macintosh/.test(s)) os = 'macOS';
|
||||
else if (/linux/.test(s)) os = 'Linux';
|
||||
|
||||
return { deviceType, os };
|
||||
}
|
||||
|
||||
// 从请求中提取真实客户端 IP(兼容反向代理)。
|
||||
export function extractIp(headers: Record<string, unknown>, fallback?: string): string {
|
||||
const xff = headers['x-forwarded-for'];
|
||||
if (typeof xff === 'string' && xff.length > 0) {
|
||||
return xff.split(',')[0].trim();
|
||||
}
|
||||
const real = headers['x-real-ip'];
|
||||
if (typeof real === 'string' && real.length > 0) {
|
||||
return real;
|
||||
}
|
||||
return (fallback || '').replace('::ffff:', '') || 'unknown';
|
||||
}
|
||||
Reference in New Issue
Block a user