Compare commits
16 Commits
9ff222c22c
...
d47c60a5e0
| Author | SHA1 | Date | |
|---|---|---|---|
| d47c60a5e0 | |||
| 242a4347df | |||
| 70c69e16cc | |||
| a1b17c11c1 | |||
| bdd6c0828c | |||
| 8a14c5244e | |||
| 4d1003e467 | |||
| f55ff5fc24 | |||
| 4c22109a95 | |||
| a4a18f2b82 | |||
| a9ae4bce44 | |||
| 5367ad61db | |||
| 3f4a294128 | |||
| 2ee194ebdd | |||
| 4ac6e85aca | |||
| dc666897c6 |
@@ -50,7 +50,7 @@ public class SecurityConfig {
|
||||
// WebSocket
|
||||
.requestMatchers("/ws/**").permitAll()
|
||||
// 静态资源
|
||||
.requestMatchers("/webapp/**", "/assets/**", "/uploads/**", "/files/avatar/**", "/api/files/avatar/**", "/api/messages/file/**").permitAll()
|
||||
.requestMatchers("/webapp/**", "/assets/**", "/uploads/**", "/files/avatar/**", "/api/files/avatar/**", "/api/messages/file/**", "/api/messages/conversation/**").permitAll()
|
||||
// 前端页面路由(所有非 /api/ 的路由,由 Vue Router 处理)
|
||||
.requestMatchers("/", "/login", "/register", "/desktop/**", "/favicon.ico").permitAll()
|
||||
// 其他所有请求需要认证
|
||||
|
||||
@@ -276,4 +276,12 @@ public class MessageController {
|
||||
messageService.markAsRead(id);
|
||||
return ResponseEntity.ok(Map.of("message", "已标记已读"));
|
||||
}
|
||||
|
||||
@DeleteMapping("/conversation/{withUserId}")
|
||||
public ResponseEntity<?> deleteConversation(
|
||||
@AuthenticationPrincipal UserPrincipal principal,
|
||||
@PathVariable Long withUserId) {
|
||||
messageService.deleteConversation(principal.getUserId(), withUserId);
|
||||
return ResponseEntity.ok(Map.of("message", "已删除"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,14 +48,21 @@ public class FileService {
|
||||
wrapper.eq(FileEntity::getUserId, userId)
|
||||
.eq(FileEntity::getIsDeleted, 0);
|
||||
|
||||
if (folderId != null) {
|
||||
wrapper.eq(FileEntity::getFolderId, folderId);
|
||||
} else {
|
||||
wrapper.isNull(FileEntity::getFolderId);
|
||||
}
|
||||
boolean hasKeyword = keyword != null && !keyword.isEmpty();
|
||||
|
||||
if (keyword != null && !keyword.isEmpty()) {
|
||||
if (hasKeyword) {
|
||||
// 有搜索关键词:根目录搜索查所有,子目录搜索限当前目录
|
||||
if (folderId != null) {
|
||||
wrapper.eq(FileEntity::getFolderId, folderId);
|
||||
}
|
||||
wrapper.like(FileEntity::getName, keyword);
|
||||
} else {
|
||||
// 无搜索关键词:正常浏览当前目录
|
||||
if (folderId != null) {
|
||||
wrapper.eq(FileEntity::getFolderId, folderId);
|
||||
} else {
|
||||
wrapper.isNull(FileEntity::getFolderId);
|
||||
}
|
||||
}
|
||||
|
||||
wrapper.orderByDesc(FileEntity::getIsFolder)
|
||||
@@ -344,9 +351,13 @@ public class FileService {
|
||||
new LambdaQueryWrapper<FileShare>().eq(FileShare::getShareToUserId, userId)
|
||||
);
|
||||
|
||||
// 按 fileId 去重,只保留一条记录
|
||||
return shares.stream()
|
||||
.map(share -> fileMapper.selectById(share.getFileId()))
|
||||
.filter(f -> f != null && f.getIsDeleted() == 0)
|
||||
.collect(Collectors.toMap(FileEntity::getId, f -> f, (a, b) -> a))
|
||||
.values()
|
||||
.stream()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -358,7 +369,7 @@ public class FileService {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 检查是否有共享权限
|
||||
// 检查是否有共享权限(当前用户是被共享的对象)
|
||||
List<FileShare> shares = fileShareMapper.selectList(
|
||||
new LambdaQueryWrapper<FileShare>()
|
||||
.eq(FileShare::getFileId, folderId)
|
||||
@@ -369,13 +380,33 @@ public class FileService {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 返回该文件夹内的文件
|
||||
LambdaQueryWrapper<FileEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(FileEntity::getFolderId, folderId)
|
||||
.eq(FileEntity::getIsDeleted, 0)
|
||||
.isNull(FileEntity::getDeletedAt);
|
||||
// 用文件夹原主人的 ID 查询子文件(属于文件夹所有者的文件)
|
||||
Long ownerId = folder.getUserId();
|
||||
|
||||
// 返回该文件夹内的文件(包括自己上传的和别人共享进来的)
|
||||
// 属于 owner 的文件:用 ownerId 查
|
||||
LambdaQueryWrapper<FileEntity> ownerWrapper = new LambdaQueryWrapper<>();
|
||||
ownerWrapper.eq(FileEntity::getFolderId, folderId)
|
||||
.eq(FileEntity::getUserId, ownerId)
|
||||
.eq(FileEntity::getIsDeleted, 0);
|
||||
|
||||
return fileMapper.selectList(wrapper);
|
||||
List<FileEntity> results = new ArrayList<>(fileMapper.selectList(ownerWrapper));
|
||||
|
||||
// 属于当前用户的文件(通过共享进入的子文件夹),去掉 deleted 条件
|
||||
LambdaQueryWrapper<FileEntity> userWrapper = new LambdaQueryWrapper<>();
|
||||
userWrapper.eq(FileEntity::getFolderId, folderId)
|
||||
.ne(FileEntity::getUserId, ownerId)
|
||||
.eq(FileEntity::getIsDeleted, 0);
|
||||
List<FileEntity> sharedChildren = fileMapper.selectList(userWrapper);
|
||||
|
||||
for (FileEntity child : sharedChildren) {
|
||||
// 只有当前用户有权访问的才加入(直接共享或通过父文件夹共享)
|
||||
if (canAccessFile(child.getId(), userId)) {
|
||||
results.add(child);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
} catch (Exception e) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.filesystem.entity.Message;
|
||||
import com.filesystem.mapper.MessageMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
@@ -14,8 +16,16 @@ public class MessageService {
|
||||
|
||||
@Resource
|
||||
private MessageMapper messageMapper;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
private static final String CONV_DELETED_KEY = "msg:del:";
|
||||
|
||||
public List<Message> getMessages(Long userId1, Long userId2) {
|
||||
String convKey = buildConvKey(userId1, userId2);
|
||||
String deletedKey = CONV_DELETED_KEY + userId1 + ":" + convKey;
|
||||
|
||||
return messageMapper.selectList(
|
||||
new LambdaQueryWrapper<Message>()
|
||||
.and(wrapper -> wrapper
|
||||
@@ -26,6 +36,31 @@ public class MessageService {
|
||||
.orderByAsc(Message::getCreateTime)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除与某个用户的聊天记录(双向,本人会看不到)
|
||||
*/
|
||||
public void deleteConversation(Long userId, Long withUserId) {
|
||||
String convKey = buildConvKey(userId, withUserId);
|
||||
String deletedKey = CONV_DELETED_KEY + userId + ":" + convKey;
|
||||
// 保留7天
|
||||
redisTemplate.opsForValue().set(deletedKey, "1", java.time.Duration.ofDays(7));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查与某人的对话是否已被当前用户删除
|
||||
*/
|
||||
public boolean isConversationDeleted(Long userId, Long withUserId) {
|
||||
String convKey = buildConvKey(userId, withUserId);
|
||||
String deletedKey = CONV_DELETED_KEY + userId + ":" + convKey;
|
||||
return Boolean.TRUE.equals(redisTemplate.hasKey(deletedKey));
|
||||
}
|
||||
|
||||
private String buildConvKey(Long userId1, Long userId2) {
|
||||
long min = Math.min(userId1, userId2);
|
||||
long max = Math.max(userId1, userId2);
|
||||
return min + "_" + max;
|
||||
}
|
||||
|
||||
public Message sendMessage(Long fromUserId, Long toUserId, String content, String type) {
|
||||
Message message = new Message();
|
||||
|
||||
@@ -3,15 +3,15 @@ server:
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://192.168.31.182:13306/system?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&createDatabaseIfNotExist=true
|
||||
username: dream
|
||||
password: info_dream
|
||||
url: jdbc:mysql://127.0.0.1:13306/chat_app?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&createDatabaseIfNotExist=true
|
||||
username: root
|
||||
password: root_dream
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
|
||||
|
||||
data:
|
||||
redis:
|
||||
host: 192.168.31.194
|
||||
port: 16379
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
database: 0
|
||||
timeout: 10000ms
|
||||
password: admin
|
||||
|
||||
@@ -10,6 +10,8 @@ export const getUnreadList = () => request.get('/messages/unreadList')
|
||||
|
||||
export const markAsRead = (id) => request.post(`/messages/${id}/read`)
|
||||
|
||||
export const deleteConversation = (withUserId) => request.delete(`/messages/conversation/${withUserId}`)
|
||||
|
||||
export const getUsers = () => request.get('/messages/users')
|
||||
|
||||
// 聊天文件上传(图片和文件统一接口)
|
||||
|
||||
@@ -19,6 +19,22 @@ request.interceptors.request.use(
|
||||
request.interceptors.response.use(
|
||||
response => response.data,
|
||||
error => {
|
||||
// 后端不可用(网络错误、无响应),跳转到登录页
|
||||
if (!error.response) {
|
||||
const isLoginPage = window.location.pathname === '/login'
|
||||
if (!isLoginPage) {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('username')
|
||||
localStorage.removeItem('userId')
|
||||
localStorage.removeItem('nickname')
|
||||
localStorage.removeItem('avatar')
|
||||
localStorage.removeItem('storageUsed')
|
||||
localStorage.removeItem('storageLimit')
|
||||
window.location.href = '/login'
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
const status = error.response?.status
|
||||
|
||||
// 只有 401/403 才清理 token 并跳转登录页
|
||||
|
||||
@@ -39,9 +39,11 @@
|
||||
<div class="sidebar-item-name">{{ chat.name }}</div>
|
||||
<div class="sidebar-item-msg">{{ chat.lastMsg || '暂无消息' }}</div>
|
||||
</div>
|
||||
<el-button v-if="chat.lastMsg" class="delete-btn" link type="danger" @click.stop="deleteRecentChat(chat)">
|
||||
<el-icon><Close /></el-icon>
|
||||
</el-button>
|
||||
<el-tooltip content="删除会话" placement="top">
|
||||
<el-button link type="danger" @click.stop="deleteRecentChat(chat)">
|
||||
<el-icon><Delete /></el-icon>
|
||||
</el-button>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
<div v-if="recentChats.length === 0" class="sidebar-empty">暂无最近聊天</div>
|
||||
</div>
|
||||
@@ -215,9 +217,9 @@
|
||||
<script setup>
|
||||
import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { ChatLineRound, User, Search, Close, Loading, Picture, Paperclip, Download, ChatDotRound, Promotion } from '@element-plus/icons-vue'
|
||||
import { ChatLineRound, User, Search, Loading, Picture, Paperclip, Download, ChatDotRound, Promotion, Delete } from '@element-plus/icons-vue'
|
||||
import { useUserStore } from '@/store/user'
|
||||
import { getUsers, getMessages, sendMessage as sendMessageApi, uploadChatFile, getUnreadList } from '@/api/message'
|
||||
import { getUsers, getMessages, sendMessage as sendMessageApi, uploadChatFile, getUnreadList, deleteConversation } from '@/api/message'
|
||||
import { chatService } from '@/services/chat'
|
||||
|
||||
const props = defineProps({ modelValue: Boolean })
|
||||
@@ -571,7 +573,12 @@ const downloadFile = async (msg) => {
|
||||
|
||||
// ======== 其他操作 ========
|
||||
|
||||
const deleteRecentChat = (chat) => {
|
||||
const deleteRecentChat = async (chat) => {
|
||||
try {
|
||||
await deleteConversation(chat.id)
|
||||
} catch (e) {
|
||||
// 即使 API 失败也删本地
|
||||
}
|
||||
const idx = recentChats.value.findIndex(c => c.id === chat.id)
|
||||
if (idx > -1) recentChats.value.splice(idx, 1)
|
||||
if (currentContact.value?.id === chat.id) currentContact.value = null
|
||||
@@ -684,8 +691,6 @@ onUnmounted(() => { if (unsubscribeWs) unsubscribeWs() })
|
||||
.sidebar-item-status { font-size: 12px; color: #909399; }
|
||||
.sidebar-item-status.online { color: #67c23a; }
|
||||
.sidebar-empty { padding: 20px; text-align: center; color: #909399; font-size: 12px; }
|
||||
.delete-btn { opacity: 0; transition: opacity 0.2s; }
|
||||
.sidebar-item:hover .delete-btn { opacity: 1; }
|
||||
|
||||
.chat-main { flex: 1; display: flex; flex-direction: column; }
|
||||
.chat-header { display: flex; align-items: center; gap: 8px; padding: 12px; border-bottom: 1px solid #e4e7ed; }
|
||||
|
||||
@@ -44,13 +44,33 @@ const router = createRouter({
|
||||
routes
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const token = localStorage.getItem('token')
|
||||
|
||||
if (to.meta.requiresAuth && !token) {
|
||||
next('/login')
|
||||
} else if (to.path === '/login' && token) {
|
||||
next('/desktop')
|
||||
} else if (to.meta.requiresAuth && token) {
|
||||
// 有 token 且要进需认证页面,先验证后端是否可用(5秒超时)
|
||||
try {
|
||||
const controller = new AbortController()
|
||||
const timeoutId = setTimeout(() => controller.abort(), 5000)
|
||||
const res = await fetch('/api/files/test', { signal: controller.signal })
|
||||
clearTimeout(timeoutId)
|
||||
if (!res.ok) throw new Error('backend error')
|
||||
next()
|
||||
} catch {
|
||||
// 后端不可用,清除登录状态跳转登录页
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('username')
|
||||
localStorage.removeItem('userId')
|
||||
localStorage.removeItem('nickname')
|
||||
localStorage.removeItem('avatar')
|
||||
localStorage.removeItem('storageUsed')
|
||||
localStorage.removeItem('storageLimit')
|
||||
window.location.href = '/login'
|
||||
}
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
|
||||
@@ -61,7 +61,13 @@ const handleLogin = async () => {
|
||||
// res.data = { token, user }
|
||||
emit('success', res.data)
|
||||
} catch (e) {
|
||||
ElMessage.error(e.response?.data?.message || '账号或密码错误')
|
||||
if (!e.response) {
|
||||
ElMessage.error('后端服务不可用,请稍后重试')
|
||||
} else if (e.response.status >= 500) {
|
||||
ElMessage.error('服务器异常,请稍后重试')
|
||||
} else {
|
||||
ElMessage.error(e.response.data?.message || '账号或密码错误')
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
@@ -107,7 +107,13 @@ const handleRegister = async () => {
|
||||
ElMessage.success('注册成功,请登录')
|
||||
emit('success')
|
||||
} catch (e) {
|
||||
ElMessage.error(e.response?.data?.message || '注册失败,请重试')
|
||||
if (!e.response) {
|
||||
ElMessage.error('后端服务不可用,请稍后重试')
|
||||
} else if (e.response.status >= 500) {
|
||||
ElMessage.error('服务器异常,请稍后重试')
|
||||
} else {
|
||||
ElMessage.error(e.response.data?.message || '注册失败,请重试')
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user