mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
Compare commits
34 Commits
codex/add-
...
codex/add-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd73747164 | ||
|
|
0ee58df868 | ||
|
|
6fed8131f6 | ||
|
|
d75c08396a | ||
|
|
3a742fbb00 | ||
|
|
9c2b1f6e98 | ||
|
|
28b33d8c44 | ||
|
|
1f99a10322 | ||
|
|
743c3dbc72 | ||
|
|
d46a446f2b | ||
|
|
75a785f612 | ||
|
|
e79b75f340 | ||
|
|
1f6f470ab5 | ||
|
|
583d4042f5 | ||
|
|
8437c1c714 | ||
|
|
2613fe6cf1 | ||
|
|
a15d541b72 | ||
|
|
8657a06f52 | ||
|
|
09900b34aa | ||
|
|
4e1c3f5839 | ||
|
|
d97cc7df5e | ||
|
|
151242f3ba | ||
|
|
b2783a0168 | ||
|
|
c79bcac217 | ||
|
|
9a06da3bc1 | ||
|
|
98bbc36453 | ||
|
|
4a04f4ec17 | ||
|
|
77be2bfebb | ||
|
|
cf4ca89e19 | ||
|
|
094fc78d92 | ||
|
|
da3d2a6a71 | ||
|
|
15cba0c96e | ||
|
|
98a79acad9 | ||
|
|
809a78fee3 |
@@ -18,14 +18,14 @@ public class ChannelInitializer implements CommandLineRunner {
|
|||||||
chat.setChannel(true);
|
chat.setChannel(true);
|
||||||
chat.setName("吹水群");
|
chat.setName("吹水群");
|
||||||
chat.setDescription("吹水聊天");
|
chat.setDescription("吹水聊天");
|
||||||
chat.setAvatar("/default-avatar.svg");
|
chat.setAvatar("https://openisle-1307107697.cos.accelerate.myqcloud.com/dynamic_assert/32647273e2334d14adfd4a6ce9db0643.jpeg");
|
||||||
conversationRepository.save(chat);
|
conversationRepository.save(chat);
|
||||||
|
|
||||||
MessageConversation tech = new MessageConversation();
|
MessageConversation tech = new MessageConversation();
|
||||||
tech.setChannel(true);
|
tech.setChannel(true);
|
||||||
tech.setName("技术讨论群");
|
tech.setName("技术讨论群");
|
||||||
tech.setDescription("讨论技术相关话题");
|
tech.setDescription("讨论技术相关话题");
|
||||||
tech.setAvatar("/default-avatar.svg");
|
tech.setAvatar("https://openisle-1307107697.cos.accelerate.myqcloud.com/dynamic_assert/5edde9a5864e471caa32491dbcdaa8b2.png");
|
||||||
conversationRepository.save(tech);
|
conversationRepository.save(tech);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,6 +121,7 @@ public class SecurityConfig {
|
|||||||
.requestMatchers(HttpMethod.GET, "/api/reaction-types").permitAll()
|
.requestMatchers(HttpMethod.GET, "/api/reaction-types").permitAll()
|
||||||
.requestMatchers(HttpMethod.GET, "/api/activities/**").permitAll()
|
.requestMatchers(HttpMethod.GET, "/api/activities/**").permitAll()
|
||||||
.requestMatchers(HttpMethod.GET, "/api/sitemap.xml").permitAll()
|
.requestMatchers(HttpMethod.GET, "/api/sitemap.xml").permitAll()
|
||||||
|
.requestMatchers(HttpMethod.GET, "/api/channels").permitAll()
|
||||||
.requestMatchers(HttpMethod.GET, "/api/rss").permitAll()
|
.requestMatchers(HttpMethod.GET, "/api/rss").permitAll()
|
||||||
.requestMatchers(HttpMethod.GET, "/api/point-goods").permitAll()
|
.requestMatchers(HttpMethod.GET, "/api/point-goods").permitAll()
|
||||||
.requestMatchers(HttpMethod.POST, "/api/point-goods").permitAll()
|
.requestMatchers(HttpMethod.POST, "/api/point-goods").permitAll()
|
||||||
@@ -156,7 +157,7 @@ public class SecurityConfig {
|
|||||||
uri.startsWith("/api/search") || uri.startsWith("/api/users") ||
|
uri.startsWith("/api/search") || uri.startsWith("/api/users") ||
|
||||||
uri.startsWith("/api/reaction-types") || uri.startsWith("/api/config") ||
|
uri.startsWith("/api/reaction-types") || uri.startsWith("/api/config") ||
|
||||||
uri.startsWith("/api/activities") || uri.startsWith("/api/push/public-key") ||
|
uri.startsWith("/api/activities") || uri.startsWith("/api/push/public-key") ||
|
||||||
uri.startsWith("/api/point-goods") ||
|
uri.startsWith("/api/point-goods") || uri.startsWith("/api/channels") ||
|
||||||
uri.startsWith("/api/sitemap.xml") || uri.startsWith("/api/medals") ||
|
uri.startsWith("/api/sitemap.xml") || uri.startsWith("/api/medals") ||
|
||||||
uri.startsWith("/api/rss"));
|
uri.startsWith("/api/rss"));
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.openisle.dto.ChannelDto;
|
|||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
import com.openisle.repository.UserRepository;
|
import com.openisle.repository.UserRepository;
|
||||||
import com.openisle.service.ChannelService;
|
import com.openisle.service.ChannelService;
|
||||||
|
import com.openisle.service.MessageService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -15,6 +16,7 @@ import java.util.List;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ChannelController {
|
public class ChannelController {
|
||||||
private final ChannelService channelService;
|
private final ChannelService channelService;
|
||||||
|
private final MessageService messageService;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
private Long getCurrentUserId(Authentication auth) {
|
private Long getCurrentUserId(Authentication auth) {
|
||||||
@@ -32,4 +34,9 @@ public class ChannelController {
|
|||||||
public ChannelDto joinChannel(@PathVariable Long channelId, Authentication auth) {
|
public ChannelDto joinChannel(@PathVariable Long channelId, Authentication auth) {
|
||||||
return channelService.joinChannel(channelId, getCurrentUserId(auth));
|
return channelService.joinChannel(channelId, getCurrentUserId(auth));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/unread-count")
|
||||||
|
public long unreadCount(Authentication auth) {
|
||||||
|
return messageService.getUnreadChannelCount(getCurrentUserId(auth));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ public class ChannelDto {
|
|||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private String avatar;
|
private String avatar;
|
||||||
|
private MessageDto lastMessage;
|
||||||
private long memberCount;
|
private long memberCount;
|
||||||
private boolean joined;
|
private boolean joined;
|
||||||
private long unreadCount;
|
private long unreadCount;
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ package com.openisle.model;
|
|||||||
public enum ReactionType {
|
public enum ReactionType {
|
||||||
LIKE,
|
LIKE,
|
||||||
DISLIKE,
|
DISLIKE,
|
||||||
|
SMILE,
|
||||||
RECOMMEND,
|
RECOMMEND,
|
||||||
|
CONGRATULATIONS,
|
||||||
ANGRY,
|
ANGRY,
|
||||||
FLUSHED,
|
FLUSHED,
|
||||||
STAR_STRUCK,
|
STAR_STRUCK,
|
||||||
@@ -26,5 +28,5 @@ public enum ReactionType {
|
|||||||
CHINA,
|
CHINA,
|
||||||
USA,
|
USA,
|
||||||
JAPAN,
|
JAPAN,
|
||||||
KOREA
|
KOREA,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,22 @@
|
|||||||
package com.openisle.repository;
|
package com.openisle.repository;
|
||||||
|
|
||||||
import com.openisle.model.MessageConversation;
|
import com.openisle.model.MessageConversation;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
import java.util.List;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import com.openisle.model.User;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface MessageConversationRepository extends JpaRepository<MessageConversation, Long> {
|
public interface MessageConversationRepository extends JpaRepository<MessageConversation, Long> {
|
||||||
@Query("SELECT c FROM MessageConversation c JOIN c.participants p1 JOIN c.participants p2 WHERE p1.user = :user1 AND p2.user = :user2")
|
@Query("SELECT c FROM MessageConversation c " +
|
||||||
Optional<MessageConversation> findConversationByUsers(@Param("user1") User user1, @Param("user2") User user2);
|
"WHERE c.channel = false AND size(c.participants) = 2 " +
|
||||||
|
"AND EXISTS (SELECT 1 FROM c.participants p1 WHERE p1.user = :user1) " +
|
||||||
|
"AND EXISTS (SELECT 1 FROM c.participants p2 WHERE p2.user = :user2) " +
|
||||||
|
"ORDER BY c.createdAt DESC")
|
||||||
|
List<MessageConversation> findConversationsByUsers(@Param("user1") User user1, @Param("user2") User user2);
|
||||||
|
|
||||||
@Query("SELECT DISTINCT c FROM MessageConversation c " +
|
@Query("SELECT DISTINCT c FROM MessageConversation c " +
|
||||||
"JOIN c.participants p " +
|
"JOIN c.participants p " +
|
||||||
@@ -32,4 +31,4 @@ public interface MessageConversationRepository extends JpaRepository<MessageConv
|
|||||||
List<MessageConversation> findByChannelTrue();
|
List<MessageConversation> findByChannelTrue();
|
||||||
|
|
||||||
long countByChannelTrue();
|
long countByChannelTrue();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.openisle.service;
|
package com.openisle.service;
|
||||||
|
|
||||||
import com.openisle.dto.ChannelDto;
|
import com.openisle.dto.ChannelDto;
|
||||||
|
import com.openisle.dto.MessageDto;
|
||||||
|
import com.openisle.dto.UserSummaryDto;
|
||||||
|
import com.openisle.model.Message;
|
||||||
import com.openisle.model.MessageConversation;
|
import com.openisle.model.MessageConversation;
|
||||||
import com.openisle.model.MessageParticipant;
|
import com.openisle.model.MessageParticipant;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
@@ -54,6 +57,9 @@ public class ChannelService {
|
|||||||
dto.setName(channel.getName());
|
dto.setName(channel.getName());
|
||||||
dto.setDescription(channel.getDescription());
|
dto.setDescription(channel.getDescription());
|
||||||
dto.setAvatar(channel.getAvatar());
|
dto.setAvatar(channel.getAvatar());
|
||||||
|
if (channel.getLastMessage() != null) {
|
||||||
|
dto.setLastMessage(toMessageDto(channel.getLastMessage()));
|
||||||
|
}
|
||||||
dto.setMemberCount(channel.getParticipants().size());
|
dto.setMemberCount(channel.getParticipants().size());
|
||||||
boolean joined = channel.getParticipants().stream()
|
boolean joined = channel.getParticipants().stream()
|
||||||
.anyMatch(p -> p.getUser().getId().equals(userId));
|
.anyMatch(p -> p.getUser().getId().equals(userId));
|
||||||
@@ -73,4 +79,20 @@ public class ChannelService {
|
|||||||
}
|
}
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MessageDto toMessageDto(Message message) {
|
||||||
|
MessageDto dto = new MessageDto();
|
||||||
|
dto.setId(message.getId());
|
||||||
|
dto.setContent(message.getContent());
|
||||||
|
dto.setConversationId(message.getConversation().getId());
|
||||||
|
dto.setCreatedAt(message.getCreatedAt());
|
||||||
|
|
||||||
|
UserSummaryDto userDto = new UserSummaryDto();
|
||||||
|
userDto.setId(message.getSender().getId());
|
||||||
|
userDto.setUsername(message.getSender().getUsername());
|
||||||
|
userDto.setAvatar(message.getSender().getAvatar());
|
||||||
|
dto.setSender(userDto);
|
||||||
|
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,6 +120,9 @@ public class MessageService {
|
|||||||
long unreadCount = getUnreadMessageCount(participant.getUser().getId());
|
long unreadCount = getUnreadMessageCount(participant.getUser().getId());
|
||||||
String username = participant.getUser().getUsername();
|
String username = participant.getUser().getUsername();
|
||||||
messagingTemplate.convertAndSendToUser(username, "/queue/unread-count", unreadCount);
|
messagingTemplate.convertAndSendToUser(username, "/queue/unread-count", unreadCount);
|
||||||
|
|
||||||
|
long channelUnread = getUnreadChannelCount(participant.getUser().getId());
|
||||||
|
messagingTemplate.convertAndSendToUser(username, "/queue/channel-unread", channelUnread);
|
||||||
}
|
}
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
@@ -151,7 +154,8 @@ public class MessageService {
|
|||||||
|
|
||||||
private MessageConversation findOrCreateConversation(User user1, User user2) {
|
private MessageConversation findOrCreateConversation(User user1, User user2) {
|
||||||
log.info("Searching for existing conversation between {} and {}", user1.getUsername(), user2.getUsername());
|
log.info("Searching for existing conversation between {} and {}", user1.getUsername(), user2.getUsername());
|
||||||
return conversationRepository.findConversationByUsers(user1, user2)
|
return conversationRepository.findConversationsByUsers(user1, user2).stream()
|
||||||
|
.findFirst()
|
||||||
.orElseGet(() -> {
|
.orElseGet(() -> {
|
||||||
log.info("No existing conversation found. Creating a new one.");
|
log.info("No existing conversation found. Creating a new one.");
|
||||||
MessageConversation conversation = new MessageConversation();
|
MessageConversation conversation = new MessageConversation();
|
||||||
@@ -260,10 +264,26 @@ public class MessageService {
|
|||||||
List<MessageParticipant> participations = participantRepository.findByUserId(userId);
|
List<MessageParticipant> participations = participantRepository.findByUserId(userId);
|
||||||
long totalUnreadCount = 0;
|
long totalUnreadCount = 0;
|
||||||
for (MessageParticipant p : participations) {
|
for (MessageParticipant p : participations) {
|
||||||
|
if (p.getConversation().isChannel()) continue;
|
||||||
LocalDateTime lastRead = p.getLastReadAt() == null ? LocalDateTime.of(1970, 1, 1, 0, 0) : p.getLastReadAt();
|
LocalDateTime lastRead = p.getLastReadAt() == null ? LocalDateTime.of(1970, 1, 1, 0, 0) : p.getLastReadAt();
|
||||||
// 只计算别人发送给当前用户的未读消息
|
// 只计算别人发送给当前用户的未读消息
|
||||||
totalUnreadCount += messageRepository.countByConversationIdAndCreatedAtAfterAndSenderIdNot(p.getConversation().getId(), lastRead, userId);
|
totalUnreadCount += messageRepository.countByConversationIdAndCreatedAtAfterAndSenderIdNot(p.getConversation().getId(), lastRead, userId);
|
||||||
}
|
}
|
||||||
return totalUnreadCount;
|
return totalUnreadCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public long getUnreadChannelCount(Long userId) {
|
||||||
|
List<MessageParticipant> participations = participantRepository.findByUserId(userId);
|
||||||
|
long unreadChannelCount = 0;
|
||||||
|
for (MessageParticipant p : participations) {
|
||||||
|
if (!p.getConversation().isChannel()) continue;
|
||||||
|
LocalDateTime lastRead = p.getLastReadAt() == null ? LocalDateTime.of(1970, 1, 1, 0, 0) : p.getLastReadAt();
|
||||||
|
long unread = messageRepository.countByConversationIdAndCreatedAtAfterAndSenderIdNot(p.getConversation().getId(), lastRead, userId);
|
||||||
|
if (unread > 0) {
|
||||||
|
unreadChannelCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return unreadChannelCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<GlobalPopups />
|
<GlobalPopups />
|
||||||
<ConfirmDialog />
|
<ConfirmDialog />
|
||||||
|
<ChatFloating />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -30,6 +31,7 @@ import HeaderComponent from '~/components/HeaderComponent.vue'
|
|||||||
import MenuComponent from '~/components/MenuComponent.vue'
|
import MenuComponent from '~/components/MenuComponent.vue'
|
||||||
import GlobalPopups from '~/components/GlobalPopups.vue'
|
import GlobalPopups from '~/components/GlobalPopups.vue'
|
||||||
import ConfirmDialog from '~/components/ConfirmDialog.vue'
|
import ConfirmDialog from '~/components/ConfirmDialog.vue'
|
||||||
|
import ChatFloating from '~/components/ChatFloating.vue'
|
||||||
import { useIsMobile } from '~/utils/screen'
|
import { useIsMobile } from '~/utils/screen'
|
||||||
|
|
||||||
const isMobile = useIsMobile()
|
const isMobile = useIsMobile()
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="timeline">
|
<div class="timeline" :class="{ 'hover-enabled': hover }">
|
||||||
<div class="timeline-item" v-for="(item, idx) in items" :key="idx">
|
<div class="timeline-item" v-for="(item, idx) in items" :key="idx">
|
||||||
<div
|
<div
|
||||||
class="timeline-icon"
|
class="timeline-icon"
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
>
|
>
|
||||||
<img v-if="item.src" :src="item.src" class="timeline-img" alt="timeline item" />
|
<img v-if="item.src" :src="item.src" class="timeline-img" alt="timeline item" />
|
||||||
<i v-else-if="item.icon" :class="item.icon"></i>
|
<i v-else-if="item.icon" :class="item.icon"></i>
|
||||||
<span v-else-if="item.emoji" class="timeline-emoji">{{ item.emoji }}</span>
|
<img v-else-if="item.emoji" :src="item.emoji" class="timeline-emoji" alt="emoji" />
|
||||||
</div>
|
</div>
|
||||||
<div class="timeline-content">
|
<div class="timeline-content">
|
||||||
<slot name="item" :item="item">{{ item.content }}</slot>
|
<slot name="item" :item="item">{{ item.content }}</slot>
|
||||||
@@ -22,6 +22,7 @@ export default {
|
|||||||
name: 'BaseTimeline',
|
name: 'BaseTimeline',
|
||||||
props: {
|
props: {
|
||||||
items: { type: Array, default: () => [] },
|
items: { type: Array, default: () => [] },
|
||||||
|
hover: { type: Boolean, default: false },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -41,7 +42,7 @@ export default {
|
|||||||
margin-top: 10px;
|
margin-top: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.timeline-item:hover {
|
.hover-enabled .timeline-item:hover {
|
||||||
background-color: var(--menu-selected-background-color);
|
background-color: var(--menu-selected-background-color);
|
||||||
transition: background-color 0.2s;
|
transition: background-color 0.2s;
|
||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
@@ -73,8 +74,9 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.timeline-emoji {
|
.timeline-emoji {
|
||||||
font-size: 20px;
|
width: 20px;
|
||||||
line-height: 1;
|
height: 20px;
|
||||||
|
object-fit: contain;
|
||||||
}
|
}
|
||||||
|
|
||||||
.timeline-item::before {
|
.timeline-item::before {
|
||||||
|
|||||||
56
frontend_nuxt/components/ChatFloating.vue
Normal file
56
frontend_nuxt/components/ChatFloating.vue
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<template>
|
||||||
|
<div v-if="chatFloating" class="chat-floating">
|
||||||
|
<iframe :src="iframeSrc" class="chat-frame"></iframe>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { computed } from 'vue'
|
||||||
|
|
||||||
|
const chatFloating = useState('chatFloating', () => false)
|
||||||
|
const chatPath = useState('chatPath', () => '/message-box')
|
||||||
|
|
||||||
|
const iframeSrc = computed(() =>
|
||||||
|
chatPath.value.includes('?') ? `${chatPath.value}&float=1` : `${chatPath.value}?float=1`,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (process.client) {
|
||||||
|
window.addEventListener('message', (event) => {
|
||||||
|
if (event.data?.type === 'maximize-chat') {
|
||||||
|
chatFloating.value = false
|
||||||
|
navigateTo(event.data.path || chatPath.value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.chat-floating {
|
||||||
|
position: fixed;
|
||||||
|
bottom: 20px;
|
||||||
|
right: 20px;
|
||||||
|
width: 400px;
|
||||||
|
height: 70vh;
|
||||||
|
max-height: 600px;
|
||||||
|
background: var(--background-color);
|
||||||
|
border: 1px solid var(--normal-border-color);
|
||||||
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
|
||||||
|
z-index: 2000;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chat-frame {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 500px) {
|
||||||
|
.chat-floating {
|
||||||
|
right: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 60vh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
@close="closeMilkTeaPopup"
|
@close="closeMilkTeaPopup"
|
||||||
/>
|
/>
|
||||||
<NotificationSettingPopup :visible="showNotificationPopup" @close="closeNotificationPopup" />
|
<NotificationSettingPopup :visible="showNotificationPopup" @close="closeNotificationPopup" />
|
||||||
|
<MessagePopup :visible="showMessagePopup" @close="closeMessagePopup" />
|
||||||
<MedalPopup :visible="showMedalPopup" :medals="newMedals" @close="closeMedalPopup" />
|
<MedalPopup :visible="showMedalPopup" :medals="newMedals" @close="closeMedalPopup" />
|
||||||
|
|
||||||
<ActivityPopup
|
<ActivityPopup
|
||||||
@@ -22,6 +23,7 @@
|
|||||||
import ActivityPopup from '~/components/ActivityPopup.vue'
|
import ActivityPopup from '~/components/ActivityPopup.vue'
|
||||||
import MedalPopup from '~/components/MedalPopup.vue'
|
import MedalPopup from '~/components/MedalPopup.vue'
|
||||||
import NotificationSettingPopup from '~/components/NotificationSettingPopup.vue'
|
import NotificationSettingPopup from '~/components/NotificationSettingPopup.vue'
|
||||||
|
import MessagePopup from '~/components/MessagePopup.vue'
|
||||||
import { authState } from '~/utils/auth'
|
import { authState } from '~/utils/auth'
|
||||||
|
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
@@ -33,6 +35,7 @@ const milkTeaIcon = ref('')
|
|||||||
const inviteCodeIcon = ref('')
|
const inviteCodeIcon = ref('')
|
||||||
|
|
||||||
const showNotificationPopup = ref(false)
|
const showNotificationPopup = ref(false)
|
||||||
|
const showMessagePopup = ref(false)
|
||||||
const showMedalPopup = ref(false)
|
const showMedalPopup = ref(false)
|
||||||
const newMedals = ref([])
|
const newMedals = ref([])
|
||||||
|
|
||||||
@@ -43,6 +46,9 @@ onMounted(async () => {
|
|||||||
await checkInviteCodeActivity()
|
await checkInviteCodeActivity()
|
||||||
if (showInviteCodePopup.value) return
|
if (showInviteCodePopup.value) return
|
||||||
|
|
||||||
|
await checkMessageFeature()
|
||||||
|
if (showMessagePopup.value) return
|
||||||
|
|
||||||
await checkNotificationSetting()
|
await checkNotificationSetting()
|
||||||
if (showNotificationPopup.value) return
|
if (showNotificationPopup.value) return
|
||||||
|
|
||||||
@@ -97,6 +103,18 @@ const closeMilkTeaPopup = () => {
|
|||||||
showMilkTeaPopup.value = false
|
showMilkTeaPopup.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const checkMessageFeature = async () => {
|
||||||
|
if (!import.meta.client) return
|
||||||
|
if (!authState.loggedIn) return
|
||||||
|
if (localStorage.getItem('messageFeaturePopupShown')) return
|
||||||
|
showMessagePopup.value = true
|
||||||
|
}
|
||||||
|
const closeMessagePopup = () => {
|
||||||
|
if (!import.meta.client) return
|
||||||
|
localStorage.setItem('messageFeaturePopupShown', 'true')
|
||||||
|
showMessagePopup.value = false
|
||||||
|
}
|
||||||
|
|
||||||
const checkNotificationSetting = async () => {
|
const checkNotificationSetting = async () => {
|
||||||
if (!import.meta.client) return
|
if (!import.meta.client) return
|
||||||
if (!authState.loggedIn) return
|
if (!authState.loggedIn) return
|
||||||
|
|||||||
@@ -6,7 +6,10 @@
|
|||||||
<button class="menu-btn" ref="menuBtn" @click="$emit('toggle-menu')">
|
<button class="menu-btn" ref="menuBtn" @click="$emit('toggle-menu')">
|
||||||
<i class="fas fa-bars"></i>
|
<i class="fas fa-bars"></i>
|
||||||
</button>
|
</button>
|
||||||
<span v-if="isMobile && unreadMessageCount > 0" class="menu-unread-dot"></span>
|
<span
|
||||||
|
v-if="isMobile && (unreadMessageCount > 0 || hasChannelUnread)"
|
||||||
|
class="menu-unread-dot"
|
||||||
|
></span>
|
||||||
</div>
|
</div>
|
||||||
<NuxtLink class="logo-container" :to="`/`" @click="refrechData">
|
<NuxtLink class="logo-container" :to="`/`" @click="refrechData">
|
||||||
<img
|
<img
|
||||||
@@ -53,6 +56,7 @@
|
|||||||
<span v-if="unreadMessageCount > 0" class="unread-badge">{{
|
<span v-if="unreadMessageCount > 0" class="unread-badge">{{
|
||||||
unreadMessageCount
|
unreadMessageCount
|
||||||
}}</span>
|
}}</span>
|
||||||
|
<span v-else-if="hasChannelUnread" class="unread-dot"></span>
|
||||||
</div>
|
</div>
|
||||||
</ToolTip>
|
</ToolTip>
|
||||||
|
|
||||||
@@ -85,6 +89,7 @@ import ToolTip from '~/components/ToolTip.vue'
|
|||||||
import SearchDropdown from '~/components/SearchDropdown.vue'
|
import SearchDropdown from '~/components/SearchDropdown.vue'
|
||||||
import { authState, clearToken, loadCurrentUser } from '~/utils/auth'
|
import { authState, clearToken, loadCurrentUser } from '~/utils/auth'
|
||||||
import { useUnreadCount } from '~/composables/useUnreadCount'
|
import { useUnreadCount } from '~/composables/useUnreadCount'
|
||||||
|
import { useChannelsUnreadCount } from '~/composables/useChannelsUnreadCount'
|
||||||
import { useIsMobile } from '~/utils/screen'
|
import { useIsMobile } from '~/utils/screen'
|
||||||
import { themeState, cycleTheme, ThemeMode } from '~/utils/theme'
|
import { themeState, cycleTheme, ThemeMode } from '~/utils/theme'
|
||||||
import { toast } from '~/main'
|
import { toast } from '~/main'
|
||||||
@@ -103,6 +108,7 @@ const props = defineProps({
|
|||||||
const isLogin = computed(() => authState.loggedIn)
|
const isLogin = computed(() => authState.loggedIn)
|
||||||
const isMobile = useIsMobile()
|
const isMobile = useIsMobile()
|
||||||
const { count: unreadMessageCount, fetchUnreadCount } = useUnreadCount()
|
const { count: unreadMessageCount, fetchUnreadCount } = useUnreadCount()
|
||||||
|
const { hasUnread: hasChannelUnread, fetchChannelUnread } = useChannelsUnreadCount()
|
||||||
const avatar = ref('')
|
const avatar = ref('')
|
||||||
const showSearch = ref(false)
|
const showSearch = ref(false)
|
||||||
const searchDropdown = ref(null)
|
const searchDropdown = ref(null)
|
||||||
@@ -143,8 +149,20 @@ const copyInviteLink = async () => {
|
|||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
const inviteLink = data.token ? `${WEBSITE_BASE_URL}/signup?invite_token=${data.token}` : ''
|
const inviteLink = data.token ? `${WEBSITE_BASE_URL}/signup?invite_token=${data.token}` : ''
|
||||||
await navigator.clipboard.writeText(inviteLink)
|
/**
|
||||||
toast.success('邀请链接已复制')
|
* navigator.clipboard在webkit中有点奇怪的行为
|
||||||
|
* https://stackoverflow.com/questions/62327358/javascript-clipboard-api-safari-ios-notallowederror-message
|
||||||
|
* https://webkit.org/blog/10247/new-webkit-features-in-safari-13-1/
|
||||||
|
*/
|
||||||
|
setTimeout(() => {
|
||||||
|
navigator.clipboard.writeText(inviteLink)
|
||||||
|
.then(() => {
|
||||||
|
toast.success('邀请链接已复制')
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
toast.error('邀请链接复制失败')
|
||||||
|
})
|
||||||
|
}, 0)
|
||||||
} else {
|
} else {
|
||||||
const data = await res.json().catch(() => ({}))
|
const data = await res.json().catch(() => ({}))
|
||||||
toast.error(data.error || '生成邀请链接失败')
|
toast.error(data.error || '生成邀请链接失败')
|
||||||
@@ -227,8 +245,10 @@ onMounted(async () => {
|
|||||||
}
|
}
|
||||||
const updateUnread = async () => {
|
const updateUnread = async () => {
|
||||||
if (authState.loggedIn) {
|
if (authState.loggedIn) {
|
||||||
// Initialize the unread count composable
|
|
||||||
fetchUnreadCount()
|
fetchUnreadCount()
|
||||||
|
fetchChannelUnread()
|
||||||
|
} else {
|
||||||
|
fetchChannelUnread()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -413,6 +433,16 @@ onMounted(async () => {
|
|||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.unread-dot {
|
||||||
|
position: absolute;
|
||||||
|
top: -2px;
|
||||||
|
right: -4px;
|
||||||
|
width: 8px;
|
||||||
|
height: 8px;
|
||||||
|
border-radius: 50%;
|
||||||
|
background-color: #ff4d4f;
|
||||||
|
}
|
||||||
|
|
||||||
.rss-icon {
|
.rss-icon {
|
||||||
animation: rss-glow 2s 3;
|
animation: rss-glow 2s 3;
|
||||||
}
|
}
|
||||||
|
|||||||
74
frontend_nuxt/components/MessagePopup.vue
Normal file
74
frontend_nuxt/components/MessagePopup.vue
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
<template>
|
||||||
|
<BasePopup :visible="visible" @close="close">
|
||||||
|
<div class="message-popup">
|
||||||
|
<div class="message-popup-title">📨 站内信上线啦</div>
|
||||||
|
<div class="message-popup-text">现在可以在右上角使用站内信功能</div>
|
||||||
|
<div class="message-popup-actions">
|
||||||
|
<div class="message-popup-close" @click="close">知道了</div>
|
||||||
|
<div class="message-popup-button" @click="gotoMessage">去看看</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</BasePopup>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import BasePopup from '~/components/BasePopup.vue'
|
||||||
|
|
||||||
|
defineProps({
|
||||||
|
visible: { type: Boolean, default: false },
|
||||||
|
})
|
||||||
|
const emit = defineEmits(['close'])
|
||||||
|
|
||||||
|
const gotoMessage = () => {
|
||||||
|
emit('close')
|
||||||
|
navigateTo('/message-box', { replace: true })
|
||||||
|
}
|
||||||
|
const close = () => emit('close')
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.message-popup {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
text-align: center;
|
||||||
|
gap: 10px;
|
||||||
|
min-width: 200px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-popup-title {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: bold;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-popup-actions {
|
||||||
|
margin-top: 10px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
gap: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-popup-button {
|
||||||
|
background-color: var(--primary-color);
|
||||||
|
color: #fff;
|
||||||
|
padding: 8px 16px;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-popup-button:hover {
|
||||||
|
background-color: var(--primary-color-hover);
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-popup-close {
|
||||||
|
cursor: pointer;
|
||||||
|
color: var(--primary-color);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-popup-close:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -3,20 +3,37 @@
|
|||||||
<div class="reactions-viewer">
|
<div class="reactions-viewer">
|
||||||
<div
|
<div
|
||||||
class="reactions-viewer-item-container"
|
class="reactions-viewer-item-container"
|
||||||
@click="openPanel"
|
|
||||||
@mouseenter="cancelHide"
|
@mouseenter="cancelHide"
|
||||||
@mouseleave="scheduleHide"
|
@mouseleave="scheduleHide"
|
||||||
>
|
>
|
||||||
<template v-if="displayedReactions.length">
|
<template v-if="reactions.length < 4">
|
||||||
<div v-for="r in displayedReactions" :key="r.type" class="reactions-viewer-item">
|
<div
|
||||||
{{ reactionEmojiMap[r.type] }}
|
v-for="r in displayedReactions"
|
||||||
|
:key="r.type"
|
||||||
|
class="reactions-viewer-single-item"
|
||||||
|
:class="{ selected: userReacted(r.type) }"
|
||||||
|
@click="toggleReaction(r.type)"
|
||||||
|
>
|
||||||
|
<img :src="reactionEmojiMap[r.type]" class="emoji" alt="emoji" />
|
||||||
|
<div>{{ counts[r.type] }}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="reactions-viewer-item placeholder" @click="openPanel">
|
||||||
|
<i class="far fa-smile"></i>
|
||||||
|
<!-- <span class="reactions-viewer-item-placeholder-text">点击以表态</span> -->
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<template v-else-if="displayedReactions.length">
|
||||||
|
<div
|
||||||
|
v-for="r in displayedReactions"
|
||||||
|
:key="r.type"
|
||||||
|
class="reactions-viewer-item"
|
||||||
|
@click="openPanel"
|
||||||
|
>
|
||||||
|
<img :src="reactionEmojiMap[r.type]" class="emoji" alt="emoji" />
|
||||||
</div>
|
</div>
|
||||||
<div class="reactions-count">{{ totalCount }}</div>
|
<div class="reactions-count">{{ totalCount }}</div>
|
||||||
</template>
|
</template>
|
||||||
<div v-else class="reactions-viewer-item placeholder">
|
|
||||||
<i class="far fa-smile"></i>
|
|
||||||
<span class="reactions-viewer-item-placeholder-text">点击以表态</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="make-reaction-container">
|
<div class="make-reaction-container">
|
||||||
@@ -40,7 +57,9 @@
|
|||||||
@click="toggleReaction(t)"
|
@click="toggleReaction(t)"
|
||||||
:class="{ selected: userReacted(t) }"
|
:class="{ selected: userReacted(t) }"
|
||||||
>
|
>
|
||||||
{{ reactionEmojiMap[t] }}<span v-if="counts[t]">{{ counts[t] }}</span>
|
<img :src="reactionEmojiMap[t]" class="emoji" alt="emoji" /><span v-if="counts[t]">{{
|
||||||
|
counts[t]
|
||||||
|
}}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -217,13 +236,6 @@ onMounted(async () => {
|
|||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.reactions-viewer-item.placeholder {
|
|
||||||
opacity: 0.5;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: row;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.reactions-viewer-item-placeholder-text {
|
.reactions-viewer-item-placeholder-text {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
padding-left: 5px;
|
padding-left: 5px;
|
||||||
@@ -262,18 +274,16 @@ onMounted(async () => {
|
|||||||
|
|
||||||
.reactions-panel {
|
.reactions-panel {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
bottom: 40px;
|
bottom: 50px;
|
||||||
left: -20px;
|
|
||||||
background-color: var(--background-color);
|
background-color: var(--background-color);
|
||||||
border: 1px solid var(--normal-border-color);
|
border: 1px solid var(--normal-border-color);
|
||||||
border-radius: 5px;
|
border-radius: 20px;
|
||||||
padding: 5px;
|
padding: 5px 10px;
|
||||||
max-width: 240px;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
gap: 2px;
|
gap: 5px;
|
||||||
box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
|
box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,6 +297,27 @@ onMounted(async () => {
|
|||||||
gap: 2px;
|
gap: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.reactions-viewer-item.placeholder,
|
||||||
|
.reactions-viewer-single-item {
|
||||||
|
display: flex;
|
||||||
|
cursor: pointer;
|
||||||
|
flex-direction: row;
|
||||||
|
padding: 2px 10px;
|
||||||
|
gap: 5px;
|
||||||
|
border: 1px solid var(--normal-border-color);
|
||||||
|
border-radius: 10px;
|
||||||
|
margin-right: 5px;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--text-color);
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reactions-viewer-item.placeholder,
|
||||||
|
.reactions-viewer-single-item.selected {
|
||||||
|
background-color: var(--menu-selected-background-color);
|
||||||
|
}
|
||||||
|
|
||||||
.reaction-option.selected {
|
.reaction-option.selected {
|
||||||
background-color: var(--menu-selected-background-color);
|
background-color: var(--menu-selected-background-color);
|
||||||
}
|
}
|
||||||
|
|||||||
92
frontend_nuxt/composables/useChannelsUnreadCount.js
Normal file
92
frontend_nuxt/composables/useChannelsUnreadCount.js
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import { ref, computed, watch } from 'vue'
|
||||||
|
import { useWebSocket } from './useWebSocket'
|
||||||
|
import { getToken } from '~/utils/auth'
|
||||||
|
|
||||||
|
const count = ref(0)
|
||||||
|
let isInitialized = false
|
||||||
|
let wsSubscription = null
|
||||||
|
|
||||||
|
export function useChannelsUnreadCount() {
|
||||||
|
const config = useRuntimeConfig()
|
||||||
|
const API_BASE_URL = config.public.apiBaseUrl
|
||||||
|
const { subscribe, isConnected, connect } = useWebSocket()
|
||||||
|
|
||||||
|
const fetchChannelUnread = async () => {
|
||||||
|
const token = getToken()
|
||||||
|
if (!token) {
|
||||||
|
count.value = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE_URL}/api/channels/unread-count`, {
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
})
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json()
|
||||||
|
count.value = data
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to fetch channel unread count:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialize = () => {
|
||||||
|
const token = getToken()
|
||||||
|
if (!token) {
|
||||||
|
count.value = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fetchChannelUnread()
|
||||||
|
if (!isConnected.value) {
|
||||||
|
connect(token)
|
||||||
|
}
|
||||||
|
setupWebSocketListener()
|
||||||
|
}
|
||||||
|
|
||||||
|
const setupWebSocketListener = () => {
|
||||||
|
if (!wsSubscription) {
|
||||||
|
watch(
|
||||||
|
isConnected,
|
||||||
|
(newValue) => {
|
||||||
|
if (newValue && !wsSubscription) {
|
||||||
|
wsSubscription = subscribe('/user/queue/channel-unread', (message) => {
|
||||||
|
const unread = parseInt(message.body, 10)
|
||||||
|
if (!isNaN(unread)) {
|
||||||
|
count.value = unread
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const setFromList = (channels) => {
|
||||||
|
count.value = Array.isArray(channels) ? channels.filter((c) => c.unreadCount > 0).length : 0
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasUnread = computed(() => count.value > 0)
|
||||||
|
|
||||||
|
const token = getToken()
|
||||||
|
if (token) {
|
||||||
|
if (!isInitialized) {
|
||||||
|
isInitialized = true
|
||||||
|
initialize()
|
||||||
|
} else {
|
||||||
|
fetchChannelUnread()
|
||||||
|
if (!isConnected.value) {
|
||||||
|
connect(token)
|
||||||
|
}
|
||||||
|
setupWebSocketListener()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
count,
|
||||||
|
hasUnread,
|
||||||
|
fetchChannelUnread,
|
||||||
|
initialize,
|
||||||
|
setFromList,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -55,7 +55,10 @@ const subscribe = (destination, callback) => {
|
|||||||
try {
|
try {
|
||||||
const subscription = client.value.subscribe(destination, (message) => {
|
const subscription = client.value.subscribe(destination, (message) => {
|
||||||
try {
|
try {
|
||||||
if (destination.includes('/queue/unread-count')) {
|
if (
|
||||||
|
destination.includes('/queue/unread-count') ||
|
||||||
|
destination.includes('/queue/channel-unread')
|
||||||
|
) {
|
||||||
callback(message)
|
callback(message)
|
||||||
} else {
|
} else {
|
||||||
const parsedMessage = JSON.parse(message.body)
|
const parsedMessage = JSON.parse(message.body)
|
||||||
|
|||||||
@@ -652,6 +652,10 @@ const sanitizeDescription = (text) => stripMarkdown(text)
|
|||||||
}
|
}
|
||||||
|
|
||||||
@container home-page (max-width: 768px) {
|
@container home-page (max-width: 768px) {
|
||||||
|
.topic-item-container {
|
||||||
|
margin-left: 0px;
|
||||||
|
gap: 0px;
|
||||||
|
}
|
||||||
.article-main-container,
|
.article-main-container,
|
||||||
.header-item.main-item {
|
.header-item.main-item {
|
||||||
width: calc(70% - 20px);
|
width: calc(70% - 20px);
|
||||||
@@ -709,6 +713,16 @@ const sanitizeDescription = (text) => stripMarkdown(text)
|
|||||||
|
|
||||||
.topic-container {
|
.topic-container {
|
||||||
position: initial;
|
position: initial;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.topic-item {
|
||||||
|
padding: 10px 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.topic-select-container {
|
||||||
|
margin-left: 10px;
|
||||||
|
margin-top: 10px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -7,6 +7,10 @@
|
|||||||
<h2 class="participant-name">
|
<h2 class="participant-name">
|
||||||
{{ isChannel ? conversationName : otherParticipant?.username }}
|
{{ isChannel ? conversationName : otherParticipant?.username }}
|
||||||
</h2>
|
</h2>
|
||||||
|
<div class="chat-controls">
|
||||||
|
<i v-if="!isFloat" class="fas fa-window-minimize control-icon" @click="minimizeChat"></i>
|
||||||
|
<i v-else class="fas fa-expand control-icon" @click="maximizeChat"></i>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="messages-list" ref="messagesListEl">
|
<div class="messages-list" ref="messagesListEl">
|
||||||
@@ -20,10 +24,15 @@
|
|||||||
{{ loadingMore ? '加载中...' : '查看更多消息' }}
|
{{ loadingMore ? '加载中...' : '查看更多消息' }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<BaseTimeline :items="messages">
|
<BaseTimeline :items="messages" hover>
|
||||||
<template #item="{ item }">
|
<template #item="{ item }">
|
||||||
<div class="message-timestamp">
|
<div class="message-header">
|
||||||
{{ TimeManager.format(item.createdAt) }}
|
<div class="user-name">
|
||||||
|
{{ item.sender.username }}
|
||||||
|
</div>
|
||||||
|
<div class="message-timestamp">
|
||||||
|
{{ TimeManager.format(item.createdAt) }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="message-content">
|
<div class="message-content">
|
||||||
<div class="info-content-text" v-html="renderMarkdown(item.content)"></div>
|
<div class="info-content-text" v-html="renderMarkdown(item.content)"></div>
|
||||||
@@ -64,6 +73,7 @@ import { renderMarkdown } from '~/utils/markdown'
|
|||||||
import MessageEditor from '~/components/MessageEditor.vue'
|
import MessageEditor from '~/components/MessageEditor.vue'
|
||||||
import { useWebSocket } from '~/composables/useWebSocket'
|
import { useWebSocket } from '~/composables/useWebSocket'
|
||||||
import { useUnreadCount } from '~/composables/useUnreadCount'
|
import { useUnreadCount } from '~/composables/useUnreadCount'
|
||||||
|
import { useChannelsUnreadCount } from '~/composables/useChannelsUnreadCount'
|
||||||
import TimeManager from '~/utils/time'
|
import TimeManager from '~/utils/time'
|
||||||
import BaseTimeline from '~/components/BaseTimeline.vue'
|
import BaseTimeline from '~/components/BaseTimeline.vue'
|
||||||
import BasePlaceholder from '~/components/BasePlaceholder.vue'
|
import BasePlaceholder from '~/components/BasePlaceholder.vue'
|
||||||
@@ -73,8 +83,13 @@ const route = useRoute()
|
|||||||
const API_BASE_URL = config.public.apiBaseUrl
|
const API_BASE_URL = config.public.apiBaseUrl
|
||||||
const { connect, disconnect, subscribe, isConnected } = useWebSocket()
|
const { connect, disconnect, subscribe, isConnected } = useWebSocket()
|
||||||
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount()
|
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount()
|
||||||
|
const { fetchChannelUnread: refreshChannelUnread } = useChannelsUnreadCount()
|
||||||
let subscription = null
|
let subscription = null
|
||||||
|
|
||||||
|
const chatFloating = useState('chatFloating', () => false)
|
||||||
|
const chatPath = useState('chatPath', () => '/message-box')
|
||||||
|
const isFloat = computed(() => route.query.float === '1')
|
||||||
|
|
||||||
const messages = ref([])
|
const messages = ref([])
|
||||||
const participants = ref([])
|
const participants = ref([])
|
||||||
const loading = ref(true)
|
const loading = ref(true)
|
||||||
@@ -108,6 +123,24 @@ function handleAvatarError(event) {
|
|||||||
event.target.src = '/default-avatar.svg'
|
event.target.src = '/default-avatar.svg'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function minimizeChat() {
|
||||||
|
chatPath.value = route.fullPath
|
||||||
|
chatFloating.value = true
|
||||||
|
navigateTo('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
function maximizeChat() {
|
||||||
|
if (window.parent) {
|
||||||
|
window.parent.postMessage(
|
||||||
|
{
|
||||||
|
type: 'maximize-chat',
|
||||||
|
path: route.fullPath.replace('?float=1', '').replace('&float=1', ''),
|
||||||
|
},
|
||||||
|
'*',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// No changes needed here, as renderMarkdown is now imported.
|
// No changes needed here, as renderMarkdown is now imported.
|
||||||
// The old function is removed.
|
// The old function is removed.
|
||||||
|
|
||||||
@@ -253,6 +286,7 @@ async function markConversationAsRead() {
|
|||||||
})
|
})
|
||||||
// After marking as read, refresh the global unread count
|
// After marking as read, refresh the global unread count
|
||||||
refreshGlobalUnreadCount()
|
refreshGlobalUnreadCount()
|
||||||
|
refreshChannelUnread()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to mark conversation as read', e)
|
console.error('Failed to mark conversation as read', e)
|
||||||
}
|
}
|
||||||
@@ -403,6 +437,7 @@ onUnmounted(() => {
|
|||||||
font-size: 18px;
|
font-size: 18px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.messages-list {
|
.messages-list {
|
||||||
@@ -448,10 +483,22 @@ onUnmounted(() => {
|
|||||||
.message-timestamp {
|
.message-timestamp {
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
color: var(--text-color-secondary);
|
color: var(--text-color-secondary);
|
||||||
margin-top: 5px;
|
|
||||||
opacity: 0.6;
|
opacity: 0.6;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.message-header {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-name {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-color);
|
||||||
|
}
|
||||||
|
|
||||||
.message-item.sent {
|
.message-item.sent {
|
||||||
align-self: flex-end;
|
align-self: flex-end;
|
||||||
flex-direction: row-reverse;
|
flex-direction: row-reverse;
|
||||||
@@ -504,4 +551,15 @@ onUnmounted(() => {
|
|||||||
margin-left: 10px;
|
margin-left: 10px;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chat-controls {
|
||||||
|
margin-left: auto;
|
||||||
|
cursor: pointer;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.control-icon {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="messages-container">
|
<div class="messages-container">
|
||||||
|
<div class="chat-controls">
|
||||||
|
<i v-if="!isFloat" class="fas fa-window-minimize control-icon" @click="minimizeChat"></i>
|
||||||
|
<i v-else class="fas fa-expand control-icon" @click="maximizeChat"></i>
|
||||||
|
</div>
|
||||||
<div class="tabs">
|
<div class="tabs">
|
||||||
<div :class="['tab', { active: activeTab === 'messages' }]" @click="activeTab = 'messages'">
|
<div :class="['tab', { active: activeTab === 'messages' }]" @click="activeTab = 'messages'">
|
||||||
站内信
|
站内信
|
||||||
@@ -94,10 +98,17 @@
|
|||||||
{{ ch.name }}
|
{{ ch.name }}
|
||||||
<span v-if="ch.unreadCount > 0" class="unread-dot"></span>
|
<span v-if="ch.unreadCount > 0" class="unread-dot"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="message-time">成员 {{ ch.memberCount }}</div>
|
<div class="message-time">
|
||||||
|
{{ formatTime(ch.lastMessage?.createdAt || ch.createdAt) }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="last-message-row">
|
<div class="last-message-row">
|
||||||
<div class="last-message">{{ ch.description }}</div>
|
<div class="last-message">
|
||||||
|
{{
|
||||||
|
ch.lastMessage ? stripMarkdownLength(ch.lastMessage.content, 100) : ch.description
|
||||||
|
}}
|
||||||
|
</div>
|
||||||
|
<div class="member-count">成员 {{ ch.memberCount }}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -107,12 +118,13 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onUnmounted, watch, onActivated } from 'vue'
|
import { ref, onUnmounted, watch, onActivated, computed } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter, useRoute } from 'vue-router'
|
||||||
import { getToken, fetchCurrentUser } from '~/utils/auth'
|
import { getToken, fetchCurrentUser } from '~/utils/auth'
|
||||||
import { toast } from '~/main'
|
import { toast } from '~/main'
|
||||||
import { useWebSocket } from '~/composables/useWebSocket'
|
import { useWebSocket } from '~/composables/useWebSocket'
|
||||||
import { useUnreadCount } from '~/composables/useUnreadCount'
|
import { useUnreadCount } from '~/composables/useUnreadCount'
|
||||||
|
import { useChannelsUnreadCount } from '~/composables/useChannelsUnreadCount'
|
||||||
import TimeManager from '~/utils/time'
|
import TimeManager from '~/utils/time'
|
||||||
import { stripMarkdownLength } from '~/utils/markdown'
|
import { stripMarkdownLength } from '~/utils/markdown'
|
||||||
import SearchPersonDropdown from '~/components/SearchPersonDropdown.vue'
|
import SearchPersonDropdown from '~/components/SearchPersonDropdown.vue'
|
||||||
@@ -123,12 +135,19 @@ const conversations = ref([])
|
|||||||
const loading = ref(true)
|
const loading = ref(true)
|
||||||
const error = ref(null)
|
const error = ref(null)
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const route = useRoute()
|
||||||
const currentUser = ref(null)
|
const currentUser = ref(null)
|
||||||
const API_BASE_URL = config.public.apiBaseUrl
|
const API_BASE_URL = config.public.apiBaseUrl
|
||||||
const { connect, disconnect, subscribe, isConnected } = useWebSocket()
|
const { connect, disconnect, subscribe, isConnected } = useWebSocket()
|
||||||
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount()
|
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount()
|
||||||
|
const { fetchChannelUnread: refreshChannelUnread, setFromList: setChannelUnreadFromList } =
|
||||||
|
useChannelsUnreadCount()
|
||||||
let subscription = null
|
let subscription = null
|
||||||
|
|
||||||
|
const chatFloating = useState('chatFloating', () => false)
|
||||||
|
const chatPath = useState('chatPath', () => '/message-box')
|
||||||
|
const isFloat = computed(() => route.query.float === '1')
|
||||||
|
|
||||||
const activeTab = ref('messages')
|
const activeTab = ref('messages')
|
||||||
const channels = ref([])
|
const channels = ref([])
|
||||||
const loadingChannels = ref(false)
|
const loadingChannels = ref(false)
|
||||||
@@ -185,7 +204,9 @@ async function fetchChannels() {
|
|||||||
headers: { Authorization: `Bearer ${token}` },
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
})
|
})
|
||||||
if (!response.ok) throw new Error('无法加载频道')
|
if (!response.ok) throw new Error('无法加载频道')
|
||||||
channels.value = await response.json()
|
const data = await response.json()
|
||||||
|
channels.value = data
|
||||||
|
setChannelUnreadFromList(data)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.error(e.message)
|
toast.error(e.message)
|
||||||
} finally {
|
} finally {
|
||||||
@@ -217,6 +238,24 @@ async function goToChannel(id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function minimizeChat() {
|
||||||
|
chatPath.value = route.fullPath
|
||||||
|
chatFloating.value = true
|
||||||
|
navigateTo('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
function maximizeChat() {
|
||||||
|
if (window.parent) {
|
||||||
|
window.parent.postMessage(
|
||||||
|
{
|
||||||
|
type: 'maximize-chat',
|
||||||
|
path: route.fullPath.replace('?float=1', '').replace('&float=1', ''),
|
||||||
|
},
|
||||||
|
'*',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onActivated(async () => {
|
onActivated(async () => {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
currentUser.value = await fetchCurrentUser()
|
currentUser.value = await fetchCurrentUser()
|
||||||
@@ -224,6 +263,7 @@ onActivated(async () => {
|
|||||||
if (currentUser.value) {
|
if (currentUser.value) {
|
||||||
await fetchConversations()
|
await fetchConversations()
|
||||||
refreshGlobalUnreadCount() // Refresh global count when entering the list
|
refreshGlobalUnreadCount() // Refresh global count when entering the list
|
||||||
|
refreshChannelUnread()
|
||||||
const token = getToken()
|
const token = getToken()
|
||||||
if (token && !isConnected.value) {
|
if (token && !isConnected.value) {
|
||||||
connect(token)
|
connect(token)
|
||||||
@@ -244,6 +284,9 @@ watch(isConnected, (newValue) => {
|
|||||||
|
|
||||||
subscription = subscribe(destination, (message) => {
|
subscription = subscribe(destination, (message) => {
|
||||||
fetchConversations()
|
fetchConversations()
|
||||||
|
if (activeTab.value === 'channels') {
|
||||||
|
fetchChannels()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -262,8 +305,7 @@ function goToConversation(id) {
|
|||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.messages-container {
|
.messages-container {
|
||||||
margin: 0 auto;
|
position: relative;
|
||||||
padding: 20px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.tabs {
|
.tabs {
|
||||||
@@ -273,13 +315,13 @@ function goToConversation(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.tab {
|
.tab {
|
||||||
padding: 8px 16px;
|
padding: 10px 20px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tab.active {
|
.tab.active {
|
||||||
font-weight: 600;
|
|
||||||
border-bottom: 2px solid var(--primary-color);
|
border-bottom: 2px solid var(--primary-color);
|
||||||
|
color: var(--primary-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
.loading-message {
|
.loading-message {
|
||||||
@@ -291,6 +333,8 @@ function goToConversation(id) {
|
|||||||
|
|
||||||
.search-container {
|
.search-container {
|
||||||
margin-bottom: 24px;
|
margin-bottom: 24px;
|
||||||
|
margin-left: 20px;
|
||||||
|
margin-right: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.messages-header {
|
.messages-header {
|
||||||
@@ -330,6 +374,8 @@ function goToConversation(id) {
|
|||||||
.conversation-item {
|
.conversation-item {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
margin-left: 20px;
|
||||||
|
margin-right: 20px;
|
||||||
padding: 8px 10px;
|
padding: 8px 10px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: background-color 0.2s ease;
|
transition: background-color 0.2s ease;
|
||||||
@@ -369,6 +415,12 @@ function goToConversation(id) {
|
|||||||
color: var(--text-color);
|
color: var(--text-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.member-count {
|
||||||
|
font-size: 12px;
|
||||||
|
color: gray;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
.message-time {
|
.message-time {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: gray;
|
color: gray;
|
||||||
@@ -413,10 +465,23 @@ function goToConversation(id) {
|
|||||||
margin-left: 4px;
|
margin-left: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chat-controls {
|
||||||
|
position: absolute;
|
||||||
|
top: 10px;
|
||||||
|
right: 10px;
|
||||||
|
cursor: pointer;
|
||||||
|
z-index: 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
.control-icon {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
/* 响应式设计 */
|
/* 响应式设计 */
|
||||||
@media (max-width: 768px) {
|
@media (max-width: 768px) {
|
||||||
.messages-container {
|
.conversation-item {
|
||||||
padding: 10px 10px;
|
margin-left: 10px;
|
||||||
|
margin-right: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.messages-title {
|
.messages-title {
|
||||||
|
|||||||
@@ -1,25 +1,34 @@
|
|||||||
export const reactionEmojiMap = {
|
const toCdnUrl = (emoji) => {
|
||||||
LIKE: '❤️',
|
const codepoints = Array.from(emoji)
|
||||||
DISLIKE: '👎',
|
.map((c) => c.codePointAt(0).toString(16))
|
||||||
RECOMMEND: '👏',
|
.join('_')
|
||||||
ANGRY: '😡',
|
return `https://fonts.gstatic.com/s/e/notoemoji/latest/${codepoints}/emoji.svg`
|
||||||
FLUSHED: '😳',
|
}
|
||||||
STAR_STRUCK: '🤩',
|
|
||||||
ROFL: '🤣',
|
export const reactionEmojiMap = {
|
||||||
HOLDING_BACK_TEARS: '🥹',
|
LIKE: toCdnUrl('❤️'),
|
||||||
MIND_BLOWN: '🤯',
|
SMILE: toCdnUrl('😁'),
|
||||||
POOP: '💩',
|
DISLIKE: toCdnUrl('👎'),
|
||||||
CLOWN: '🤡',
|
RECOMMEND: toCdnUrl('👏'),
|
||||||
SKULL: '☠️',
|
CONGRATULATIONS: toCdnUrl('🎉'),
|
||||||
FIRE: '🔥',
|
ANGRY: toCdnUrl('😡'),
|
||||||
EYES: '👀',
|
FLUSHED: toCdnUrl('😳'),
|
||||||
FROWN: '☹️',
|
STAR_STRUCK: toCdnUrl('🤩'),
|
||||||
HOT: '🥵',
|
ROFL: toCdnUrl('🤣'),
|
||||||
EAGLE: '🦅',
|
HOLDING_BACK_TEARS: toCdnUrl('🥹'),
|
||||||
SPIDER: '🕷️',
|
MIND_BLOWN: toCdnUrl('🤯'),
|
||||||
BAT: '🦇',
|
POOP: toCdnUrl('💩'),
|
||||||
CHINA: '🇨🇳',
|
CLOWN: toCdnUrl('🤡'),
|
||||||
USA: '🇺🇸',
|
SKULL: toCdnUrl('☠️'),
|
||||||
JAPAN: '🇯🇵',
|
FIRE: toCdnUrl('🔥'),
|
||||||
KOREA: '🇰🇷',
|
EYES: toCdnUrl('👀'),
|
||||||
|
FROWN: toCdnUrl('☹️'),
|
||||||
|
HOT: toCdnUrl('🥵'),
|
||||||
|
EAGLE: toCdnUrl('🦅'),
|
||||||
|
SPIDER: toCdnUrl('🕷️'),
|
||||||
|
BAT: toCdnUrl('🦇'),
|
||||||
|
CHINA: toCdnUrl('🇨🇳'),
|
||||||
|
USA: toCdnUrl('🇺🇸'),
|
||||||
|
JAPAN: toCdnUrl('🇯🇵'),
|
||||||
|
KOREA: toCdnUrl('🇰🇷'),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import Vditor from 'vditor'
|
|||||||
import { getToken, authState } from './auth'
|
import { getToken, authState } from './auth'
|
||||||
import { searchUsers, fetchFollowings, fetchAdmins } from './user'
|
import { searchUsers, fetchFollowings, fetchAdmins } from './user'
|
||||||
import { tiebaEmoji } from './tiebaEmoji'
|
import { tiebaEmoji } from './tiebaEmoji'
|
||||||
|
import vditorPostCitation from './vditorPostCitation.js'
|
||||||
|
|
||||||
export function getEditorTheme() {
|
export function getEditorTheme() {
|
||||||
return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
|
return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'
|
||||||
@@ -79,6 +80,7 @@ export function createVditor(editorId, options = {}) {
|
|||||||
}))
|
}))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
vditorPostCitation(API_BASE_URL),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
cdn: 'https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/vditor',
|
cdn: 'https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/vditor',
|
||||||
|
|||||||
37
frontend_nuxt/utils/vditorPostCitation.js
Normal file
37
frontend_nuxt/utils/vditorPostCitation.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { authState, getToken } from '~/utils/auth'
|
||||||
|
|
||||||
|
async function searchPost(apiBaseUrl, keyword) {
|
||||||
|
return await fetch(`${apiBaseUrl}/api/search/posts/title?keyword=${keyword}`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${getToken()}`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export default (apiBaseUrl) => {
|
||||||
|
return {
|
||||||
|
key: '#',
|
||||||
|
hint: async (keyword) => {
|
||||||
|
if (!keyword.trim()) return []
|
||||||
|
try {
|
||||||
|
const response = await searchPost(apiBaseUrl, keyword)
|
||||||
|
if (response.ok) {
|
||||||
|
const body = await response.json()
|
||||||
|
let value = ''
|
||||||
|
return (
|
||||||
|
body.map((item) => ({
|
||||||
|
value: `[${item.title}](/posts/${item.id})`,
|
||||||
|
html: `<div>${item.title}</div>`,
|
||||||
|
})) ?? []
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user