Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | 16x 16x 16x 5x 5x 5x 5x 1x 1x 4x 1x 1x 3x 3x 3x 16x 2x 2x 2x 2x 1x 1x 1x 16x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 16x 1x 1x 1x 1x 1x 1x 16x 3x 3x 3x 3x 2x 2x 1x 1x 1x 2x 2x 2x 2x 16x 3x 3x 3x 3x 3x 3x 3x 3x 3x 16x | /**
* 声望系统服务
*
* 功能:
* - 查询用户声望
* - 获取声望排行榜
* - 手动触发声望重算(仅用于数据修复)
*
* 注意:
* 声望由 Supabase Database Functions 自动计算
* 当用户档案、评论、宠物等数据变更时,自动触发更新
*/
import { supabase } from '../client';
import { convertKeysToCamel, logger, wrapResponse, type SupabaseResponse } from '../helpers';
import type { PostgrestError } from '@supabase/supabase-js';
/**
* 声望等级
*/
export type ReputationLevel = 'novice' | 'intermediate' | 'advanced' | 'expert';
/**
* 声望等级中文名称
*/
export const REPUTATION_LEVEL_NAMES: Record<ReputationLevel, string> = {
novice: '新手',
intermediate: '进阶',
advanced: '高级',
expert: '专家',
};
/**
* 声望等级颜色
*/
export const REPUTATION_LEVEL_COLORS: Record<ReputationLevel, string> = {
novice: '#94A3B8', // 灰色
intermediate: '#3B82F6', // 蓝色
advanced: '#8B5CF6', // 紫色
expert: '#F59E0B', // 金色
};
/**
* 声望汇总数据
*/
export interface ReputationSummary {
userId: string;
profileCompleteness: number; // 档案完整度 (0-20)
reviewQuality: number; // 评论质量 (0-40)
communityContribution: number; // 社区贡献 (0-30)
compliance: number; // 合规性 (0-10)
score: number; // 总分 (0-100)
level: ReputationLevel; // 等级
updatedAt: string;
}
/**
* 声望详情(包含用户信息)
*/
export interface ReputationDetail extends ReputationSummary {
user: {
username: string;
avatarUrl?: string;
};
}
/**
* 获取用户声望
*/
export const getUserReputation = async (
userId: string
): Promise<SupabaseResponse<ReputationSummary>> => {
logger.query('reputation', 'get_user', { userId });
try {
const { data, error } = await supabase
.from('reputation_summaries')
.select('*')
.eq('user_id', userId)
.single();
if (error) {
logger.error('reputation', 'get_user', error);
return wrapResponse<ReputationSummary>(null, error);
}
if (!data) {
logger.error('reputation', 'get_user', 'not_found');
return wrapResponse<ReputationSummary>(null, {
message: '声望数据不存在',
code: 'NOT_FOUND',
details: '',
hint: '',
} as PostgrestError);
}
const reputation = convertKeysToCamel(data) as ReputationSummary;
logger.success('reputation', 'get_user', data.score);
return wrapResponse<ReputationSummary>(reputation, null);
} catch (err) {
logger.error('reputation', 'get_user', err);
return wrapResponse<ReputationSummary>(null, {
message: String(err),
code: 'EXCEPTION',
details: '',
hint: '',
} as PostgrestError);
}
};
/**
* 获取当前用户声望
*/
export const getMyReputation = async (): Promise<SupabaseResponse<ReputationSummary>> => {
logger.query('reputation', 'get_my');
try {
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
logger.error('reputation', 'get_my', 'not_authenticated');
return wrapResponse<ReputationSummary>(null, {
message: '未登录',
code: 'UNAUTHENTICATED',
details: '',
hint: '',
} as PostgrestError);
}
return await getUserReputation(user.id);
} catch (err) {
logger.error('reputation', 'get_my', err);
return wrapResponse<ReputationSummary>(null, {
message: String(err),
code: 'EXCEPTION',
details: '',
hint: '',
} as PostgrestError);
}
};
/**
* 获取声望排行榜
*/
export const getReputationLeaderboard = async (
params: {
page?: number;
pageSize?: number;
} = {}
): Promise<SupabaseResponse<ReputationDetail[]>> => {
const { page = 1, pageSize = 20 } = params;
const from = (page - 1) * pageSize;
const to = from + pageSize - 1;
logger.query('reputation', 'leaderboard', params);
try {
const { data, error } = await supabase
.from('reputation_summaries')
.select(
`
*,
user:profiles(username, avatar_url)
`
)
.order('score', { ascending: false })
.range(from, to);
Iif (error) {
logger.error('reputation', 'leaderboard', error);
return wrapResponse<ReputationDetail[]>(null, error);
}
const leaderboard = data.map((item: any) => convertKeysToCamel(item)) as ReputationDetail[];
logger.success('reputation', 'leaderboard', leaderboard.length);
return wrapResponse<ReputationDetail[]>(leaderboard, null);
} catch (err) {
logger.error('reputation', 'leaderboard', err);
return wrapResponse<ReputationDetail[]>(null, {
message: String(err),
code: 'EXCEPTION',
details: '',
hint: '',
} as PostgrestError);
}
};
/**
* 获取指定等级的用户数量
*/
export const getReputationLevelCount = async (
level: ReputationLevel
): Promise<SupabaseResponse<number>> => {
logger.query('reputation', 'level_count', { level });
try {
const { count, error } = await supabase
.from('reputation_summaries')
.select('*', { count: 'exact', head: true })
.eq('level', level);
Iif (error) {
logger.error('reputation', 'level_count', error);
return wrapResponse<number>(null, error);
}
logger.success('reputation', 'level_count', count || 0);
return wrapResponse<number>(count || 0, null);
} catch (err) {
logger.error('reputation', 'level_count', err);
return wrapResponse<number>(null, {
message: String(err),
code: 'EXCEPTION',
details: '',
hint: '',
} as PostgrestError);
}
};
/**
* 手动触发声望重算
*
* 注意:正常情况下不需要调用此方法
* 声望会在数据变更时自动更新
* 此方法仅用于数据修复或调试
*/
export const recalculateReputation = async (userId?: string): Promise<SupabaseResponse<void>> => {
logger.query('reputation', 'recalculate', { userId });
try {
// 如果没有指定 userId,使用当前用户
let targetUserId = userId;
if (!targetUserId) {
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
logger.error('reputation', 'recalculate', 'not_authenticated');
return wrapResponse(null, {
message: '未登录',
code: 'UNAUTHENTICATED',
details: '',
hint: '',
} as PostgrestError);
}
targetUserId = user.id;
}
// 调用 Supabase RPC
const { error } = await supabase.rpc('calculate_reputation', {
user_uuid: targetUserId,
});
Iif (error) {
logger.error('reputation', 'recalculate', error);
return wrapResponse(null, error);
}
logger.success('reputation', 'recalculate');
return wrapResponse(null, null);
} catch (err) {
logger.error('reputation', 'recalculate', err);
return wrapResponse(null, {
message: String(err),
code: 'EXCEPTION',
details: '',
hint: '',
} as PostgrestError);
}
};
/**
* 获取声望进度信息
*
* 用于展示距离下一等级还需要多少分
*/
export const getReputationProgress = (
reputation: ReputationSummary
): {
currentLevel: ReputationLevel;
currentLevelName: string;
nextLevel: ReputationLevel | null;
nextLevelName: string | null;
currentScore: number;
nextLevelScore: number;
progress: number; // 0-100
} => {
const { score, level } = reputation;
const levelThresholds: Record<ReputationLevel, number> = {
novice: 0,
intermediate: 40,
advanced: 60,
expert: 80,
};
const levels: ReputationLevel[] = ['novice', 'intermediate', 'advanced', 'expert'];
const currentIndex = levels.indexOf(level);
const nextLevel = currentIndex < levels.length - 1 ? levels[currentIndex + 1] : null;
const currentLevelScore = levelThresholds[level];
const nextLevelScore = nextLevel ? levelThresholds[nextLevel] : 100;
const progress = nextLevel
? Math.round(((score - currentLevelScore) / (nextLevelScore - currentLevelScore)) * 100)
: 100;
return {
currentLevel: level,
currentLevelName: REPUTATION_LEVEL_NAMES[level],
nextLevel,
nextLevelName: nextLevel ? REPUTATION_LEVEL_NAMES[nextLevel] : null,
currentScore: score,
nextLevelScore,
progress,
};
};
/**
* 导出声望服务
*/
export const supabaseReputationService = {
getUserReputation,
getMyReputation,
getReputationLeaderboard,
getReputationLevelCount,
recalculateReputation,
getReputationProgress,
};
|