From 82c2970a851c2f8519802e512b65c818ed4a65d5 Mon Sep 17 00:00:00 2001
From: lafay <2021211506@stu.hit.edu.cn>
Date: Sat, 4 Apr 2026 08:01:45 +0800
Subject: [PATCH] refactor(database): migrate to new modular database layer and
unify data access
- Remove legacy database.ts, LocalDataSource.ts, and MessageRepository.ts
- Create new src/database/ module with messageRepository, userCacheRepository, conversationRepository, and groupCacheRepository
- Update all consumers to import from @/database instead of services/database
- Add web platform blur handling for modal components to fix focus issues
- Flatten SystemMessageItem and NotificationsScreen styles for consistent design
- Add draggable slider in ChatSettingsScreen and dynamic font size support
- Introduce 9 new chat color themes
- Add profile screens for about, terms, and privacy policy with navigation routes
- Add policy links to login and registration screens
- Fix post share URL format from /posts/ to /post/
---
app/(app)/(tabs)/profile/_layout.tsx | 4 +
app/(app)/(tabs)/profile/about.tsx | 5 +
app/(app)/(tabs)/profile/privacy.tsx | 5 +
app/(app)/(tabs)/profile/terms.tsx | 5 +
src/components/business/QRCodeScanner.tsx | 7 +
.../business/ReportDialog/ReportDialog.tsx | 9 +-
src/components/business/SystemMessageItem.tsx | 101 +--
src/components/call/IncomingCallModal.tsx | 5 +
src/components/common/AdaptiveLayout.tsx | 5 +
src/components/common/AppDialogHost.tsx | 14 +-
src/components/common/ImageGallery.tsx | 5 +
src/components/common/VideoPlayerModal.tsx | 10 +-
src/core/usecases/ProcessMessageUseCase.ts | 79 +-
src/data/datasources/index.ts | 2 +-
src/data/datasources/interfaces.ts | 2 +
src/data/repositories/MessageRepository.ts | 220 -----
src/data/repositories/PostRepository.ts | 11 +-
.../LocalDataSource.ts | 141 +---
src/database/core/DatabaseConfig.ts | 14 +
src/database/core/DatabaseManager.ts | 196 +++++
src/database/core/LockManager.ts | 34 +
src/database/index.ts | 54 ++
.../repositories/ConversationRepository.ts | 197 +++++
.../repositories/GroupCacheRepository.ts | 78 ++
.../repositories/MessageRepository.ts | 152 ++++
.../repositories/PostCacheRepository.ts | 44 +
.../repositories/UserCacheRepository.ts | 81 ++
src/database/repositories/index.ts | 5 +
src/database/schema/ConversationSchema.ts | 40 +
src/database/schema/GroupSchema.ts | 20 +
src/database/schema/MessageSchema.ts | 41 +
src/database/schema/MigrationManager.ts | 57 ++
src/database/schema/PostSchema.ts | 9 +
src/database/schema/UserSchema.ts | 16 +
src/database/schema/index.ts | 6 +
src/database/types/CachedMessage.ts | 14 +
src/database/types/index.ts | 1 +
src/navigation/hrefs.ts | 14 +
src/screens/auth/LoginScreen.tsx | 22 +
src/screens/auth/RegisterStep3Profile.tsx | 56 +-
src/screens/home/HomeScreen.tsx | 9 +-
src/screens/home/PostDetailScreen.tsx | 2 +-
src/screens/message/CreateGroupScreen.tsx | 10 +-
src/screens/message/GroupInfoScreen.tsx | 7 +
src/screens/message/GroupMembersScreen.tsx | 8 +
src/screens/message/MessageListScreen.tsx | 7 +
src/screens/message/NotificationsScreen.tsx | 490 ++++++-----
src/screens/message/PrivateChatInfoScreen.tsx | 9 +-
.../components/ChatScreen/EmojiPanel.tsx | 9 +-
.../components/ChatScreen/SegmentRenderer.tsx | 29 +-
.../components/ChatScreen/useChatScreen.ts | 9 +-
.../components/ConversationListRow.tsx | 4 +-
.../components/MutualFollowSelectorModal.tsx | 4 +
src/screens/profile/AboutScreen.tsx | 583 +++++++++++++
src/screens/profile/AccountSecurityScreen.tsx | 390 +++++----
src/screens/profile/ChatSettingsScreen.tsx | 227 ++----
src/screens/profile/PrivacyPolicyScreen.tsx | 320 ++++++++
src/screens/profile/SettingsScreen.tsx | 30 +-
src/screens/profile/TermsOfServiceScreen.tsx | 248 ++++++
src/screens/profile/index.ts | 3 +
src/screens/schedule/ScheduleScreen.tsx | 8 +
src/services/database.ts | 767 ------------------
src/services/messageService.ts | 27 +-
src/stores/authStore.ts | 31 +-
src/stores/chatSettingsStore.ts | 126 +++
src/stores/conversationListSources.ts | 4 +-
src/stores/group/GroupManager.ts | 91 +--
src/stores/groupListSources.ts | 6 +-
src/stores/groupMemberProfileResolver.ts | 4 +-
.../services/ConversationOperations.ts | 4 +-
.../message/services/MessageSendService.ts | 4 +-
.../message/services/MessageSyncService.ts | 50 +-
.../message/services/ReadReceiptManager.ts | 4 +-
.../message/services/UserCacheService.ts | 6 +-
.../message/services/WSMessageHandler.ts | 8 +-
src/stores/user/UserManager.ts | 63 +-
76 files changed, 3382 insertions(+), 2000 deletions(-)
create mode 100644 app/(app)/(tabs)/profile/about.tsx
create mode 100644 app/(app)/(tabs)/profile/privacy.tsx
create mode 100644 app/(app)/(tabs)/profile/terms.tsx
delete mode 100644 src/data/repositories/MessageRepository.ts
rename src/{data/datasources => database}/LocalDataSource.ts (50%)
create mode 100644 src/database/core/DatabaseConfig.ts
create mode 100644 src/database/core/DatabaseManager.ts
create mode 100644 src/database/core/LockManager.ts
create mode 100644 src/database/index.ts
create mode 100644 src/database/repositories/ConversationRepository.ts
create mode 100644 src/database/repositories/GroupCacheRepository.ts
create mode 100644 src/database/repositories/MessageRepository.ts
create mode 100644 src/database/repositories/PostCacheRepository.ts
create mode 100644 src/database/repositories/UserCacheRepository.ts
create mode 100644 src/database/repositories/index.ts
create mode 100644 src/database/schema/ConversationSchema.ts
create mode 100644 src/database/schema/GroupSchema.ts
create mode 100644 src/database/schema/MessageSchema.ts
create mode 100644 src/database/schema/MigrationManager.ts
create mode 100644 src/database/schema/PostSchema.ts
create mode 100644 src/database/schema/UserSchema.ts
create mode 100644 src/database/schema/index.ts
create mode 100644 src/database/types/CachedMessage.ts
create mode 100644 src/database/types/index.ts
create mode 100644 src/screens/profile/AboutScreen.tsx
create mode 100644 src/screens/profile/PrivacyPolicyScreen.tsx
create mode 100644 src/screens/profile/TermsOfServiceScreen.tsx
delete mode 100644 src/services/database.ts
diff --git a/app/(app)/(tabs)/profile/_layout.tsx b/app/(app)/(tabs)/profile/_layout.tsx
index 8a50c48..12acaac 100644
--- a/app/(app)/(tabs)/profile/_layout.tsx
+++ b/app/(app)/(tabs)/profile/_layout.tsx
@@ -36,6 +36,10 @@ export default function ProfileStackLayout() {
+
+
+
+
);
}
diff --git a/app/(app)/(tabs)/profile/about.tsx b/app/(app)/(tabs)/profile/about.tsx
new file mode 100644
index 0000000..5f64223
--- /dev/null
+++ b/app/(app)/(tabs)/profile/about.tsx
@@ -0,0 +1,5 @@
+import { AboutScreen } from '../../../../src/screens/profile/AboutScreen';
+
+export default function AboutRoute() {
+ return ;
+}
diff --git a/app/(app)/(tabs)/profile/privacy.tsx b/app/(app)/(tabs)/profile/privacy.tsx
new file mode 100644
index 0000000..7ecc3f4
--- /dev/null
+++ b/app/(app)/(tabs)/profile/privacy.tsx
@@ -0,0 +1,5 @@
+import { PrivacyPolicyScreen } from '../../../../src/screens/profile/PrivacyPolicyScreen';
+
+export default function PrivacyPolicyRoute() {
+ return ;
+}
diff --git a/app/(app)/(tabs)/profile/terms.tsx b/app/(app)/(tabs)/profile/terms.tsx
new file mode 100644
index 0000000..11b97e8
--- /dev/null
+++ b/app/(app)/(tabs)/profile/terms.tsx
@@ -0,0 +1,5 @@
+import { TermsOfServiceScreen } from '../../../../src/screens/profile/TermsOfServiceScreen';
+
+export default function TermsOfServiceRoute() {
+ return ;
+}
diff --git a/src/components/business/QRCodeScanner.tsx b/src/components/business/QRCodeScanner.tsx
index f05cf62..8b7775f 100644
--- a/src/components/business/QRCodeScanner.tsx
+++ b/src/components/business/QRCodeScanner.tsx
@@ -155,6 +155,13 @@ const QRCodeScannerWeb: React.FC = ({ visible, onClose }) =>
const themeColors = useAppColors();
const styles = useMemo(() => createQrScannerStyles(themeColors), [themeColors]);
+ useEffect(() => {
+ if (visible && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [visible]);
+
return (
diff --git a/src/components/business/ReportDialog/ReportDialog.tsx b/src/components/business/ReportDialog/ReportDialog.tsx
index 6237962..bb68cab 100644
--- a/src/components/business/ReportDialog/ReportDialog.tsx
+++ b/src/components/business/ReportDialog/ReportDialog.tsx
@@ -4,7 +4,7 @@
* 提供友好的用户界面和流畅的交互体验
*/
-import React, { useState, useCallback, useMemo } from 'react';
+import React, { useState, useCallback, useMemo, useEffect } from 'react';
import {
Modal,
View,
@@ -62,6 +62,13 @@ const ReportDialog: React.FC = ({
const targetConfig = TARGET_CONFIG[targetType];
+ useEffect(() => {
+ if (visible && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [visible]);
+
// 判断补充说明是否必填(选择"其他原因"时必填)
const isDescriptionRequired = selectedReason === 'other';
const descriptionPlaceholder = isDescriptionRequired
diff --git a/src/components/business/SystemMessageItem.tsx b/src/components/business/SystemMessageItem.tsx
index 45ccf14..b722eb7 100644
--- a/src/components/business/SystemMessageItem.tsx
+++ b/src/components/business/SystemMessageItem.tsx
@@ -1,7 +1,7 @@
/**
- * SystemMessageItem 系统消息项组件 - 美化版
+ * SystemMessageItem 系统消息项组件 - 扁平化风格
* 根据系统消息类型显示不同图标和内容
- * 采用卡片式设计,符合胡萝卜BBS整体风格
+ * 与登录、注册、设置页面风格保持一致
*/
import React, { useMemo } from 'react';
@@ -9,7 +9,7 @@ import { View, TouchableOpacity, StyleSheet } from 'react-native';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { formatDistanceToNow } from 'date-fns';
import { zhCN } from 'date-fns/locale';
-import { spacing, borderRadius, shadows, fontSizes, useAppColors, type AppColors } from '../../theme';
+import { spacing, borderRadius, fontSizes, useAppColors, type AppColors } from '../../theme';
import { SystemMessageResponse, SystemMessageType } from '../../types/dto';
import Text from '../common/Text';
import Avatar from '../common/Avatar';
@@ -124,17 +124,13 @@ function createSystemMessageStyles(colors: AppColors) {
container: {
flexDirection: 'row',
alignItems: 'flex-start',
- padding: spacing.md,
- backgroundColor: colors.background.paper,
- borderRadius: borderRadius.lg,
- marginHorizontal: spacing.md,
- marginBottom: spacing.sm,
- ...shadows.sm,
+ padding: 16,
+ backgroundColor: '#fff',
+ borderBottomWidth: 1,
+ borderBottomColor: colors.divider || '#E5E5EA',
},
unreadContainer: {
- backgroundColor: colors.primary.light + '08',
- borderLeftWidth: 3,
- borderLeftColor: colors.primary.main,
+ backgroundColor: '#FFF8F6',
},
unreadIndicator: {
position: 'absolute',
@@ -143,19 +139,19 @@ function createSystemMessageStyles(colors: AppColors) {
marginTop: -4,
width: 8,
height: 8,
- borderRadius: borderRadius.full,
- backgroundColor: colors.primary.main,
+ borderRadius: 4,
+ backgroundColor: '#FF6B35',
},
iconContainer: {
- marginRight: spacing.md,
+ marginRight: 14,
},
avatarWrapper: {
position: 'relative',
},
iconWrapper: {
- width: 48,
- height: 48,
- borderRadius: borderRadius.lg,
+ width: 44,
+ height: 44,
+ borderRadius: 12,
justifyContent: 'center',
alignItems: 'center',
},
@@ -163,13 +159,13 @@ function createSystemMessageStyles(colors: AppColors) {
position: 'absolute',
bottom: -2,
right: -2,
- width: 20,
- height: 20,
- borderRadius: borderRadius.full,
+ width: 18,
+ height: 18,
+ borderRadius: 9,
justifyContent: 'center',
alignItems: 'center',
borderWidth: 2,
- borderColor: colors.background.paper,
+ borderColor: '#fff',
},
content: {
flex: 1,
@@ -180,17 +176,17 @@ function createSystemMessageStyles(colors: AppColors) {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
- marginBottom: spacing.xs,
+ marginBottom: 4,
},
titleLeft: {
flexDirection: 'row',
alignItems: 'center',
flex: 1,
- marginRight: spacing.sm,
+ marginRight: 8,
},
title: {
fontWeight: '500',
- fontSize: fontSizes.md,
+ fontSize: 15,
color: colors.text.primary,
},
unreadTitle: {
@@ -200,10 +196,10 @@ function createSystemMessageStyles(colors: AppColors) {
statusBadge: {
flexDirection: 'row',
alignItems: 'center',
- paddingHorizontal: spacing.xs,
+ paddingHorizontal: 6,
paddingVertical: 2,
- borderRadius: borderRadius.sm,
- marginLeft: spacing.xs,
+ borderRadius: 4,
+ marginLeft: 6,
},
statusText: {
fontSize: 10,
@@ -211,55 +207,59 @@ function createSystemMessageStyles(colors: AppColors) {
marginLeft: 2,
},
time: {
- fontSize: fontSizes.sm,
+ fontSize: 13,
flexShrink: 0,
+ color: colors.text.hint,
},
messageContent: {
lineHeight: 20,
- fontSize: fontSizes.sm,
+ fontSize: 14,
+ color: colors.text.secondary,
},
extraInfo: {
flexDirection: 'row',
alignItems: 'center',
- marginTop: spacing.xs,
- backgroundColor: colors.primary.light + '12',
- paddingHorizontal: spacing.sm,
- paddingVertical: spacing.xs,
- borderRadius: borderRadius.md,
+ marginTop: 8,
+ backgroundColor: '#F5F5F7',
+ paddingHorizontal: 10,
+ paddingVertical: 6,
+ borderRadius: 8,
alignSelf: 'flex-start',
},
extraText: {
- marginLeft: spacing.xs,
+ marginLeft: 6,
flex: 1,
fontWeight: '500',
+ fontSize: 13,
},
actionsRow: {
flexDirection: 'row',
- marginTop: spacing.sm,
- gap: spacing.sm,
+ marginTop: 12,
+ gap: 10,
},
actionBtn: {
flexDirection: 'row',
alignItems: 'center',
- paddingVertical: spacing.xs,
- paddingHorizontal: spacing.md,
- borderRadius: borderRadius.md,
+ paddingVertical: 8,
+ paddingHorizontal: 16,
+ borderRadius: 10,
borderWidth: 1,
- gap: spacing.xs,
+ gap: 4,
},
rejectBtn: {
- borderColor: `${colors.error.main}55`,
- backgroundColor: `${colors.error.main}18`,
+ borderColor: colors.error.main + '40',
+ backgroundColor: colors.error.light + '20',
},
approveBtn: {
- borderColor: `${colors.success.main}55`,
- backgroundColor: `${colors.success.main}18`,
+ borderColor: colors.success.main + '40',
+ backgroundColor: colors.success.light + '20',
},
actionBtnText: {
fontWeight: '500',
+ fontSize: 13,
},
arrowContainer: {
- marginLeft: spacing.sm,
+ marginLeft: 8,
justifyContent: 'center',
alignItems: 'center',
height: 20,
@@ -318,6 +318,13 @@ const getSystemMessageTitle = (message: SystemMessageResponse): string => {
}
};
+// 胡萝卜橙主题色
+const THEME_COLORS = {
+ primary: '#FF6B35',
+ primaryLight: '#FF8C5A',
+ primaryDark: '#E55A2B',
+};
+
const SystemMessageItem: React.FC = ({
message,
onPress,
diff --git a/src/components/call/IncomingCallModal.tsx b/src/components/call/IncomingCallModal.tsx
index 7c7324a..d386acc 100644
--- a/src/components/call/IncomingCallModal.tsx
+++ b/src/components/call/IncomingCallModal.tsx
@@ -9,6 +9,7 @@ import {
Modal,
StatusBar,
Dimensions,
+ Platform,
} from 'react-native';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { callStore } from '../../stores/callStore';
@@ -25,6 +26,10 @@ const IncomingCallModal: React.FC = () => {
useEffect(() => {
if (incomingCall) {
+ if (Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
const pulse = Animated.loop(
Animated.sequence([
Animated.timing(pulseAnim, {
diff --git a/src/components/common/AdaptiveLayout.tsx b/src/components/common/AdaptiveLayout.tsx
index 9f542f3..2596623 100644
--- a/src/components/common/AdaptiveLayout.tsx
+++ b/src/components/common/AdaptiveLayout.tsx
@@ -15,6 +15,7 @@ import {
Modal,
Pressable,
Dimensions,
+ Platform,
} from 'react-native';
import {
useResponsive,
@@ -159,6 +160,10 @@ export function AdaptiveLayout({
// 抽屉动画
useEffect(() => {
if (isDrawerOpen) {
+ if (Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
Animated.parallel([
Animated.timing(slideAnim, {
toValue: 1,
diff --git a/src/components/common/AppDialogHost.tsx b/src/components/common/AppDialogHost.tsx
index 6be4aa9..922b6a2 100644
--- a/src/components/common/AppDialogHost.tsx
+++ b/src/components/common/AppDialogHost.tsx
@@ -1,5 +1,5 @@
import React, { useEffect, useMemo, useState } from 'react';
-import { Modal, Pressable, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
+import { Modal, Pressable, StyleSheet, Text, TouchableOpacity, View, Platform } from 'react-native';
import type { AlertButton } from 'react-native';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { LinearGradient } from 'expo-linear-gradient';
@@ -93,6 +93,12 @@ function createDialogHostStyles(colors: AppColors) {
});
}
+const blurActiveElementOnWeb = () => {
+ if (Platform.OS !== 'web') return;
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+};
+
const AppDialogHost: React.FC = () => {
const [dialog, setDialog] = useState(null);
const colors = useAppColors();
@@ -105,6 +111,12 @@ const AppDialogHost: React.FC = () => {
return unbind;
}, []);
+ useEffect(() => {
+ if (dialog) {
+ blurActiveElementOnWeb();
+ }
+ }, [dialog]);
+
const actions = useMemo(() => {
if (!dialog?.actions?.length) return [{ text: '确定' }];
return dialog.actions;
diff --git a/src/components/common/ImageGallery.tsx b/src/components/common/ImageGallery.tsx
index a4a14ee..f66fd87 100644
--- a/src/components/common/ImageGallery.tsx
+++ b/src/components/common/ImageGallery.tsx
@@ -14,6 +14,7 @@ import {
Text,
StatusBar,
Alert,
+ Platform,
} from 'react-native';
import { Image as ExpoImage } from 'expo-image';
import {
@@ -126,6 +127,10 @@ export const ImageGallery: React.FC = ({
// 打开/关闭时重置状态
useEffect(() => {
if (visible) {
+ if (Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
setCurrentIndex(initialIndex);
setShowControls(true);
setError(false);
diff --git a/src/components/common/VideoPlayerModal.tsx b/src/components/common/VideoPlayerModal.tsx
index 8678dbc..d4fd000 100644
--- a/src/components/common/VideoPlayerModal.tsx
+++ b/src/components/common/VideoPlayerModal.tsx
@@ -3,7 +3,7 @@
* 全屏模态播放视频,基于 expo-video
*/
-import React, { useCallback } from 'react';
+import React, { useCallback, useEffect } from 'react';
import {
Modal,
View,
@@ -12,6 +12,7 @@ import {
TouchableOpacity,
Text,
StatusBar,
+ Platform,
} from 'react-native';
import { VideoView, useVideoPlayer } from 'expo-video';
import { MaterialCommunityIcons } from '@expo/vector-icons';
@@ -79,6 +80,13 @@ export const VideoPlayerModal: React.FC = ({
url,
onClose,
}) => {
+ useEffect(() => {
+ if (visible && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [visible]);
+
return (
({
+ id: cached.id,
+ conversationId: cached.conversationId,
+ senderId: cached.senderId,
+ seq: cached.seq,
+ segments: cached.segments || [],
+ createdAt: cached.createdAt,
+ status: cached.status as Message['status'],
+});
+
// 事件类型定义
export type MessageUseCaseEventType =
| 'message_received'
@@ -182,7 +192,16 @@ class ProcessMessageUseCase {
// 异步保存到本地数据库
const isRead = _isAck || sender_id === this.currentUserId;
- messageRepository.saveMessage(newMessage, isRead).catch((error) => {
+ messageRepository.saveMessage({
+ id: newMessage.id,
+ conversationId: newMessage.conversationId,
+ senderId: newMessage.senderId,
+ seq: newMessage.seq,
+ segments: newMessage.segments,
+ createdAt: newMessage.createdAt,
+ status: newMessage.status || 'normal',
+ isRead,
+ }).catch((error) => {
console.error('[ProcessMessageUseCase] 保存消息失败:', error);
});
@@ -227,7 +246,7 @@ class ProcessMessageUseCase {
*/
private async getSenderInfo(userId: string): Promise {
// 先检查本地缓存
- const cachedUser = await messageRepository.getUserCache(userId);
+ const cachedUser = await userCacheRepository.get(userId);
if (cachedUser) {
return cachedUser;
}
@@ -255,9 +274,9 @@ class ProcessMessageUseCase {
*/
private async fetchUserInfo(userId: string): Promise {
try {
- const response = await api.get(`/users/${userId}`);
+ const response = await api.get(`/users/${userId}`);
if (response.code === 0 && response.data) {
- await messageRepository.saveUserCache(response.data);
+ await userCacheRepository.save(response.data as any);
return response.data;
}
return null;
@@ -322,8 +341,8 @@ class ProcessMessageUseCase {
// 更新本地数据库状态
messageRepository
- .updateMessageStatus(message_id, 'recalled', true)
- .catch((error) => {
+ .updateStatus(message_id, 'recalled', true)
+ .catch((error: unknown) => {
console.error('[ProcessMessageUseCase] 更新撤回状态失败:', error);
});
@@ -349,8 +368,8 @@ class ProcessMessageUseCase {
// 更新本地数据库状态
messageRepository
- .updateMessageStatus(message_id, 'recalled', true)
- .catch((error) => {
+ .updateStatus(message_id, 'recalled', true)
+ .catch((error: unknown) => {
console.error('[ProcessMessageUseCase] 更新撤回状态失败:', error);
});
@@ -506,7 +525,16 @@ class ProcessMessageUseCase {
});
// 保存到本地数据库
- await messageRepository.saveMessage(message, true);
+ await messageRepository.saveMessage({
+ id: message.id,
+ conversationId: message.conversationId,
+ senderId: message.senderId,
+ seq: message.seq,
+ segments: message.segments,
+ createdAt: message.createdAt,
+ status: message.status || 'normal',
+ isRead: true,
+ });
return message;
}
@@ -522,7 +550,8 @@ class ProcessMessageUseCase {
* 获取本地消息
*/
async getLocalMessages(conversationId: string, limit: number = 20): Promise {
- return messageRepository.getMessagesByConversation(conversationId, limit);
+ const cached = await messageRepository.getByConversation(conversationId, limit);
+ return cached.map(cachedMessageToMessage);
}
/**
@@ -534,14 +563,14 @@ class ProcessMessageUseCase {
limit: number = 20
): Promise {
// 先从本地获取
- const localMessages = await messageRepository.getMessagesBeforeSeq(
+ const localMessages = await messageRepository.getBeforeSeq(
conversationId,
beforeSeq,
limit
);
if (localMessages.length >= limit) {
- return localMessages;
+ return localMessages.map(cachedMessageToMessage);
}
// 本地数据不足,从服务端获取
@@ -567,7 +596,16 @@ class ProcessMessageUseCase {
);
// 保存到本地
- await messageRepository.saveMessages(serverMessages, false);
+ await messageRepository.saveMessagesBatch(serverMessages.map((m: any) => ({
+ id: m.id,
+ conversationId: m.conversation_id || conversationId,
+ senderId: m.sender_id,
+ seq: m.seq,
+ segments: m.segments || [],
+ createdAt: m.created_at,
+ status: m.status || 'normal',
+ isRead: false,
+ })));
return serverMessages;
}
@@ -575,7 +613,7 @@ class ProcessMessageUseCase {
console.error('[ProcessMessageUseCase] 获取历史消息失败:', error);
}
- return localMessages;
+ return localMessages.map(cachedMessageToMessage);
}
/**
@@ -602,7 +640,16 @@ class ProcessMessageUseCase {
);
// 保存到本地
- await messageRepository.saveMessages(messages, false);
+ await messageRepository.saveMessagesBatch(messages.map((m: any) => ({
+ id: m.id,
+ conversationId: m.conversation_id || conversationId,
+ senderId: m.sender_id,
+ seq: m.seq,
+ segments: m.segments || [],
+ createdAt: m.created_at,
+ status: m.status || 'normal',
+ isRead: false,
+ })));
return messages;
}
diff --git a/src/data/datasources/index.ts b/src/data/datasources/index.ts
index 683b5ec..80dceed 100644
--- a/src/data/datasources/index.ts
+++ b/src/data/datasources/index.ts
@@ -5,5 +5,5 @@
export * from './interfaces';
export * from './ApiDataSource';
-export * from './LocalDataSource';
+export { LocalDataSource, localDataSource } from '@/database';
export * from './CacheDataSource';
diff --git a/src/data/datasources/interfaces.ts b/src/data/datasources/interfaces.ts
index c2c9b46..78bd3d9 100644
--- a/src/data/datasources/interfaces.ts
+++ b/src/data/datasources/interfaces.ts
@@ -20,6 +20,8 @@ export interface ILocalDataSource {
run(sql: string, params?: any[]): Promise<{ lastInsertRowId: number; changes: number }>;
transaction(operations: () => Promise): Promise;
batch(operations: (db: ILocalDataSource) => Promise): Promise;
+ enqueueWrite(operation: () => Promise): Promise;
+ initialize(userId?: string): Promise;
}
// 缓存数据源接口
diff --git a/src/data/repositories/MessageRepository.ts b/src/data/repositories/MessageRepository.ts
deleted file mode 100644
index 5ad28a2..0000000
--- a/src/data/repositories/MessageRepository.ts
+++ /dev/null
@@ -1,220 +0,0 @@
-/**
- * MessageRepository - 消息仓库实现
- * 封装所有SQLite数据库操作,不依赖任何UI或状态管理
- */
-
-import {
- saveMessage as dbSaveMessage,
- saveMessagesBatch as dbSaveMessagesBatch,
- getMessagesByConversation as dbGetMessagesByConversation,
- getMaxSeq as dbGetMaxSeq,
- getMinSeq as dbGetMinSeq,
- getMessagesBeforeSeq as dbGetMessagesBeforeSeq,
- markConversationAsRead as dbMarkConversationAsRead,
- updateConversationCacheUnreadCount as dbUpdateConversationCacheUnreadCount,
- getUserCache as dbGetUserCache,
- saveUserCache as dbSaveUserCache,
- deleteConversation as dbDeleteConversation,
- updateMessageStatus as dbUpdateMessageStatus,
- CachedMessage,
-} from '../../services/database';
-import { Message, Conversation, createMessage, createConversation } from '../../core/entities/Message';
-
-// 数据库消息到领域实体的转换
-const cachedMessageToMessage = (cached: CachedMessage): Message => ({
- id: cached.id,
- conversationId: cached.conversationId,
- senderId: cached.senderId,
- seq: cached.seq,
- segments: cached.segments || [],
- createdAt: cached.createdAt,
- status: cached.status as Message['status'],
-});
-
-// 领域实体到数据库消息的转换
-const messageToCachedMessage = (message: Message, isRead: boolean): CachedMessage => ({
- id: message.id,
- conversationId: message.conversationId,
- senderId: message.senderId,
- content: message.segments
- .filter((s) => s.type === 'text')
- .map((s) => s.data?.text || '')
- .join(''),
- type: message.segments.find((s) => s.type === 'image') ? 'image' : 'text',
- isRead,
- createdAt: message.createdAt,
- seq: message.seq,
- status: message.status,
- segments: message.segments,
-});
-
-class MessageRepository {
- // ==================== 消息操作 ====================
-
- /**
- * 保存单条消息
- */
- async saveMessage(message: Message, isRead: boolean): Promise {
- try {
- const cachedMessage = messageToCachedMessage(message, isRead);
- await dbSaveMessage(cachedMessage);
- } catch (error) {
- console.error('[MessageRepository] 保存消息失败:', error);
- throw error;
- }
- }
-
- /**
- * 批量保存消息
- */
- async saveMessages(messages: Message[], isRead: boolean): Promise {
- if (!messages || messages.length === 0) return;
-
- try {
- const cachedMessages = messages.map((m) => messageToCachedMessage(m, isRead));
- await dbSaveMessagesBatch(cachedMessages);
- } catch (error) {
- console.error('[MessageRepository] 批量保存消息失败:', error);
- throw error;
- }
- }
-
- /**
- * 获取会话的消息
- */
- async getMessagesByConversation(
- conversationId: string,
- limit: number = 20
- ): Promise {
- try {
- const cachedMessages = await dbGetMessagesByConversation(conversationId, limit);
- return cachedMessages.map(cachedMessageToMessage);
- } catch (error) {
- console.error('[MessageRepository] 获取消息失败:', error);
- return [];
- }
- }
-
- /**
- * 获取会话的最大消息序号
- */
- async getMaxSeq(conversationId: string): Promise {
- try {
- return await dbGetMaxSeq(conversationId);
- } catch (error) {
- console.error('[MessageRepository] 获取最大序号失败:', error);
- return 0;
- }
- }
-
- /**
- * 获取会话的最小消息序号
- */
- async getMinSeq(conversationId: string): Promise {
- try {
- return await dbGetMinSeq(conversationId);
- } catch (error) {
- console.error('[MessageRepository] 获取最小序号失败:', error);
- return 0;
- }
- }
-
- /**
- * 获取指定seq之前的历史消息
- */
- async getMessagesBeforeSeq(
- conversationId: string,
- beforeSeq: number,
- limit: number = 20
- ): Promise {
- try {
- const cachedMessages = await dbGetMessagesBeforeSeq(conversationId, beforeSeq, limit);
- return cachedMessages.map(cachedMessageToMessage);
- } catch (error) {
- console.error('[MessageRepository] 获取历史消息失败:', error);
- return [];
- }
- }
-
- /**
- * 标记会话的所有消息为已读
- */
- async markConversationAsRead(conversationId: string): Promise {
- try {
- await dbMarkConversationAsRead(conversationId);
- } catch (error) {
- console.error('[MessageRepository] 标记会话已读失败:', error);
- throw error;
- }
- }
-
- /**
- * 更新会话缓存的未读数
- */
- async updateConversationUnreadCount(
- conversationId: string,
- count: number
- ): Promise {
- try {
- await dbUpdateConversationCacheUnreadCount(conversationId, count);
- } catch (error) {
- console.error('[MessageRepository] 更新会话未读数失败:', error);
- }
- }
-
- /**
- * 更新消息状态(如撤回)
- */
- async updateMessageStatus(
- messageId: string,
- status: string,
- clearContent: boolean = false
- ): Promise {
- try {
- await dbUpdateMessageStatus(messageId, status, clearContent);
- } catch (error) {
- console.error('[MessageRepository] 更新消息状态失败:', error);
- throw error;
- }
- }
-
- /**
- * 删除会话及其所有消息
- */
- async deleteConversation(conversationId: string): Promise {
- try {
- await dbDeleteConversation(conversationId);
- } catch (error) {
- console.error('[MessageRepository] 删除会话失败:', error);
- throw error;
- }
- }
-
- // ==================== 用户缓存操作 ====================
-
- /**
- * 获取用户缓存
- */
- async getUserCache(userId: string): Promise {
- try {
- return await dbGetUserCache(userId);
- } catch (error) {
- console.error('[MessageRepository] 获取用户缓存失败:', error);
- return null;
- }
- }
-
- /**
- * 保存用户缓存
- */
- async saveUserCache(user: any): Promise {
- try {
- await dbSaveUserCache(user);
- } catch (error) {
- console.error('[MessageRepository] 保存用户缓存失败:', error);
- }
- }
-}
-
-export const messageRepository = new MessageRepository();
-export default messageRepository;
diff --git a/src/data/repositories/PostRepository.ts b/src/data/repositories/PostRepository.ts
index ebc0caf..407df0e 100644
--- a/src/data/repositories/PostRepository.ts
+++ b/src/data/repositories/PostRepository.ts
@@ -4,7 +4,7 @@
*/
import { ApiDataSource, apiDataSource } from '../datasources/ApiDataSource';
-import { LocalDataSource, localDataSource } from '../datasources/LocalDataSource';
+import { localDataSource } from '@/database';
import { PostMapper } from '../mappers/PostMapper';
import type { Post, PostImage, PostStatus } from '../../core/entities/Post';
import { createPost } from '../../core/entities/Post';
@@ -79,13 +79,12 @@ interface CachedPost {
export class PostRepository implements IPostRepository {
private api: ApiDataSource;
- private localDb: LocalDataSource;
+ private localDb = localDataSource;
private memoryCache: Map = new Map();
- private readonly CACHE_TTL = 5 * 60 * 1000; // 5分钟缓存过期时间
+ private readonly CACHE_TTL = 5 * 60 * 1000;
- constructor(api: ApiDataSource, localDb: LocalDataSource) {
+ constructor(api: ApiDataSource) {
this.api = api;
- this.localDb = localDb;
}
// ==================== 私有辅助方法 ====================
@@ -575,5 +574,5 @@ export class PostRepository implements IPostRepository {
// ==================== 单例导出 ====================
-export const postRepository = new PostRepository(apiDataSource, localDataSource);
+export const postRepository = new PostRepository(apiDataSource);
export default postRepository;
diff --git a/src/data/datasources/LocalDataSource.ts b/src/database/LocalDataSource.ts
similarity index 50%
rename from src/data/datasources/LocalDataSource.ts
rename to src/database/LocalDataSource.ts
index ef919bf..be45a82 100644
--- a/src/data/datasources/LocalDataSource.ts
+++ b/src/database/LocalDataSource.ts
@@ -1,111 +1,21 @@
-/**
- * 本地数据库数据源实现
- * 复用 database.ts 的共享连接,避免 OPFS 句柄冲突
- */
-
import * as SQLite from 'expo-sqlite';
-import { ILocalDataSource, DataSourceError } from './interfaces';
-import {
- initDatabase,
- getCurrentDbUserId,
- isDbInitialized,
- getDbInstance,
-} from '../../services/database';
+import { ILocalDataSource, DataSourceError } from '@/data/datasources/interfaces';
+import { databaseManager } from './core/DatabaseManager';
let writeQueue: Promise = Promise.resolve();
-export interface LocalDataSourceConfig {
- userId?: string;
-}
-
export class LocalDataSource implements ILocalDataSource {
- private userId: string | null = null;
- private initialized = false;
- private initializingPromise: Promise | null = null;
-
- constructor(config: LocalDataSourceConfig = {}) {
- this.userId = config.userId || null;
+ async initialize(userId?: string): Promise {
+ await databaseManager.initialize(userId);
}
- async initialize(): Promise {
- if (this.initialized && isDbInitialized()) {
- return;
- }
-
- if (this.initializingPromise) {
- await this.initializingPromise;
- return;
- }
-
- this.initializingPromise = this.doInitialize();
- try {
- await this.initializingPromise;
- } finally {
- this.initializingPromise = null;
- }
- }
-
- private async doInitialize(): Promise {
- try {
- // 如果 database.ts 已经初始化,复用它的连接
- if (isDbInitialized()) {
- const currentUserId = getCurrentDbUserId();
- if (this.userId && this.userId !== currentUserId) {
- await initDatabase(this.userId);
- }
- } else if (this.userId) {
- await initDatabase(this.userId);
- } else {
- const currentUserId = getCurrentDbUserId();
- if (!currentUserId) {
- console.warn('[LocalDataSource] 没有用户登录,跳过初始化');
- return;
- }
- }
-
- const db = getDbInstance();
- if (db) {
- await db.execAsync(`
- CREATE TABLE IF NOT EXISTS posts_cache (
- id TEXT PRIMARY KEY NOT NULL,
- data TEXT NOT NULL,
- updatedAt TEXT NOT NULL
- )
- `);
- }
-
- this.initialized = true;
- } catch (error) {
- this.handleError(error, 'INITIALIZE');
- }
- }
-
- private handleError(error: unknown, operation: string): never {
- const message = error instanceof Error ? error.message : 'Unknown error';
- throw new DataSourceError(
- `Database ${operation} failed: ${message}`,
- 'DB_ERROR',
- 'LocalDataSource',
- error instanceof Error ? error : undefined
- );
- }
-
- private ensureDb(): SQLite.SQLiteDatabase {
- const db = getDbInstance();
- if (!db) {
- throw new DataSourceError(
- 'Database not initialized',
- 'DB_NOT_INITIALIZED',
- 'LocalDataSource'
- );
- }
- return db;
+ private async ensureDb(): Promise {
+ return databaseManager.getDb();
}
async query(sql: string, params?: any[]): Promise {
try {
- await this.initialize();
- const db = this.ensureDb();
+ const db = await this.ensureDb();
if (params && params.length > 0) {
return await db.getAllAsync(sql, params as any);
}
@@ -117,8 +27,7 @@ export class LocalDataSource implements ILocalDataSource {
async getFirst(sql: string, params?: any[]): Promise {
try {
- await this.initialize();
- const db = this.ensureDb();
+ const db = await this.ensureDb();
if (params && params.length > 0) {
return await db.getFirstAsync(sql, params as any);
}
@@ -130,8 +39,7 @@ export class LocalDataSource implements ILocalDataSource {
async execute(sql: string, params?: any[]): Promise {
try {
- await this.initialize();
- const db = this.ensureDb();
+ const db = await this.ensureDb();
await db.execAsync(sql);
} catch (error) {
this.handleError(error, 'EXECUTE');
@@ -140,30 +48,19 @@ export class LocalDataSource implements ILocalDataSource {
async run(sql: string, params?: any[]): Promise<{ lastInsertRowId: number; changes: number }> {
try {
- await this.initialize();
- const db = this.ensureDb();
+ const db = await this.ensureDb();
if (params && params.length > 0) {
return await db.runAsync(sql, params as any);
}
return await db.runAsync(sql);
} catch (error) {
- if (this.isRecoverableError(error)) {
- this.initialized = false;
- await this.initialize();
- const db = this.ensureDb();
- if (params && params.length > 0) {
- return await db.runAsync(sql, params as any);
- }
- return await db.runAsync(sql);
- }
this.handleError(error, 'RUN');
}
}
async transaction(operations: () => Promise): Promise {
try {
- await this.initialize();
- const db = this.ensureDb();
+ const db = await this.ensureDb();
await db.withTransactionAsync(async () => {
await operations();
});
@@ -183,15 +80,13 @@ export class LocalDataSource implements ILocalDataSource {
}
async enqueueWrite(operation: () => Promise): Promise {
- await this.initialize();
const wrappedOperation = async () => {
try {
return await operation();
} catch (error) {
if (this.isRecoverableError(error)) {
console.error('[LocalDataSource] 数据库写入异常,尝试重连后重试:', error);
- this.initialized = false;
- await this.initialize();
+ await databaseManager.initialize(databaseManager.getCurrentUserId());
return operation();
}
throw error;
@@ -213,6 +108,16 @@ export class LocalDataSource implements ILocalDataSource {
message.includes('cannot create file')
);
}
+
+ private handleError(error: unknown, operation: string): never {
+ const message = error instanceof Error ? error.message : 'Unknown error';
+ throw new DataSourceError(
+ `Database ${operation} failed: ${message}`,
+ 'DB_ERROR',
+ 'LocalDataSource',
+ error instanceof Error ? error : undefined
+ );
+ }
}
-export const localDataSource = new LocalDataSource();
+export const localDataSource = new LocalDataSource();
\ No newline at end of file
diff --git a/src/database/core/DatabaseConfig.ts b/src/database/core/DatabaseConfig.ts
new file mode 100644
index 0000000..0d3c5cf
--- /dev/null
+++ b/src/database/core/DatabaseConfig.ts
@@ -0,0 +1,14 @@
+export const DB_CONFIG = {
+ MAX_RETRIES: 4,
+ WEB_RETRY_DELAY_BASE: 500,
+ NATIVE_RETRY_DELAY: 100,
+ WEB_CLOSE_DELAY: 300,
+ WEB_FINAL_CLOSE_DELAY: 200,
+ LOCK_PREFIX: 'db-',
+ DEFAULT_DB_NAME: 'carrot_bbs.db',
+ DB_NAME_PREFIX: 'carrot_bbs_',
+} as const;
+
+export const getDbName = (userId: string | null): string => {
+ return userId ? `${DB_CONFIG.DB_NAME_PREFIX}${userId}.db` : DB_CONFIG.DEFAULT_DB_NAME;
+};
\ No newline at end of file
diff --git a/src/database/core/DatabaseManager.ts b/src/database/core/DatabaseManager.ts
new file mode 100644
index 0000000..36b902b
--- /dev/null
+++ b/src/database/core/DatabaseManager.ts
@@ -0,0 +1,196 @@
+import * as SQLite from 'expo-sqlite';
+import { LockManager } from './LockManager';
+import { DB_CONFIG, getDbName } from './DatabaseConfig';
+import { runMigrations } from '../schema/MigrationManager';
+
+type SQLiteDatabase = SQLite.SQLiteDatabase;
+
+class DatabaseManager {
+ private static instance: DatabaseManager;
+ private db: SQLiteDatabase | null = null;
+ private currentUserId: string | null = null;
+ private initPromise: Promise | null = null;
+ private initTargetUserId: string | null = null;
+ private isInitialized = false;
+
+ static getInstance(): DatabaseManager {
+ if (!DatabaseManager.instance) {
+ DatabaseManager.instance = new DatabaseManager();
+ }
+ return DatabaseManager.instance;
+ }
+
+ async initialize(userId?: string | null): Promise {
+ const targetUserId = userId || null;
+ const callId = Date.now();
+
+ console.log(`[DatabaseManager ${callId}] initialize 开始, userId=${targetUserId}, 当前db=${!!this.db}, 当前userId=${this.currentUserId}`);
+
+ if (this.db && this.currentUserId === targetUserId) {
+ console.log(`[DatabaseManager ${callId}] 已是目标数据库,直接返回`);
+ this.isInitialized = true;
+ return;
+ }
+
+ if (this.initPromise && this.initTargetUserId === targetUserId) {
+ console.log(`[DatabaseManager ${callId}] 等待进行中的初始化`);
+ await this.initPromise;
+ console.log(`[DatabaseManager ${callId}] 初始化完成`);
+ return;
+ }
+
+ if (this.initPromise) {
+ console.log(`[DatabaseManager ${callId}] 等待其他初始化完成`);
+ await this.initPromise.catch(() => {});
+ }
+
+ if (this.db && this.currentUserId === targetUserId) {
+ console.log(`[DatabaseManager ${callId}] 其他初始化已完成目标,直接返回`);
+ this.isInitialized = true;
+ return;
+ }
+
+ this.initTargetUserId = targetUserId;
+ console.log(`[DatabaseManager ${callId}] 开始实际初始化`);
+
+ this.initPromise = LockManager.withMutex(async () => {
+ const mutexId = callId;
+ console.log(`[DatabaseManager ${mutexId}] 获得互斥锁`);
+
+ if (this.db && this.currentUserId === targetUserId) {
+ console.log(`[DatabaseManager ${mutexId}] 互斥锁内检查:已是目标数据库`);
+ this.isInitialized = true;
+ return;
+ }
+
+ const isWeb = typeof window !== 'undefined';
+
+ if (this.db) {
+ console.log(`[DatabaseManager ${mutexId}] 关闭旧数据库连接...`);
+ try {
+ await this.db.closeAsync();
+ console.log(`[DatabaseManager ${mutexId}] 旧连接已关闭`);
+ } catch (e) {
+ console.warn(`[DatabaseManager ${mutexId}] 关闭旧连接失败:`, e);
+ }
+ this.db = null;
+ this.currentUserId = null;
+ this.isInitialized = false;
+
+ if (isWeb) {
+ await new Promise(r => setTimeout(r, DB_CONFIG.WEB_CLOSE_DELAY));
+ }
+ }
+
+ const dbName = getDbName(targetUserId);
+ console.log(`[DatabaseManager ${mutexId}] 打开数据库: ${dbName}`);
+
+ const database = await this.openDatabaseWithRetry(dbName);
+ console.log(`[DatabaseManager ${mutexId}] 数据库已打开,执行迁移`);
+
+ await runMigrations(database);
+ console.log(`[DatabaseManager ${mutexId}] 迁移完成`);
+
+ this.db = database;
+ this.currentUserId = targetUserId;
+ this.isInitialized = true;
+ console.log(`[DatabaseManager ${mutexId}] 初始化完成`);
+ }).finally(() => {
+ console.log(`[DatabaseManager ${callId}] 清理初始化状态`);
+ this.initPromise = null;
+ this.initTargetUserId = null;
+ });
+
+ await this.initPromise;
+ console.log(`[DatabaseManager ${callId}] initialize 返回`);
+ }
+
+ private async openDatabaseWithRetry(dbName: string): Promise {
+ const isWeb = typeof window !== 'undefined';
+
+ return LockManager.withWebLock(`${DB_CONFIG.LOCK_PREFIX}${dbName}`, async () => {
+ for (let attempt = 0; attempt <= DB_CONFIG.MAX_RETRIES; attempt++) {
+ try {
+ const database = await SQLite.openDatabaseAsync(dbName);
+ console.log(`[DatabaseManager] 成功打开数据库 (尝试 ${attempt + 1}/${DB_CONFIG.MAX_RETRIES + 1})`);
+ return database;
+ } catch (error) {
+ const msg = String(error);
+ console.error(`[DatabaseManager] 打开失败 (尝试 ${attempt + 1}/${DB_CONFIG.MAX_RETRIES + 1}):`, msg);
+
+ if (attempt < DB_CONFIG.MAX_RETRIES) {
+ const delay = isWeb ? DB_CONFIG.WEB_RETRY_DELAY_BASE * (attempt + 1) : DB_CONFIG.NATIVE_RETRY_DELAY;
+ console.warn(`[DatabaseManager] ${delay}ms 后重试...`);
+ await new Promise(r => setTimeout(r, delay));
+ } else {
+ throw error;
+ }
+ }
+ }
+ throw new Error('打开数据库失败');
+ });
+ }
+
+ async close(): Promise {
+ if (this.initPromise) {
+ await this.initPromise.catch(() => {});
+ }
+
+ const dbName = getDbName(this.currentUserId);
+
+ await LockManager.withWebLock(`${DB_CONFIG.LOCK_PREFIX}${dbName}`, async () => {
+ await LockManager.withMutex(async () => {
+ if (!this.db) return;
+
+ try {
+ await this.db.closeAsync();
+ console.log('[DatabaseManager] 数据库已关闭');
+
+ if (typeof window !== 'undefined') {
+ await new Promise(r => setTimeout(r, DB_CONFIG.WEB_FINAL_CLOSE_DELAY));
+ }
+ } catch (e) {
+ console.warn('[DatabaseManager] 关闭失败:', e);
+ }
+ this.db = null;
+ this.currentUserId = null;
+ this.isInitialized = false;
+ });
+ });
+ }
+
+ async getDb(timeout: number = 10000): Promise {
+ if (this.isInitialized && this.db) return this.db;
+
+ if (this.initPromise) {
+ const timeoutPromise = new Promise((_, reject) =>
+ setTimeout(() => reject(new Error('等待数据库初始化超时')), timeout)
+ );
+
+ await Promise.race([this.initPromise, timeoutPromise]);
+
+ if (this.db) return this.db;
+ }
+
+ throw new Error('数据库未初始化,请先调用 initialize(userId)');
+ }
+
+ getDbSync(): SQLiteDatabase | null {
+ return this.db;
+ }
+
+ getCurrentUserId(): string | null {
+ return this.currentUserId;
+ }
+
+ isReady(): boolean {
+ return this.isInitialized && this.db !== null;
+ }
+
+ async withMutex(fn: () => Promise): Promise {
+ return LockManager.withMutex(fn);
+ }
+}
+
+export const databaseManager = DatabaseManager.getInstance();
+export { DatabaseManager };
\ No newline at end of file
diff --git a/src/database/core/LockManager.ts b/src/database/core/LockManager.ts
new file mode 100644
index 0000000..5003784
--- /dev/null
+++ b/src/database/core/LockManager.ts
@@ -0,0 +1,34 @@
+export class LockManager {
+ private static mutex: Promise = Promise.resolve();
+
+ static async withMutex(fn: () => Promise): Promise {
+ const prev = LockManager.mutex;
+ let release: () => void = () => {};
+ LockManager.mutex = new Promise((r) => { release = r; });
+ try {
+ await prev;
+ return await fn();
+ } finally {
+ release();
+ }
+ }
+
+ static async withWebLock(lockName: string, fn: () => Promise): Promise {
+ if (typeof navigator !== 'undefined' && navigator.locks) {
+ return new Promise((resolve, reject) => {
+ navigator.locks.request(lockName, { mode: 'exclusive' }, async (lock) => {
+ if (!lock) {
+ return reject(new Error('无法获取跨标签页锁'));
+ }
+ try {
+ const result = await fn();
+ resolve(result);
+ } catch (e) {
+ reject(e);
+ }
+ });
+ });
+ }
+ return fn();
+ }
+}
\ No newline at end of file
diff --git a/src/database/index.ts b/src/database/index.ts
new file mode 100644
index 0000000..398d5b4
--- /dev/null
+++ b/src/database/index.ts
@@ -0,0 +1,54 @@
+import { localDataSource } from './LocalDataSource';
+import { databaseManager } from './core/DatabaseManager';
+
+export { databaseManager } from './core/DatabaseManager';
+export { DatabaseManager } from './core/DatabaseManager';
+export { localDataSource, LocalDataSource } from './LocalDataSource';
+
+export {
+ messageRepository, MessageRepository,
+ conversationRepository, ConversationRepository,
+ userCacheRepository, UserCacheRepository,
+ groupCacheRepository, GroupCacheRepository,
+ postCacheRepository, PostCacheRepository,
+} from './repositories';
+
+export { CachedMessage } from './types';
+
+export async function clearAllData(): Promise {
+ await localDataSource.enqueueWrite(async () => {
+ await localDataSource.execute(`DELETE FROM messages`);
+ await localDataSource.execute(`DELETE FROM conversations`);
+ await localDataSource.execute(`DELETE FROM conversation_cache`);
+ await localDataSource.execute(`DELETE FROM conversation_list_cache`);
+ await localDataSource.execute(`DELETE FROM users`);
+ await localDataSource.execute(`DELETE FROM current_user_cache`);
+ await localDataSource.execute(`DELETE FROM groups`);
+ await localDataSource.execute(`DELETE FROM group_members`);
+ await localDataSource.execute(`DELETE FROM posts_cache`);
+ });
+}
+
+export const initDatabase = async (userId?: string | null): Promise => {
+ await databaseManager.initialize(userId);
+};
+
+export const closeDatabase = async (): Promise => {
+ await databaseManager.close();
+};
+
+export const getCurrentDbUserId = (): string | null => {
+ return databaseManager.getCurrentUserId();
+};
+
+export const isDbInitialized = (): boolean => {
+ return databaseManager.isReady();
+};
+
+export const getDbInstance = () => {
+ return databaseManager.getDbSync();
+};
+
+export const getDb = async (timeout?: number) => {
+ return databaseManager.getDb(timeout);
+};
\ No newline at end of file
diff --git a/src/database/repositories/ConversationRepository.ts b/src/database/repositories/ConversationRepository.ts
new file mode 100644
index 0000000..73588fa
--- /dev/null
+++ b/src/database/repositories/ConversationRepository.ts
@@ -0,0 +1,197 @@
+import { localDataSource } from '../LocalDataSource';
+import type { ILocalDataSource } from '@/data/datasources/interfaces';
+import type { ConversationResponse, ConversationDetailResponse } from '@/types/dto';
+
+type ConversationCacheData = ConversationResponse | ConversationDetailResponse;
+
+const safeParseJson = (value: string): T | null => {
+ try { return JSON.parse(value) as T; } catch { return null; }
+};
+
+export interface IConversationRepository {
+ save(conv: { id: string; participantId: string; lastMessageId?: string; lastSeq?: number; unreadCount?: number; createdAt: string; updatedAt: string }): Promise;
+ getAll(): Promise;
+ updateUnreadCount(id: string, count: number): Promise;
+ updateLastMessage(id: string, lastMessageId: string, lastSeq: number, updatedAt: string): Promise;
+ delete(conversationId: string): Promise;
+ getCache(id: string): Promise;
+ getListCache(): Promise;
+ saveCache(conv: ConversationCacheData): Promise;
+ saveListCache(convs: ConversationResponse[]): Promise;
+ updateListEntry(id: string, updates: Partial): Promise;
+ updateCacheUnreadCount(id: string, count: number, myLastReadSeq?: number): Promise;
+ saveWithRelated(convs: ConversationResponse[], users?: any[], groups?: any[]): Promise;
+}
+
+export class ConversationRepository implements IConversationRepository {
+ constructor(private dataSource: ILocalDataSource = localDataSource) {}
+
+ async save(conv: { id: string; participantId: string; lastMessageId?: string; lastSeq?: number; unreadCount?: number; createdAt: string; updatedAt: string }): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO conversations (id, participantId, lastMessageId, lastSeq, unreadCount, createdAt, updatedAt)
+ VALUES (?, ?, ?, ?, ?, ?, ?)`,
+ [conv.id, conv.participantId, conv.lastMessageId || null, conv.lastSeq || 0, conv.unreadCount || 0, conv.createdAt, conv.updatedAt]
+ );
+ });
+ }
+
+ async getAll(): Promise {
+ return this.dataSource.query(`SELECT * FROM conversations ORDER BY updatedAt DESC`);
+ }
+
+ async updateUnreadCount(id: string, count: number): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`UPDATE conversations SET unreadCount = ? WHERE id = ?`, [count, id]);
+ });
+ }
+
+ async updateLastMessage(id: string, lastMessageId: string, lastSeq: number, updatedAt: string): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(
+ `UPDATE conversations SET lastMessageId = ?, lastSeq = ?, updatedAt = ? WHERE id = ?`,
+ [lastMessageId, lastSeq, updatedAt, id]
+ );
+ });
+ }
+
+ async delete(conversationId: string): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`DELETE FROM messages WHERE conversationId = ?`, [conversationId]);
+ await this.dataSource.run(`DELETE FROM conversations WHERE id = ?`, [conversationId]);
+ });
+ }
+
+ async getCache(id: string): Promise {
+ const r = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM conversation_cache WHERE id = ?`, [String(id)]
+ );
+ return r?.data ? safeParseJson(r.data) : null;
+ }
+
+ async getListCache(): Promise {
+ let rows = await this.dataSource.query<{ data: string }>(
+ `SELECT data FROM conversation_list_cache ORDER BY updatedAt DESC`
+ );
+ if (!rows.length) {
+ rows = await this.dataSource.query<{ data: string }>(
+ `SELECT data FROM conversation_cache ORDER BY updatedAt DESC`
+ );
+ }
+ const list = rows.map(r => safeParseJson(r.data))
+ .filter((c): c is ConversationResponse => Boolean(c))
+ .filter(c => typeof c.id !== 'undefined' && typeof c.type !== 'undefined');
+ return list.sort((a, b) => {
+ const aPinned = a.is_pinned ? 1 : 0;
+ const bPinned = b.is_pinned ? 1 : 0;
+ if (aPinned !== bPinned) return bPinned - aPinned;
+ const aTime = new Date(a.last_message_at || a.updated_at || 0).getTime();
+ const bTime = new Date(b.last_message_at || b.updated_at || 0).getTime();
+ return bTime - aTime;
+ });
+ }
+
+ async saveCache(conv: ConversationCacheData): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ const updatedAt = ('updated_at' in conv && conv.updated_at) ? String(conv.updated_at) : new Date().toISOString();
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO conversation_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(conv.id), JSON.stringify(conv), updatedAt]
+ );
+ });
+ }
+
+ async saveListCache(convs: ConversationResponse[]): Promise {
+ if (!convs?.length) return;
+ await this.dataSource.enqueueWrite(async () => {
+ for (const c of convs) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO conversation_list_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(c.id), JSON.stringify(c), c.updated_at || new Date().toISOString()]
+ );
+ }
+ });
+ }
+
+ async updateListEntry(id: string, updates: Partial): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ const r = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM conversation_list_cache WHERE id = ?`, [String(id)]
+ );
+ if (!r?.data) return;
+ const conv = safeParseJson(r.data);
+ if (!conv) return;
+ const updated = { ...conv, ...updates };
+ const updatedAt = updates.last_message_at || updated.last_message_at || new Date().toISOString();
+ await this.dataSource.run(
+ `UPDATE conversation_list_cache SET data = ?, updatedAt = ? WHERE id = ?`,
+ [JSON.stringify(updated), updatedAt, String(id)]
+ );
+ });
+ }
+
+ async updateCacheUnreadCount(id: string, count: number, myLastReadSeq?: number): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ const listRow = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM conversation_list_cache WHERE id = ?`, [String(id)]
+ );
+ if (listRow?.data) {
+ const conv = safeParseJson(listRow.data);
+ if (conv) {
+ conv.unread_count = count;
+ if (typeof myLastReadSeq === 'number' && !Number.isNaN(myLastReadSeq)) conv.my_last_read_seq = myLastReadSeq;
+ await this.dataSource.run(
+ `UPDATE conversation_list_cache SET data = ? WHERE id = ?`,
+ [JSON.stringify(conv), String(id)]
+ );
+ }
+ }
+
+ const cacheRow = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM conversation_cache WHERE id = ?`, [String(id)]
+ );
+ if (cacheRow?.data) {
+ const conv = safeParseJson>(cacheRow.data);
+ if (conv) {
+ conv.unread_count = count;
+ if (typeof myLastReadSeq === 'number' && !Number.isNaN(myLastReadSeq)) conv.my_last_read_seq = myLastReadSeq;
+ await this.dataSource.run(
+ `UPDATE conversation_cache SET data = ? WHERE id = ?`,
+ [JSON.stringify(conv), String(id)]
+ );
+ }
+ }
+ });
+ }
+
+ async saveWithRelated(convs: ConversationResponse[], users?: any[], groups?: any[]): Promise {
+ if (!convs?.length) return;
+ const now = new Date().toISOString();
+ await this.dataSource.enqueueWrite(async () => {
+ for (const c of convs) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO conversation_list_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(c.id), JSON.stringify(c), c.updated_at || now]
+ );
+ }
+ if (users?.length) {
+ for (const u of users) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO users (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(u.id), JSON.stringify(u), now]
+ );
+ }
+ }
+ if (groups?.length) {
+ for (const g of groups) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO groups (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(g.id), JSON.stringify(g), now]
+ );
+ }
+ }
+ });
+ }
+}
+
+export const conversationRepository = new ConversationRepository();
\ No newline at end of file
diff --git a/src/database/repositories/GroupCacheRepository.ts b/src/database/repositories/GroupCacheRepository.ts
new file mode 100644
index 0000000..5e79702
--- /dev/null
+++ b/src/database/repositories/GroupCacheRepository.ts
@@ -0,0 +1,78 @@
+import { localDataSource } from '../LocalDataSource';
+import type { ILocalDataSource } from '@/data/datasources/interfaces';
+import type { GroupResponse, GroupMemberResponse } from '@/types/dto';
+
+const safeParseJson = (value: string): T | null => {
+ try { return JSON.parse(value) as T; } catch { return null; }
+};
+
+export interface IGroupCacheRepository {
+ save(group: GroupResponse): Promise;
+ saveBatch(groups: GroupResponse[]): Promise;
+ get(groupId: string): Promise;
+ getAll(): Promise;
+ saveMembers(groupId: string, members: GroupMemberResponse[]): Promise;
+ getMembers(groupId: string): Promise;
+}
+
+export class GroupCacheRepository implements IGroupCacheRepository {
+ constructor(private dataSource: ILocalDataSource = localDataSource) {}
+
+ async save(group: GroupResponse): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO groups (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(group.id), JSON.stringify(group), new Date().toISOString()]
+ );
+ });
+ }
+
+ async saveBatch(groups: GroupResponse[]): Promise {
+ if (!groups?.length) return;
+ await this.dataSource.enqueueWrite(async () => {
+ const now = new Date().toISOString();
+ for (const g of groups) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO groups (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(g.id), JSON.stringify(g), now]
+ );
+ }
+ });
+ }
+
+ async get(groupId: string): Promise {
+ const r = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM groups WHERE id = ?`, [String(groupId)]
+ );
+ return r?.data ? safeParseJson(r.data) : null;
+ }
+
+ async getAll(): Promise {
+ const rows = await this.dataSource.query<{ data: string }>(
+ `SELECT data FROM groups ORDER BY updatedAt DESC`
+ );
+ return rows.map(r => safeParseJson(r.data)).filter((g): g is GroupResponse => Boolean(g));
+ }
+
+ async saveMembers(groupId: string, members: GroupMemberResponse[]): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ const now = new Date().toISOString();
+ await this.dataSource.run(`DELETE FROM group_members WHERE groupId = ?`, [String(groupId)]);
+ for (const m of members) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO group_members (groupId, userId, data, updatedAt) VALUES (?, ?, ?, ?)`,
+ [String(groupId), String(m.user_id), JSON.stringify(m), now]
+ );
+ }
+ });
+ }
+
+ async getMembers(groupId: string): Promise {
+ const rows = await this.dataSource.query<{ data: string }>(
+ `SELECT data FROM group_members WHERE groupId = ? ORDER BY updatedAt DESC`, [String(groupId)]
+ );
+ return rows.map(r => safeParseJson(r.data)).filter((m): m is GroupMemberResponse => Boolean(m));
+ }
+}
+
+export const groupCacheRepository = new GroupCacheRepository();
\ No newline at end of file
diff --git a/src/database/repositories/MessageRepository.ts b/src/database/repositories/MessageRepository.ts
new file mode 100644
index 0000000..961bd41
--- /dev/null
+++ b/src/database/repositories/MessageRepository.ts
@@ -0,0 +1,152 @@
+import { localDataSource } from '../LocalDataSource';
+import { CachedMessage } from '../types';
+import type { ILocalDataSource } from '@/data/datasources/interfaces';
+
+export interface IMessageRepository {
+ saveMessage(message: CachedMessage): Promise;
+ saveMessagesBatch(messages: CachedMessage[]): Promise;
+ getByConversation(conversationId: string, limit?: number): Promise;
+ getMaxSeq(conversationId: string): Promise;
+ getMinSeq(conversationId: string): Promise;
+ getBeforeSeq(conversationId: string, beforeSeq: number, limit?: number): Promise;
+ getCount(conversationId: string): Promise;
+ getCountBeforeSeq(conversationId: string, beforeSeq: number): Promise;
+ markAsRead(messageId: string): Promise;
+ markConversationAsRead(conversationId: string): Promise;
+ delete(messageId: string): Promise;
+ updateStatus(messageId: string, status: string, clearContent?: boolean): Promise;
+ clearConversation(conversationId: string): Promise;
+ search(keyword: string): Promise;
+ getStats(): Promise<{ totalMessages: number; totalConversations: number }>;
+}
+
+export class MessageRepository implements IMessageRepository {
+ constructor(private dataSource: ILocalDataSource = localDataSource) {}
+
+ async saveMessage(message: CachedMessage): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO messages (id, conversationId, senderId, content, type, isRead, createdAt, seq, status, segments)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
+ [message.id, message.conversationId, message.senderId, message.content || '',
+ message.type || 'text', message.isRead ? 1 : 0, message.createdAt,
+ message.seq || 0, message.status || 'normal',
+ message.segments ? JSON.stringify(message.segments) : null]
+ );
+ });
+ }
+
+ async saveMessagesBatch(messages: CachedMessage[]): Promise {
+ if (!messages?.length) return;
+ await this.dataSource.enqueueWrite(async () => {
+ for (const msg of messages) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO messages (id, conversationId, senderId, content, type, isRead, createdAt, seq, status, segments)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
+ [msg.id, msg.conversationId, msg.senderId, msg.content || '',
+ msg.type || 'text', msg.isRead ? 1 : 0, msg.createdAt,
+ msg.seq || 0, msg.status || 'normal',
+ msg.segments ? JSON.stringify(msg.segments) : null]
+ );
+ }
+ });
+ }
+
+ async getByConversation(conversationId: string, limit: number = 20): Promise {
+ const rows = await this.dataSource.query(
+ `SELECT * FROM messages WHERE conversationId = ? ORDER BY seq DESC LIMIT ?`,
+ [conversationId, limit]
+ );
+ return rows.reverse().map(r => ({
+ ...r, isRead: r.isRead === 1, segments: r.segments ? JSON.parse(r.segments) : undefined
+ }));
+ }
+
+ async getMaxSeq(conversationId: string): Promise {
+ const r = await this.dataSource.getFirst<{ maxSeq: number }>(
+ `SELECT MAX(seq) as maxSeq FROM messages WHERE conversationId = ?`, [conversationId]
+ );
+ return r?.maxSeq || 0;
+ }
+
+ async getMinSeq(conversationId: string): Promise {
+ const r = await this.dataSource.getFirst<{ minSeq: number }>(
+ `SELECT MIN(seq) as minSeq FROM messages WHERE conversationId = ?`, [conversationId]
+ );
+ return r?.minSeq || 0;
+ }
+
+ async getBeforeSeq(conversationId: string, beforeSeq: number, limit: number = 20): Promise {
+ const rows = await this.dataSource.query(
+ `SELECT * FROM messages WHERE conversationId = ? AND seq < ? ORDER BY seq DESC LIMIT ?`,
+ [conversationId, beforeSeq, limit]
+ );
+ return rows.map(r => ({ ...r, isRead: r.isRead === 1, segments: r.segments ? JSON.parse(r.segments) : undefined })).reverse();
+ }
+
+ async getCount(conversationId: string): Promise {
+ const r = await this.dataSource.getFirst<{ count: number }>(
+ `SELECT COUNT(*) as count FROM messages WHERE conversationId = ?`, [conversationId]
+ );
+ return r?.count || 0;
+ }
+
+ async getCountBeforeSeq(conversationId: string, beforeSeq: number): Promise {
+ const r = await this.dataSource.getFirst<{ count: number }>(
+ `SELECT COUNT(*) as count FROM messages WHERE conversationId = ? AND seq < ?`, [conversationId, beforeSeq]
+ );
+ return r?.count || 0;
+ }
+
+ async markAsRead(messageId: string): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`UPDATE messages SET isRead = 1 WHERE id = ?`, [messageId]);
+ });
+ }
+
+ async markConversationAsRead(conversationId: string): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`UPDATE messages SET isRead = 1 WHERE conversationId = ?`, [conversationId]);
+ });
+ }
+
+ async delete(messageId: string): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`DELETE FROM messages WHERE id = ?`, [messageId]);
+ });
+ }
+
+ async updateStatus(messageId: string, status: string, clearContent: boolean = false): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ if (clearContent) {
+ await this.dataSource.run(
+ `UPDATE messages SET status = ?, content = '', segments = '[]' WHERE id = ?`,
+ [status, messageId]
+ );
+ } else {
+ await this.dataSource.run(`UPDATE messages SET status = ? WHERE id = ?`, [status, messageId]);
+ }
+ });
+ }
+
+ async clearConversation(conversationId: string): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`DELETE FROM messages WHERE conversationId = ?`, [conversationId]);
+ });
+ }
+
+ async search(keyword: string): Promise {
+ const rows = await this.dataSource.query(
+ `SELECT * FROM messages WHERE content LIKE ? ORDER BY createdAt DESC LIMIT 50`, [`%${keyword}%`]
+ );
+ return rows.map(r => ({ ...r, isRead: r.isRead === 1, segments: r.segments ? JSON.parse(r.segments) : undefined }));
+ }
+
+ async getStats(): Promise<{ totalMessages: number; totalConversations: number }> {
+ const msgCount = await this.dataSource.getFirst<{ count: number }>(`SELECT COUNT(*) as count FROM messages`);
+ const convCount = await this.dataSource.getFirst<{ count: number }>(`SELECT COUNT(*) as count FROM conversations`);
+ return { totalMessages: msgCount?.count || 0, totalConversations: convCount?.count || 0 };
+ }
+}
+
+export const messageRepository = new MessageRepository();
\ No newline at end of file
diff --git a/src/database/repositories/PostCacheRepository.ts b/src/database/repositories/PostCacheRepository.ts
new file mode 100644
index 0000000..bc535bb
--- /dev/null
+++ b/src/database/repositories/PostCacheRepository.ts
@@ -0,0 +1,44 @@
+import { localDataSource } from '../LocalDataSource';
+import type { ILocalDataSource } from '@/data/datasources/interfaces';
+
+export interface IPostCacheRepository {
+ save(id: string, data: any): Promise;
+ get(id: string): Promise;
+ getAll(): Promise;
+ delete(id: string): Promise;
+}
+
+export class PostCacheRepository implements IPostCacheRepository {
+ constructor(private dataSource: ILocalDataSource = localDataSource) {}
+
+ async save(id: string, data: any): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO posts_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(id), JSON.stringify(data), new Date().toISOString()]
+ );
+ });
+ }
+
+ async get(id: string): Promise {
+ const r = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM posts_cache WHERE id = ?`, [String(id)]
+ );
+ return r?.data ? JSON.parse(r.data) : null;
+ }
+
+ async getAll(): Promise {
+ const rows = await this.dataSource.query<{ data: string }>(
+ `SELECT data FROM posts_cache ORDER BY updatedAt DESC`
+ );
+ return rows.map(r => JSON.parse(r.data));
+ }
+
+ async delete(id: string): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`DELETE FROM posts_cache WHERE id = ?`, [String(id)]);
+ });
+ }
+}
+
+export const postCacheRepository = new PostCacheRepository();
\ No newline at end of file
diff --git a/src/database/repositories/UserCacheRepository.ts b/src/database/repositories/UserCacheRepository.ts
new file mode 100644
index 0000000..dbf7975
--- /dev/null
+++ b/src/database/repositories/UserCacheRepository.ts
@@ -0,0 +1,81 @@
+import { localDataSource } from '../LocalDataSource';
+import type { ILocalDataSource } from '@/data/datasources/interfaces';
+import type { UserDTO } from '@/types/dto';
+
+const safeParseJson = (value: string): T | null => {
+ try { return JSON.parse(value) as T; } catch { return null; }
+};
+
+export interface IUserCacheRepository {
+ save(user: UserDTO): Promise;
+ saveBatch(users: UserDTO[]): Promise;
+ get(userId: string): Promise;
+ getLatest(): Promise;
+ saveCurrent(user: UserDTO): Promise;
+ getCurrent(): Promise;
+ clearCurrent(): Promise;
+}
+
+export class UserCacheRepository implements IUserCacheRepository {
+ constructor(private dataSource: ILocalDataSource = localDataSource) {}
+
+ async save(user: UserDTO): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO users (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(user.id), JSON.stringify(user), new Date().toISOString()]
+ );
+ });
+ }
+
+ async saveBatch(users: UserDTO[]): Promise {
+ if (!users?.length) return;
+ await this.dataSource.enqueueWrite(async () => {
+ const now = new Date().toISOString();
+ for (const u of users) {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO users (id, data, updatedAt) VALUES (?, ?, ?)`,
+ [String(u.id), JSON.stringify(u), now]
+ );
+ }
+ });
+ }
+
+ async get(userId: string): Promise {
+ const r = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM users WHERE id = ?`, [String(userId)]
+ );
+ return r?.data ? safeParseJson(r.data) : null;
+ }
+
+ async getLatest(): Promise {
+ const r = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM users ORDER BY updatedAt DESC LIMIT 1`
+ );
+ return r?.data ? safeParseJson(r.data) : null;
+ }
+
+ async saveCurrent(user: UserDTO): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(
+ `INSERT OR REPLACE INTO current_user_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
+ ['me', JSON.stringify(user), new Date().toISOString()]
+ );
+ });
+ }
+
+ async getCurrent(): Promise {
+ const r = await this.dataSource.getFirst<{ data: string }>(
+ `SELECT data FROM current_user_cache WHERE id = 'me'`
+ );
+ return r?.data ? safeParseJson(r.data) : null;
+ }
+
+ async clearCurrent(): Promise {
+ await this.dataSource.enqueueWrite(async () => {
+ await this.dataSource.run(`DELETE FROM current_user_cache WHERE id = 'me'`);
+ });
+ }
+}
+
+export const userCacheRepository = new UserCacheRepository();
\ No newline at end of file
diff --git a/src/database/repositories/index.ts b/src/database/repositories/index.ts
new file mode 100644
index 0000000..c978220
--- /dev/null
+++ b/src/database/repositories/index.ts
@@ -0,0 +1,5 @@
+export { IMessageRepository, MessageRepository, messageRepository } from './MessageRepository';
+export { IConversationRepository, ConversationRepository, conversationRepository } from './ConversationRepository';
+export { IUserCacheRepository, UserCacheRepository, userCacheRepository } from './UserCacheRepository';
+export { IGroupCacheRepository, GroupCacheRepository, groupCacheRepository } from './GroupCacheRepository';
+export { IPostCacheRepository, PostCacheRepository, postCacheRepository } from './PostCacheRepository';
\ No newline at end of file
diff --git a/src/database/schema/ConversationSchema.ts b/src/database/schema/ConversationSchema.ts
new file mode 100644
index 0000000..0508531
--- /dev/null
+++ b/src/database/schema/ConversationSchema.ts
@@ -0,0 +1,40 @@
+import type { SQLiteDatabase } from 'expo-sqlite';
+
+export const CONVERSATIONS_TABLE = 'conversations';
+export const CONVERSATION_CACHE_TABLE = 'conversation_cache';
+export const CONVERSATION_LIST_CACHE_TABLE = 'conversation_list_cache';
+
+export const CREATE_CONVERSATIONS_TABLE = `CREATE TABLE IF NOT EXISTS conversations (
+ id TEXT PRIMARY KEY NOT NULL,
+ participantId TEXT NOT NULL,
+ lastMessageId TEXT,
+ lastSeq INTEGER DEFAULT 0,
+ unreadCount INTEGER DEFAULT 0,
+ createdAt TEXT NOT NULL,
+ updatedAt TEXT NOT NULL
+)`;
+
+export const CREATE_CONVERSATION_CACHE_TABLE = `CREATE TABLE IF NOT EXISTS conversation_cache (
+ id TEXT PRIMARY KEY NOT NULL,
+ data TEXT NOT NULL,
+ updatedAt TEXT NOT NULL
+)`;
+
+export const CREATE_CONVERSATION_LIST_CACHE_TABLE = `CREATE TABLE IF NOT EXISTS conversation_list_cache (
+ id TEXT PRIMARY KEY NOT NULL,
+ data TEXT NOT NULL,
+ updatedAt TEXT NOT NULL
+)`;
+
+export const MIGRATE_CONVERSATIONS_TABLE = async (db: SQLiteDatabase): Promise => {
+ try {
+ const tableInfo = await db.getAllAsync<{ name: string }>(`PRAGMA table_info(conversations)`);
+ const columns = tableInfo.map(col => col.name);
+
+ if (!columns.includes('lastSeq')) {
+ await db.execAsync(`ALTER TABLE conversations ADD COLUMN lastSeq INTEGER DEFAULT 0`);
+ }
+ } catch (error) {
+ console.error('[ConversationSchema] 迁移失败:', error);
+ }
+};
\ No newline at end of file
diff --git a/src/database/schema/GroupSchema.ts b/src/database/schema/GroupSchema.ts
new file mode 100644
index 0000000..5e4b365
--- /dev/null
+++ b/src/database/schema/GroupSchema.ts
@@ -0,0 +1,20 @@
+export const GROUPS_TABLE = 'groups';
+export const GROUP_MEMBERS_TABLE = 'group_members';
+
+export const CREATE_GROUPS_TABLE = `CREATE TABLE IF NOT EXISTS groups (
+ id TEXT PRIMARY KEY NOT NULL,
+ data TEXT NOT NULL,
+ updatedAt TEXT NOT NULL
+)`;
+
+export const CREATE_GROUP_MEMBERS_TABLE = `CREATE TABLE IF NOT EXISTS group_members (
+ groupId TEXT NOT NULL,
+ userId TEXT NOT NULL,
+ data TEXT NOT NULL,
+ updatedAt TEXT NOT NULL,
+ PRIMARY KEY (groupId, userId)
+)`;
+
+export const CREATE_GROUP_INDEXES = [
+ `CREATE INDEX IF NOT EXISTS idx_group_members_groupId ON group_members(groupId)`,
+];
\ No newline at end of file
diff --git a/src/database/schema/MessageSchema.ts b/src/database/schema/MessageSchema.ts
new file mode 100644
index 0000000..bdf0e56
--- /dev/null
+++ b/src/database/schema/MessageSchema.ts
@@ -0,0 +1,41 @@
+import type { SQLiteDatabase } from 'expo-sqlite';
+
+export const MESSAGES_TABLE = 'messages';
+
+export const CREATE_MESSAGES_TABLE = `CREATE TABLE IF NOT EXISTS messages (
+ id TEXT PRIMARY KEY NOT NULL,
+ conversationId TEXT NOT NULL,
+ senderId TEXT NOT NULL,
+ content TEXT,
+ type TEXT DEFAULT 'text',
+ isRead INTEGER DEFAULT 0,
+ createdAt TEXT NOT NULL,
+ seq INTEGER DEFAULT 0,
+ status TEXT DEFAULT 'normal',
+ segments TEXT
+)`;
+
+export const CREATE_MESSAGES_INDEXES = [
+ `CREATE INDEX IF NOT EXISTS idx_messages_conversationId ON messages(conversationId)`,
+ `CREATE INDEX IF NOT EXISTS idx_messages_seq ON messages(seq)`,
+ `CREATE INDEX IF NOT EXISTS idx_messages_conversation_seq ON messages(conversationId, seq)`,
+];
+
+export const MIGRATE_MESSAGES_TABLE = async (db: SQLiteDatabase): Promise => {
+ try {
+ const tableInfo = await db.getAllAsync<{ name: string }>(`PRAGMA table_info(messages)`);
+ const columns = tableInfo.map(col => col.name);
+
+ if (!columns.includes('seq')) {
+ await db.execAsync(`ALTER TABLE messages ADD COLUMN seq INTEGER DEFAULT 0`);
+ }
+ if (!columns.includes('status')) {
+ await db.execAsync(`ALTER TABLE messages ADD COLUMN status TEXT DEFAULT 'normal'`);
+ }
+ if (!columns.includes('segments')) {
+ await db.execAsync(`ALTER TABLE messages ADD COLUMN segments TEXT`);
+ }
+ } catch (error) {
+ console.error('[MessageSchema] 迁移失败:', error);
+ }
+};
\ No newline at end of file
diff --git a/src/database/schema/MigrationManager.ts b/src/database/schema/MigrationManager.ts
new file mode 100644
index 0000000..790145d
--- /dev/null
+++ b/src/database/schema/MigrationManager.ts
@@ -0,0 +1,57 @@
+import type { SQLiteDatabase } from 'expo-sqlite';
+import {
+ CREATE_MESSAGES_TABLE,
+ CREATE_MESSAGES_INDEXES,
+ MIGRATE_MESSAGES_TABLE,
+} from './MessageSchema';
+import {
+ CREATE_CONVERSATIONS_TABLE,
+ CREATE_CONVERSATION_CACHE_TABLE,
+ CREATE_CONVERSATION_LIST_CACHE_TABLE,
+ MIGRATE_CONVERSATIONS_TABLE,
+} from './ConversationSchema';
+import {
+ CREATE_USERS_TABLE,
+ CREATE_CURRENT_USER_CACHE_TABLE,
+} from './UserSchema';
+import {
+ CREATE_GROUPS_TABLE,
+ CREATE_GROUP_MEMBERS_TABLE,
+ CREATE_GROUP_INDEXES,
+} from './GroupSchema';
+import {
+ CREATE_POSTS_CACHE_TABLE,
+} from './PostSchema';
+
+export async function runMigrations(db: SQLiteDatabase): Promise {
+ if (typeof window !== 'undefined') {
+ try {
+ await db.execAsync(`PRAGMA locking_mode = EXCLUSIVE;`);
+ await db.execAsync(`PRAGMA journal_mode = MEMORY;`);
+ await db.execAsync(`PRAGMA temp_store = MEMORY;`);
+ console.log('[MigrationManager] Web: 已设置 EXCLUSIVE locking + MEMORY journal/temp 模式');
+ } catch (e) {
+ console.warn('[MigrationManager] 设置 PRAGMA 失败:', e);
+ }
+ }
+
+ await db.execAsync(CREATE_MESSAGES_TABLE);
+ await db.execAsync(CREATE_CONVERSATIONS_TABLE);
+ await db.execAsync(CREATE_CONVERSATION_CACHE_TABLE);
+ await db.execAsync(CREATE_CONVERSATION_LIST_CACHE_TABLE);
+ await db.execAsync(CREATE_USERS_TABLE);
+ await db.execAsync(CREATE_CURRENT_USER_CACHE_TABLE);
+ await db.execAsync(CREATE_GROUPS_TABLE);
+ await db.execAsync(CREATE_GROUP_MEMBERS_TABLE);
+ await db.execAsync(CREATE_POSTS_CACHE_TABLE);
+
+ for (const indexSql of CREATE_MESSAGES_INDEXES) {
+ await db.execAsync(indexSql);
+ }
+ for (const indexSql of CREATE_GROUP_INDEXES) {
+ await db.execAsync(indexSql);
+ }
+
+ await MIGRATE_MESSAGES_TABLE(db);
+ await MIGRATE_CONVERSATIONS_TABLE(db);
+}
\ No newline at end of file
diff --git a/src/database/schema/PostSchema.ts b/src/database/schema/PostSchema.ts
new file mode 100644
index 0000000..809980a
--- /dev/null
+++ b/src/database/schema/PostSchema.ts
@@ -0,0 +1,9 @@
+export const POSTS_CACHE_TABLE = 'posts_cache';
+
+export const CREATE_POSTS_CACHE_TABLE = `CREATE TABLE IF NOT EXISTS posts_cache (
+ id TEXT PRIMARY KEY NOT NULL,
+ data TEXT NOT NULL,
+ updatedAt TEXT NOT NULL
+)`;
+
+export const CREATE_POSTS_INDEXES: string[] = [];
\ No newline at end of file
diff --git a/src/database/schema/UserSchema.ts b/src/database/schema/UserSchema.ts
new file mode 100644
index 0000000..3b861f4
--- /dev/null
+++ b/src/database/schema/UserSchema.ts
@@ -0,0 +1,16 @@
+export const USERS_TABLE = 'users';
+export const CURRENT_USER_CACHE_TABLE = 'current_user_cache';
+
+export const CREATE_USERS_TABLE = `CREATE TABLE IF NOT EXISTS users (
+ id TEXT PRIMARY KEY NOT NULL,
+ data TEXT NOT NULL,
+ updatedAt TEXT NOT NULL
+)`;
+
+export const CREATE_CURRENT_USER_CACHE_TABLE = `CREATE TABLE IF NOT EXISTS current_user_cache (
+ id TEXT PRIMARY KEY NOT NULL,
+ data TEXT NOT NULL,
+ updatedAt TEXT NOT NULL
+)`;
+
+export const CREATE_USER_INDEXES: string[] = [];
\ No newline at end of file
diff --git a/src/database/schema/index.ts b/src/database/schema/index.ts
new file mode 100644
index 0000000..5d19fb2
--- /dev/null
+++ b/src/database/schema/index.ts
@@ -0,0 +1,6 @@
+export * from './MessageSchema';
+export * from './ConversationSchema';
+export * from './UserSchema';
+export * from './GroupSchema';
+export * from './PostSchema';
+export { runMigrations } from './MigrationManager';
\ No newline at end of file
diff --git a/src/database/types/CachedMessage.ts b/src/database/types/CachedMessage.ts
new file mode 100644
index 0000000..ea18b40
--- /dev/null
+++ b/src/database/types/CachedMessage.ts
@@ -0,0 +1,14 @@
+export interface CachedMessage {
+ id: string;
+ conversationId: string;
+ senderId: string;
+ content?: string;
+ type?: string;
+ isRead: boolean;
+ createdAt: string;
+ seq: number;
+ status: string;
+ segments?: any;
+}
+
+export { CachedMessage as default };
\ No newline at end of file
diff --git a/src/database/types/index.ts b/src/database/types/index.ts
new file mode 100644
index 0000000..d5fc297
--- /dev/null
+++ b/src/database/types/index.ts
@@ -0,0 +1 @@
+export { CachedMessage } from './CachedMessage';
\ No newline at end of file
diff --git a/src/navigation/hrefs.ts b/src/navigation/hrefs.ts
index c543dda..9a5e260 100644
--- a/src/navigation/hrefs.ts
+++ b/src/navigation/hrefs.ts
@@ -177,3 +177,17 @@ export function hrefMaterialSubject(subjectId: string): string {
export function hrefMaterialDetail(materialId: string): string {
return `/apps/materials/detail?materialId=${encodeURIComponent(materialId)}`;
}
+
+// ==================== Profile Settings (设置相关) ====================
+
+export function hrefProfileAbout(): string {
+ return '/profile/about';
+}
+
+export function hrefProfileTerms(): string {
+ return '/profile/terms';
+}
+
+export function hrefProfilePrivacy(): string {
+ return '/profile/privacy';
+}
diff --git a/src/screens/auth/LoginScreen.tsx b/src/screens/auth/LoginScreen.tsx
index b7c7a05..a8a1271 100644
--- a/src/screens/auth/LoginScreen.tsx
+++ b/src/screens/auth/LoginScreen.tsx
@@ -237,6 +237,16 @@ export const LoginScreen: React.FC = () => {
立即注册
+
+ {/* 协议提示 */}
+
+
+ 登录即表示您同意
+ router.push(hrefs.hrefProfileTerms())}>《用户协议》
+ 和
+ router.push(hrefs.hrefProfilePrivacy())}>《隐私政策》
+
+
@@ -376,6 +386,18 @@ function createLoginStyles(colors: AppColors) {
fontWeight: '600',
marginLeft: 6,
},
+ policyFooter: {
+ alignItems: 'center',
+ marginTop: 24,
+ },
+ policyText: {
+ fontSize: 13,
+ color: colors.text.hint,
+ },
+ policyLink: {
+ color: THEME_COLORS.primary,
+ fontWeight: '500',
+ },
});
}
diff --git a/src/screens/auth/RegisterStep3Profile.tsx b/src/screens/auth/RegisterStep3Profile.tsx
index 7df92fc..c1d6617 100644
--- a/src/screens/auth/RegisterStep3Profile.tsx
+++ b/src/screens/auth/RegisterStep3Profile.tsx
@@ -12,9 +12,11 @@ import {
TouchableOpacity,
ActivityIndicator,
} from 'react-native';
+import { useRouter } from 'expo-router';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { useAppColors, type AppColors } from '../../theme';
import { RegisterStepProps } from './types';
+import * as hrefs from '../../navigation/hrefs';
const THEME_COLORS = {
primary: '#FF6B35',
@@ -32,6 +34,7 @@ export const RegisterStep3Profile: React.FC = ({
}) => {
const colors = propColors || useAppColors();
const styles = createStyles(colors);
+ const router = useRouter();
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
@@ -185,28 +188,40 @@ export const RegisterStep3Profile: React.FC = ({
})}
{/* 服务条款 */}
- {
- setAgreedToTerms(!agreedToTerms);
- if (errors.terms) {
- setErrors((prev) => ({ ...prev, terms: '' }));
- }
- }}
- activeOpacity={0.8}
- >
-
+
+ {
+ setAgreedToTerms(!agreedToTerms);
+ if (errors.terms) {
+ setErrors((prev) => ({ ...prev, terms: '' }));
+ }
+ }}
+ activeOpacity={0.8}
+ style={styles.checkboxWrapper}
+ >
+
+
我已阅读并同意
- 《用户协议》
+ router.push(hrefs.hrefProfileTerms())}
+ >
+ 《用户协议》
+
和
- 《隐私政策》
+ router.push(hrefs.hrefProfilePrivacy())}
+ >
+ 《隐私政策》
+
-
+
{errors.terms ? (
{errors.terms}
@@ -295,10 +310,13 @@ function createStyles(colors: AppColors) {
},
termsContainer: {
flexDirection: 'row',
- alignItems: 'center',
+ alignItems: 'flex-start',
marginBottom: 20,
marginTop: 4,
},
+ checkboxWrapper: {
+ paddingTop: 2,
+ },
termsText: {
fontSize: 14,
color: colors.text?.secondary || '#666',
diff --git a/src/screens/home/HomeScreen.tsx b/src/screens/home/HomeScreen.tsx
index 7d8a973..df1a3ef 100644
--- a/src/screens/home/HomeScreen.tsx
+++ b/src/screens/home/HomeScreen.tsx
@@ -203,6 +203,13 @@ export const HomeScreen: React.FC = () => {
// 发帖弹窗状态
const [showCreatePost, setShowCreatePost] = useState(false);
+
+ useEffect(() => {
+ if (showCreatePost && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [showCreatePost]);
// 用于跟踪当前页面显示的帖子 ID,以便从 store 同步状态
const postIdsRef = useRef>(new Set());
@@ -599,7 +606,7 @@ export const HomeScreen: React.FC = () => {
} catch (shareError) {
console.error('上报分享次数失败:', shareError);
}
- const postUrl = `https://browser.littlelan.cn/posts/${encodeURIComponent(post.id)}`;
+ const postUrl = `https://browser.littlelan.cn/post/${encodeURIComponent(post.id)}`;
Clipboard.setString(postUrl);
Alert.alert('已复制', '帖子链接已复制到剪贴板');
};
diff --git a/src/screens/home/PostDetailScreen.tsx b/src/screens/home/PostDetailScreen.tsx
index eda7c53..0998030 100644
--- a/src/screens/home/PostDetailScreen.tsx
+++ b/src/screens/home/PostDetailScreen.tsx
@@ -536,7 +536,7 @@ export const PostDetailScreen: React.FC = () => {
} catch (error) {
console.error('上报分享次数失败:', error);
}
- const postUrl = `https://browser.littlelan.cn/posts/${encodeURIComponent(post.id)}`;
+ const postUrl = `https://browser.littlelan.cn/post/${encodeURIComponent(post.id)}`;
Clipboard.setString(postUrl);
Alert.alert('已复制', '帖子链接已复制到剪贴板');
}, [post?.id]);
diff --git a/src/screens/message/CreateGroupScreen.tsx b/src/screens/message/CreateGroupScreen.tsx
index 1e764c9..248e158 100644
--- a/src/screens/message/CreateGroupScreen.tsx
+++ b/src/screens/message/CreateGroupScreen.tsx
@@ -4,7 +4,7 @@
* 支持响应式布局
*/
-import React, { useState, useMemo } from 'react';
+import React, { useState, useMemo, useEffect } from 'react';
import {
View,
StyleSheet,
@@ -14,6 +14,7 @@ import {
TextInput,
FlatList,
Image,
+ Platform,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useRouter } from 'expo-router';
@@ -45,6 +46,13 @@ const CreateGroupScreen: React.FC = () => {
// 邀请成员模态框状态
const [inviteModalVisible, setInviteModalVisible] = useState(false);
+ useEffect(() => {
+ if (inviteModalVisible && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [inviteModalVisible]);
+
// 移除已选成员
const removeSelectedMember = (userId: string) => {
const newSelectedIds = new Set(selectedMemberIds);
diff --git a/src/screens/message/GroupInfoScreen.tsx b/src/screens/message/GroupInfoScreen.tsx
index c97ab65..a9c94f9 100644
--- a/src/screens/message/GroupInfoScreen.tsx
+++ b/src/screens/message/GroupInfoScreen.tsx
@@ -118,6 +118,13 @@ const GroupInfoScreen: React.FC = () => {
const [inviteModalVisible, setInviteModalVisible] = useState(false);
const [inviting, setInviting] = useState(false);
+ useEffect(() => {
+ if ((editModalVisible || announcementModalVisible || transferModalVisible || joinTypeModalVisible || inviteModalVisible) && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [editModalVisible, announcementModalVisible, transferModalVisible, joinTypeModalVisible, inviteModalVisible]);
+
// 计算当前用户的角色
const isOwner = currentMember?.role === 'owner';
const isAdmin = currentMember?.role === 'admin' || isOwner;
diff --git a/src/screens/message/GroupMembersScreen.tsx b/src/screens/message/GroupMembersScreen.tsx
index 2359288..b9d4918 100644
--- a/src/screens/message/GroupMembersScreen.tsx
+++ b/src/screens/message/GroupMembersScreen.tsx
@@ -17,6 +17,7 @@ import {
Modal,
TextInput,
Dimensions,
+ Platform,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useLocalSearchParams, useRouter } from 'expo-router';
@@ -131,6 +132,13 @@ const GroupMembersScreen: React.FC = () => {
const [nicknameModalVisible, setNicknameModalVisible] = useState(false);
const [newNickname, setNewNickname] = useState('');
+ useEffect(() => {
+ if ((actionModalVisible || nicknameModalVisible) && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [actionModalVisible, nicknameModalVisible]);
+
// 计算当前用户的角色
const isOwner = currentMember?.role === 'owner';
const isAdmin = currentMember?.role === 'admin' || isOwner;
diff --git a/src/screens/message/MessageListScreen.tsx b/src/screens/message/MessageListScreen.tsx
index 958942f..2faca66 100644
--- a/src/screens/message/MessageListScreen.tsx
+++ b/src/screens/message/MessageListScreen.tsx
@@ -136,6 +136,13 @@ export const MessageListScreen: React.FC = () => {
const [isSearching, setIsSearching] = useState(false);
const [activeSearchTab, setActiveSearchTab] = useState<'chat' | 'user'>('chat');
const [actionMenuVisible, setActionMenuVisible] = useState(false);
+
+ useEffect(() => {
+ if (actionMenuVisible && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [actionMenuVisible]);
const [scannerVisible, setScannerVisible] = useState(false);
// 系统通知显示状态 - 用于在移动端显示通知页面
diff --git a/src/screens/message/NotificationsScreen.tsx b/src/screens/message/NotificationsScreen.tsx
index 5c69eed..97a50f0 100644
--- a/src/screens/message/NotificationsScreen.tsx
+++ b/src/screens/message/NotificationsScreen.tsx
@@ -1,8 +1,14 @@
/**
- * 通知页 NotificationsScreen
+ * 通知页 NotificationsScreen(扁平化风格)
* 胡萝卜BBS - 系统消息列表
* 【游标分页】使用 messageService.getSystemMessagesCursor
* 支持响应式布局
+ *
+ * 设计风格:
+ * - 纯白背景,扁平化设计
+ * - 简洁的标题区域
+ * - 分段式筛选标签
+ * - 与登录、注册、设置页面风格一致
*/
import React, { useState, useCallback, useEffect, useMemo, useRef } from 'react';
@@ -15,17 +21,19 @@ import {
ActivityIndicator,
Dimensions,
BackHandler,
+ Animated,
+ StatusBar,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useIsFocused } from '@react-navigation/native';
import { useRouter } from 'expo-router';
import { MaterialCommunityIcons } from '@expo/vector-icons';
-import { spacing, borderRadius, shadows, useAppColors, type AppColors } from '../../theme';
+import { spacing, borderRadius, useAppColors, type AppColors } from '../../theme';
import { SystemMessageResponse } from '../../types/dto';
import { messageService } from '../../services/messageService';
import { commentService } from '../../services/commentService';
import { SystemMessageItem } from '../../components/business';
-import { Text, EmptyState, ResponsiveContainer, AppBackButton } from '../../components/common';
+import { Text, ResponsiveContainer } from '../../components/common';
import { useCursorPagination } from '../../hooks/useCursorPagination';
import * as hrefs from '../../navigation/hrefs';
import { useMessageManagerSystemUnreadCount } from '../../stores';
@@ -41,6 +49,13 @@ const MESSAGE_TYPES = [
{ key: 'announcement', title: '公告' },
];
+// 胡萝卜橙主题色
+const THEME_COLORS = {
+ primary: '#FF6B35',
+ primaryLight: '#FF8C5A',
+ primaryDark: '#E55A2B',
+};
+
const LIKE_SYSTEM_TYPES = new Set(['like_post', 'like_comment', 'like_reply', 'favorite_post']);
const GROUP_SYSTEM_TYPES = new Set(['group_invite', 'group_join_apply', 'group_join_approved', 'group_join_rejected']);
@@ -51,6 +66,10 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
const { setSystemUnreadCount, decrementSystemUnreadCount } = useMessageManagerSystemUnreadCount();
const router = useRouter();
+ // 动画值
+ const fadeAnim = useRef(new Animated.Value(0)).current;
+ const slideAnim = useRef(new Animated.Value(20)).current;
+
// 修复竞态条件:使用本地状态管理窗口尺寸,避免 hydration 问题
const [windowSize, setWindowSize] = useState({ width: 0, height: 0 });
const [isHydrated, setIsHydrated] = useState(false);
@@ -177,6 +196,22 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
}
}, [isFocused]); // 只依赖 isFocused,函数使用 ref 稳定引用
+ // 启动入场动画
+ useEffect(() => {
+ Animated.parallel([
+ Animated.timing(fadeAnim, {
+ toValue: 1,
+ duration: 400,
+ useNativeDriver: true,
+ }),
+ Animated.timing(slideAnim, {
+ toValue: 0,
+ duration: 400,
+ useNativeDriver: true,
+ }),
+ ]).start();
+ }, []);
+
// 屏幕失去焦点时,如果有 onBack 回调则调用它(用于内嵌模式)
useEffect(() => {
if (!isFocused && onBack) {
@@ -343,7 +378,7 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
);
};
- // 渲染头部(包含返回按钮)
+ // 渲染头部(包含返回按钮)- 扁平化风格
const renderHeader = () => {
// 大屏模式(>= lg 断点)隐藏返回按钮
const shouldShowBackButton = onBack && !isWideScreen;
@@ -351,7 +386,11 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
return (
{shouldShowBackButton ? (
-
+
+
+
+
+
) : (
)}
@@ -370,14 +409,14 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
);
};
- // 渲染空状态
+ // 渲染空状态 - 扁平化风格
const renderEmpty = () => (
-
+
+
+
+ 暂无通知
+ 关注一些用户来获取通知吧
);
@@ -385,8 +424,9 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
if (!isHydrated) {
return (
+
-
+
);
@@ -394,164 +434,175 @@ export const NotificationsScreen: React.FC<{ onBack?: () => void }> = ({ onBack
return (
- {isWideScreen ? (
-
- {/* 头部 - 大屏模式下隐藏返回按钮 */}
- {renderHeader()}
- {/* 分类筛选 */}
-
- {MESSAGE_TYPES.map(type => {
- const count = type.key === 'all'
- ? displayMessages.length
- : displayMessages.filter((m) => {
- if (type.key === 'like_post') {
- return LIKE_SYSTEM_TYPES.has(m.system_type);
- }
- if (type.key === 'group') {
- return GROUP_SYSTEM_TYPES.has(m.system_type);
- }
- return m.system_type === type.key;
- }).length;
- const isActive = activeType === type.key;
- return (
- setActiveType(type.key)}
- activeOpacity={0.8}
- >
-
+
+ {isWideScreen ? (
+
+ {/* 头部 - 大屏模式下隐藏返回按钮 */}
+ {renderHeader()}
+ {/* 分类筛选 */}
+
+ {MESSAGE_TYPES.map(type => {
+ const count = type.key === 'all'
+ ? displayMessages.length
+ : displayMessages.filter((m) => {
+ if (type.key === 'like_post') {
+ return LIKE_SYSTEM_TYPES.has(m.system_type);
+ }
+ if (type.key === 'group') {
+ return GROUP_SYSTEM_TYPES.has(m.system_type);
+ }
+ return m.system_type === type.key;
+ }).length;
+ const isActive = activeType === type.key;
+ return (
+ setActiveType(type.key)}
+ activeOpacity={0.8}
>
- {type.title}
-
- {count > 0 && (
-
-
- {count}
-
-
- )}
-
- );
- })}
-
-
- {/* 消息列表 */}
- {isLoading && messages.length === 0 ? (
-
-
+
+ {type.title}
+
+ {count > 0 && (
+
+
+ {count}
+
+
+ )}
+
+ );
+ })}
- ) : (
- item.id.toString()}
- contentContainerStyle={[styles.listContent, isWideScreen && styles.listContentWide]}
- showsVerticalScrollIndicator={false}
- ListEmptyComponent={renderEmpty}
- ListFooterComponent={renderFooter}
- onEndReached={onEndReached}
- onEndReachedThreshold={0.3}
- refreshControl={
-
- }
- />
- )}
-
- ) : (
- <>
- {/* 头部 - 小屏模式下显示返回按钮 */}
- {renderHeader()}
- {/* 分类筛选 */}
-
- {MESSAGE_TYPES.map(type => {
- const count = type.key === 'all'
- ? displayMessages.length
- : displayMessages.filter((m) => {
- if (type.key === 'like_post') {
- return LIKE_SYSTEM_TYPES.has(m.system_type);
- }
- if (type.key === 'group') {
- return GROUP_SYSTEM_TYPES.has(m.system_type);
- }
- return m.system_type === type.key;
- }).length;
- const isActive = activeType === type.key;
- return (
- setActiveType(type.key)}
- activeOpacity={0.8}
- >
-
+
+
+ ) : (
+ item.id.toString()}
+ contentContainerStyle={[styles.listContent, isWideScreen && styles.listContentWide]}
+ showsVerticalScrollIndicator={false}
+ ListEmptyComponent={renderEmpty}
+ ListFooterComponent={renderFooter}
+ onEndReached={onEndReached}
+ onEndReachedThreshold={0.3}
+ refreshControl={
+
+ }
+ />
+ )}
+
+ ) : (
+ <>
+ {/* 头部 - 小屏模式下显示返回按钮 */}
+ {renderHeader()}
+ {/* 分类筛选 */}
+
+ {MESSAGE_TYPES.map(type => {
+ const count = type.key === 'all'
+ ? displayMessages.length
+ : displayMessages.filter((m) => {
+ if (type.key === 'like_post') {
+ return LIKE_SYSTEM_TYPES.has(m.system_type);
+ }
+ if (type.key === 'group') {
+ return GROUP_SYSTEM_TYPES.has(m.system_type);
+ }
+ return m.system_type === type.key;
+ }).length;
+ const isActive = activeType === type.key;
+ return (
+ setActiveType(type.key)}
+ activeOpacity={0.8}
>
- {type.title}
-
- {count > 0 && (
-
-
- {count}
-
-
- )}
-
- );
- })}
-
-
- {/* 消息列表 */}
- {isLoading && messages.length === 0 ? (
-
-
+
+ {type.title}
+
+ {count > 0 && (
+
+
+ {count}
+
+
+ )}
+
+ );
+ })}
- ) : (
- item.id.toString()}
- contentContainerStyle={styles.listContent}
- showsVerticalScrollIndicator={false}
- ListEmptyComponent={renderEmpty}
- ListFooterComponent={renderFooter}
- onEndReached={onEndReached}
- onEndReachedThreshold={0.3}
- refreshControl={
-
- }
- />
- )}
- >
- )}
+
+ {/* 消息列表 */}
+ {isLoading && messages.length === 0 ? (
+
+
+
+ ) : (
+ item.id.toString()}
+ contentContainerStyle={styles.listContent}
+ showsVerticalScrollIndicator={false}
+ ListEmptyComponent={renderEmpty}
+ ListFooterComponent={renderFooter}
+ onEndReached={onEndReached}
+ onEndReachedThreshold={0.3}
+ refreshControl={
+
+ }
+ />
+ )}
+ >
+ )}
+
);
};
@@ -560,21 +611,23 @@ function createNotificationsStyles(colors: AppColors) {
return StyleSheet.create({
container: {
flex: 1,
- backgroundColor: colors.background.default,
+ backgroundColor: '#fff',
},
- // Header 样式 - 美化版
+ content: {
+ flex: 1,
+ },
+ // Header 样式 - 扁平化风格
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
- paddingHorizontal: spacing.lg,
- paddingVertical: spacing.md,
- backgroundColor: colors.background.paper,
- ...shadows.sm,
+ paddingHorizontal: 20,
+ paddingVertical: 16,
+ backgroundColor: '#fff',
},
headerWide: {
- paddingHorizontal: spacing.xl,
- paddingVertical: spacing.lg,
+ paddingHorizontal: 32,
+ paddingVertical: 20,
},
headerTitleContainer: {
flexDirection: 'row',
@@ -591,7 +644,7 @@ function createNotificationsStyles(colors: AppColors) {
fontSize: 20,
},
unreadBadge: {
- backgroundColor: colors.primary.main,
+ backgroundColor: THEME_COLORS.primary,
borderRadius: 10,
paddingHorizontal: 6,
paddingVertical: 2,
@@ -601,7 +654,7 @@ function createNotificationsStyles(colors: AppColors) {
justifyContent: 'center',
},
unreadBadgeText: {
- color: colors.text.inverse,
+ color: '#fff',
fontSize: 11,
fontWeight: '600',
},
@@ -609,50 +662,59 @@ function createNotificationsStyles(colors: AppColors) {
width: 40,
height: 40,
justifyContent: 'center',
- alignItems: 'flex-start',
+ alignItems: 'center',
+ },
+ backIconButton: {
+ width: 40,
+ height: 40,
+ borderRadius: 20,
+ backgroundColor: '#F5F5F7',
+ justifyContent: 'center',
+ alignItems: 'center',
},
backButtonPlaceholder: {
width: 40,
},
- // 分类筛选 - 美化版(使用分段式设计)
+ // 分类筛选 - 扁平化风格(分段式设计)
filterContainer: {
flexDirection: 'row',
- paddingHorizontal: spacing.md,
- paddingVertical: spacing.sm,
- backgroundColor: colors.background.paper,
+ paddingHorizontal: 16,
+ paddingVertical: 12,
+ backgroundColor: '#fff',
+ borderBottomWidth: 1,
+ borderBottomColor: colors.divider || '#E5E5EA',
},
filterContainerWideWeb: {
- paddingHorizontal: spacing.xl,
- paddingVertical: spacing.md,
+ paddingHorizontal: 32,
+ paddingVertical: 16,
justifyContent: 'center',
- backgroundColor: colors.background.paper,
},
filterTag: {
flexDirection: 'row',
alignItems: 'center',
- paddingHorizontal: spacing.md,
- paddingVertical: spacing.sm,
- marginRight: spacing.xs,
- borderRadius: borderRadius.lg,
- backgroundColor: colors.background.default,
+ paddingHorizontal: 14,
+ paddingVertical: 8,
+ marginRight: 8,
+ borderRadius: 14,
+ backgroundColor: '#F5F5F7',
borderWidth: 1,
borderColor: 'transparent',
},
filterTagWide: {
- paddingHorizontal: spacing.lg,
- paddingVertical: spacing.md,
- marginRight: spacing.sm,
+ paddingHorizontal: 18,
+ paddingVertical: 10,
+ marginRight: 10,
},
filterTagActive: {
- backgroundColor: colors.primary.main,
- borderColor: colors.primary.main,
+ backgroundColor: THEME_COLORS.primary,
+ borderColor: THEME_COLORS.primary,
},
filterTagTextActive: {
fontWeight: '600',
},
filterCount: {
- marginLeft: spacing.xs,
- backgroundColor: colors.primary.light + '40',
+ marginLeft: 6,
+ backgroundColor: THEME_COLORS.primaryLight + '40',
paddingHorizontal: 6,
paddingVertical: 1,
borderRadius: 8,
@@ -664,37 +726,59 @@ function createNotificationsStyles(colors: AppColors) {
},
listContent: {
flexGrow: 1,
- paddingTop: spacing.md,
- paddingBottom: spacing.lg,
+ paddingTop: 8,
+ paddingBottom: 24,
},
listContentWide: {
- paddingHorizontal: spacing.xl,
+ paddingHorizontal: 32,
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
- paddingVertical: spacing.xl * 2,
+ paddingVertical: 80,
},
loadingMore: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
- paddingVertical: spacing.lg,
+ paddingVertical: 20,
},
loadingMoreText: {
- marginLeft: spacing.sm,
+ marginLeft: 8,
},
+ // 空状态 - 扁平化风格
emptyContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
- paddingTop: spacing['3xl'],
+ paddingTop: 80,
+ paddingHorizontal: 40,
},
emptyContainerWeb: {
minHeight: 500,
justifyContent: 'center',
paddingTop: 100,
},
+ emptyIconContainer: {
+ width: 120,
+ height: 120,
+ borderRadius: 60,
+ backgroundColor: '#F5F5F7',
+ justifyContent: 'center',
+ alignItems: 'center',
+ marginBottom: 24,
+ },
+ emptyTitle: {
+ fontSize: 20,
+ fontWeight: '600',
+ color: colors.text.primary,
+ marginBottom: 8,
+ },
+ emptyDescription: {
+ fontSize: 15,
+ color: colors.text.secondary,
+ textAlign: 'center',
+ },
});
}
diff --git a/src/screens/message/PrivateChatInfoScreen.tsx b/src/screens/message/PrivateChatInfoScreen.tsx
index 2b8ace7..18c3d96 100644
--- a/src/screens/message/PrivateChatInfoScreen.tsx
+++ b/src/screens/message/PrivateChatInfoScreen.tsx
@@ -21,10 +21,7 @@ import { useAuthStore } from '../../stores';
import { authService } from '../../services/authService';
import { messageService } from '../../services/messageService';
import { ApiError } from '../../services/api';
-import {
- clearConversationMessages,
- getConversationCache,
-} from '../../services/database';
+import { messageRepository, conversationRepository } from '@/database';
import { messageManager } from '../../stores/messageManager';
import { userManager } from '../../stores/userManager';
import { Avatar, Text, Button, Loading, Divider } from '../../components/common';
@@ -81,7 +78,7 @@ const PrivateChatInfoScreen: React.FC = () => {
return;
}
- const cached = await getConversationCache(conversationId);
+ const cached = await conversationRepository.getCache(conversationId);
if (cached) {
setIsPinned(Boolean((cached as any).is_pinned));
return;
@@ -137,7 +134,7 @@ const PrivateChatInfoScreen: React.FC = () => {
style: 'destructive',
onPress: async () => {
try {
- await clearConversationMessages(conversationId);
+ await messageRepository.clearConversation(conversationId);
Alert.alert('成功', '聊天记录已清空');
} catch (error) {
Alert.alert('错误', '清空聊天记录失败');
diff --git a/src/screens/message/components/ChatScreen/EmojiPanel.tsx b/src/screens/message/components/ChatScreen/EmojiPanel.tsx
index d206fe0..1d3f757 100644
--- a/src/screens/message/components/ChatScreen/EmojiPanel.tsx
+++ b/src/screens/message/components/ChatScreen/EmojiPanel.tsx
@@ -5,7 +5,7 @@
*/
import React, { useState, useEffect, useCallback, useMemo } from 'react';
-import { View, TouchableOpacity, ActivityIndicator, Modal, Alert, Dimensions, StyleSheet, FlatList, ListRenderItem, ScrollView } from 'react-native';
+import { View, TouchableOpacity, ActivityIndicator, Modal, Alert, Dimensions, StyleSheet, FlatList, ListRenderItem, ScrollView, Platform } from 'react-native';
import { Image as ExpoImage } from 'expo-image';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import * as ImagePicker from 'expo-image-picker';
@@ -18,6 +18,12 @@ import { useAppColors, spacing } from '../../../../theme';
const { height: SCREEN_HEIGHT } = Dimensions.get('window');
+const blurActiveElementOnWeb = () => {
+ if (Platform.OS !== 'web') return;
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+};
+
// 表情尺寸配置 - 固定宽度 40px,使用 flex 布局
const EMOJI_SIZES = {
mobile: { size: 24, itemWidth: 40, itemHeight: 40 },
@@ -176,6 +182,7 @@ export const EmojiPanel: React.FC = ({
// 打开管理界面
const handleOpenManage = () => {
+ blurActiveElementOnWeb();
setShowManageModal(true);
setManageMode(false);
setSelectedStickers(new Set());
diff --git a/src/screens/message/components/ChatScreen/SegmentRenderer.tsx b/src/screens/message/components/ChatScreen/SegmentRenderer.tsx
index 6fc85a1..5b4bac6 100644
--- a/src/screens/message/components/ChatScreen/SegmentRenderer.tsx
+++ b/src/screens/message/components/ChatScreen/SegmentRenderer.tsx
@@ -33,6 +33,7 @@ import {
} from '../../../../types/dto';
import { spacing, useAppColors, type AppColors } from '../../../../theme';
import { SenderInfo } from './types';
+import { useChatSettingsStore } from '../../../../stores/chatSettingsStore';
const IMAGE_MAX_WIDTH = 200;
const IMAGE_MAX_HEIGHT = 260;
@@ -617,7 +618,8 @@ export const ReplyPreviewSegment: React.FC = ({
getSenderInfo,
}) => {
const themeColors = useAppColors();
- const styles = useMemo(() => createSegmentStyles(themeColors), [themeColors]);
+ const fontSize = useChatSettingsStore((s) => s.fontSize);
+ const styles = useMemo(() => createSegmentStyles(themeColors, fontSize), [themeColors, fontSize]);
if (!replyMessage) {
// 如果没有引用消息详情,只显示引用ID
@@ -729,7 +731,8 @@ export const MessageSegmentsRenderer: React.FC<{
getSenderInfo,
}) => {
const themeColors = useAppColors();
- const segStyles = useMemo(() => createSegmentStyles(themeColors), [themeColors]);
+ const fontSize = useChatSettingsStore((s) => s.fontSize);
+ const segStyles = useMemo(() => createSegmentStyles(themeColors, fontSize), [themeColors, fontSize]);
const replySegment = segments.find(s => s.type === 'reply');
const otherSegments = segments.filter(s => s.type !== 'reply');
@@ -815,7 +818,7 @@ const formatDuration = (seconds: number): string => {
return mins > 0 ? `${mins}:${secs.toString().padStart(2, '0')}` : `0:${secs.toString().padStart(2, '0')}`;
};
-function createSegmentStyles(colors: AppColors) {
+function createSegmentStyles(colors: AppColors, fontSize: number = 16) {
return StyleSheet.create({
// 容器
segmentsContainer: {
@@ -847,10 +850,10 @@ function createSegmentStyles(colors: AppColors) {
marginTop: 2,
},
- // 文本 - 适配暗色模式
+ // 文本 - 适配暗色模式,支持动态字号
textContent: {
- fontSize: 17.5,
- lineHeight: 25,
+ fontSize,
+ lineHeight: Math.round(fontSize * 1.43),
letterSpacing: 0.1,
fontWeight: '400',
},
@@ -861,10 +864,10 @@ function createSegmentStyles(colors: AppColors) {
color: colors.chat.textPrimary,
},
- // @提及 - 微信/QQ 风格:更精致的高亮
+ // @提及 - 微信/QQ 风格:更精致的高亮,支持动态字号
atText: {
- fontSize: 16,
- lineHeight: 22,
+ fontSize,
+ lineHeight: Math.round(fontSize * 1.4),
fontWeight: '500',
},
atTextMe: {
@@ -904,7 +907,7 @@ function createSegmentStyles(colors: AppColors) {
height: 28,
},
faceText: {
- fontSize: 16,
+ fontSize,
color: '#666',
},
@@ -930,7 +933,7 @@ function createSegmentStyles(colors: AppColors) {
},
voiceDuration: {
marginLeft: spacing.sm,
- fontSize: 15,
+ fontSize: fontSize - 1,
fontWeight: '500',
},
@@ -1113,14 +1116,14 @@ function createSegmentStyles(colors: AppColors) {
minWidth: 0,
},
replySender: {
- fontSize: 13,
+ fontSize: fontSize - 3,
fontWeight: '600',
color: colors.chat.link,
marginRight: 6,
flexShrink: 0,
},
replyText: {
- fontSize: 13,
+ fontSize: fontSize - 3,
color: colors.chat.textTertiary,
flex: 1,
flexShrink: 1,
diff --git a/src/screens/message/components/ChatScreen/useChatScreen.ts b/src/screens/message/components/ChatScreen/useChatScreen.ts
index 58e0deb..a179f80 100644
--- a/src/screens/message/components/ChatScreen/useChatScreen.ts
+++ b/src/screens/message/components/ChatScreen/useChatScreen.ts
@@ -42,10 +42,7 @@ import {
PendingChatAttachment,
} from './types';
import { TIME_SEPARATOR_INTERVAL, MEMBERS_PAGE_SIZE, MAX_PENDING_CHAT_IMAGES } from './constants';
-import {
- deleteMessage as deleteMessageFromDb,
- clearConversationMessages,
-} from '../../../../services/database';
+import { messageRepository } from '@/database';
export const useChatScreen = () => {
const getSendErrorMessage = useCallback((error: unknown, fallback: string) => {
@@ -1209,7 +1206,7 @@ export const useChatScreen = () => {
const updatedMessages = messages.filter(m => m.id !== messageId);
// 注意:这里我们依赖 MessageManager 的数据,所以需要通过其他方式刷新
// 暂时只删除本地数据库
- await deleteMessageFromDb(messageId);
+ await messageRepository.delete(messageId);
} catch (error) {
console.error('删除消息失败:', error);
Alert.alert('删除失败', '无法删除消息');
@@ -1224,7 +1221,7 @@ export const useChatScreen = () => {
setLastSeq(0);
setFirstSeq(0);
setHasMoreHistory(true);
- await clearConversationMessages(conversationId);
+ await messageRepository.clearConversation(conversationId);
// 刷新消息列表
await refreshMessages();
} catch (error) {
diff --git a/src/screens/message/components/ConversationListRow.tsx b/src/screens/message/components/ConversationListRow.tsx
index 70a593b..7e111b1 100644
--- a/src/screens/message/components/ConversationListRow.tsx
+++ b/src/screens/message/components/ConversationListRow.tsx
@@ -14,7 +14,7 @@ import {
MessageSegment,
} from '../../../types/dto';
import { authService } from '../../../services';
-import { getUserCache } from '../../../services/database';
+import { userCacheRepository } from '@/database';
import { Avatar, Text } from '../../../components/common';
const AnimatedTouchable = Animated.createAnimatedComponent(TouchableOpacity);
@@ -62,7 +62,7 @@ const AsyncMessagePreview: React.FC<{
try {
const asyncText = await extractTextFromSegmentsAsync(
segments,
- getUserCache,
+ userCacheRepository.get.bind(userCacheRepository),
authService.getUserById.bind(authService)
);
if (isMountedRef.current && asyncText !== initialText) {
diff --git a/src/screens/message/components/MutualFollowSelectorModal.tsx b/src/screens/message/components/MutualFollowSelectorModal.tsx
index 2b04d75..450dd75 100644
--- a/src/screens/message/components/MutualFollowSelectorModal.tsx
+++ b/src/screens/message/components/MutualFollowSelectorModal.tsx
@@ -57,6 +57,10 @@ const MutualFollowSelectorModal: React.FC = ({
useEffect(() => {
if (!visible) return;
+ if (Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
if (!currentUserId) return;
const nextSelectedIds = new Set(stableInitialSelectedIds);
diff --git a/src/screens/profile/AboutScreen.tsx b/src/screens/profile/AboutScreen.tsx
new file mode 100644
index 0000000..c1f617f
--- /dev/null
+++ b/src/screens/profile/AboutScreen.tsx
@@ -0,0 +1,583 @@
+/**
+ * 关于我们页面 AboutScreen
+ * 胡萝卜BBS - 应用信息、版本检查与更新
+ * 扁平化设计风格,参考登录、注册、设置页面
+ */
+
+import React, { useMemo, useState, useCallback } from 'react';
+import {
+ View,
+ StyleSheet,
+ TouchableOpacity,
+ ScrollView,
+ Alert,
+ ActivityIndicator,
+ Linking,
+ Platform,
+} from 'react-native';
+import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context';
+import { MaterialCommunityIcons } from '@expo/vector-icons';
+import Constants from 'expo-constants';
+import { useRouter } from 'expo-router';
+import {
+ useAppColors,
+ spacing,
+ fontSizes,
+ borderRadius,
+ type AppColors,
+} from '../../theme';
+import { Text } from '../../components/common';
+import { useResponsive, useResponsiveSpacing } from '../../hooks';
+import { checkForAPKUpdate, type VersionCheckResult } from '../../services/apkUpdateService';
+import * as hrefs from '../../navigation/hrefs';
+
+const CONTENT_MAX_WIDTH = 720;
+
+const THEME_COLORS = {
+ primary: '#FF6B35',
+ primaryLight: '#FF8C5A',
+ primaryDark: '#E55A2B',
+};
+
+const APP_NAME = '萝卜社区';
+const APP_SLOGAN = '连接校园,分享生活';
+const APP_VERSION = Constants.expoConfig?.version || '1.0.0';
+const BUILD_NUMBER = Constants.expoConfig?.android?.versionCode || Constants.expoConfig?.ios?.buildNumber || '1';
+
+function createAboutStyles(colors: AppColors) {
+ return StyleSheet.create({
+ container: {
+ flex: 1,
+ backgroundColor: colors.background.paper,
+ },
+ scrollContent: {
+ paddingVertical: spacing.lg,
+ },
+ content: {
+ maxWidth: CONTENT_MAX_WIDTH,
+ alignSelf: 'center',
+ width: '100%',
+ },
+
+ header: {
+ alignItems: 'center',
+ marginBottom: spacing['3xl'],
+ paddingTop: spacing.xl,
+ },
+ logoContainer: {
+ width: 80,
+ height: 80,
+ borderRadius: 20,
+ backgroundColor: THEME_COLORS.primary,
+ justifyContent: 'center',
+ alignItems: 'center',
+ marginBottom: spacing.lg,
+ },
+ logoText: {
+ fontSize: 32,
+ fontWeight: '700',
+ color: '#fff',
+ },
+ appName: {
+ fontSize: 24,
+ fontWeight: '700',
+ color: colors.text.primary,
+ marginBottom: spacing.xs,
+ },
+ appSlogan: {
+ fontSize: 14,
+ color: colors.text.secondary,
+ marginBottom: spacing.sm,
+ },
+ versionText: {
+ fontSize: 13,
+ color: colors.text.hint,
+ },
+
+ groupContainer: {
+ marginBottom: spacing['2xl'],
+ maxWidth: CONTENT_MAX_WIDTH,
+ alignSelf: 'center',
+ width: '100%',
+ },
+ groupHeader: {
+ paddingHorizontal: spacing['2xl'],
+ marginBottom: spacing.sm,
+ marginTop: spacing.sm,
+ },
+ groupTitle: {
+ fontWeight: '600',
+ fontSize: fontSizes.sm,
+ color: colors.text.secondary,
+ textTransform: 'uppercase',
+ letterSpacing: 0.5,
+ },
+
+ settingItem: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ justifyContent: 'space-between',
+ paddingVertical: 16,
+ paddingHorizontal: spacing['2xl'],
+ borderBottomWidth: StyleSheet.hairlineWidth,
+ borderBottomColor: colors.divider,
+ },
+ settingItemLast: {
+ borderBottomWidth: 0,
+ },
+ settingItemLeft: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ flex: 1,
+ },
+ iconContainer: {
+ width: 24,
+ height: 24,
+ justifyContent: 'center',
+ alignItems: 'center',
+ marginRight: spacing.lg,
+ },
+ settingContent: {
+ flex: 1,
+ },
+ subtitle: {
+ marginTop: 2,
+ },
+
+ updateSection: {
+ paddingHorizontal: spacing['2xl'],
+ marginBottom: spacing['2xl'],
+ },
+ updateCard: {
+ backgroundColor: colors.background.default,
+ borderRadius: borderRadius.xl,
+ padding: spacing.lg,
+ },
+ updateHeader: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ marginBottom: spacing.lg,
+ },
+ updateIconContainer: {
+ width: 44,
+ height: 44,
+ borderRadius: borderRadius.lg,
+ backgroundColor: colors.background.paper,
+ justifyContent: 'center',
+ alignItems: 'center',
+ marginRight: spacing.md,
+ },
+ updateTitle: {
+ fontSize: 16,
+ fontWeight: '600',
+ color: colors.text.primary,
+ },
+ updateSubtitle: {
+ fontSize: 13,
+ color: colors.text.secondary,
+ marginTop: 2,
+ },
+
+ checkUpdateButton: {
+ height: 48,
+ borderRadius: borderRadius.lg,
+ backgroundColor: THEME_COLORS.primary,
+ alignItems: 'center',
+ justifyContent: 'center',
+ flexDirection: 'row',
+ },
+ checkUpdateText: {
+ fontSize: 15,
+ fontWeight: '600',
+ color: '#fff',
+ marginLeft: spacing.sm,
+ },
+
+ updateStatusContainer: {
+ alignItems: 'center',
+ paddingVertical: spacing.lg,
+ },
+ updateStatusTitle: {
+ fontSize: 15,
+ fontWeight: '600',
+ color: colors.text.primary,
+ marginTop: spacing.md,
+ marginBottom: spacing.xs,
+ },
+ updateStatusDesc: {
+ fontSize: 13,
+ color: colors.text.secondary,
+ textAlign: 'center',
+ },
+
+ newVersionContainer: {
+ backgroundColor: colors.background.paper,
+ borderRadius: borderRadius.lg,
+ padding: spacing.lg,
+ marginTop: spacing.md,
+ },
+ newVersionHeader: {
+ flexDirection: 'row',
+ alignItems: 'center',
+ marginBottom: spacing.xs,
+ },
+ newVersionText: {
+ fontSize: 15,
+ fontWeight: '600',
+ color: colors.text.primary,
+ },
+ newVersionBadge: {
+ backgroundColor: THEME_COLORS.primary,
+ paddingHorizontal: spacing.sm,
+ paddingVertical: 2,
+ borderRadius: borderRadius.sm,
+ marginLeft: spacing.sm,
+ },
+ newVersionBadgeText: {
+ color: '#fff',
+ fontSize: 10,
+ fontWeight: '600',
+ },
+ releaseDate: {
+ fontSize: 12,
+ color: colors.text.hint,
+ marginBottom: spacing.md,
+ },
+ releaseNotes: {
+ fontSize: 14,
+ color: colors.text.secondary,
+ lineHeight: 20,
+ marginBottom: spacing.lg,
+ },
+ updateActions: {
+ flexDirection: 'row',
+ gap: spacing.md,
+ },
+ updateButton: {
+ flex: 1,
+ height: 40,
+ borderRadius: borderRadius.md,
+ alignItems: 'center',
+ justifyContent: 'center',
+ flexDirection: 'row',
+ },
+ primaryButton: {
+ backgroundColor: THEME_COLORS.primary,
+ },
+ secondaryButton: {
+ backgroundColor: colors.background.default,
+ },
+ primaryButtonText: {
+ fontSize: 14,
+ fontWeight: '600',
+ color: '#fff',
+ marginLeft: spacing.xs,
+ },
+ secondaryButtonText: {
+ fontSize: 14,
+ fontWeight: '600',
+ color: colors.text.primary,
+ marginLeft: spacing.xs,
+ },
+
+ infoValue: {
+ fontSize: 15,
+ color: colors.text.secondary,
+ },
+
+ footer: {
+ alignItems: 'center',
+ marginTop: spacing.xl,
+ paddingBottom: spacing.xl,
+ },
+ copyright: {
+ fontSize: 12,
+ color: colors.text.hint,
+ textAlign: 'center',
+ },
+ companyName: {
+ fontSize: 12,
+ color: colors.text.hint,
+ marginTop: spacing.xs,
+ },
+ });
+}
+
+type UpdateStatus = 'idle' | 'checking' | 'noUpdate' | 'hasUpdate' | 'error' | 'notSupported';
+
+export const AboutScreen: React.FC = () => {
+ const colors = useAppColors();
+ const styles = useMemo(() => createAboutStyles(colors), [colors]);
+ const insets = useSafeAreaInsets();
+ const router = useRouter();
+ const { isMobile } = useResponsive();
+ const responsivePadding = useResponsiveSpacing({ xs: 8, sm: 12, md: 16, lg: 24, xl: 32 });
+
+ const [updateStatus, setUpdateStatus] = useState('idle');
+ const [versionInfo, setVersionInfo] = useState(null);
+ const [errorMessage, setErrorMessage] = useState('');
+
+ const scrollBottomInset = isMobile ? 64 + 24 + insets.bottom + spacing.md : spacing.md;
+
+ const checkForUpdate = useCallback(async () => {
+ setUpdateStatus('checking');
+ setErrorMessage('');
+
+ try {
+ if (Platform.OS !== 'android') {
+ setUpdateStatus('notSupported');
+ return;
+ }
+
+ const result = await checkForAPKUpdate(true);
+
+ if (!result) {
+ setErrorMessage('无法获取版本信息,请检查网络连接');
+ setUpdateStatus('error');
+ return;
+ }
+
+ setVersionInfo(result);
+
+ if (result.hasUpdate) {
+ setUpdateStatus('hasUpdate');
+ } else {
+ setUpdateStatus('noUpdate');
+ }
+ } catch (error) {
+ setErrorMessage('检查更新失败,请稍后重试');
+ setUpdateStatus('error');
+ }
+ }, []);
+
+ const handleUpdate = useCallback(() => {
+ if (!versionInfo?.downloadUrl) return;
+
+ Alert.alert(
+ '更新应用',
+ `即将下载版本 ${versionInfo.latestVersion},是否继续?`,
+ [
+ { text: '取消', style: 'cancel' },
+ {
+ text: '立即更新',
+ onPress: async () => {
+ try {
+ const canOpen = await Linking.canOpenURL(versionInfo.downloadUrl);
+ if (canOpen) {
+ await Linking.openURL(versionInfo.downloadUrl);
+ } else {
+ Alert.alert('错误', '无法打开下载链接');
+ }
+ } catch (error) {
+ Alert.alert('错误', '打开下载链接失败');
+ }
+ },
+ },
+ ]
+ );
+ }, [versionInfo]);
+
+ const renderUpdateStatus = () => {
+ switch (updateStatus) {
+ case 'checking':
+ return (
+
+
+ 正在检查更新...
+
+ );
+
+ case 'noUpdate':
+ return (
+
+
+ 已是最新版本
+ 当前版本 {APP_VERSION} 已是最新版本
+ setUpdateStatus('idle')}
+ >
+
+ 重新检查
+
+
+ );
+
+ case 'hasUpdate':
+ return (
+
+
+ 发现新版本
+
+ New
+
+
+ 版本 {versionInfo?.latestVersion} · 当前版本 {versionInfo?.currentVersion}
+ 新版本已发布,建议立即更新以获得更好的体验。
+
+ setUpdateStatus('idle')}
+ >
+ 稍后再说
+
+
+
+ 立即更新
+
+
+
+ );
+
+ case 'error':
+ return (
+
+
+ 检查失败
+ {errorMessage || '请检查网络连接后重试'}
+
+
+ 重新检查
+
+
+ );
+
+ case 'notSupported':
+ return (
+
+
+ iOS 平台
+ iOS 用户请前往 App Store 检查更新
+ setUpdateStatus('idle')}
+ >
+
+ 重新检查
+
+
+ );
+
+ default:
+ return (
+
+
+ 检查新版本
+
+ );
+ }
+ };
+
+ const legalItems = [
+ { key: 'terms', icon: 'file-document-outline', title: '用户协议', action: () => router.push(hrefs.hrefProfileTerms()) },
+ { key: 'privacy', icon: 'shield-outline', title: '隐私政策', action: () => router.push(hrefs.hrefProfilePrivacy()) },
+ ];
+
+ const appInfoItems = [
+ { key: 'name', icon: 'information-outline', title: '应用名称', value: APP_NAME },
+ { key: 'version', icon: 'tag-outline', title: '版本号', value: `v${APP_VERSION} (${BUILD_NUMBER})` },
+ { key: 'developer', icon: 'office-building-outline', title: '开发者', value: '青春之旅电子信息科技' },
+ ];
+
+ const renderSettingItem = (item: { key: string; icon: string; title: string; value?: string; action?: () => void }, index: number, total: number, showArrow: boolean = false) => (
+
+
+
+
+
+
+
+ {item.title}
+
+ {item.value && (
+
+ {item.value}
+
+ )}
+
+
+ {showArrow && (
+
+ )}
+ {!showArrow && item.value && (
+ {item.value}
+ )}
+
+ );
+
+ return (
+
+
+
+
+
+ 萝卜
+
+ {APP_NAME}
+ {APP_SLOGAN}
+ 版本 {APP_VERSION}
+
+
+
+
+
+
+
+
+
+ 版本更新
+ 检查并获取最新版本
+
+
+ {renderUpdateStatus()}
+
+
+
+
+
+
+ 法律信息
+
+
+
+ {legalItems.map((item, index) => renderSettingItem(item, index, legalItems.length, true))}
+
+
+
+
+
+
+ 应用信息
+
+
+
+ {appInfoItems.map((item, index) => renderSettingItem(item, index, appInfoItems.length, false))}
+
+
+
+
+ © 2026 青春之旅电子信息科技(威海)有限公司
+ 鲁ICP备XXXXXXXX号-1
+
+
+
+
+ );
+};
+
+export default AboutScreen;
diff --git a/src/screens/profile/AccountSecurityScreen.tsx b/src/screens/profile/AccountSecurityScreen.tsx
index c036ef2..13cad11 100644
--- a/src/screens/profile/AccountSecurityScreen.tsx
+++ b/src/screens/profile/AccountSecurityScreen.tsx
@@ -15,6 +15,14 @@ import { useResponsive, useResponsiveSpacing } from '../../hooks';
// 内容最大宽度
const CONTENT_MAX_WIDTH = 720;
+
+// 胡萝卜橙主题色
+const THEME_COLORS = {
+ primary: '#FF6B35',
+ primaryLight: '#FF8C5A',
+ primaryDark: '#E55A2B',
+};
+
import { authService, resolveAuthApiError } from '../../services/authService';
import { showPrompt } from '../../services/promptService';
import { useAuthStore } from '../../stores';
@@ -186,167 +194,173 @@ export const AccountSecurityScreen: React.FC = () => {
}
};
- const content = (
- <>
-
-
-
- 邮箱验证
-
-
-
-
- 当前状态
-
-
- {emailStatusText}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 0) && styles.buttonDisabled]}
- onPress={handleSendCode}
- disabled={sendingCode || countdown > 0}
- >
- {sendingCode ? (
-
- ) : (
- {countdown > 0 ? `${countdown}s` : '发送验证码'}
- )}
-
-
-
-
- {verifyingEmail ? (
-
- ) : (
- 验证邮箱
- )}
-
-
-
-
-
-
-
- 修改密码
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 0) && styles.buttonDisabled]}
- onPress={handleSendChangePasswordCode}
- disabled={sendingChangePwdCode || changePwdCountdown > 0}
- >
- {sendingChangePwdCode ? (
-
- ) : (
-
- {changePwdCountdown > 0 ? `${changePwdCountdown}s` : '发送验证码'}
-
- )}
-
-
-
-
-
-
-
-
- {updatingPassword ? (
-
- ) : (
- 更新密码
- )}
-
-
-
- >
- );
-
const responsivePadding = useResponsiveSpacing({ xs: 8, sm: 12, md: 16, lg: 24, xl: 32 });
return (
- {content}
+
+ {/* 邮箱验证分组 */}
+
+
+ 邮箱验证
+
+
+
+ {/* 状态显示 */}
+
+ 当前状态
+
+
+ {emailStatusText}
+
+
+
+
+ {/* 邮箱输入框 */}
+
+
+
+
+
+ {/* 验证码行 */}
+
+
+
+
+
+ 0) && styles.buttonDisabled]}
+ onPress={handleSendCode}
+ disabled={sendingCode || countdown > 0}
+ >
+ {sendingCode ? (
+
+ ) : (
+ {countdown > 0 ? `${countdown}s` : '发送验证码'}
+ )}
+
+
+
+ {/* 验证按钮 */}
+
+ {verifyingEmail ? (
+
+ ) : (
+ 验证邮箱
+ )}
+
+
+
+ {/* 修改密码分组 */}
+
+
+ 修改密码
+
+
+
+ {/* 当前密码 */}
+
+
+
+
+
+ {/* 新密码 */}
+
+
+
+
+
+ {/* 确认新密码 */}
+
+
+
+
+
+ {/* 验证码行 */}
+
+
+
+
+
+ 0) && styles.buttonDisabled]}
+ onPress={handleSendChangePasswordCode}
+ disabled={sendingChangePwdCode || changePwdCountdown > 0}
+ >
+ {sendingChangePwdCode ? (
+
+ ) : (
+
+ {changePwdCountdown > 0 ? `${changePwdCountdown}s` : '发送验证码'}
+
+ )}
+
+
+
+ {/* 更新密码按钮 */}
+
+ {updatingPassword ? (
+
+ ) : (
+ 更新密码
+ )}
+
+
+
);
@@ -356,37 +370,51 @@ function createAccountSecurityStyles(colors: AppColors) {
return StyleSheet.create({
container: {
flex: 1,
- backgroundColor: colors.background.paper,
+ backgroundColor: '#fff',
},
scrollContent: {
paddingVertical: spacing.lg,
},
- section: {
- marginBottom: spacing['2xl'],
+ content: {
+ maxWidth: CONTENT_MAX_WIDTH,
+ alignSelf: 'center',
+ width: '100%',
},
- sectionHeader: {
+ // 分组标题
+ groupHeader: {
paddingHorizontal: spacing['2xl'],
marginBottom: spacing.sm,
marginTop: spacing.sm,
},
- sectionTitle: {
+ groupTitle: {
fontWeight: '600',
fontSize: fontSizes.sm,
color: colors.text.secondary,
textTransform: 'uppercase',
letterSpacing: 0.5,
},
+ // 卡片样式 - 统一
card: {
+ backgroundColor: '#F5F5F7',
+ borderRadius: 16,
+ padding: 6,
+ marginBottom: spacing['2xl'],
marginHorizontal: spacing['2xl'],
- maxWidth: CONTENT_MAX_WIDTH,
- alignSelf: 'center',
- width: '100%',
},
+ // 状态显示
statusRow: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
+ paddingHorizontal: spacing.md,
+ paddingVertical: spacing.md,
marginBottom: spacing.md,
+ backgroundColor: '#fff',
+ borderRadius: 12,
+ },
+ statusLabel: {
+ fontSize: 15,
+ color: colors.text.secondary,
},
statusBadge: {
paddingHorizontal: spacing.sm,
@@ -394,24 +422,29 @@ function createAccountSecurityStyles(colors: AppColors) {
borderRadius: borderRadius.sm,
},
statusVerified: {
- backgroundColor: colors.success.light + '40',
+ backgroundColor: '#E8F5E9',
},
statusUnverified: {
- backgroundColor: colors.warning.light + '40',
+ backgroundColor: '#FFF3E0',
+ },
+ statusText: {
+ fontSize: 13,
+ fontWeight: '600',
},
statusVerifiedText: {
- color: colors.success.dark,
+ color: '#2E7D32',
},
statusUnverifiedText: {
- color: colors.warning.dark,
+ color: '#E65100',
},
+ // 输入框样式 - 统一
inputWrapper: {
flexDirection: 'row',
alignItems: 'center',
- backgroundColor: colors.background.default,
+ backgroundColor: '#fff',
borderRadius: 14,
paddingHorizontal: spacing.md,
- height: 50,
+ height: 56,
marginBottom: spacing.sm,
},
inputIcon: {
@@ -421,8 +454,9 @@ function createAccountSecurityStyles(colors: AppColors) {
flex: 1,
color: colors.text.primary,
fontSize: fontSizes.md,
- height: 50,
+ height: 56,
},
+ // 验证码行
codeRow: {
flexDirection: 'row',
alignItems: 'center',
@@ -433,30 +467,32 @@ function createAccountSecurityStyles(colors: AppColors) {
flex: 1,
marginBottom: 0,
},
+ // 发送验证码按钮
sendCodeButton: {
- height: 50,
+ height: 56,
minWidth: 110,
borderRadius: 14,
- backgroundColor: colors.primary.main,
+ backgroundColor: THEME_COLORS.primary,
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: spacing.sm,
},
sendCodeButtonText: {
- color: colors.text.inverse,
+ color: '#fff',
fontSize: fontSizes.sm,
fontWeight: '600',
},
+ // 主按钮样式 - 统一
primaryButton: {
- height: 50,
+ height: 56,
borderRadius: 14,
- backgroundColor: colors.primary.main,
+ backgroundColor: THEME_COLORS.primary,
alignItems: 'center',
justifyContent: 'center',
marginTop: spacing.xs,
},
primaryButtonText: {
- color: colors.text.inverse,
+ color: '#fff',
fontSize: fontSizes.md,
fontWeight: '600',
},
diff --git a/src/screens/profile/ChatSettingsScreen.tsx b/src/screens/profile/ChatSettingsScreen.tsx
index 8bbc8a6..975cd1f 100644
--- a/src/screens/profile/ChatSettingsScreen.tsx
+++ b/src/screens/profile/ChatSettingsScreen.tsx
@@ -3,17 +3,16 @@
* 胡萝卜BBS - 聊天个性化设置
*/
-import React, { useMemo, useCallback } from 'react';
+import React, { useMemo, useCallback, useRef } from 'react';
import {
View,
StyleSheet,
TouchableOpacity,
ScrollView,
Dimensions,
+ PanResponder,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
-import { useRouter } from 'expo-router';
-import { MaterialCommunityIcons } from '@expo/vector-icons';
import {
useAppColors,
spacing,
@@ -28,7 +27,6 @@ import {
useChatSettingsActions,
CHAT_THEMES,
} from '../../stores/chatSettingsStore';
-import { useCurrentUser } from '../../stores';
const { width: SCREEN_WIDTH } = Dimensions.get('window');
const CARD_MAX_WIDTH = 720;
@@ -43,7 +41,7 @@ interface SliderProps {
showValue?: boolean;
}
-// 自定义滑块组件
+// 自定义滑块组件(支持拖动)
const Slider: React.FC = ({
value,
min,
@@ -55,34 +53,51 @@ const Slider: React.FC = ({
}) => {
const colors = useAppColors();
const styles = useMemo(() => createSliderStyles(colors), [colors]);
+ const trackWidthRef = useRef(0);
- const percentage = ((value - min) / (max - min)) * 100;
-
- const handlePress = useCallback((event: any) => {
- const { locationX } = event.nativeEvent;
- const sliderWidth = SCREEN_WIDTH - 64; // 考虑边距
- const newPercentage = Math.max(0, Math.min(100, (locationX / sliderWidth) * 100));
+ const percentage = isNaN(value) ? 0 : ((value - min) / (max - min)) * 100;
+
+ const updateValue = useCallback((locationX: number) => {
+ const width = trackWidthRef.current;
+ if (width <= 0) return;
+
+ const newPercentage = Math.max(0, Math.min(100, (locationX / width) * 100));
const newValue = Math.round(min + (newPercentage / 100) * (max - min));
onValueChange(newValue);
}, [min, max, onValueChange]);
+ const panResponder = useMemo(() => PanResponder.create({
+ onStartShouldSetPanResponder: () => true,
+ onMoveShouldSetPanResponder: () => true,
+ onPanResponderGrant: (evt) => {
+ updateValue(evt.nativeEvent.locationX);
+ },
+ onPanResponderMove: (evt) => {
+ updateValue(evt.nativeEvent.locationX);
+ },
+ }), [updateValue]);
+
+ const handleLayout = useCallback((event: any) => {
+ trackWidthRef.current = event.nativeEvent.layout.width;
+ }, []);
+
return (
{leftLabel && {leftLabel}}
- {showValue && {value}}
+ {showValue && {isNaN(value) ? 0 : value}}
{rightLabel && {rightLabel}}
-
-
+
);
};
@@ -168,29 +183,32 @@ function createStyles(colors: AppColors) {
minHeight: 140,
},
previewMessage: {
- backgroundColor: colors.background.paper,
- borderRadius: borderRadius.md,
+ backgroundColor: '#FFFFFF',
padding: spacing.sm,
marginBottom: spacing.sm,
maxWidth: '80%',
alignSelf: 'flex-start',
+ minWidth: 44,
+ shadowColor: '#000',
+ shadowOffset: { width: 0, height: 1 },
+ shadowOpacity: 0.06,
+ shadowRadius: 2,
+ elevation: 1,
},
previewReply: {
- borderRadius: borderRadius.md,
padding: spacing.sm,
maxWidth: '80%',
alignSelf: 'flex-end',
+ minWidth: 44,
+ shadowColor: '#000',
+ shadowOffset: { width: 0, height: 1 },
+ shadowOpacity: 0.06,
+ shadowRadius: 2,
+ elevation: 1,
},
previewText: {
- fontSize: fontSizes.sm,
color: colors.text.primary,
},
- previewTime: {
- fontSize: fontSizes.xs,
- color: colors.text.hint,
- marginTop: 2,
- alignSelf: 'flex-end',
- },
// 主题选择样式
themeList: {
flexDirection: 'row',
@@ -240,106 +258,56 @@ function createStyles(colors: AppColors) {
marginTop: 4,
color: colors.text.secondary,
},
- // 设置项样式 - 扁平化
- settingItem: {
- flexDirection: 'row',
- alignItems: 'center',
- justifyContent: 'space-between',
- paddingVertical: 16,
- borderBottomWidth: StyleSheet.hairlineWidth,
- borderBottomColor: colors.divider,
- },
- settingItemLast: {
- borderBottomWidth: 0,
- },
- settingItemLeft: {
- flexDirection: 'row',
- alignItems: 'center',
- flex: 1,
- },
- iconContainer: {
- width: 24,
- height: 24,
- justifyContent: 'center',
- alignItems: 'center',
- marginRight: spacing.lg,
- },
- settingTitle: {
- fontSize: fontSizes.md,
- color: colors.text.primary,
- },
- settingSubtitle: {
- fontSize: fontSizes.sm,
- color: colors.text.secondary,
- marginTop: 2,
- },
- // 滑块容器 - 扁平化
sliderContainer: {
paddingVertical: 8,
},
- // 开关样式
- switch: {
- width: 50,
- height: 28,
- borderRadius: 14,
- backgroundColor: colors.divider,
- padding: 2,
- },
- switchActive: {
- backgroundColor: colors.primary.main,
- },
- switchThumb: {
- width: 24,
- height: 24,
- borderRadius: 12,
- backgroundColor: colors.background.paper,
- },
});
}
export const ChatSettingsScreen: React.FC = () => {
- const router = useRouter();
const colors = useAppColors();
const styles = useMemo(() => createStyles(colors), [colors]);
const responsivePadding = useResponsiveSpacing({ xs: 8, sm: 12, md: 16, lg: 24, xl: 32 });
- // 获取当前用户信息
- const currentUser = useCurrentUser();
-
// 从 store 获取状态
const fontSize = useChatSettingsStore((s) => s.fontSize);
const messageRadius = useChatSettingsStore((s) => s.messageRadius);
const themeIndex = useChatSettingsStore((s) => s.themeIndex);
- const nightMode = useChatSettingsStore((s) => s.nightMode);
- // 获取操作方法
- const { setFontSize, setMessageRadius, setTheme, toggleNightMode } = useChatSettingsActions();
+ const { setFontSize, setMessageRadius, setTheme } = useChatSettingsActions();
const currentTheme = CHAT_THEMES[themeIndex];
- // 用户显示名称
- const userName = currentUser?.nickname || currentUser?.username || '我';
-
// 渲染聊天预览
const renderChatPreview = () => (
预览
-
- {userName}
-
+
+
早上好!👋
-
+
你知道现在几点吗?
- AM12:42
-
+
现在是威海的早晨😎
- AM12:57
@@ -410,87 +378,18 @@ export const ChatSettingsScreen: React.FC = () => {
);
- // 渲染其他设置项
- const renderOtherSettings = () => (
-
- 其他
-
-
-
-
-
-
- 更改聊天壁纸
-
-
-
-
-
-
-
-
-
-
-
- 更改名称颜色
- MiMQy
-
-
-
-
-
- );
-
- // 渲染夜间模式开关
- const renderNightMode = () => (
-
-
-
-
-
-
- 切换到夜间模式
-
- toggleNightMode()}
- >
-
-
-
-
- );
-
- // 渲染浏览其他主题
- const renderBrowseThemes = () => (
-
-
-
-
-
-
- 浏览其他主题
-
-
-
-
- );
-
return (
{renderFontSizeSlider()}
{renderChatPreview()}
{renderThemeColors()}
{renderRadiusSlider()}
- {renderOtherSettings()}
- {renderNightMode()}
- {renderBrowseThemes()}
);
diff --git a/src/screens/profile/PrivacyPolicyScreen.tsx b/src/screens/profile/PrivacyPolicyScreen.tsx
new file mode 100644
index 0000000..6ffffca
--- /dev/null
+++ b/src/screens/profile/PrivacyPolicyScreen.tsx
@@ -0,0 +1,320 @@
+/**
+ * 隐私政策页面 PrivacyPolicyScreen
+ * 胡萝卜BBS - 隐私政策
+ * 扁平化设计风格
+ */
+
+import React, { useMemo } from 'react';
+import {
+ View,
+ StyleSheet,
+ ScrollView,
+} from 'react-native';
+import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context';
+import {
+ useAppColors,
+ spacing,
+ type AppColors,
+} from '../../theme';
+import { Text } from '../../components/common';
+import { useResponsive, useResponsiveSpacing } from '../../hooks';
+
+// 内容最大宽度
+const CONTENT_MAX_WIDTH = 400;
+
+// 胡萝卜橙主题色
+const THEME_COLORS = {
+ primary: '#FF6B35',
+};
+
+// 政策最后更新日期
+const LAST_UPDATED = '2026年4月4日';
+
+// 隐私政策内容
+const PRIVACY_SECTIONS = [
+ {
+ title: '引言',
+ content: `萝卜社区(以下简称"我们"或"本应用")非常重视用户的隐私和个人信息保护。我们深知个人信息对您的重要性,并会尽全力保护您的个人信息安全可靠。
+
+本隐私政策旨在帮助您了解我们如何收集、使用、存储、共享和保护您的个人信息,以及您享有的相关权利。请您在使用我们的服务前,仔细阅读并理解本隐私政策的全部内容。
+
+【敏感个人信息提示】请您注意,您发布的照片、视频可能包含生物识别信息,您的位置信息属于行踪轨迹信息,通讯录信息属于社交关系信息,以上均属于敏感个人信息。我们收集这些信息是为了实现校园社交的核心功能,请您谨慎考虑是否提供。拒绝提供敏感个人信息仅会影响相关功能的使用,不会影响您使用其他服务。`,
+ },
+ {
+ title: '一、我们收集的信息',
+ content: `1.1 您主动提供的信息
+当您注册、登录或使用我们的服务时,您可能需要提供以下信息:
+• 账号信息:手机号、邮箱地址、用户名、密码等
+• 个人资料:昵称、头像、性别、生日、学校、专业等
+• 联系信息:真实姓名、地址(用于特定功能)
+• 内容信息:您发布的内容、评论、私信等
+
+1.2 我们在您使用服务时收集的信息
+• 设备信息:包括设备型号、操作系统版本、设备设置、唯一设备标识符(如IMEI、Android ID、IDFA、OAID、MAC地址等)、IP地址等
+• 日志信息:IP地址、访问时间、浏览记录、操作日志等
+• 位置信息:为了向您提供"校园圈"、"附近的人"等基于位置的服务,经您授权,我们会收集您的精确地理位置信息。该信息属于敏感个人信息,拒绝提供仅会影响相关功能,不影响其他服务的使用
+• 相机/相册权限:用于发布动态、更换头像。我们仅在您主动触发相关功能时申请权限,且不会在后台持续收集
+• 通讯录权限:经您授权后用于查找好友
+
+1.3 从第三方获取的信息
+如您使用第三方账号登录功能(具体以应用实际提供的登录方式为准),我们会从第三方获取您授权共享的账号信息(如昵称、头像等)。`,
+ },
+ {
+ title: '二、我们如何使用信息',
+ content: `2.1 我们使用收集的信息用于以下目的:
+• 为您提供注册、登录及身份验证服务
+• 为您展示个性化的内容推荐
+• 实现社交功能,如好友推荐、内容分享、附近的人
+• 向您发送服务通知、安全提示
+• 改善和优化我们的产品和服务
+• 保护账号安全,防范欺诈和恶意行为
+• 遵守法律法规的要求
+
+2.2 我们使用Cookie和同类技术
+我们使用Cookie和同类技术(如Web Beacon、Pixel标签、本地存储)来确保应用正常运转、记住您的偏好、分析您使用我们服务的情况。具体包括:
+• 用于记住您的登录状态
+• 了解您的使用偏好,提供个性化服务
+• 分析服务使用情况,优化产品体验
+• 用于广告归因和效果分析
+我们不会将Cookie用于本政策所述目的之外的任何用途。您可以通过设备设置管理Cookie,但这可能影响某些功能的使用。`,
+ },
+ {
+ title: '三、信息的共享与披露',
+ content: `3.1 我们不会将您的个人信息出售给任何第三方,但在以下情况下可能会共享或披露您的信息:
+
+3.2 经您同意的情况
+在获得您的明确同意后,我们会与第三方共享您的个人信息。
+
+3.3 与我们的关联公司共享
+为向您提供一致的服务体验,我们可能会与关联公司共享必要的个人信息。
+
+3.4 与服务提供商共享
+我们可能会委托第三方服务提供商处理您的信息,如云服务提供商、支付服务提供商、数据分析服务商、推送服务商等。这些服务提供商仅能在我们授权的范围内使用您的信息,且我们要求其遵守严格的保密义务。
+
+3.5 法律法规要求及无需征得同意的情形
+在以下情况下,我们可能会披露您的信息,且无需征得您的授权同意:
+(1)与国家安全、国防安全有关的;
+(2)与公共安全、公共卫生、重大公共利益有关的;
+(3)与犯罪侦查、起诉、审判和判决执行等有关的;
+(4)出于维护个人信息主体或其他个人的生命、财产等重大合法权益但又很难得到本人同意的;
+(5)所收集的个人信息是个人信息主体自行向社会公众公开的;
+(6)从合法公开披露的信息中收集个人信息的,如合法的新闻报道、政府信息公开等渠道;
+(7)根据个人信息主体要求签订和履行合同所必需的;
+(8)为配合学校、教育机构或相关主管部门履行法定管理职责(如学籍管理、校园安全、突发事件处理等)的;
+(9)法律法规规定的其他情形。
+
+3.6 匿名化信息
+我们可能会对您的个人信息进行匿名化处理,使其无法识别特定个人,然后使用或共享这些匿名化信息。`,
+ },
+ {
+ title: '四、信息的存储与保护',
+ content: `4.1 信息存储
+• 原则上,我们在中华人民共和国境内收集和产生的个人信息,将存储在中华人民共和国境内
+• 如部分服务需要使用境外服务器(如跨境数据传输),我们会单独征得您的授权同意,并确保符合法律法规要求
+• 我们会采取合理的技术和管理措施保护您的信息安全
+• 我们只会在达成本隐私政策所述目的所需的期限内保留您的信息
+
+4.2 安全措施
+• 使用加密技术保护数据传输安全
+• 实施访问控制,限制对个人信息的访问
+• 定期进行安全审计和风险评估
+• 对员工进行隐私保护培训
+
+4.3 安全事件处理
+如发生个人信息泄露等安全事件,我们会:
+• 及时采取补救措施
+• 按照法律法规要求通知您和监管部门
+• 配合相关部门进行调查`,
+ },
+ {
+ title: '五、您的权利',
+ content: `5.1 访问和更正
+您有权访问和更正您的个人信息。您可以在应用内的"设置"-"编辑资料"中查看和修改大部分个人信息。
+
+5.2 删除
+在以下情况下,您可以要求我们删除您的个人信息:
+• 我们处理信息的行为违反法律法规
+• 我们未经您同意收集、使用您的信息
+• 您不再使用我们的服务并要求删除
+• 我们违反与您的约定处理您的信息
+
+5.3 撤回同意
+您可以随时撤回对我们收集、使用您个人信息的同意。但请注意,撤回同意可能会影响您使用某些服务。
+
+5.4 注销账号
+您可以在"设置"-"账号安全"中申请注销账号。注销后,我们将停止为您提供服务,并删除或匿名化您的个人信息。但根据法律法规规定(如《网络安全法》要求的日志留存不少于6个月),我们需要保留部分必要信息以配合监管、处理历史纠纷或履行法定义务。
+
+5.5 投诉与建议
+如果您对我们的隐私政策或信息处理有任何疑问或建议,请联系我们:system@qczlit.cn`,
+ },
+ {
+ title: '六、未成年人保护',
+ content: `6.1 我们非常重视对未成年人个人信息的保护。我们的服务主要面向大学生及成年用户。
+
+6.2 如果您是未满18周岁的未成年人,在使用我们的服务前,请确保在监护人的陪同下阅读本隐私政策,并在获得监护人同意后再使用我们的服务。
+
+6.3 如果您是未成年人的监护人,请监督您的被监护人使用本服务。
+
+6.4 如果我们发现自己在未事先获得监护人同意的情况下收集了未成年人的个人信息,我们会尽快删除相关数据。`,
+ },
+ {
+ title: '七、第三方SDK目录',
+ content: `为了保障App的稳定运行或实现特定功能,我们可能接入第三方SDK。截至本隐私政策更新之日,我们主要使用以下服务:
+
+• Expo推送服务:用于消息推送功能。可能收集设备标识符、设备信息。
+
+如您后续接入其他第三方SDK(如微信登录、分享等功能),我们将在本章节更新相关说明,并告知您对应SDK收集的信息类型和用途。更新后的SDK目录将在应用内公布,请以最新版本为准。`,
+ },
+ {
+ title: '八、隐私政策的更新',
+ content: `8.1 我们可能会不时更新本隐私政策。更新后的隐私政策将在应用内公布,并标注更新日期。
+
+8.2 对于重大变更,我们会在变更生效前通过应用内通知、弹窗等方式告知您。
+
+8.3 如您不同意变更后的内容,应立即停止使用本服务。如您继续使用本服务,即视为您已阅读并同意受变更后的隐私政策约束。`,
+ },
+ {
+ title: '九、联系我们',
+ content: `如果您对本隐私政策有任何疑问、意见或建议,或者您希望行使您的权利,请通过以下方式与我们联系:
+
+• 邮箱:system@qczlit.cn
+• 公司:青春之旅电子信息科技(威海)有限公司
+• 地址:山东省威海市环翠区哈尔滨工业大学(威海)
+
+我们将在收到您的请求后的15个工作日内予以回复。对于复杂的请求,我们可能会延长回复期限,并及时告知您延长的原因和预计回复时间。`,
+ },
+];
+
+function createPrivacyStyles(colors: AppColors) {
+ return StyleSheet.create({
+ container: {
+ flex: 1,
+ backgroundColor: '#fff',
+ },
+ scrollContent: {
+ paddingHorizontal: 28,
+ paddingTop: 24,
+ },
+
+ // 内容区域
+ content: {
+ maxWidth: CONTENT_MAX_WIDTH,
+ alignSelf: 'center',
+ width: '100%',
+ },
+
+ // 更新日期
+ lastUpdated: {
+ fontSize: 13,
+ color: colors.text.hint,
+ marginBottom: 20,
+ },
+
+ // 重点提示框
+ highlightBox: {
+ backgroundColor: '#FFF5F0',
+ borderRadius: 12,
+ padding: 16,
+ marginBottom: 20,
+ borderLeftWidth: 4,
+ borderLeftColor: THEME_COLORS.primary,
+ },
+ highlightTitle: {
+ fontSize: 15,
+ fontWeight: '600',
+ color: THEME_COLORS.primary,
+ marginBottom: 8,
+ },
+ highlightText: {
+ fontSize: 14,
+ lineHeight: 22,
+ color: colors.text.secondary,
+ },
+
+ // 隐私政策内容卡片
+ privacyCard: {
+ backgroundColor: '#F5F5F7',
+ borderRadius: 16,
+ padding: 20,
+ marginBottom: 24,
+ },
+ section: {
+ marginBottom: 24,
+ },
+ sectionLast: {
+ marginBottom: 0,
+ },
+ sectionTitle: {
+ fontSize: 16,
+ fontWeight: '600',
+ color: THEME_COLORS.primary,
+ marginBottom: 12,
+ },
+ sectionContent: {
+ fontSize: 14,
+ lineHeight: 24,
+ color: colors.text.primary,
+ },
+
+ // 底部版权
+ footer: {
+ alignItems: 'center',
+ },
+ footerText: {
+ fontSize: 13,
+ color: colors.text.hint,
+ textAlign: 'center',
+ marginBottom: 4,
+ },
+ });
+}
+
+export const PrivacyPolicyScreen: React.FC = () => {
+ const colors = useAppColors();
+ const styles = useMemo(() => createPrivacyStyles(colors), [colors]);
+ const insets = useSafeAreaInsets();
+ const { isMobile } = useResponsive();
+ const responsivePadding = useResponsiveSpacing({ xs: 20, sm: 24, md: 28, lg: 32, xl: 40 });
+
+ // 底部间距,避免被 TabBar 遮挡
+ const scrollBottomInset = isMobile ? 64 + 24 + insets.bottom + spacing.md : spacing.lg;
+
+ return (
+
+
+
+ {/* 更新日期 */}
+ 最后更新:{LAST_UPDATED}
+
+ {/* 重点提示 */}
+
+ 我们承诺保护您的隐私
+
+ 萝卜社区致力于保护用户的个人信息安全。我们仅收集提供服务所必需的信息,并采取严格的安全措施保护您的数据。
+
+
+
+ {/* 隐私政策内容 */}
+
+ {PRIVACY_SECTIONS.map((section, index) => (
+
+ {section.title}
+ {section.content}
+
+ ))}
+
+
+ {/* 底部版权 */}
+
+ © 2026 青春之旅电子信息科技(威海)有限公司
+
+
+
+
+ );
+};
+
+export default PrivacyPolicyScreen;
diff --git a/src/screens/profile/SettingsScreen.tsx b/src/screens/profile/SettingsScreen.tsx
index 037dccd..07825cc 100644
--- a/src/screens/profile/SettingsScreen.tsx
+++ b/src/screens/profile/SettingsScreen.tsx
@@ -62,7 +62,6 @@ const SETTINGS_GROUPS_BASE = [
icon: 'bell-outline',
items: [
{ key: 'notification_settings', title: '通知设置', icon: 'bell-cog-outline', showArrow: true, subtitle: '推送、震动、提示音' },
- { key: 'language', title: '语言设置', icon: 'translate', showArrow: true, subtitle: '简体中文' },
{ key: 'data_storage', title: '数据与存储', icon: 'database-outline', showArrow: true },
],
},
@@ -72,8 +71,6 @@ const SETTINGS_GROUPS_BASE = [
items: [
{ key: 'about', title: '关于我们', icon: 'information-outline', showArrow: true, subtitle: `版本 ${APP_VERSION}` },
{ key: 'help', title: '帮助与反馈', icon: 'help-circle-outline', showArrow: true },
- { key: 'terms', title: '用户协议', icon: 'file-document-outline', showArrow: true },
- { key: 'privacy_policy', title: '隐私政策', icon: 'shield-outline', showArrow: true },
],
},
];
@@ -240,31 +237,18 @@ export const SettingsScreen: React.FC = () => {
);
break;
}
- case 'language': {
- Alert.alert(
- '语言设置',
- '选择应用显示语言',
- [
- { text: '取消', style: 'cancel' },
- { text: '简体中文', onPress: () => {} },
- { text: '繁體中文', onPress: () => {} },
- { text: 'English', onPress: () => {} },
- ]
- );
- break;
- }
+
case 'data_storage': {
Alert.alert('数据与存储', '缓存清理功能即将上线!');
break;
}
- case 'terms': {
- Alert.alert('用户协议', '用户协议页面即将上线!');
+ case 'about':
+ router.push(hrefs.hrefProfileAbout());
break;
- }
- case 'privacy_policy': {
- Alert.alert('隐私政策', '隐私政策页面即将上线!');
+ case 'help':
+ Alert.alert('帮助与反馈', '帮助中心即将上线!');
break;
- }
+
case 'edit_profile':
router.push(hrefs.hrefProfileEdit());
break;
@@ -372,7 +356,7 @@ export const SettingsScreen: React.FC = () => {
萝卜社区 v{APP_VERSION}
- © 2024 Carrot BBS. All rights reserved.
+ © 2026 青春之旅电子信息科技(威海)有限公司
>
diff --git a/src/screens/profile/TermsOfServiceScreen.tsx b/src/screens/profile/TermsOfServiceScreen.tsx
new file mode 100644
index 0000000..7b953b3
--- /dev/null
+++ b/src/screens/profile/TermsOfServiceScreen.tsx
@@ -0,0 +1,248 @@
+/**
+ * 用户协议页面 TermsOfServiceScreen
+ * 胡萝卜BBS - 用户服务协议
+ * 扁平化设计风格
+ */
+
+import React, { useMemo } from 'react';
+import {
+ View,
+ StyleSheet,
+ ScrollView,
+} from 'react-native';
+import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context';
+import {
+ useAppColors,
+ spacing,
+ type AppColors,
+} from '../../theme';
+import { Text } from '../../components/common';
+import { useResponsive, useResponsiveSpacing } from '../../hooks';
+
+// 内容最大宽度
+const CONTENT_MAX_WIDTH = 400;
+
+// 胡萝卜橙主题色
+const THEME_COLORS = {
+ primary: '#FF6B35',
+};
+
+// 协议最后更新日期
+const LAST_UPDATED = '2026年4月4日';
+
+// 协议条款内容
+const TERMS_SECTIONS = [
+ {
+ title: '一、协议的范围',
+ content: `1.1 本协议是您与萝卜社区(以下简称"我们"或"本应用")之间关于您使用本应用服务所订立的协议。
+
+1.2 本协议内容包括协议正文及所有我们已经发布或将来可能发布的各类规则、公告或通知。所有规则为本协议不可分割的组成部分,与协议正文具有同等法律效力。
+
+1.3 我们有权根据法律法规的变化及业务调整的需要,不时修改本协议及各类规则。修改后的协议将在应用内公示。如您不同意变更后的内容,应立即停止使用本服务。如您继续使用本服务,即视为您已阅读并同意受变更后的协议约束。`,
+ },
+ {
+ title: '二、账号注册与使用',
+ content: `2.1 您确认,在您开始注册程序使用本应用服务前,您应当具备中华人民共和国法律规定的与您行为相适应的民事行为能力。若您是未满18周岁的未成年人,应在监护人监护、指导下并获得监护人同意后使用本应用服务。
+
+2.2 您可以使用手机号、邮箱进行注册(具体以应用实际提供的注册方式为准)。您应当提供真实、准确、完整、合法有效的注册信息,并在信息变更时及时更新。
+
+2.3 您的账号仅限于您本人使用,禁止赠与、借用、租用、转让或售卖。我们有权对账号进行安全验证,如发现异常登录或使用情况,有权采取相应措施。
+
+2.4 您应妥善保管账号和密码,对账号下的所有行为承担法律责任。因您保管不善导致的账号被盗或密码泄露,我们不承担责任。`,
+ },
+ {
+ title: '三、服务内容',
+ content: `3.1 本应用向您提供校园社交服务,包括但不限于:发布动态、评论互动、私信聊天、群组交流、学习资料分享等功能。
+
+3.2 我们有权根据实际情况随时调整服务内容,包括但不限于增加、删除或修改功能。服务调整将在应用内进行公告。
+
+3.3 我们致力于为您提供优质的服务,但不保证服务不会中断,对服务的及时性、安全性、准确性不作担保。`,
+ },
+ {
+ title: '四、用户行为规范',
+ content: `4.1 您在使用本应用服务时,必须遵守中华人民共和国相关法律法规,不得利用本应用从事违法违规活动。
+
+4.2 您不得发布、传播以下内容:
+(1)反对宪法所确定的基本原则的;
+(2)危害国家安全,泄露国家秘密,颠覆国家政权,破坏国家统一的;
+(3)损害国家荣誉和利益的;
+(4)煽动民族仇恨、民族歧视,破坏民族团结的;
+(5)破坏国家宗教政策,宣扬邪教和封建迷信的;
+(6)散布谣言,扰乱社会秩序,破坏社会稳定的;
+(7)散布淫秽、色情、赌博、暴力、凶杀、恐怖或者教唆犯罪的;
+(8)侮辱或者诽谤他人,侵害他人合法权益的;
+(9)含有法律、行政法规禁止的其他内容的。
+
+4.3 您不得从事以下行为:
+(1)冒充他人或使用虚假身份;
+(2)恶意注册账号,包括但不限于批量注册、使用非法手段注册;
+(3)利用技术手段批量发布内容或进行其他操作;
+(4)干扰或破坏本应用服务或服务器;
+(5)侵犯他人知识产权、隐私权等合法权益;
+(6)发布垃圾信息、广告信息或进行其他骚扰行为。
+
+4.4 若您是未满18周岁的未成年人,应在监护人监护、指导下并获得监护人同意后使用本应用服务。我们非常重视对未成年人个人信息的保护,若您是未成年人的监护人,请监督您的被监护人使用本服务。
+
+4.5 若您认为本应用内的内容侵犯了您的合法权益,或您发现任何违法违规内容,请通过 system@qczlit.cn 联系我们进行投诉举报,我们将在收到通知后依法尽快处理。`,
+ },
+ {
+ title: '五、知识产权',
+ content: `5.1 本应用的商标、标识、界面设计、程序代码、数据库等所有知识产权均归我们及我们的关联公司所有或已获得合法授权。
+
+5.2 您在使用本应用服务过程中产生的内容(包括但不限于文字、图片、视频等),您保留对该内容的知识产权。您授予我们一项全球范围内、免费、非独家、可转授权的许可,允许我们使用、复制、修改、改编、出版、翻译、据以创作衍生作品、传播、表演和展示此等内容,以及用于本应用的运营、推广、宣传和商业用途。
+
+5.3 未经我们书面许可,您不得复制、修改、传播、出售、出租本应用或其任何部分,不得对本应用进行反向工程、反编译或试图以其他方式发现其源代码。`,
+ },
+ {
+ title: '六、隐私保护',
+ content: `6.1 我们非常重视您的隐私保护,具体隐私政策请参见《隐私政策》。
+
+6.2 您同意我们可以按照《隐私政策》的规定收集、使用、存储和分享您的个人信息。
+
+6.3 我们将采取合理的技术和管理措施保护您的个人信息安全,但不对因不可抗力或第三方原因导致的信息泄露承担责任。`,
+ },
+ {
+ title: '七、免责声明',
+ content: `7.1 您理解并同意,本应用服务是按照现有技术和条件所能达到的现状提供的。我们会尽最大努力确保服务的连贯性和安全性,但不能随时预见和防范法律、技术以及其他风险。
+
+7.2 您理解并同意,我们对以下情形不承担责任:
+(1)因不可抗力导致的服务中断或数据丢失;
+(2)因您的过错导致的任何损失;
+(3)因第三方原因导致的服务中断或信息泄露;
+(4)其他非因我们原因导致的损失。
+
+7.3 您通过本应用与其他用户进行交往或交易,由此产生的任何纠纷由您自行解决并承担责任。`,
+ },
+ {
+ title: '八、违约责任',
+ content: `8.1 如果我们发现或收到他人举报您违反本协议约定的,我们有权根据情节轻重对相关内容进行删除、屏蔽,并视行为情节对违规账号处以包括但不限于警告、限制功能、暂停使用、永久封禁等处罚。因法律法规要求或紧急情况下,我们可能在不提前通知的情况下采取上述措施。
+
+8.2 账号被封禁或注销后,账号内的虚拟资产(包括但不限于积分、虚拟币、会员权益等)将不予退还或清零,您不得因此向我们主张任何赔偿或补偿。
+
+8.3 因您违反本协议或相关规则,导致或产生任何第三方主张的任何索赔、要求或损失,您应当独立承担责任,并赔偿我们因此遭受的损失。`,
+ },
+ {
+ title: '九、协议的终止',
+ content: `9.1 您有权随时终止使用本应用服务,可以通过注销账号的方式终止本协议。
+
+9.2 我们有权在以下情况下终止本协议:
+(1)您违反本协议或相关规则;
+(2)根据法律法规或政府部门要求;
+(3)因不可抗力导致无法提供服务。
+
+9.3 协议终止后,我们有权保留或删除您的账号信息及内容,且不承担任何责任。`,
+ },
+ {
+ title: '十、其他',
+ content: `10.1 本协议的订立、执行和解释及争议的解决均应适用中华人民共和国法律。
+
+10.2 因本协议引起的或与本协议有关的任何争议,由协议各方协商解决,也可由有关部门调解。协商或调解不成的,应向山东省威海市环翠区人民法院提起诉讼。
+
+10.3 本协议中的任何条款无论因何种原因完全或部分无效或不具有执行力,本协议的其余条款仍应有效并且有约束力。`,
+ },
+];
+
+function createTermsStyles(colors: AppColors) {
+ return StyleSheet.create({
+ container: {
+ flex: 1,
+ backgroundColor: '#fff',
+ },
+ scrollContent: {
+ paddingHorizontal: 28,
+ paddingTop: 24,
+ },
+
+ // 内容区域
+ content: {
+ maxWidth: CONTENT_MAX_WIDTH,
+ alignSelf: 'center',
+ width: '100%',
+ },
+
+ // 更新日期
+ lastUpdated: {
+ fontSize: 13,
+ color: colors.text.hint,
+ marginBottom: 20,
+ },
+
+ // 协议内容卡片
+ termsCard: {
+ backgroundColor: '#F5F5F7',
+ borderRadius: 16,
+ padding: 20,
+ marginBottom: 24,
+ },
+ section: {
+ marginBottom: 24,
+ },
+ sectionLast: {
+ marginBottom: 0,
+ },
+ sectionTitle: {
+ fontSize: 16,
+ fontWeight: '600',
+ color: THEME_COLORS.primary,
+ marginBottom: 12,
+ },
+ sectionContent: {
+ fontSize: 14,
+ lineHeight: 24,
+ color: colors.text.primary,
+ },
+
+ // 底部版权
+ footer: {
+ alignItems: 'center',
+ },
+ footerText: {
+ fontSize: 13,
+ color: colors.text.hint,
+ textAlign: 'center',
+ marginBottom: 4,
+ },
+ });
+}
+
+export const TermsOfServiceScreen: React.FC = () => {
+ const colors = useAppColors();
+ const styles = useMemo(() => createTermsStyles(colors), [colors]);
+ const insets = useSafeAreaInsets();
+ const { isMobile } = useResponsive();
+ const responsivePadding = useResponsiveSpacing({ xs: 20, sm: 24, md: 28, lg: 32, xl: 40 });
+
+ // 底部间距,避免被 TabBar 遮挡
+ const scrollBottomInset = isMobile ? 64 + 24 + insets.bottom + spacing.md : spacing.lg;
+
+ return (
+
+
+
+ {/* 更新日期 */}
+ 最后更新:{LAST_UPDATED}
+
+ {/* 协议内容 */}
+
+ {TERMS_SECTIONS.map((section, index) => (
+
+ {section.title}
+ {section.content}
+
+ ))}
+
+
+ {/* 底部版权 */}
+
+ © 2026 青春之旅电子信息科技(威海)有限公司
+
+
+
+
+ );
+};
+
+export default TermsOfServiceScreen;
diff --git a/src/screens/profile/index.ts b/src/screens/profile/index.ts
index 5372dcd..85a8cb9 100644
--- a/src/screens/profile/index.ts
+++ b/src/screens/profile/index.ts
@@ -14,6 +14,9 @@ export { default as FollowListScreen } from './FollowListScreen';
export { NotificationSettingsScreen } from './NotificationSettingsScreen';
export { BlockedUsersScreen } from './BlockedUsersScreen';
export { AccountSecurityScreen } from './AccountSecurityScreen';
+export { AboutScreen } from './AboutScreen';
+export { TermsOfServiceScreen } from './TermsOfServiceScreen';
+export { PrivacyPolicyScreen } from './PrivacyPolicyScreen';
// 导出 Hook 供需要自定义的场景使用
export { useUserProfile, createSharedProfileStyles, TABS, TAB_ICONS } from './useUserProfile';
diff --git a/src/screens/schedule/ScheduleScreen.tsx b/src/screens/schedule/ScheduleScreen.tsx
index 11f7e93..89f9101 100644
--- a/src/screens/schedule/ScheduleScreen.tsx
+++ b/src/screens/schedule/ScheduleScreen.tsx
@@ -16,6 +16,7 @@ import {
TextInput,
Alert,
ActivityIndicator,
+ Platform,
} from 'react-native';
import { PanGestureHandler, State } from 'react-native-gesture-handler';
import type { PanGestureHandlerStateChangeEvent } from 'react-native-gesture-handler';
@@ -212,6 +213,13 @@ export const ScheduleScreen: React.FC = () => {
const [syncPassword, setSyncPassword] = useState('');
const [isSyncing, setIsSyncing] = useState(false);
+ useEffect(() => {
+ if ((isAddModalVisible || isSettingsModalVisible || isSyncModalVisible) && Platform.OS === 'web') {
+ const active = (globalThis as any)?.document?.activeElement as { blur?: () => void } | undefined;
+ active?.blur?.();
+ }
+ }, [isAddModalVisible, isSettingsModalVisible, isSyncModalVisible]);
+
// 组件卸载时设置标记
useEffect(() => {
return () => {
diff --git a/src/services/database.ts b/src/services/database.ts
deleted file mode 100644
index 07cb59d..0000000
--- a/src/services/database.ts
+++ /dev/null
@@ -1,767 +0,0 @@
-/**
- * 本地数据库服务
- * 使用 SQLite 存储聊天记录
- * 每个用户使用独立的数据库文件,避免数据串用
- *
- * Web/OPFS 关键设计:
- * - expo-sqlite 在 Web 上使用 OPFS Worker,文件句柄严格唯一
- * - 数据库打开后永不关闭(直到用户切换或登出)
- * - 使用全局单例 + 互斥锁,避免并发打开
- * - React Strict Mode 双次 useEffect 会被幂等处理
- */
-
-import * as SQLite from 'expo-sqlite';
-import type {
- UserDTO,
- GroupResponse,
- GroupMemberResponse,
- ConversationResponse,
- ConversationDetailResponse,
-} from '../types/dto';
-
-let db: SQLite.SQLiteDatabase | null = null;
-let currentDbUserId: string | null = null;
-let writeQueue: Promise = Promise.resolve();
-
-let initPromise: Promise | null = null;
-let initTargetUserId: string | null = null;
-let isInitialized = false;
-
-let dbMutex: Promise = Promise.resolve();
-
-const ensureInitialized = async (): Promise => {
- if (isInitialized && db) return;
- if (initPromise) {
- await initPromise;
- return;
- }
- throw new Error('数据库未初始化');
-};
-
-async function withMutex(fn: () => Promise): Promise {
- const prev = dbMutex;
- let release: () => void = () => {};
- dbMutex = new Promise((r) => { release = r; });
- try {
- await prev;
- return await fn();
- } finally {
- release();
- }
-}
-
-async function openDatabaseWithRetry(dbName: string, maxRetries: number = 3): Promise {
- const isWeb = typeof window !== 'undefined';
-
- if (isWeb) {
- try {
- const tempDb = await SQLite.openDatabaseAsync(dbName);
- await tempDb.closeAsync();
- console.log('[Database] Web: 预打开成功,已释放可能残留的句柄');
- } catch (e) {
- console.warn('[Database] Web: 预打开失败:', String(e));
- }
- await new Promise(r => setTimeout(r, 50));
- }
-
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
- try {
- const database = await SQLite.openDatabaseAsync(dbName);
- return database;
- } catch (error) {
- const msg = String(error);
- console.error(`[Database] 打开失败 (尝试 ${attempt + 1}/${maxRetries + 1}):`, msg);
-
- if (attempt < maxRetries) {
- const delay = isWeb ? 500 * (attempt + 1) : 100;
- console.warn(`[Database] ${delay}ms 后重试...`);
- await new Promise(r => setTimeout(r, delay));
-
- if (msg.includes('Invalid VFS state')) {
- try {
- await SQLite.deleteDatabaseAsync(dbName);
- console.warn('[Database] 已删除损坏的数据库文件');
- } catch {}
- }
- } else {
- throw error;
- }
- }
- }
- throw new Error('打开数据库失败');
-}
-
-async function createTables(database: SQLite.SQLiteDatabase): Promise {
- if (typeof window !== 'undefined') {
- try {
- // 关键:使用 EXCLUSIVE 锁定模式,避免 OPFS 多句柄冲突
- // MEMORY journal 避免 journal 文件创建
- // MEMORY temp_store 避免 temp 文件创建
- await database.execAsync(`PRAGMA locking_mode = EXCLUSIVE;`);
- await database.execAsync(`PRAGMA journal_mode = MEMORY;`);
- await database.execAsync(`PRAGMA temp_store = MEMORY;`);
- console.log('[Database] Web: 已设置 EXCLUSIVE locking + MEMORY journal/temp 模式');
- } catch (e) {
- console.warn('[Database] 设置 PRAGMA 失败:', e);
- }
- }
-
- // 拆分为单独的 execAsync 调用,避免 OPFS 句柄冲突
- await database.execAsync(`CREATE TABLE IF NOT EXISTS messages (
- id TEXT PRIMARY KEY NOT NULL,
- conversationId TEXT NOT NULL,
- senderId TEXT NOT NULL,
- content TEXT,
- type TEXT DEFAULT 'text',
- isRead INTEGER DEFAULT 0,
- createdAt TEXT NOT NULL,
- seq INTEGER DEFAULT 0,
- status TEXT DEFAULT 'normal',
- segments TEXT
- )`);
-
- await database.execAsync(`CREATE TABLE IF NOT EXISTS conversations (
- id TEXT PRIMARY KEY NOT NULL,
- participantId TEXT NOT NULL,
- lastMessageId TEXT,
- lastSeq INTEGER DEFAULT 0,
- unreadCount INTEGER DEFAULT 0,
- createdAt TEXT NOT NULL,
- updatedAt TEXT NOT NULL
- )`);
-
- await database.execAsync(`CREATE TABLE IF NOT EXISTS conversation_cache (
- id TEXT PRIMARY KEY NOT NULL,
- data TEXT NOT NULL,
- updatedAt TEXT NOT NULL
- )`);
-
- await database.execAsync(`CREATE TABLE IF NOT EXISTS conversation_list_cache (
- id TEXT PRIMARY KEY NOT NULL,
- data TEXT NOT NULL,
- updatedAt TEXT NOT NULL
- )`);
-
- await database.execAsync(`CREATE TABLE IF NOT EXISTS users (
- id TEXT PRIMARY KEY NOT NULL,
- data TEXT NOT NULL,
- updatedAt TEXT NOT NULL
- )`);
-
- await database.execAsync(`CREATE TABLE IF NOT EXISTS current_user_cache (
- id TEXT PRIMARY KEY NOT NULL,
- data TEXT NOT NULL,
- updatedAt TEXT NOT NULL
- )`);
-
- await database.execAsync(`CREATE TABLE IF NOT EXISTS groups (
- id TEXT PRIMARY KEY NOT NULL,
- data TEXT NOT NULL,
- updatedAt TEXT NOT NULL
- )`);
-
- await database.execAsync(`CREATE TABLE IF NOT EXISTS group_members (
- groupId TEXT NOT NULL,
- userId TEXT NOT NULL,
- data TEXT NOT NULL,
- updatedAt TEXT NOT NULL,
- PRIMARY KEY (groupId, userId)
- )`);
-
- await database.execAsync(`CREATE INDEX IF NOT EXISTS idx_messages_conversationId ON messages(conversationId)`);
- await database.execAsync(`CREATE INDEX IF NOT EXISTS idx_group_members_groupId ON group_members(groupId)`);
-
- await migrateDatabase(database);
-
- await database.execAsync(`CREATE INDEX IF NOT EXISTS idx_messages_seq ON messages(seq)`);
- await database.execAsync(`CREATE INDEX IF NOT EXISTS idx_messages_conversation_seq ON messages(conversationId, seq)`);
-}
-
-async function migrateDatabase(database: SQLite.SQLiteDatabase): Promise {
- try {
- const tableInfo = await database.getAllAsync(`PRAGMA table_info(messages)`);
- const columns = tableInfo.map(col => col.name);
-
- if (!columns.includes('seq')) {
- await database.execAsync(`ALTER TABLE messages ADD COLUMN seq INTEGER DEFAULT 0`);
- }
- if (!columns.includes('status')) {
- await database.execAsync(`ALTER TABLE messages ADD COLUMN status TEXT DEFAULT 'normal'`);
- }
- if (!columns.includes('segments')) {
- await database.execAsync(`ALTER TABLE messages ADD COLUMN segments TEXT`);
- }
-
- const convTableInfo = await database.getAllAsync(`PRAGMA table_info(conversations)`);
- const convColumns = convTableInfo.map(col => col.name);
- if (!convColumns.includes('lastSeq')) {
- await database.execAsync(`ALTER TABLE conversations ADD COLUMN lastSeq INTEGER DEFAULT 0`);
- }
- } catch (error) {
- console.error('[Database] 迁移失败:', error);
- }
-}
-
-export const initDatabase = async (userId?: string): Promise => {
- const targetUserId = userId || null;
- const callId = Date.now();
-
- console.log(`[Database ${callId}] initDatabase 开始, userId=${targetUserId}, 当前db=${!!db}, 当前userId=${currentDbUserId}`);
-
- if (db && currentDbUserId === targetUserId) {
- console.log(`[Database ${callId}] 已是目标数据库,直接返回`);
- isInitialized = true;
- return;
- }
-
- if (initPromise && initTargetUserId === targetUserId) {
- console.log(`[Database ${callId}] 等待进行中的初始化`);
- await initPromise;
- console.log(`[Database ${callId}] 初始化完成`);
- return;
- }
-
- if (initPromise) {
- console.log(`[Database ${callId}] 等待其他初始化完成`);
- await initPromise.catch(() => {});
- }
-
- if (db && currentDbUserId === targetUserId) {
- console.log(`[Database ${callId}] 其他初始化已完成目标,直接返回`);
- isInitialized = true;
- return;
- }
-
- initTargetUserId = targetUserId;
- console.log(`[Database ${callId}] 开始实际初始化`);
-
- initPromise = withMutex(async () => {
- const mutexId = callId;
- console.log(`[Database ${mutexId}] 获得互斥锁`);
-
- if (db && currentDbUserId === targetUserId) {
- console.log(`[Database ${mutexId}] 互斥锁内检查:已是目标数据库`);
- isInitialized = true;
- return;
- }
-
- const isWeb = typeof window !== 'undefined';
-
- if (db) {
- console.log(`[Database ${mutexId}] 关闭旧数据库连接...`);
- try {
- await db.closeAsync();
- console.log(`[Database ${mutexId}] 旧连接已关闭`);
- } catch (e) {
- console.warn(`[Database ${mutexId}] 关闭旧连接失败:`, e);
- }
- db = null;
- currentDbUserId = null;
- isInitialized = false;
-
- if (isWeb) {
- await new Promise(r => setTimeout(r, 100));
- }
- }
-
- const dbName = targetUserId ? `carrot_bbs_${targetUserId}.db` : 'carrot_bbs.db';
- console.log(`[Database ${mutexId}] 打开数据库: ${dbName}`);
-
- const database = await openDatabaseWithRetry(dbName);
- console.log(`[Database ${mutexId}] 数据库已打开,创建表`);
-
- await createTables(database);
- console.log(`[Database ${mutexId}] 表创建完成`);
-
- db = database;
- currentDbUserId = targetUserId;
- isInitialized = true;
- console.log(`[Database ${mutexId}] 初始化完成`);
- }).finally(() => {
- console.log(`[Database ${callId}] 清理初始化状态`);
- initPromise = null;
- initTargetUserId = null;
- });
-
- await initPromise;
- console.log(`[Database ${callId}] initDatabase 返回`);
-};
-
-export const closeDatabase = async (): Promise => {
- if (initPromise) {
- await initPromise.catch(() => {});
- }
-
- await withMutex(async () => {
- if (!db) return;
-
- try {
- await db.closeAsync();
- console.log('[Database] 数据库已关闭');
- } catch (e) {
- console.warn('[Database] 关闭失败:', e);
- }
- db = null;
- currentDbUserId = null;
- isInitialized = false;
- });
-};
-
-export const getCurrentDbUserId = (): string | null => currentDbUserId;
-
-export const isDbInitialized = (): boolean => isInitialized && db !== null;
-
-export const getDbInstance = (): SQLite.SQLiteDatabase | null => db;
-
-const getDb = async (timeout: number = 10000): Promise => {
- if (isInitialized && db) return db;
-
- if (initPromise) {
- const timeoutPromise = new Promise((_, reject) =>
- setTimeout(() => reject(new Error('等待数据库初始化超时')), timeout)
- );
-
- await Promise.race([initPromise, timeoutPromise]);
-
- if (db) return db;
- }
-
- throw new Error('数据库未初始化,请先调用 initDatabase(userId)');
-};
-
-const enqueueWrite = async (op: (db: SQLite.SQLiteDatabase) => Promise): Promise => {
- let result: T;
- const wrapped = async () => {
- result = await withMutex(async () => {
- const database = await getDb();
- return op(database);
- });
- };
- const queued = writeQueue.then(wrapped, wrapped);
- writeQueue = queued.then(() => {}, () => {});
- await queued;
- return result!;
-};
-
-const withDbRead = async (op: (db: SQLite.SQLiteDatabase) => Promise): Promise => {
- return withMutex(async () => {
- const database = await getDb();
- return op(database);
- });
-};
-
-export interface CachedMessage {
- id: string;
- conversationId: string;
- senderId: string;
- content?: string;
- type?: string;
- isRead: boolean;
- createdAt: string;
- seq: number;
- status: string;
- segments?: any;
-}
-
-export const saveMessage = async (message: CachedMessage): Promise => {
- await enqueueWrite(async (database) => {
- await database.runAsync(
- `INSERT OR REPLACE INTO messages (id, conversationId, senderId, content, type, isRead, createdAt, seq, status, segments)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
- [message.id, message.conversationId, message.senderId, message.content || '', message.type || 'text',
- message.isRead ? 1 : 0, message.createdAt, message.seq || 0, message.status || 'normal',
- message.segments ? JSON.stringify(message.segments) : null]
- );
- });
-};
-
-export const saveMessagesBatch = async (messages: CachedMessage[]): Promise => {
- if (!messages?.length) return;
- await enqueueWrite(async (database) => {
- for (const msg of messages) {
- await database.runAsync(
- `INSERT OR REPLACE INTO messages (id, conversationId, senderId, content, type, isRead, createdAt, seq, status, segments)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
- [msg.id, msg.conversationId, msg.senderId, msg.content || '', msg.type || 'text',
- msg.isRead ? 1 : 0, msg.createdAt, msg.seq || 0, msg.status || 'normal',
- msg.segments ? JSON.stringify(msg.segments) : null]
- );
- }
- });
-};
-
-export const getMessagesByConversation = async (conversationId: string, limit: number = 20): Promise => {
- return withDbRead(async (database) => {
- const rows = await database.getAllAsync(
- `SELECT * FROM messages WHERE conversationId = ? ORDER BY seq DESC LIMIT ?`,
- [conversationId, limit]
- );
- return rows.reverse().map(r => ({
- ...r, isRead: r.isRead === 1, segments: r.segments ? JSON.parse(r.segments) : undefined
- }));
- });
-};
-
-export const getMaxSeq = async (conversationId: string): Promise => {
- return withDbRead(async (database) => {
- const r = await database.getFirstAsync<{ maxSeq: number }>(
- `SELECT MAX(seq) as maxSeq FROM messages WHERE conversationId = ?`, [conversationId]
- );
- return r?.maxSeq || 0;
- });
-};
-
-export const getMinSeq = async (conversationId: string): Promise => {
- return withDbRead(async (database) => {
- const r = await database.getFirstAsync<{ minSeq: number }>(
- `SELECT MIN(seq) as minSeq FROM messages WHERE conversationId = ?`, [conversationId]
- );
- return r?.minSeq || 0;
- });
-};
-
-export const getMessagesBeforeSeq = async (conversationId: string, beforeSeq: number, limit: number = 20): Promise => {
- return withDbRead(async (database) => {
- const rows = await database.getAllAsync(
- `SELECT * FROM messages WHERE conversationId = ? AND seq < ? ORDER BY seq DESC LIMIT ?`,
- [conversationId, beforeSeq, limit]
- );
- return rows.map(r => ({ ...r, isRead: r.isRead === 1, segments: r.segments ? JSON.parse(r.segments) : undefined })).reverse();
- });
-};
-
-export const getLocalMessageCountBeforeSeq = async (conversationId: string, beforeSeq: number): Promise => {
- return withDbRead(async (database) => {
- const r = await database.getFirstAsync<{ count: number }>(
- `SELECT COUNT(*) as count FROM messages WHERE conversationId = ? AND seq < ?`, [conversationId, beforeSeq]
- );
- return r?.count || 0;
- });
-};
-
-export const getMessageCount = async (conversationId: string): Promise => {
- return withDbRead(async (database) => {
- const r = await database.getFirstAsync<{ count: number }>(
- `SELECT COUNT(*) as count FROM messages WHERE conversationId = ?`, [conversationId]
- );
- return r?.count || 0;
- });
-};
-
-export const markMessageAsRead = async (messageId: string): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`UPDATE messages SET isRead = 1 WHERE id = ?`, [messageId]);
- });
-};
-
-export const markConversationAsRead = async (conversationId: string): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`UPDATE messages SET isRead = 1 WHERE conversationId = ?`, [conversationId]);
- });
-};
-
-export const deleteMessage = async (messageId: string): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`DELETE FROM messages WHERE id = ?`, [messageId]);
- });
-};
-
-export const updateMessageStatus = async (messageId: string, status: string, clearContent: boolean = false): Promise => {
- await enqueueWrite(async (db) => {
- if (clearContent) {
- await db.runAsync(`UPDATE messages SET status = ?, content = '', segments = '[]' WHERE id = ?`, [status, messageId]);
- } else {
- await db.runAsync(`UPDATE messages SET status = ? WHERE id = ?`, [status, messageId]);
- }
- });
-};
-
-export const clearConversationMessages = async (conversationId: string): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`DELETE FROM messages WHERE conversationId = ?`, [conversationId]);
- });
-};
-
-export const saveConversation = async (conv: {
- id: string; participantId: string; lastMessageId?: string; lastSeq?: number; unreadCount?: number;
- createdAt: string; updatedAt: string;
-}): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(
- `INSERT OR REPLACE INTO conversations (id, participantId, lastMessageId, lastSeq, unreadCount, createdAt, updatedAt)
- VALUES (?, ?, ?, ?, ?, ?, ?)`,
- [conv.id, conv.participantId, conv.lastMessageId || null, conv.lastSeq || 0, conv.unreadCount || 0, conv.createdAt, conv.updatedAt]
- );
- });
-};
-
-export const getAllConversations = async (): Promise => {
- return withDbRead(async (db) => db.getAllAsync(`SELECT * FROM conversations ORDER BY updatedAt DESC`));
-};
-
-export const updateConversationUnreadCount = async (id: string, count: number): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`UPDATE conversations SET unreadCount = ? WHERE id = ?`, [count, id]);
- });
-};
-
-export const updateConversationLastMessage = async (id: string, lastMessageId: string, lastSeq: number, updatedAt: string): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`UPDATE conversations SET lastMessageId = ?, lastSeq = ?, updatedAt = ? WHERE id = ?`,
- [lastMessageId, lastSeq, updatedAt, id]);
- });
-};
-
-export const deleteConversation = async (conversationId: string): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`DELETE FROM messages WHERE conversationId = ?`, [conversationId]);
- await db.runAsync(`DELETE FROM conversations WHERE id = ?`, [conversationId]);
- });
-};
-
-export const searchMessages = async (keyword: string): Promise => {
- return withDbRead(async (db) => {
- const rows = await db.getAllAsync(
- `SELECT * FROM messages WHERE content LIKE ? ORDER BY createdAt DESC LIMIT 50`, [`%${keyword}%`]
- );
- return rows.map(r => ({ ...r, isRead: r.isRead === 1, segments: r.segments ? JSON.parse(r.segments) : undefined }));
- });
-};
-
-export const getDatabaseStats = async (): Promise<{ totalMessages: number; totalConversations: number }> => {
- return withDbRead(async (db) => {
- const msgCount = await db.getFirstAsync<{ count: number }>(`SELECT COUNT(*) as count FROM messages`);
- const convCount = await db.getFirstAsync<{ count: number }>(`SELECT COUNT(*) as count FROM conversations`);
- return { totalMessages: msgCount?.count || 0, totalConversations: convCount?.count || 0 };
- });
-};
-
-export const clearAllData = async (): Promise => {
- await enqueueWrite(async (db) => {
- await db.execAsync(`DELETE FROM messages`);
- await db.execAsync(`DELETE FROM conversations`);
- await db.execAsync(`DELETE FROM conversation_cache`);
- await db.execAsync(`DELETE FROM conversation_list_cache`);
- await db.execAsync(`DELETE FROM users`);
- await db.execAsync(`DELETE FROM current_user_cache`);
- await db.execAsync(`DELETE FROM groups`);
- await db.execAsync(`DELETE FROM group_members`);
- });
-};
-
-const safeParseJson = (value: string): T | null => {
- try { return JSON.parse(value) as T; } catch { return null; }
-};
-
-export const saveUserCache = async (user: UserDTO): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`INSERT OR REPLACE INTO users (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(user.id), JSON.stringify(user), new Date().toISOString()]);
- });
-};
-
-export const saveUsersCache = async (users: UserDTO[]): Promise => {
- if (!users?.length) return;
- await enqueueWrite(async (db) => {
- const now = new Date().toISOString();
- for (const u of users) {
- await db.runAsync(`INSERT OR REPLACE INTO users (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(u.id), JSON.stringify(u), now]);
- }
- });
-};
-
-export const getUserCache = async (userId: string): Promise => {
- return withDbRead(async (db) => {
- const r = await db.getFirstAsync<{ data: string }>(`SELECT data FROM users WHERE id = ?`, [String(userId)]);
- return r?.data ? safeParseJson(r.data) : null;
- });
-};
-
-export const getLatestUserCache = async (): Promise => {
- return withDbRead(async (db) => {
- const r = await db.getFirstAsync<{ data: string }>(`SELECT data FROM users ORDER BY updatedAt DESC LIMIT 1`);
- return r?.data ? safeParseJson(r.data) : null;
- });
-};
-
-export const saveCurrentUserCache = async (user: UserDTO): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`INSERT OR REPLACE INTO current_user_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
- ['me', JSON.stringify(user), new Date().toISOString()]);
- });
-};
-
-export const getCurrentUserCache = async (): Promise => {
- return withDbRead(async (db) => {
- const r = await db.getFirstAsync<{ data: string }>(`SELECT data FROM current_user_cache WHERE id = 'me'`);
- return r?.data ? safeParseJson(r.data) : null;
- });
-};
-
-export const clearCurrentUserCache = async (): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`DELETE FROM current_user_cache WHERE id = 'me'`);
- });
-};
-
-export const saveGroupCache = async (group: GroupResponse): Promise => {
- await enqueueWrite(async (db) => {
- await db.runAsync(`INSERT OR REPLACE INTO groups (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(group.id), JSON.stringify(group), new Date().toISOString()]);
- });
-};
-
-export const saveGroupsCache = async (groups: GroupResponse[]): Promise => {
- if (!groups?.length) return;
- await enqueueWrite(async (db) => {
- const now = new Date().toISOString();
- for (const g of groups) {
- await db.runAsync(`INSERT OR REPLACE INTO groups (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(g.id), JSON.stringify(g), now]);
- }
- });
-};
-
-export const getGroupCache = async (groupId: string): Promise => {
- return withDbRead(async (db) => {
- const r = await db.getFirstAsync<{ data: string }>(`SELECT data FROM groups WHERE id = ?`, [String(groupId)]);
- return r?.data ? safeParseJson(r.data) : null;
- });
-};
-
-export const getAllGroupsCache = async (): Promise => {
- return withDbRead(async (db) => {
- const rows = await db.getAllAsync<{ data: string }>(`SELECT data FROM groups ORDER BY updatedAt DESC`);
- return rows.map(r => safeParseJson(r.data)).filter((g): g is GroupResponse => Boolean(g));
- });
-};
-
-export const saveGroupMembersCache = async (groupId: string, members: GroupMemberResponse[]): Promise => {
- await enqueueWrite(async (db) => {
- const now = new Date().toISOString();
- await db.runAsync(`DELETE FROM group_members WHERE groupId = ?`, [String(groupId)]);
- for (const m of members) {
- await db.runAsync(`INSERT OR REPLACE INTO group_members (groupId, userId, data, updatedAt) VALUES (?, ?, ?, ?)`,
- [String(groupId), String(m.user_id), JSON.stringify(m), now]);
- }
- });
-};
-
-export const getGroupMembersCache = async (groupId: string): Promise => {
- return withDbRead(async (db) => {
- const rows = await db.getAllAsync<{ data: string }>(
- `SELECT data FROM group_members WHERE groupId = ? ORDER BY updatedAt DESC`, [String(groupId)]
- );
- return rows.map(r => safeParseJson(r.data)).filter((m): m is GroupMemberResponse => Boolean(m));
- });
-};
-
-type ConversationCacheData = ConversationResponse | ConversationDetailResponse;
-
-export const saveConversationCache = async (conv: ConversationCacheData): Promise => {
- await enqueueWrite(async (db) => {
- const updatedAt = ('updated_at' in conv && conv.updated_at) ? String(conv.updated_at) : new Date().toISOString();
- await db.runAsync(`INSERT OR REPLACE INTO conversation_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(conv.id), JSON.stringify(conv), updatedAt]);
- });
-};
-
-export const saveConversationsCache = async (convs: ConversationResponse[]): Promise => {
- if (!convs?.length) return;
- await enqueueWrite(async (db) => {
- for (const c of convs) {
- await db.runAsync(`INSERT OR REPLACE INTO conversation_list_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(c.id), JSON.stringify(c), c.updated_at || new Date().toISOString()]);
- }
- });
-};
-
-export const getConversationCache = async (id: string): Promise => {
- return withDbRead(async (db) => {
- const r = await db.getFirstAsync<{ data: string }>(`SELECT data FROM conversation_cache WHERE id = ?`, [String(id)]);
- return r?.data ? safeParseJson(r.data) : null;
- });
-};
-
-export const getConversationListCache = async (): Promise => {
- return withDbRead(async (db) => {
- let rows = await db.getAllAsync<{ data: string }>(`SELECT data FROM conversation_list_cache ORDER BY updatedAt DESC`);
- if (!rows.length) {
- rows = await db.getAllAsync<{ data: string }>(`SELECT data FROM conversation_cache ORDER BY updatedAt DESC`);
- }
- const list = rows.map(r => safeParseJson(r.data))
- .filter((c): c is ConversationResponse => Boolean(c))
- .filter(c => typeof c.id !== 'undefined' && typeof c.type !== 'undefined');
- return list.sort((a, b) => {
- const aPinned = a.is_pinned ? 1 : 0;
- const bPinned = b.is_pinned ? 1 : 0;
- if (aPinned !== bPinned) return bPinned - aPinned;
- const aTime = new Date(a.last_message_at || a.updated_at || 0).getTime();
- const bTime = new Date(b.last_message_at || b.updated_at || 0).getTime();
- return bTime - aTime;
- });
- });
-};
-
-export const updateConversationListCacheEntry = async (id: string, updates: Partial): Promise => {
- await enqueueWrite(async (db) => {
- const r = await db.getFirstAsync<{ data: string }>(`SELECT data FROM conversation_list_cache WHERE id = ?`, [String(id)]);
- if (!r?.data) return;
- const conv = safeParseJson(r.data);
- if (!conv) return;
- const updated = { ...conv, ...updates };
- const updatedAt = updates.last_message_at || updated.last_message_at || new Date().toISOString();
- await db.runAsync(`UPDATE conversation_list_cache SET data = ?, updatedAt = ? WHERE id = ?`,
- [JSON.stringify(updated), updatedAt, String(id)]);
- });
-};
-
-export const updateConversationCacheUnreadCount = async (id: string, count: number, myLastReadSeq?: number): Promise => {
- await enqueueWrite(async (db) => {
- const listRow = await db.getFirstAsync<{ data: string }>(`SELECT data FROM conversation_list_cache WHERE id = ?`, [String(id)]);
- if (listRow?.data) {
- const conv = safeParseJson(listRow.data);
- if (conv) {
- conv.unread_count = count;
- if (typeof myLastReadSeq === 'number' && !Number.isNaN(myLastReadSeq)) conv.my_last_read_seq = myLastReadSeq;
- await db.runAsync(`UPDATE conversation_list_cache SET data = ? WHERE id = ?`, [JSON.stringify(conv), String(id)]);
- }
- }
-
- const cacheRow = await db.getFirstAsync<{ data: string }>(`SELECT data FROM conversation_cache WHERE id = ?`, [String(id)]);
- if (cacheRow?.data) {
- const conv = safeParseJson>(cacheRow.data);
- if (conv) {
- conv.unread_count = count;
- if (typeof myLastReadSeq === 'number' && !Number.isNaN(myLastReadSeq)) conv.my_last_read_seq = myLastReadSeq;
- await db.runAsync(`UPDATE conversation_cache SET data = ? WHERE id = ?`, [JSON.stringify(conv), String(id)]);
- }
- }
- });
-};
-
-export const saveConversationsWithRelatedCache = async (
- convs: ConversationResponse[], users?: UserDTO[], groups?: GroupResponse[]
-): Promise => {
- if (!convs?.length) return;
- const now = new Date().toISOString();
- await enqueueWrite(async (db) => {
- for (const c of convs) {
- await db.runAsync(`INSERT OR REPLACE INTO conversation_list_cache (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(c.id), JSON.stringify(c), c.updated_at || now]);
- }
- if (users?.length) {
- for (const u of users) {
- await db.runAsync(`INSERT OR REPLACE INTO users (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(u.id), JSON.stringify(u), now]);
- }
- }
- if (groups?.length) {
- for (const g of groups) {
- await db.runAsync(`INSERT OR REPLACE INTO groups (id, data, updatedAt) VALUES (?, ?, ?)`,
- [String(g.id), JSON.stringify(g), now]);
- }
- }
- });
-};
\ No newline at end of file
diff --git a/src/services/messageService.ts b/src/services/messageService.ts
index 78c864b..893e79e 100644
--- a/src/services/messageService.ts
+++ b/src/services/messageService.ts
@@ -22,14 +22,7 @@ import {
CursorPaginationRequest,
CursorPaginationResponse,
} from '../types/dto';
-import {
- getConversationCache,
- getConversationListCache,
- saveConversationCache,
- saveConversationsWithRelatedCache,
- updateConversationCacheUnreadCount,
- saveUsersCache,
-} from './database';
+import { conversationRepository, userCacheRepository } from '@/database';
/** 远端会话列表分页(offset / cursor)统一结果:拉取成功后均已执行本地缓存同步 */
export interface RemoteConversationListPageResult {
@@ -55,7 +48,7 @@ class MessageService {
const groups = list
.map(conv => conv.group)
.filter((group): group is NonNullable => Boolean(group));
- await saveConversationsWithRelatedCache(list, users, groups);
+ await conversationRepository.saveWithRelated(list, users, groups);
}
/** offset 会话列表单页:请求 + 落库 */
@@ -152,12 +145,12 @@ class MessageService {
const response = await api.get(
`/conversations/${encodeURIComponent(id)}`
);
- await saveConversationCache(response.data);
+ await conversationRepository.saveCache(response.data);
if (response.data.participants?.length) {
- await saveUsersCache(response.data.participants);
+ await userCacheRepository.saveBatch(response.data.participants);
}
if (response.data.last_message?.sender) {
- await saveUsersCache([response.data.last_message.sender]);
+ await userCacheRepository.save(response.data.last_message.sender);
}
return response.data;
}
@@ -195,7 +188,7 @@ class MessageService {
): Promise {
// 如果强制刷新,直接从服务器获取,不使用缓存
if (!forceRefresh) {
- const cachedList = await getConversationListCache();
+ const cachedList = await conversationRepository.getListCache();
if (cachedList.length > 0) {
// 后台刷新数据,但不阻塞当前返回
this.refreshConversationsFromServer(page, pageSize).catch(error => {
@@ -235,9 +228,9 @@ class MessageService {
const response = await api.post('/conversations', {
user_id: userId,
});
- await saveConversationCache(response.data);
+ await conversationRepository.saveCache(response.data);
if (response.data.participants?.length) {
- await saveUsersCache(response.data.participants);
+ await userCacheRepository.saveBatch(response.data.participants);
}
return response.data;
}
@@ -247,7 +240,7 @@ class MessageService {
* GET /api/v1/conversations/:id
*/
async getConversationById(id: string): Promise {
- const cached = await getConversationCache(id);
+ const cached = await conversationRepository.getCache(id);
if (cached) {
this.refreshConversationDetailFromServer(id).catch(error => {
console.error('后台刷新会话详情失败:', error);
@@ -564,7 +557,7 @@ class MessageService {
last_read_seq: Number(seq),
});
// 立即清零本地缓存中的 unread_count,并写入已读游标,避免冷启动仍显示旧红点
- updateConversationCacheUnreadCount(conversationId, 0, Number(seq)).catch(error => {
+ conversationRepository.updateCacheUnreadCount(conversationId, 0, Number(seq)).catch(error => {
console.error('更新本地会话未读数失败:', error);
});
}
diff --git a/src/stores/authStore.ts b/src/stores/authStore.ts
index 687aceb..a512332 100644
--- a/src/stores/authStore.ts
+++ b/src/stores/authStore.ts
@@ -20,10 +20,10 @@ import { useSessionStore } from './sessionStore';
import {
initDatabase,
closeDatabase,
- saveUserCache,
- saveCurrentUserCache,
- clearCurrentUserCache,
-} from '../services/database';
+ isDbInitialized,
+ getCurrentDbUserId,
+} from '@/database';
+import { userCacheRepository } from '@/database';
// AsyncStorage 中保存已登录用户 ID 的 key
const USER_ID_KEY = 'auth_user_id';
@@ -54,8 +54,8 @@ async function clearUserId(): Promise {
// ── 写用户缓存(DB 已就绪后调用)──
async function cacheUser(user: User): Promise {
try {
- await saveUserCache(user);
- await saveCurrentUserCache(user);
+ await userCacheRepository.save(user);
+ await userCacheRepository.saveCurrent(user);
} catch (err) {
console.warn('[AuthStore] 写用户缓存失败:', err);
}
@@ -225,7 +225,7 @@ export const useAuthStore = create((set) => ({
// 3. 重置通话状态(清理所有事件订阅和定时器)
callStore.getState().reset();
// 4. 清除 DB 中的用户缓存(DB 此时一定已初始化)
- await clearCurrentUserCache().catch(() => {});
+ await userCacheRepository.clearCurrent().catch(() => {});
// 5. 关闭数据库连接
await closeDatabase();
// 6. 清除持久化的 userId
@@ -262,7 +262,7 @@ export const useAuthStore = create((set) => ({
try {
await initDatabase(savedUserId);
} catch (dbErr) {
- console.warn('[AuthStore] DB 预初始化失败,将尝试在 API 成功后重新初始化:', dbErr);
+ console.warn('[AuthStore] DB 预初始化失败,将在 API 成功后重试:', dbErr);
}
}
@@ -272,16 +272,21 @@ export const useAuthStore = create((set) => ({
if (user) {
const userId = String(user.id);
- // 4. 若 userId 发生变化(极少数情况),重新初始化 DB 并更新持久化
- if (!savedUserId || savedUserId !== userId) {
+ // 4. 确保 DB 已初始化(如果预初始化失败,这里会重试)
+ const dbReady = isDbInitialized() && getCurrentDbUserId() === userId;
+ if (!dbReady) {
await initDatabase(userId);
+ }
+
+ // 5. 更新持久化的 userId
+ if (!savedUserId || savedUserId !== userId) {
await saveUserId(userId);
}
- // 5. DB 已就绪,更新用户缓存
+ // 6. DB 已就绪,更新用户缓存
await cacheUser(user);
- // 6. 同步 userId 到 sessionStore
+ // 7. 同步 userId 到 sessionStore
useSessionStore.getState().setUserId(userId);
set({
@@ -290,7 +295,7 @@ export const useAuthStore = create((set) => ({
isLoading: false,
});
- // 7. 启动 SSE
+ // 8. 启动 SSE
await startRealtime();
} else {
// Token 已失效或不存在
diff --git a/src/stores/chatSettingsStore.ts b/src/stores/chatSettingsStore.ts
index f92fcea..0b9bf06 100644
--- a/src/stores/chatSettingsStore.ts
+++ b/src/stores/chatSettingsStore.ts
@@ -81,6 +81,132 @@ export const CHAT_THEMES = [
textPrimary: '#1A1A1A',
textSecondary: '#666666',
},
+ {
+ id: 'coral',
+ name: '珊瑚橙',
+ primary: '#FF6B6B',
+ secondary: '#FFF0F0',
+ bubble: '#FFD4D4',
+ icon: '🐠',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#FFF5F5',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'rose',
+ name: '玫瑰粉',
+ primary: '#E91E63',
+ secondary: '#FCE4EC',
+ bubble: '#F8BBD9',
+ icon: '🌹',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#FCE4EC',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'indigo',
+ name: '靛蓝紫',
+ primary: '#3F51B5',
+ secondary: '#E8EAF6',
+ bubble: '#C5CAE9',
+ icon: '🐉',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#E8EAF6',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'mint',
+ name: '薄荷绿',
+ primary: '#00BCD4',
+ secondary: '#E0F7FA',
+ bubble: '#B2EBF2',
+ icon: '🧊',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#E0F7FA',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'cherry',
+ name: '樱桃红',
+ primary: '#D32F2F',
+ secondary: '#FFEBEE',
+ bubble: '#FFCDD2',
+ icon: '🍒',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#FFEBEE',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'lavender',
+ name: '薰衣草',
+ primary: '#7E57C2',
+ secondary: '#EDE7F6',
+ bubble: '#D1C4E9',
+ icon: '💜',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#EDE7F6',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'sunset',
+ name: '落日橙',
+ primary: '#FF5722',
+ secondary: '#FBE9E7',
+ bubble: '#FFCCBC',
+ icon: '🌅',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#FBE9E7',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'ocean',
+ name: '深海蓝',
+ primary: '#006064',
+ secondary: '#B2EBF2',
+ bubble: '#80DEEA',
+ icon: '🌊',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#E0F7FA',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
+ {
+ id: 'forest',
+ name: '森林绿',
+ primary: '#1B5E20',
+ secondary: '#C8E6C9',
+ bubble: '#A5D6A7',
+ icon: '🌳',
+ card: '#FFFFFF',
+ inputBg: '#FFFFFF',
+ panelBg: '#E8F5E9',
+ headerBg: '#FFFFFF',
+ textPrimary: '#1A1A1A',
+ textSecondary: '#666666',
+ },
] as const;
export type ChatThemeId = typeof CHAT_THEMES[number]['id'];
diff --git a/src/stores/conversationListSources.ts b/src/stores/conversationListSources.ts
index d091065..a1d5e14 100644
--- a/src/stores/conversationListSources.ts
+++ b/src/stores/conversationListSources.ts
@@ -4,7 +4,7 @@
import { ConversationResponse } from '../types/dto';
import { messageService } from '../services/messageService';
-import { getConversationListCache } from '../services/database';
+import { conversationRepository } from '@/database';
export const CONVERSATION_LIST_PAGE_SIZE = 20;
@@ -94,7 +94,7 @@ export class SqliteConversationListPagedSource implements IConversationListPaged
}
this.consumed = true;
try {
- const items = await getConversationListCache();
+ const items = await conversationRepository.getListCache();
return { items, hasMore: false };
} catch (e) {
console.warn('[SqliteConversationListPagedSource] 读取会话列表缓存失败:', e);
diff --git a/src/stores/group/GroupManager.ts b/src/stores/group/GroupManager.ts
index 0121344..502441e 100644
--- a/src/stores/group/GroupManager.ts
+++ b/src/stores/group/GroupManager.ts
@@ -1,10 +1,3 @@
-/**
- * GroupManager - 群组管理核心模块(重构版?
- *
- * 使用 Zustand store 进行状态管?
- * 保持与旧 API 的向后兼?
- */
-
import type {
GroupListResponse,
GroupMemberListResponse,
@@ -12,15 +5,7 @@ import type {
GroupResponse,
} from '../../types/dto';
import { groupService } from '../../services/groupService';
-import {
- getAllGroupsCache,
- getGroupCache,
- getGroupMembersCache,
- saveGroupCache,
- saveGroupMembersCache,
- saveGroupsCache,
- saveUsersCache,
-} from '../../services/database';
+import { groupCacheRepository, userCacheRepository } from '@/database';
import { useGroupManagerStore } from './groupStore';
import { createCacheEntry, isCacheExpired, DEFAULT_TTL } from '../utils/cache';
import {
@@ -33,8 +18,6 @@ import {
GROUP_MEMBER_LIST_PAGE_SIZE,
} from '../groupListSources';
-// ==================== 辅助函数 ====================
-
function buildMembersKey(groupId: string, page: number, pageSize: number): string {
return `members:${groupId}:${page}:${pageSize}`;
}
@@ -63,14 +46,7 @@ function buildMemberListResponse(
};
}
-// ==================== GroupManager ?====================
-
class GroupManager {
- // ==================== 群组列表相关 ====================
-
- /**
- * 获取群组列表
- */
async getGroups(
page = 1,
pageSize = GROUP_LIST_PAGE_SIZE,
@@ -79,20 +55,17 @@ class GroupManager {
const store = useGroupManagerStore.getState();
const entry = store.groupsListEntry;
- // 第一页且有有效缓?
if (page === 1 && !forceRefresh && entry && !isCacheExpired(entry)) {
return buildGroupListResponse(entry.data, pageSize);
}
- // 第一页且缓存过期:后台刷?
if (page === 1 && !forceRefresh && entry && isCacheExpired(entry)) {
this.refreshGroupsInBackground(page, pageSize);
return buildGroupListResponse(entry.data, pageSize);
}
- // 第一页:尝试本地缓存
if (page === 1 && !forceRefresh) {
- const localGroups = await getAllGroupsCache();
+ const localGroups = await groupCacheRepository.getAll();
if (localGroups.length > 0) {
const newEntry = createCacheEntry(localGroups, DEFAULT_TTL.GROUP);
store.setGroupsListEntry(newEntry);
@@ -101,7 +74,6 @@ class GroupManager {
}
}
- // 发起请求
return this.dedupe(`groups:list:${page}:${pageSize}`, async () => {
const store = useGroupManagerStore.getState();
const response = await groupService.getGroups(page, pageSize);
@@ -109,7 +81,7 @@ class GroupManager {
if (page === 1) {
store.setGroupsList(groups);
}
- await saveGroupsCache(groups).catch(() => {});
+ await groupCacheRepository.saveBatch(groups).catch(() => {});
return response;
});
}
@@ -122,16 +94,13 @@ class GroupManager {
if (page === 1) {
store.setGroupsList(groups);
}
- saveGroupsCache(groups).catch(() => {});
+ groupCacheRepository.saveBatch(groups).catch(() => {});
return response;
}).catch((error) => {
console.error('[GroupManager] refreshGroupsInBackground error:', error);
});
}
- /**
- * 创建群组列表数据源(用于 Sources 模式?
- */
createGroupListSource(
kind: RemoteGroupListSourceKind = 'cursor',
pageSize: number = GROUP_LIST_PAGE_SIZE
@@ -139,29 +108,21 @@ class GroupManager {
return createRemoteGroupListSource(kind, pageSize);
}
- // ==================== 群组详情相关 ====================
-
- /**
- * 获取群组详情
- */
async getGroup(groupId: string, forceRefresh = false): Promise {
const store = useGroupManagerStore.getState();
const entry = store.groupsMap.get(groupId);
- // 有效缓存
if (!forceRefresh && entry && !isCacheExpired(entry)) {
return entry.data;
}
- // 过期缓存:后台刷?
if (!forceRefresh && entry && isCacheExpired(entry)) {
this.refreshGroupInBackground(groupId);
return entry.data;
}
- // 尝试本地缓存
if (!forceRefresh) {
- const localGroup = await getGroupCache(groupId);
+ const localGroup = await groupCacheRepository.get(groupId);
if (localGroup) {
store.setGroup(groupId, localGroup);
this.refreshGroupInBackground(groupId);
@@ -169,12 +130,11 @@ class GroupManager {
}
}
- // 发起请求
return this.dedupe(`groups:detail:${groupId}`, async () => {
const store = useGroupManagerStore.getState();
const group = await groupService.getGroup(groupId);
store.setGroup(groupId, group);
- await saveGroupCache(group).catch(() => {});
+ await groupCacheRepository.save(group).catch(() => {});
return group;
});
}
@@ -184,18 +144,13 @@ class GroupManager {
const store = useGroupManagerStore.getState();
const group = await groupService.getGroup(groupId);
store.setGroup(groupId, group);
- saveGroupCache(group).catch(() => {});
+ groupCacheRepository.save(group).catch(() => {});
return group;
}).catch((error) => {
console.error('[GroupManager] refreshGroupInBackground error:', error);
});
}
- // ==================== 群组成员相关 ====================
-
- /**
- * 获取群组成员列表
- */
async getMembers(
groupId: string,
page = 1,
@@ -206,20 +161,17 @@ class GroupManager {
const store = useGroupManagerStore.getState();
const entry = store.membersMap.get(key);
- // 有效缓存
if (!forceRefresh && entry && !isCacheExpired(entry)) {
return buildMemberListResponse(entry.data, page, pageSize);
}
- // 过期缓存:后台刷?
if (!forceRefresh && entry && isCacheExpired(entry)) {
this.refreshMembersInBackground(groupId, page, pageSize);
return buildMemberListResponse(entry.data, page, pageSize);
}
- // 第一页:尝试本地缓存
if (page === 1 && !forceRefresh) {
- const localMembers = await getGroupMembersCache(groupId);
+ const localMembers = await groupCacheRepository.getMembers(groupId);
if (localMembers.length > 0) {
store.setMembers(groupId, localMembers, page, pageSize);
this.refreshMembersInBackground(groupId, page, pageSize);
@@ -227,16 +179,15 @@ class GroupManager {
}
}
- // 发起请求
return this.dedupe(`groups:members:${key}`, async () => {
const store = useGroupManagerStore.getState();
const response = await groupService.getMembers(groupId, page, pageSize);
const members = response.list || [];
store.setMembers(groupId, members, page, pageSize);
if (page === 1) {
- await saveGroupMembersCache(groupId, members).catch(() => {});
+ await groupCacheRepository.saveMembers(groupId, members).catch(() => {});
}
- await saveUsersCache(
+ await userCacheRepository.saveBatch(
members
.map((member) => member.user)
.filter((user): user is NonNullable => Boolean(user))
@@ -257,9 +208,9 @@ class GroupManager {
const members = response.list || [];
store.setMembers(groupId, members, page, pageSize);
if (page === 1) {
- saveGroupMembersCache(groupId, members).catch(() => {});
+ groupCacheRepository.saveMembers(groupId, members).catch(() => {});
}
- saveUsersCache(
+ userCacheRepository.saveBatch(
members
.map((member) => member.user)
.filter((user): user is NonNullable => Boolean(user))
@@ -270,9 +221,6 @@ class GroupManager {
});
}
- /**
- * 创建群组成员列表数据源(用于 Sources 模式?
- */
createGroupMemberListSource(
groupId: string,
kind: RemoteGroupListSourceKind = 'cursor',
@@ -281,11 +229,6 @@ class GroupManager {
return createRemoteGroupMemberListSource(groupId, kind, pageSize);
}
- // ==================== 缓存管理 ====================
-
- /**
- * 使缓存失?
- */
invalidate(groupId?: string): void {
if (!groupId) {
useGroupManagerStore.getState().invalidateAll();
@@ -294,15 +237,10 @@ class GroupManager {
useGroupManagerStore.getState().invalidateGroup(groupId);
}
- /**
- * 清除所有数?
- */
clear(): void {
useGroupManagerStore.getState().reset();
}
- // ==================== 请求去重 ====================
-
private async dedupe(key: string, fetcher: () => Promise): Promise {
const store = useGroupManagerStore.getState();
const pending = store.getPendingRequest(key);
@@ -316,11 +254,8 @@ class GroupManager {
}
}
-// ==================== 单例导出 ====================
-
export const groupManager = new GroupManager();
-// 导出类以支持类型检查和测试
export { GroupManager };
-export default groupManager;
+export default groupManager;
\ No newline at end of file
diff --git a/src/stores/groupListSources.ts b/src/stores/groupListSources.ts
index 0c7879b..e701f74 100644
--- a/src/stores/groupListSources.ts
+++ b/src/stores/groupListSources.ts
@@ -10,7 +10,7 @@ import {
CursorPaginationRequest,
} from '../types/dto';
import { groupService } from '../services/groupService';
-import { getAllGroupsCache, getGroupMembersCache } from '../services/database';
+import { groupCacheRepository } from '@/database';
export const GROUP_LIST_PAGE_SIZE = 20;
export const GROUP_MEMBER_LIST_PAGE_SIZE = 50;
@@ -130,7 +130,7 @@ export class SqliteGroupListPagedSource implements IGroupListPagedSource {
}
this.consumed = true;
try {
- const items = await getAllGroupsCache();
+ const items = await groupCacheRepository.getAll();
return { items, hasMore: false };
} catch (e) {
console.warn('[SqliteGroupListPagedSource] 读取群组列表缓存失败:', e);
@@ -265,7 +265,7 @@ export class SqliteGroupMemberListPagedSource implements IGroupMemberListPagedSo
}
this.consumed = true;
try {
- const items = await getGroupMembersCache(this.groupId);
+ const items = await groupCacheRepository.getMembers(this.groupId);
return { items, hasMore: false };
} catch (e) {
console.warn('[SqliteGroupMemberListPagedSource] 读取成员列表缓存失败:', e);
diff --git a/src/stores/groupMemberProfileResolver.ts b/src/stores/groupMemberProfileResolver.ts
index 0b14c63..bfc8ac6 100644
--- a/src/stores/groupMemberProfileResolver.ts
+++ b/src/stores/groupMemberProfileResolver.ts
@@ -1,5 +1,5 @@
import { GroupMemberResponse, UserDTO } from '../types/dto';
-import { getUserCache } from '../services/database';
+import { userCacheRepository } from '@/database';
import { userManager } from './userManager';
/**
@@ -19,7 +19,7 @@ export async function enrichGroupMembersWithUserProfiles(
await Promise.all(
userIds.map(async (userId) => {
try {
- const cachedUser = await getUserCache(userId);
+ const cachedUser = await userCacheRepository.get(userId);
if (cachedUser) {
userMap.set(userId, cachedUser);
return;
diff --git a/src/stores/message/services/ConversationOperations.ts b/src/stores/message/services/ConversationOperations.ts
index 92aa375..2ac2fbc 100644
--- a/src/stores/message/services/ConversationOperations.ts
+++ b/src/stores/message/services/ConversationOperations.ts
@@ -9,7 +9,7 @@
import type { ConversationResponse } from '../../../types/dto';
import { messageService } from '../../../services/messageService';
-import { deleteConversation } from '../../../services/database';
+import { conversationRepository } from '@/database';
import type { IConversationOperations } from '../types';
import { useMessageStore, normalizeConversationId } from '../store';
@@ -60,7 +60,7 @@ export class ConversationOperations implements IConversationOperations {
store.removeConversation(conversationId);
// 删除本地数据库中的会话
- deleteConversation(conversationId).catch(error => {
+ conversationRepository.delete(conversationId).catch(error => {
console.error('[ConversationOperations] 删除本地会话失败:', error);
});
}
diff --git a/src/stores/message/services/MessageSendService.ts b/src/stores/message/services/MessageSendService.ts
index 7347a4e..5a924bf 100644
--- a/src/stores/message/services/MessageSendService.ts
+++ b/src/stores/message/services/MessageSendService.ts
@@ -9,7 +9,7 @@
import type { MessageResponse, MessageSegment, ConversationResponse } from '../../../types/dto';
import { messageService } from '../../../services/messageService';
-import { saveMessage } from '../../../services/database';
+import { messageRepository } from '@/database';
import type { IMessageSendService } from '../types';
import { useMessageStore, mergeMessagesById } from '../store';
@@ -64,7 +64,7 @@ export class MessageSendService implements IMessageSendService {
.map((s: any) => s.data?.text || '')
.join('') || '';
- saveMessage({
+ messageRepository.saveMessage({
id: response.id,
conversationId,
senderId: currentUserId || '',
diff --git a/src/stores/message/services/MessageSyncService.ts b/src/stores/message/services/MessageSyncService.ts
index 2c1a605..efbd660 100644
--- a/src/stores/message/services/MessageSyncService.ts
+++ b/src/stores/message/services/MessageSyncService.ts
@@ -9,13 +9,7 @@
import type { ConversationResponse, MessageResponse } from '../../../types/dto';
import { messageService } from '../../../services/messageService';
-import {
- getMessagesByConversation,
- getMaxSeq,
- saveMessagesBatch,
- saveConversationsWithRelatedCache,
- getMessagesBeforeSeq,
-} from '../../../services/database';
+import { messageRepository, conversationRepository } from '@/database';
import {
type IConversationListPagedSource,
SqliteConversationListPagedSource,
@@ -213,8 +207,8 @@ export class MessageSyncService implements IMessageSyncService {
// 先从本地数据库加载
if (!hasInMemoryMessages) {
try {
- const localMessages = await getMessagesByConversation(conversationId, 20);
- const localMaxSeq = await getMaxSeq(conversationId);
+ const localMessages = await messageRepository.getByConversation(conversationId, 20);
+ const localMaxSeq = await messageRepository.getMaxSeq(conversationId);
baselineMaxSeq = localMaxSeq;
if (localMessages.length > 0) {
@@ -249,7 +243,7 @@ export class MessageSyncService implements IMessageSyncService {
store.setMessages(conversationId, mergedSnapshot);
// 持久化到本地
- saveMessagesBatch(snapshotMessages.map((m: any) => ({
+ messageRepository.saveMessagesBatch(snapshotMessages.map((m: any) => ({
id: m.id,
conversationId: m.conversation_id || conversationId,
senderId: m.sender_id,
@@ -276,7 +270,7 @@ export class MessageSyncService implements IMessageSyncService {
const mergedMessages = mergeMessagesById(existingMessages, newMessages);
store.setMessages(conversationId, mergedMessages);
- saveMessagesBatch(newMessages.map((m: any) => ({
+ messageRepository.saveMessagesBatch(newMessages.map((m: any) => ({
id: m.id,
conversationId: m.conversation_id || conversationId,
senderId: m.sender_id,
@@ -306,20 +300,20 @@ export class MessageSyncService implements IMessageSyncService {
store.setMessages(conversationId, mergedMessages);
- saveMessagesBatch(newMessages.map((m: any) => ({
- id: m.id,
- conversationId: m.conversation_id || conversationId,
- senderId: m.sender_id,
- content: m.content,
- type: m.type || 'text',
- isRead: m.is_read || false,
- createdAt: m.created_at,
- seq: m.seq,
- status: m.status || 'normal',
- segments: m.segments,
- }))).catch(error => {
- console.error('[MessageSyncService] 保存消息到本地失败:', error);
- });
+ messageRepository.saveMessagesBatch(newMessages.map((m: any) => ({
+ id: m.id,
+ conversationId: m.conversation_id || conversationId,
+ senderId: m.sender_id,
+ content: m.content,
+ type: m.type || 'text',
+ isRead: m.is_read || false,
+ createdAt: m.created_at,
+ seq: m.seq,
+ status: m.status || 'normal',
+ segments: m.segments,
+ }))).catch(error => {
+ console.error('[MessageSyncService] 保存消息到本地失败:', error);
+ });
}
}
} catch (error) {
@@ -348,7 +342,7 @@ export class MessageSyncService implements IMessageSyncService {
try {
// 先从本地获取
- const localMessages = await getMessagesBeforeSeq(conversationId, beforeSeq, limit);
+ const localMessages = await messageRepository.getBeforeSeq(conversationId, beforeSeq, limit);
if (localMessages.length >= limit) {
const formattedMessages: MessageResponse[] = [...localMessages].reverse().map(m => ({
@@ -374,7 +368,7 @@ export class MessageSyncService implements IMessageSyncService {
if (response?.messages && response.messages.length > 0) {
const serverMessages = response.messages;
- await saveMessagesBatch(serverMessages.map((m: any) => ({
+ await messageRepository.saveMessagesBatch(serverMessages.map((m: any) => ({
id: m.id,
conversationId: m.conversation_id || conversationId,
senderId: m.sender_id,
@@ -497,7 +491,7 @@ export class MessageSyncService implements IMessageSyncService {
.map(conv => conv.group)
.filter((group): group is NonNullable => Boolean(group));
- saveConversationsWithRelatedCache(list, users, groups).catch(error => {
+ conversationRepository.saveWithRelated(list, users, groups).catch(error => {
console.error('[MessageSyncService] 持久化会话列表失败:', error);
});
}
diff --git a/src/stores/message/services/ReadReceiptManager.ts b/src/stores/message/services/ReadReceiptManager.ts
index 0592d20..9d474ef 100644
--- a/src/stores/message/services/ReadReceiptManager.ts
+++ b/src/stores/message/services/ReadReceiptManager.ts
@@ -9,7 +9,7 @@
import type { ConversationResponse } from '../../../types/dto';
import { messageService } from '../../../services/messageService';
-import { markConversationAsRead, updateConversationCacheUnreadCount } from '../../../services/database';
+import { messageRepository, conversationRepository } from '@/database';
import type { ReadStateRecord, IReadReceiptManager } from '../types';
import { READ_STATE_PROTECTION_DELAY } from '../constants';
import { useMessageStore, normalizeConversationId } from '../store';
@@ -78,7 +78,7 @@ export class ReadReceiptManager implements IReadReceiptManager {
store.setUnreadCount(newTotalUnread, currentUnread.system);
// 3. 更新本地数据库
- markConversationAsRead(normalizedId).catch(console.error);
+ messageRepository.markConversationAsRead(normalizedId).catch(console.error);
// 4. 调用 API,完成后设置延迟清除保护
try {
diff --git a/src/stores/message/services/UserCacheService.ts b/src/stores/message/services/UserCacheService.ts
index 65a23d1..7d4f22a 100644
--- a/src/stores/message/services/UserCacheService.ts
+++ b/src/stores/message/services/UserCacheService.ts
@@ -5,7 +5,7 @@
import type { MessageResponse, UserDTO } from '../../../types/dto';
import { api } from '../../../services/api';
-import { getUserCache, saveUserCache } from '../../../services/database';
+import { userCacheRepository } from '@/database';
import type { IUserCacheService } from '../types';
export class UserCacheService implements IUserCacheService {
@@ -17,7 +17,7 @@ export class UserCacheService implements IUserCacheService {
*/
async getSenderInfo(userId: string): Promise {
// 1. 先检查本地缓存
- const cachedUser = await getUserCache(userId);
+ const cachedUser = await userCacheRepository.get(userId);
if (cachedUser) {
return cachedUser;
}
@@ -49,7 +49,7 @@ export class UserCacheService implements IUserCacheService {
const response = await api.get(`/users/${userId}`);
if (response.code === 0 && response.data) {
// 缓存到本地数据库
- await saveUserCache(response.data);
+ await userCacheRepository.save(response.data);
return response.data;
}
return null;
diff --git a/src/stores/message/services/WSMessageHandler.ts b/src/stores/message/services/WSMessageHandler.ts
index ff06395..f66a436 100644
--- a/src/stores/message/services/WSMessageHandler.ts
+++ b/src/stores/message/services/WSMessageHandler.ts
@@ -19,7 +19,7 @@ import type {
} from '../../../services/wsService';
import { wsService, GroupNoticeType } from '../../../services/wsService';
import { vibrateOnMessage } from '../../../services/messageVibrationService';
-import { saveMessage, updateMessageStatus } from '../../../services/database';
+import { messageRepository } from '@/database';
import type { MessageResponse, ConversationResponse } from '../../../types/dto';
import type {
IWSMessageHandler,
@@ -318,7 +318,7 @@ export class WSMessageHandler implements IWSMessageHandler {
// 异步保存到本地数据库
const textContent = segments?.filter((s: any) => s.type === 'text').map((s: any) => s.data?.text || '').join('') || '';
- saveMessage({
+ messageRepository.saveMessage({
id,
conversationId: normalizedConversationId,
senderId: sender_id || '',
@@ -358,7 +358,7 @@ export class WSMessageHandler implements IWSMessageHandler {
this.markMessageAsRecalled(normalizedConversationId, message_id);
this.syncConversationLastMessageOnRecall(normalizedConversationId, message_id);
- updateMessageStatus(message_id, 'recalled', true).catch(error => {
+ messageRepository.updateStatus(message_id, 'recalled', true).catch(error => {
console.error('[WSMessageHandler] 更新本地消息撤回状态失败:', error);
});
}
@@ -373,7 +373,7 @@ export class WSMessageHandler implements IWSMessageHandler {
this.markMessageAsRecalled(normalizedConversationId, message_id);
this.syncConversationLastMessageOnRecall(normalizedConversationId, message_id);
- updateMessageStatus(message_id, 'recalled', true).catch(error => {
+ messageRepository.updateStatus(message_id, 'recalled', true).catch(error => {
console.error('[WSMessageHandler] 更新本地消息撤回状态失败:', error);
});
}
diff --git a/src/stores/user/UserManager.ts b/src/stores/user/UserManager.ts
index 04afd3e..fc3d9ff 100644
--- a/src/stores/user/UserManager.ts
+++ b/src/stores/user/UserManager.ts
@@ -1,46 +1,34 @@
/**
- * UserManager - 用户管理核心模块(重构版?
+ * UserManager - 用户管理核心模块(重构版)
*
- * 使用 Zustand store 进行状态管?
- * 保持与旧 API 的向后兼?
+ * 使用 Zustand store 进行状态管理
+ * 保持与旧 API 的向后兼容
*/
import type { UserDTO } from '../../types/dto';
import { authService } from '../../services/authService';
-import {
- getCurrentUserCache,
- getUserCache,
- saveCurrentUserCache,
- saveUserCache,
-} from '../../services/database';
+import { userCacheRepository } from '@/database';
import { useUserManagerStore } from './userStore';
import { createCacheEntry, isCacheExpired, DEFAULT_TTL } from '../utils/cache';
-// ==================== UserManager ?====================
+// ==================== UserManager 类 ====================
class UserManager {
- /**
- * 获取当前用户
- * 支持缓存过期检查和后台刷新
- */
async getCurrentUser(forceRefresh = false): Promise {
const store = useUserManagerStore.getState();
const entry = store.currentUserEntry;
- // 有效缓存
if (!forceRefresh && entry && !isCacheExpired(entry)) {
return entry.data;
}
- // 过期缓存:后台刷新,立即返回旧数?
if (!forceRefresh && entry && isCacheExpired(entry)) {
this.refreshCurrentUserInBackground();
return entry.data;
}
- // 尝试从本地数据库加载
if (!forceRefresh) {
- const cachedFromDb = await getCurrentUserCache();
+ const cachedFromDb = await userCacheRepository.getCurrent();
if (cachedFromDb) {
const newEntry = createCacheEntry(cachedFromDb, DEFAULT_TTL.USER);
store.setCurrentUserEntry(newEntry);
@@ -49,23 +37,19 @@ class UserManager {
}
}
- // 发起 API 请求
return this.dedupe('users:current', async () => {
const user = await authService.fetchCurrentUserFromAPI();
const newEntry = createCacheEntry(user, DEFAULT_TTL.USER);
store.setCurrentUserEntry(newEntry);
if (user) {
- saveCurrentUserCache(user).catch(() => {});
- saveUserCache(user).catch(() => {});
+ userCacheRepository.saveCurrent(user).catch(() => {});
+ userCacheRepository.save(user).catch(() => {});
}
return user;
});
}
- /**
- * 后台刷新当前用户
- */
private refreshCurrentUserInBackground(): void {
this.dedupe('users:current:bg', async () => {
const store = useUserManagerStore.getState();
@@ -74,8 +58,8 @@ class UserManager {
store.setCurrentUserEntry(newEntry);
if (user) {
- saveCurrentUserCache(user).catch(() => {});
- saveUserCache(user).catch(() => {});
+ userCacheRepository.saveCurrent(user).catch(() => {});
+ userCacheRepository.save(user).catch(() => {});
}
return user;
}).catch((error) => {
@@ -83,27 +67,21 @@ class UserManager {
});
}
- /**
- * 根据 ID 获取用户
- */
async getUserById(userId: string, forceRefresh = false): Promise {
const store = useUserManagerStore.getState();
const entry = store.usersMap.get(userId);
- // 有效缓存
if (!forceRefresh && entry && !isCacheExpired(entry)) {
return entry.data;
}
- // 过期缓存:后台刷新,立即返回旧数?
if (!forceRefresh && entry && isCacheExpired(entry)) {
this.refreshUserByIdInBackground(userId);
return entry.data;
}
- // 尝试从本地数据库加载
if (!forceRefresh) {
- const cachedFromDb = await getUserCache(userId);
+ const cachedFromDb = await userCacheRepository.get(userId);
if (cachedFromDb) {
const newEntry = createCacheEntry(cachedFromDb, DEFAULT_TTL.USER);
store.setUser(userId, cachedFromDb);
@@ -112,7 +90,6 @@ class UserManager {
}
}
- // 发起 API 请求
return this.dedupe(`users:detail:${userId}`, async () => {
const store = useUserManagerStore.getState();
const user = await authService.fetchUserByIdFromAPI(userId);
@@ -120,15 +97,12 @@ class UserManager {
store.setUser(userId, user);
if (user) {
- saveUserCache(user).catch(() => {});
+ userCacheRepository.save(user).catch(() => {});
}
return user;
});
}
- /**
- * 后台刷新用户
- */
private refreshUserByIdInBackground(userId: string): void {
this.dedupe(`users:detail:bg:${userId}`, async () => {
const store = useUserManagerStore.getState();
@@ -137,7 +111,7 @@ class UserManager {
store.setUser(userId, user);
if (user) {
- saveUserCache(user).catch(() => {});
+ userCacheRepository.save(user).catch(() => {});
}
return user;
}).catch((error) => {
@@ -145,16 +119,10 @@ class UserManager {
});
}
- /**
- * 使缓存失?
- */
invalidateUsers(): void {
useUserManagerStore.getState().invalidateAll();
}
- /**
- * 请求去重
- */
private async dedupe(key: string, fetcher: () => Promise): Promise {
const store = useUserManagerStore.getState();
const pending = store.getPendingRequest(key);
@@ -168,11 +136,8 @@ class UserManager {
}
}
-// ==================== 单例导出 ====================
-
export const userManager = new UserManager();
-// 导出类以支持类型检查和测试
export { UserManager };
-export default userManager;
+export default userManager;
\ No newline at end of file