Compare commits

...

17 Commits

Author SHA1 Message Date
zzc
4fb8a05f8d feat/info-comment-jump 2026-06-25 07:35:36 +08:00
zzc
8d2337e5b7 feat: joinedDay 2026-06-22 18:13:10 +08:00
zzc
d14084c2a1 fix: main data 2026-06-22 18:07:44 +08:00
zzc
d1d4481ff5 fix: comment rick 2026-06-22 15:48:20 +08:00
zzc
a47b0e2a7a feat: rating 2026-06-22 15:33:26 +08:00
zzc
7a332ba44d fix: pk comment 2026-06-15 23:37:33 +08:00
zzc
b79e1e8204 fix: pk comment 2026-06-15 23:14:05 +08:00
zzc
88f4220432 fix: pk comment 2026-06-15 22:29:03 +08:00
zzc
3c7e520062 fix: pk comment 2026-06-15 19:59:07 +08:00
zzc
f256df8f94 fix: pk 2026-06-15 19:38:40 +08:00
zzc
082cd011d2 feat: repley 2026-06-15 08:56:18 +08:00
zzc
cd1595373b feat: repley 2026-06-15 07:59:47 +08:00
zzc
9a28a0929d feat: Notifications 2026-06-15 07:18:36 +08:00
zzc
179abe01b0 feat: rating 2026-06-15 00:15:23 +08:00
zzc
26d3536cdf feat: rating 2026-06-14 23:22:17 +08:00
zzc
68ad3b0091 feat: rating 2026-06-14 23:20:07 +08:00
zzc
4f7613f71c feat: pk 2026-06-12 01:26:41 +08:00
35 changed files with 6023 additions and 457 deletions

View File

@@ -1,6 +1,7 @@
<script>
import { useUserStore } from "./stores/user";
import { userOpenApp } from "./api/auth";
import { refreshMessageTabBarBadge } from "./utils/tabBar";
const openApp = async () => {
try {
@@ -22,10 +23,14 @@ export default {
onLaunch() {
const userStore = useUserStore();
userStore.loadFromStorage();
refreshMessageTabBarBadge();
if (userStore.userInfo.id) {
openApp();
}
},
onShow() {
refreshMessageTabBarBadge();
},
};
</script>

60
api/notification.js Normal file
View File

@@ -0,0 +1,60 @@
import { request } from "@/utils/request.js";
export const RatingNotificationTypeEnum = {
TOPIC_SUBMIT: 'topic_submit',
TOPIC_APPROVED: 'topic_approved',
TOPIC_REJECTED: 'topic_rejected',
ITEM_SUBMIT: 'item_submit',
ITEM_APPROVED: 'item_approved',
ITEM_REJECTED: 'item_rejected',
COMMENT: 'comment',
COMMENT_LIKE: 'comment_like',
SYSTEM: 'system',
}
/**
* @description: 获取当前用户的消息列表
* @param {page: number, type?: RatingNotificationTypeEnum} data
* @return
*/
export const fetchNotificationList = async (data) => {
return request({
url: "/api/rating/personal/notification/list",
method: "GET",
data,
});
};
/**
* @description: 获取当前用户未读消息数量
* @return
*/
export const fetchUnreadCount = async () => {
return request({
url: "/api/rating/personal/notification/unread_count",
method: "GET",
});
};
/**
* @description: 标记指定消息为已读
* @param {id: number} data
* @return
*/
export const markAsRead = async (id) => {
return request({
url: "/api/rating/personal/notification/read/" + id,
method: "Patch",
});
};
/**
* @description: 标记所有消息为已读
* @return
*/
export const markAllAsRead = async () => {
return request({
url: "/api/rating/personal/notification/read_all",
method: "Patch",
});
};

87
api/pk.js Normal file
View File

@@ -0,0 +1,87 @@
import { request } from "@/utils/request.js";
// 获取随机一组PK主题
export const fetchRandomTopicList = async () => {
return request({
url: "/api/rating/pk/random",
method: "GET",
});
};
/**
* 用户投票
* @param { pkId, itemId }
*/
export const vote = async (data) => {
return request({
url: "/api/rating/pk/vote",
method: "POST",
data,
});
};
/**
* 获取PK 评论列表
* @param { pkId: string, page: number, orderBy: string }
* @param { orderBy: string } 排序字段,可选值:'hot' | 'new',
*/
export const fetchCommentList = async (data) => {
return request({
url: "/api/rating/pk/comment/list",
method: "GET",
data,
});
};
/**
* 获取PK 评论回复
* @param { pkId: string, rootCommentId: string, page: number }
*/
export const fetchCommentReplyList = async (data) => {
return request({
url: "/api/rating/pk/comment/replies",
method: "GET",
data,
});
};
/**
* 发布PK 评论
* @param { pkId: string, content: string, parentCommentId?: string, replyToCommentId?: string }
* @param { parentCommentId?: string } 父评论ID回复时必填
* @param { replyToCommentId?: string } 回复的评论ID回复时必填
*/
export const publishComment = async (data) => {
return request({
url: "/api/rating/pk/comment",
method: "POST",
data,
});
};
/**
* PK 评论 点赞
* @param { commentId: string }
*/
export const likeComment = async (data) => {
return request({
url: "/api/rating/pk/comment/like",
method: "POST",
data,
});
};
/**
* 获取用户 pk 列表
* @param { page: number }
*/
export const fetchUserPkList = async (data) => {
return request({
url: "/api/rating/personal/pk/list",
method: "GET",
data,
});
};

View File

@@ -10,6 +10,7 @@ export const topicItemScore = async (data) => {
};
// 评论
// data = { topicId, itemId, content, parentCommentId?, replyToCommentId? }
export const topicItemComment = async (data) => {
return request({
url: "/api/rating/topic/comment/item",
@@ -29,13 +30,25 @@ export const topicItemCommentLike = async (data) => {
};
// 获取评论列表
export const topicItemCommentList = async (topicId, itemId, page = 1, orderBy = 1) => {
export const topicItemCommentList = async (topicId, itemId, page = 1, orderBy = 'hot') => {
return request({
url: `/api/rating/topic/comment/item/list?topicId=${topicId}&itemId=${itemId}&page=${page}&orderBy=${orderBy}`,
method: "GET",
});
};
/*
* 获取评论回复列表
* @param {rootCommentId: string, page: number} data
*/
export const topicItemCommentReplyList = async (data) => {
return request({
url: "/api/rating/topic/comment/replies",
method: "GET",
data,
});
};
// 获取评分项详情
export const fetchTopicRatingItems = async (itemId) => {
return request({
@@ -52,4 +65,4 @@ export const topicItemAdd = async (data) => {
method: "POST",
data,
});
};
};

View File

@@ -21,7 +21,7 @@
type="text"
v-model="commentText"
:disabled="sending"
placeholder="说点什么吧..."
:placeholder="inputPlaceholder"
placeholder-class="input-placeholder"
/>
<button class="send-btn" :class="{ 'is-disabled': sending }" :disabled="sending" @tap="handleSend">
@@ -32,7 +32,7 @@
</template>
<script setup>
import { ref, watch } from 'vue';
import { computed, ref, watch } from 'vue';
const props = defineProps({
initialAction: {
@@ -55,6 +55,7 @@ watch(() => props.initialAction, (newVal) => {
});
const commentText = ref('');
const inputPlaceholder = computed(() => '说点什么吧...');
const actions = [
{ label: '拉', value: 'terrible', emoji: '👎', score: 2, scoreType:1 },

View File

@@ -1,24 +1,92 @@
<template>
<view class="comment-item">
<image class="c-avatar" :src="comment.avatar" mode="aspectFill" />
<view
class="comment-item"
:id="`comment-${localComment.id}`"
:class="{ 'comment-item--anchored': localComment.id === props.activeAnchorId }"
>
<image class="c-avatar" :src="localComment.avatar" mode="aspectFill" />
<view class="c-content-wrap">
<view class="c-header">
<view class="c-user-info">
<text class="c-name">{{ comment.name }}</text>
<!-- 用户态度徽章 -->
<ActionBadge v-if="comment.userAction" :action="comment.userAction" />
<view class="c-badge" v-if="comment.badge">
<text class="b-icon">{{ comment.badgeIcon }}</text>
<text class="b-text">{{ comment.badge }}</text>
<text class="c-name">{{ localComment.name }}</text>
<view
v-if="localComment.choiceLabel"
class="choice-tag"
:class="localComment.choiceTone ? `choice-tag--${localComment.choiceTone}` : ''"
>
<text class="choice-tag-text">{{ localComment.choiceLabel }}</text>
</view>
<ActionBadge v-if="localComment.userAction" :action="localComment.userAction" />
<view class="c-badge" v-if="localComment.badge">
<text class="b-icon">{{ localComment.badgeIcon }}</text>
<text class="b-text">{{ localComment.badge }}</text>
</view>
</view>
<view class="c-like" @tap="handleLike">
<text class="like-icon" :class="{ 'is-liked': isLiked }"></text>
<text class="like-count" :class="{ 'liked-text': isLiked }">{{ currentLikes }}</text>
<view class="c-like" @tap="handleLike()">
<text class="like-icon" :class="{ 'is-liked': localComment.isLiked }"></text>
<text class="like-count" :class="{ 'liked-text': localComment.isLiked }">{{ localComment.likes }}</text>
</view>
</view>
<text class="c-time">{{ localComment.time }}</text>
<text class="c-text">{{ localComment.content }}</text>
<view class="c-actions">
<text class="c-action-btn" @tap="emitReply(localComment)">回复</text>
<text
v-if="localComment.replyCount && !localComment.repliesLoaded && !localComment.repliesLoading"
class="c-action-link"
@tap="emit('toggle-replies', localComment)"
>
查看 {{ localComment.replyCount }} 条回复
</text>
<text v-else-if="localComment.repliesLoading" class="c-action-count">回复加载中...</text>
<text v-else-if="localComment.replyCount" class="c-action-count">{{ localComment.replyCount }} 条回复</text>
</view>
<view v-if="localComment.replies && localComment.replies.length" class="reply-panel">
<view
v-for="reply in localComment.replies"
:key="reply.id"
class="reply-item"
:id="`reply-${reply.id}`"
:class="{ 'reply-item--anchored': reply.id === props.activeAnchorId }"
>
<image class="reply-avatar" :src="reply.avatar" mode="aspectFill" />
<view class="reply-main">
<view class="reply-head">
<view class="reply-user-wrap">
<text class="reply-name">{{ reply.name }}</text>
<ActionBadge v-if="reply.userAction" :action="reply.userAction" />
</view>
<view class="c-like reply-like" @tap="handleLike(reply)">
<text class="like-icon reply-like-icon" :class="{ 'is-liked': reply.isLiked }"></text>
<text class="like-count" :class="{ 'liked-text': reply.isLiked }">{{ reply.likes }}</text>
</view>
</view>
<text class="reply-time">{{ reply.time }}</text>
<text class="reply-text">
<text v-if="reply.replyToName" class="reply-at">@{{ reply.replyToName }} </text>{{ reply.content }}
</text>
<view class="reply-footer">
<text class="reply-btn" @tap="emitReply(reply)">回复</text>
</view>
</view>
</view>
<view class="reply-panel-footer">
<text
v-if="localComment.repliesHasMore && !localComment.repliesLoading"
class="reply-more-btn"
@tap="emit('more-replies', localComment)"
>
查看更多回复
</text>
<text v-else-if="localComment.repliesLoading" class="reply-more-loading">加载更多中...</text>
</view>
</view>
<text class="c-time">{{ comment.time }}</text>
<text class="c-text">{{ comment.content }}</text>
</view>
</view>
</template>
@@ -34,78 +102,161 @@ const props = defineProps({
type: Object,
required: true,
default: () => ({})
},
likeHandler: {
type: Function,
default: null
},
pageName: {
type: String,
default: 'rating_detail'
},
activeAnchorId: {
type: String,
default: ''
}
});
const emit = defineEmits(['need-login', 'like-change']);
const emit = defineEmits(['need-login', 'like-change', 'reply', 'toggle-replies', 'more-replies']);
const userStore = useUserStore();
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
const isLiked = ref(props.comment.isLiked || false);
const currentLikes = ref(props.comment.likes || 0);
let isLiking = false; // 防抖标志
watch(() => props.comment.likes, (newVal) => {
currentLikes.value = newVal;
const buildReplyItem = (reply = {}) => ({
...reply,
id: reply.id || '',
name: reply.name || '匿名用户',
avatar: reply.avatar || 'https://api.dicebear.com/7.x/avataaars/svg?seed=fallback',
time: reply.time || '刚刚',
content: reply.content || '',
likes: Number(reply.likes || 0),
isLiked: !!reply.isLiked,
replyToName: reply.replyToName || '',
parentCommentId: reply.parentCommentId || '',
replyToCommentId: reply.replyToCommentId || '',
userAction: reply.userAction || ''
});
watch(() => props.comment.isLiked, (newVal) => {
isLiked.value = newVal || false;
const buildCommentState = (comment = {}) => ({
...comment,
likes: Number(comment.likes || 0),
isLiked: !!comment.isLiked,
choiceLabel: comment.choiceLabel || '',
choiceTone: comment.choiceTone || '',
replyCount: Number(comment.replyCount || 0),
repliesLoading: !!comment.repliesLoading,
repliesLoaded: !!comment.repliesLoaded,
repliesHasMore: !!comment.repliesHasMore,
repliesPage: Number(comment.repliesPage || 1),
replies: Array.isArray(comment.replies) ? comment.replies.map(buildReplyItem) : []
});
const handleLike = async () => {
const localComment = ref(buildCommentState(props.comment));
const likingMap = ref({});
watch(
() => props.comment,
(newVal) => {
localComment.value = buildCommentState(newVal || {});
},
{ deep: true }
);
const emitReply = (target) => {
emit('reply', {
rootCommentId: localComment.value.id,
parentCommentId: target.id === localComment.value.id ? localComment.value.id : (target.parentCommentId || localComment.value.id),
replyToCommentId: target.id,
replyToName: target.name,
content: target.content
});
};
const patchLikeState = (targetId, updater) => {
if (targetId === localComment.value.id) {
localComment.value = {
...localComment.value,
...updater(localComment.value)
};
return;
}
localComment.value = {
...localComment.value,
replies: localComment.value.replies.map((reply) => {
if (reply.id !== targetId) return reply;
return {
...reply,
...updater(reply)
};
})
};
};
const handleLike = async (target = null) => {
if (!isLoggedIn.value) {
emit('need-login');
return;
}
if (isLiking) return;
isLiking = true;
const currentTarget = target || localComment.value;
if (!currentTarget?.id || likingMap.value[currentTarget.id]) return;
// 乐观更新 UI
isLiked.value = !isLiked.value;
if (isLiked.value) {
currentLikes.value += 1;
likingMap.value = {
...likingMap.value,
[currentTarget.id]: true
};
const nextLiked = !currentTarget.isLiked;
const nextLikes = nextLiked ? currentTarget.likes + 1 : Math.max(0, currentTarget.likes - 1);
patchLikeState(currentTarget.id, () => ({
isLiked: nextLiked,
likes: nextLikes
}));
if (nextLiked) {
uni.vibrateShort({ type: 'light' });
} else {
currentLikes.value -= 1;
}
emit('like-change', {
id: props.comment.id,
isLiked: isLiked.value,
likes: currentLikes.value
id: currentTarget.id,
parentId: currentTarget.id === localComment.value.id ? '' : localComment.value.id,
isReply: currentTarget.id !== localComment.value.id,
isLiked: nextLiked,
likes: nextLikes
});
try {
// 调用点赞接口 (点赞和取消点赞为同一个接口)
await topicItemCommentLike({ commentId: props.comment.id });
// 记录点赞事件
const likeRequest = props.likeHandler || topicItemCommentLike;
await likeRequest({ commentId: currentTarget.id });
uni.$trackRecord({
eventName: 'like_comment',
eventType: 'like',
elementId: `comment_${props.comment.id}`,
elementContent: `评论项_${props.comment.name}`,
elementId: `comment_${currentTarget.id}`,
elementContent: `评论项_${currentTarget.name}`,
customParams: {
comment: props.comment.content,
page: 'rating_detail'
comment: currentTarget.content,
isReply: currentTarget.id !== localComment.value.id,
page: props.pageName
}
});
} catch (error) {
// 接口调用失败,回滚 UI 状态
isLiked.value = !isLiked.value;
if (isLiked.value) {
currentLikes.value += 1;
} else {
currentLikes.value -= 1;
}
patchLikeState(currentTarget.id, () => ({
isLiked: currentTarget.isLiked,
likes: currentTarget.likes
}));
emit('like-change', {
id: props.comment.id,
isLiked: isLiked.value,
likes: currentLikes.value
id: currentTarget.id,
parentId: currentTarget.id === localComment.value.id ? '' : localComment.value.id,
isReply: currentTarget.id !== localComment.value.id,
isLiked: currentTarget.isLiked,
likes: currentTarget.likes
});
uni.showToast({ title: '操作失败', icon: 'none' });
} finally {
isLiking = false;
const nextMap = { ...likingMap.value };
delete nextMap[currentTarget.id];
likingMap.value = nextMap;
}
};
</script>
@@ -115,13 +266,21 @@ const handleLike = async () => {
display: flex;
padding: 32rpx 0;
position: relative;
transition: background-color 0.2s;
transition: background-color 0.2s, transform 0.2s;
}
.comment-item:active {
background-color: #fafbfe;
}
.comment-item--anchored {
margin: 0 -16rpx;
padding: 32rpx 16rpx;
border-radius: 24rpx;
background: linear-gradient(135deg, rgba(92, 67, 245, 0.1), rgba(92, 67, 245, 0.04));
transform: translateY(-2rpx);
}
.comment-item::after {
content: '';
position: absolute;
@@ -180,6 +339,34 @@ const handleLike = async () => {
.b-icon { font-size: 20rpx; margin-right: 6rpx; }
.b-text { font-size: 20rpx; color: #666; font-weight: bold; }
.choice-tag {
display: inline-flex;
align-items: center;
height: 42rpx;
padding: 0 16rpx;
border-radius: 999rpx;
background: rgba(79, 70, 229, 0.08);
margin-right: 12rpx;
}
.choice-tag--left {
background: rgba(92, 67, 245, 0.1);
}
.choice-tag--right {
background: rgba(255, 141, 95, 0.12);
}
.choice-tag-text {
font-size: 20rpx;
font-weight: 700;
color: #4f46e5;
}
.choice-tag--right .choice-tag-text {
color: #ff7a45;
}
.c-like {
display: flex;
align-items: center;
@@ -225,4 +412,126 @@ const handleLike = async () => {
line-height: 1.65;
word-break: break-all;
}
.c-actions {
display: flex;
align-items: center;
gap: 20rpx;
margin-top: 16rpx;
}
.c-action-btn,
.reply-btn,
.c-action-count,
.c-action-link {
font-size: 22rpx;
color: #8b8f98;
}
.c-action-btn,
.reply-btn,
.c-action-link {
font-weight: 600;
}
.reply-panel {
margin-top: 20rpx;
padding: 18rpx 18rpx 8rpx;
border-radius: 24rpx;
background: linear-gradient(180deg, #f6f7fb 0%, #f9fafc 100%);
}
.reply-item {
display: flex;
gap: 16rpx;
padding-bottom: 18rpx;
border-radius: 20rpx;
transition: background-color 0.2s, padding 0.2s;
}
.reply-item--anchored {
padding: 16rpx 14rpx 18rpx;
background: rgba(92, 67, 245, 0.08);
}
.reply-avatar {
width: 56rpx;
height: 56rpx;
border-radius: 50%;
flex-shrink: 0;
background: #eceff5;
}
.reply-main {
flex: 1;
min-width: 0;
}
.reply-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16rpx;
}
.reply-user-wrap {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8rpx;
}
.reply-name {
font-size: 24rpx;
font-weight: 700;
color: #111827;
}
.reply-time {
display: block;
margin: 6rpx 0 10rpx;
font-size: 20rpx;
color: #9ca3af;
}
.reply-text {
font-size: 24rpx;
line-height: 1.7;
color: #30343b;
word-break: break-all;
}
.reply-at {
color: #4f46e5;
font-weight: 600;
}
.reply-footer {
margin-top: 10rpx;
}
.reply-like {
margin: 0;
padding: 0;
}
.reply-like-icon {
width: 28rpx;
height: 28rpx;
}
.reply-panel-footer {
padding: 8rpx 0 10rpx 72rpx;
}
.reply-more-btn,
.reply-more-loading {
font-size: 22rpx;
color: #4f46e5;
font-weight: 600;
}
.reply-more-loading {
color: #9ca3af;
}
</style>

File diff suppressed because it is too large Load Diff

View File

@@ -94,6 +94,7 @@ import { getPlatformProvider, isSinglePageMode } from "@/utils/system";
import { uploadImage } from "@/utils/common";
import { apiLogin } from "@/api/auth.js";
import { wxLogin, alipayLogin } from "@/utils/login.js";
import { refreshMessageTabBarBadge } from "@/utils/tabBar";
import PrivacyPopup from "@/components/PrivacyPopup/PrivacyPopup.vue";
const popupRef = ref(null);
@@ -219,6 +220,7 @@ const handleAlipayLogin = async () => {
uni.hideLoading();
uni.showToast({ title: "登录成功", icon: "success" });
refreshMessageTabBarBadge();
emit("logind");
popupRef.value.close();
@@ -292,6 +294,7 @@ const confirmLogin = async () => {
userStore.setToken(loginRes.token);
uni.hideLoading();
uni.showToast({ title: "登录成功", icon: "success" });
refreshMessageTabBarBadge();
emit("logind");
popupRef.value.close();

View File

@@ -50,7 +50,7 @@
"quickapp" : {},
/* */
"mp-weixin" : {
"appid" : "wx8d0a4bfb94c9a7f0",
"appid" : "wx146a2e83b50f9f75",
"__usePrivacyCheck__" : true,
"setting" : {
"urlCheck" : false

View File

@@ -49,6 +49,31 @@
"backgroundColor": "#fbfbfb"
}
},
{
"path": "pages/pk/index",
"style": {
"navigationBarTitleText": "PK站",
"navigationStyle": "custom",
"enablePullDownRefresh": true
}
},
{
"path": "pages/pk/detail",
"style": {
"navigationBarTitleText": "PK详情",
"navigationStyle": "custom",
"enablePullDownRefresh": true,
"onReachBottomDistance": 50
}
},
{
"path": "pages/message/index",
"style": {
"navigationBarTitleText": "消息",
"enablePullDownRefresh": false,
"navigationStyle": "custom"
}
},
{
"path": "pages/webview/index",
"style": {
@@ -80,6 +105,14 @@
"enablePullDownRefresh": true
}
},
{
"path": "pages/rating/pk",
"style": {
"navigationBarTitleText": "PK 对决",
"navigationStyle": "custom",
"enablePullDownRefresh": false
}
},
{
"path": "pages/mine/topics",
"style": {
@@ -88,6 +121,15 @@
"enablePullDownRefresh": true,
"onReachBottomDistance": 50
}
},
{
"path": "pages/mine/pk",
"style": {
"navigationBarTitleText": "我参与的PK",
"navigationStyle": "custom",
"enablePullDownRefresh": true,
"onReachBottomDistance": 50
}
}
],
"globalStyle": {
@@ -112,11 +154,23 @@
"iconPath": "static/images/tabBar/home.png",
"selectedIconPath": "static/images/tabBar/home_s.png"
},
{
"text": "PK站",
"pagePath": "pages/pk/index",
"iconPath": "static/images/tabBar/pk.png",
"selectedIconPath": "static/images/tabBar/pk_s.png"
},
{
"text": "发布",
"pagePath": "pages/release/index",
"iconPath": "static/images/tabBar/creation.png",
"selectedIconPath": "static/images/tabBar/creation_s.png"
"iconPath": "static/images/tabBar/publish.png",
"selectedIconPath": "static/images/tabBar/publish_s.png"
},
{
"text": "消息",
"pagePath": "pages/message/index",
"iconPath": "static/images/tabBar/message.png",
"selectedIconPath": "static/images/tabBar/message_s.png"
},
{
"text": "我的",

View File

@@ -120,12 +120,8 @@ const loadTopicList = async (categoryId, page = 1) => {
try {
loading.value = true;
const res = await fetchTopicList(categoryId, page);
const rawList =
res?.data?.records ||
res?.data?.list ||
res?.records ||
res?.list ||
(Array.isArray(res?.data) ? res.data : []);
const rawList =res?.list || [];
const list = Array.isArray(rawList) ? rawList : [];
// 合并数据
topicList.value = [...topicList.value, ...list];

886
pages/message/index.vue Normal file
View File

@@ -0,0 +1,886 @@
<template>
<view class="message-page">
<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
<view class="nav-content">
<view>
<text class="nav-title">系统消息</text>
<text class="nav-subtitle">审核通知评论互动和系统提醒都会在这里出现</text>
</view>
<view class="nav-right">
<view v-if="unreadCount > 0" class="unread-chip">
<text class="unread-chip-text">{{ unreadCount }} 未读</text>
</view>
</view>
</view>
</view>
<view :style="{ height: statusBarHeight + 64 + 'px' }"></view>
<view class="page-body">
<view v-if="!isLoggedIn" class="state-card login-card">
<text class="state-title">登录后查看你的系统消息</text>
<text class="state-desc">审核结果评论互动点赞提醒都会同步到这里</text>
<button class="primary-btn" @tap="openLoginPopup('请先登录后查看消息')">立即登录</button>
</view>
<template v-else>
<view class="filter-bar">
<view class="filter-tabs">
<view
v-for="tab in filterTabs"
:key="tab.value"
class="filter-tab"
:class="{ active: activeFilter === tab.value }"
@tap="activeFilter = tab.value"
>
<text class="filter-tab-text">{{ tab.label }}</text>
</view>
</view>
<view
class="read-all-btn filter-read-all-btn"
:class="{ disabled: readingAll || unreadCount === 0 }"
@tap="handleMarkAllRead"
>
<text class="read-all-text">{{ readingAll ? "处理中" : "全部已读" }}</text>
</view>
</view>
<view v-if="loading && !notificationList.length" class="state-card">
<text class="state-title">消息加载中...</text>
<text class="state-desc">正在同步你的最新通知</text>
</view>
<view v-else-if="loadError && !notificationList.length" class="state-card">
<text class="state-title">消息加载失败</text>
<text class="state-desc">{{ loadError }}</text>
<button class="primary-btn" @tap="refreshList">重新加载</button>
</view>
<view v-else-if="groupedNotifications.length" class="section-list">
<view v-for="section in groupedNotifications" :key="section.day" class="section-block">
<view class="section-header">
<text class="section-title">{{ formatDayLabel(section.day) }}</text>
</view>
<view
v-for="item in section.items"
:key="item.id"
class="message-card"
:class="{ unread: !item.isRead }"
@tap="handleItemTap(item)"
>
<view class="message-avatar-wrap">
<image class="message-avatar" :src="resolveAvatar(item)" mode="aspectFill" />
<view v-if="!item.isRead" class="message-dot"></view>
</view>
<view class="message-main">
<view class="message-head">
<view class="message-head-left">
<text class="message-title">{{ item.title || typeLabelMap[item.type] || "系统通知" }}</text>
<view
class="type-tag"
:style="{
color: resolveTypeStyle(item.type).color,
background: resolveTypeStyle(item.type).background
}"
>
<text class="type-tag-text">{{ typeLabelMap[item.type] || "提醒" }}</text>
</view>
</view>
<text class="message-time">{{ formatRelativeTime(item.createdAt) }}</text>
</view>
<text class="message-content">{{ item.content || "你有一条新的系统消息" }}</text>
<view class="message-footer">
<text class="message-meta">{{ resolveActionText(item) }}</text>
<text class="message-link">{{ resolveJumpText(item) }}</text>
</view>
</view>
</view>
</view>
<view class="list-status">
<text>{{ loading ? "加载中..." : hasMore ? "上拉加载更多" : "没有更多消息了" }}</text>
</view>
</view>
<view v-else class="state-card">
<text class="state-title">{{ activeFilter === "unread" ? "暂时没有未读消息" : "还没有系统消息" }}</text>
<text class="state-desc">{{ activeFilter === "unread" ? "已帮你处理完所有提醒" : "新的审核、评论和互动提醒会显示在这里" }}</text>
</view>
</template>
</view>
<LoginPopup ref="loginPopupRef" @logind="handleLoginSuccess" />
</view>
</template>
<script setup>
import { computed, ref } from "vue";
import { onLoad, onPullDownRefresh, onReachBottom, onShow } from "@dcloudio/uni-app";
import { useUserStore } from "@/stores/user";
import { getStatusBarHeight } from "@/utils/system";
import { FILE_BASE_URL } from "@/utils/constants";
import { syncMessageTabBarBadge } from "@/utils/tabBar";
import {
fetchNotificationList,
fetchUnreadCount,
markAsRead,
markAllAsRead,
} from "@/api/notification";
import LoginPopup from "@/components/LoginPopup/LoginPopup.vue";
const DEFAULT_AVATAR = "/static/images/icon/robot.png";
const APPROVED_AVATAR = "/static/images/icon/approved.png";
const REJECTED_AVATAR = "/static/images/icon/rejected.png";
const PENDING_AVATAR = "/static/images/icon/pending.png";
const userStore = useUserStore();
const loginPopupRef = ref(null);
const statusBarHeight = ref(getStatusBarHeight() || 20);
const loading = ref(false);
const readingAll = ref(false);
const page = ref(1);
const hasMore = ref(true);
const totalCount = ref(0);
const unreadCount = ref(0);
const loadError = ref("");
const activeFilter = ref("all");
const notificationList = ref([]);
const readingIds = ref([]);
const filterTabs = [
{ label: "全部", value: "all" },
{ label: "未读", value: "unread" },
];
const typeLabelMap = {
topic_submit: "话题提交",
topic_approved: "审核通过",
topic_rejected: "审核未过",
item_submit: "评分项提交",
item_approved: "评分项通过",
item_rejected: "评分项未过",
comment: "评论互动",
comment_like: "评论获赞",
system: "系统通知",
};
const typeStyleMap = {
topic_submit: { color: "#ff7d00", background: "rgba(255, 125, 0, 0.12)" },
topic_approved: { color: "#12b76a", background: "rgba(18, 183, 106, 0.12)" },
topic_rejected: { color: "#f04438", background: "rgba(240, 68, 56, 0.12)" },
item_submit: { color: "#7a5af8", background: "rgba(122, 90, 248, 0.12)" },
item_approved: { color: "#0ba5ec", background: "rgba(11, 165, 236, 0.12)" },
item_rejected: { color: "#ef6820", background: "rgba(239, 104, 32, 0.12)" },
comment: { color: "#ec4899", background: "rgba(236, 72, 153, 0.12)" },
comment_like: { color: "#f63d68", background: "rgba(246, 61, 104, 0.12)" },
system: { color: "#667085", background: "rgba(102, 112, 133, 0.12)" },
};
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
let firstShow = true;
const todayCount = computed(() => {
const today = formatDateKey(new Date());
return notificationList.value.filter((item) => (item.day || formatDateKey(item.createdAt)) === today).length;
});
const filteredNotifications = computed(() => {
if (activeFilter.value === "unread") {
return notificationList.value.filter((item) => !item.isRead);
}
return notificationList.value;
});
const groupedNotifications = computed(() => {
const map = {};
filteredNotifications.value.forEach((item) => {
const day = item.day || formatDateKey(item.createdAt);
if (!map[day]) {
map[day] = [];
}
map[day].push(item);
});
return Object.keys(map)
.sort((a, b) => new Date(b).getTime() - new Date(a).getTime())
.map((day) => ({
day,
items: map[day].sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()),
}));
});
const openLoginPopup = (message = "请先登录后再操作") => {
loginPopupRef.value?.open();
if (message) {
uni.showToast({ title: message, icon: "none" });
}
};
const formatDateKey = (value) => {
const date = value instanceof Date ? value : new Date(value);
if (Number.isNaN(date.getTime())) return "";
const year = date.getFullYear();
const month = `${date.getMonth() + 1}`.padStart(2, "0");
const day = `${date.getDate()}`.padStart(2, "0");
return `${year}-${month}-${day}`;
};
const formatRelativeTime = (value) => {
if (!value) return "刚刚";
const date = new Date(value);
if (Number.isNaN(date.getTime())) return "刚刚";
const diff = Date.now() - date.getTime();
const minute = 60 * 1000;
const hour = 60 * minute;
if (diff < minute) return "刚刚";
if (diff < hour) return `${Math.max(1, Math.floor(diff / minute))}分钟前`;
if (diff < 24 * hour) return `${Math.max(1, Math.floor(diff / hour))}小时前`;
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = `${date.getHours()}`.padStart(2, "0");
const minutes = `${date.getMinutes()}`.padStart(2, "0");
const currentYear = new Date().getFullYear();
if (date.getFullYear() === currentYear) {
return `${month}${day}${hours}:${minutes}`;
}
return `${date.getFullYear()}-${`${month}`.padStart(2, "0")}-${`${day}`.padStart(2, "0")}`;
};
const formatDayLabel = (day) => {
if (!day) return "更早";
const today = formatDateKey(new Date());
const yesterdayDate = new Date();
yesterdayDate.setDate(yesterdayDate.getDate() - 1);
const yesterday = formatDateKey(yesterdayDate);
if (day === today) return "今天";
if (day === yesterday) return "昨天";
const date = new Date(day);
if (Number.isNaN(date.getTime())) return day;
return `${date.getMonth() + 1}${date.getDate()}`;
};
const resolveAvatar = (item) => {
const avatar = item?.fromUser?.avatar;
if (!avatar) {
if(item.title.includes("已通过")) {
return APPROVED_AVATAR;
} else if(item.title.includes("未通过")) {
return REJECTED_AVATAR;
} else if(item.title.includes("已提交审核")) {
return PENDING_AVATAR;
} else {
return DEFAULT_AVATAR;
}
}
return String(avatar).startsWith("http") ? avatar : `${FILE_BASE_URL}${avatar}`;
};
const resolveTypeStyle = (type) => {
return typeStyleMap[type] || typeStyleMap.system;
};
const resolveActionText = (item) => {
if (!item.isRead) return "点击后将标记为已读";
if (item.updatedAt && item.updatedAt !== item.createdAt) return "已处理";
return "查看详情";
};
const resolveJumpText = (item) => {
if(item.title.includes("已提交审核") || item.title.includes("未通过") || item.title.includes("评分项")) return "";
if (item.topicId) return "查看话题";
return "查看消息";
};
const normalizeListResponse = (res) => {
const source = res?.data || res || {};
const list = Array.isArray(source.list)
? source.list
: Array.isArray(res?.list)
? res.list
: [];
const total = Number(source.totalCount ?? res?.totalCount ?? list.length);
return { list, total };
};
const mergeNotificationList = (list, reset = false) => {
if (reset) {
notificationList.value = list;
return;
}
const map = new Map(notificationList.value.map((item) => [item.id, item]));
list.forEach((item) => {
map.set(item.id, item);
});
notificationList.value = Array.from(map.values()).sort(
(a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
);
};
const loadUnread = async () => {
if (!isLoggedIn.value) {
unreadCount.value = 0;
syncMessageTabBarBadge(0);
return;
}
try {
const res = await fetchUnreadCount();
const count =
Number(
res?.data?.count ??
res?.data?.unreadCount ??
res?.count ??
res?.unreadCount ??
res?.data ??
0
) || 0;
unreadCount.value = count;
syncMessageTabBarBadge(count);
} catch (error) {
unreadCount.value = unreadCount.value || 0;
syncMessageTabBarBadge(unreadCount.value);
}
};
const loadNotifications = async ({ reset = false } = {}) => {
if (!isLoggedIn.value) {
notificationList.value = [];
totalCount.value = 0;
hasMore.value = false;
loadError.value = "";
return;
}
if (loading.value) return;
if (!reset && !hasMore.value) return;
try {
loading.value = true;
loadError.value = "";
const nextPage = reset ? 1 : page.value;
const res = await fetchNotificationList({ page: nextPage });
const { list, total } = normalizeListResponse(res);
totalCount.value = total;
mergeNotificationList(list, reset);
hasMore.value = notificationList.value.length < total && list.length > 0;
page.value = nextPage + 1;
} catch (error) {
console.error("获取消息列表失败:", error);
loadError.value = "网络开了点小差,稍后再试";
if (reset) {
notificationList.value = [];
totalCount.value = 0;
}
} finally {
loading.value = false;
uni.stopPullDownRefresh();
}
};
const refreshList = async () => {
page.value = 1;
hasMore.value = true;
await Promise.all([loadUnread(), loadNotifications({ reset: true })]);
};
const setItemReadState = (id, isRead = true) => {
notificationList.value = notificationList.value.map((item) => {
if (item.id !== id) return item;
return {
...item,
isRead,
};
});
};
const handleNavigate = (item) => {
const isCommentInteraction = item?.type === 'comment' || item?.type === 'comment_like';
const targetCommentId =
item?.commentId ||
(item?.targetType === 'comment' ? item?.targetId : '') ||
item?.metadata?.commentId ||
'';
if (item.itemId) {
uni.navigateTo({
url: `/pages/rating/detail?topicId=${item.topicId || ""}&itemId=${item.itemId}${isCommentInteraction && targetCommentId ? `&commentId=${targetCommentId}` : ""}`,
});
return;
}
if (item.topicId) {
uni.navigateTo({
url: `/pages/rating/index?topicId=${item.topicId}`,
});
return;
}
};
const markSingleAsRead = async (item) => {
if (!item?.id || item.isRead || readingIds.value.includes(item.id)) return;
readingIds.value = [...readingIds.value, item.id];
try {
await markAsRead(item.id);
setItemReadState(item.id, true);
unreadCount.value = Math.max(0, unreadCount.value - 1);
syncMessageTabBarBadge(unreadCount.value);
} catch (error) {
console.error("标记消息已读失败:", error);
} finally {
readingIds.value = readingIds.value.filter((id) => id !== item.id);
}
};
const handleItemTap = async (item) => {
await markSingleAsRead(item);
if(item.title.includes("未通过") || item.title.includes("已提交审核") || item.title.includes("评分项")) return
handleNavigate(item);
};
const handleMarkAllRead = async () => {
if (!isLoggedIn.value) {
openLoginPopup("请先登录后再操作");
return;
}
if (readingAll.value || unreadCount.value === 0) return;
try {
readingAll.value = true;
await markAllAsRead();
notificationList.value = notificationList.value.map((item) => ({
...item,
isRead: true,
}));
unreadCount.value = 0;
syncMessageTabBarBadge(0);
uni.showToast({ title: "已全部标记为已读", icon: "none" });
} catch (error) {
console.error("全部标记已读失败:", error);
uni.showToast({ title: "操作失败,请稍后再试", icon: "none" });
} finally {
readingAll.value = false;
}
};
const handleLoginSuccess = async () => {
await refreshList();
};
onLoad(() => {
refreshList();
});
onShow(() => {
if (firstShow) {
firstShow = false;
return;
}
refreshList();
});
onPullDownRefresh(() => {
refreshList();
});
onReachBottom(() => {
loadNotifications();
});
</script>
<style lang="scss" scoped>
.message-page {
min-height: 100vh;
background:
radial-gradient(circle at top, rgba(255, 116, 71, 0.1) 0, rgba(255, 116, 71, 0) 36%),
#f7f7f8;
box-sizing: border-box;
}
.nav-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
background: rgba(247, 247, 248, 0.9);
backdrop-filter: blur(18rpx);
}
.nav-content {
min-height: 64px;
padding: 0 28rpx;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
}
.nav-title {
display: block;
font-size: 38rpx;
font-weight: 700;
color: #18181b;
}
.nav-subtitle {
display: block;
margin-top: 8rpx;
font-size: 22rpx;
color: #8a8f99;
}
.nav-right {
display: flex;
align-items: center;
gap: 14rpx;
flex-shrink: 0;
}
.unread-chip {
padding: 10rpx 18rpx;
border-radius: 999rpx;
background: rgba(255, 59, 48, 0.1);
}
.unread-chip-text {
font-size: 22rpx;
font-weight: 600;
color: #ff4d4f;
}
.read-all-btn {
padding: 12rpx 20rpx;
border-radius: 999rpx;
background: #111111;
}
.filter-read-all-btn {
flex-shrink: 0;
padding: 10rpx 22rpx;
}
.read-all-btn.disabled {
opacity: 0.45;
}
.read-all-text {
font-size: 22rpx;
font-weight: 600;
color: #ffffff;
}
.page-body {
padding: 20rpx 24rpx 140rpx;
}
.hero-card {
padding: 30rpx 28rpx;
border-radius: 32rpx;
background: linear-gradient(135deg, #ffffff 0%, #fff5f2 100%);
box-shadow: 0 14rpx 34rpx rgba(15, 23, 42, 0.05);
}
.hero-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24rpx;
}
.hero-title {
display: block;
font-size: 34rpx;
font-weight: 700;
color: #171717;
}
.hero-desc {
display: block;
margin-top: 10rpx;
font-size: 24rpx;
line-height: 1.6;
color: #7a7f88;
}
.hero-icon {
width: 96rpx;
height: 96rpx;
flex-shrink: 0;
}
.hero-stats {
margin-top: 28rpx;
padding-top: 24rpx;
border-top: 1rpx solid rgba(17, 17, 17, 0.06);
display: flex;
align-items: center;
}
.hero-stat {
flex: 1;
display: flex;
flex-direction: column;
gap: 8rpx;
}
.hero-stat-num {
font-size: 34rpx;
font-weight: 700;
color: #111111;
}
.hero-stat-num.accent {
color: #ff5a36;
}
.hero-stat-label {
font-size: 22rpx;
color: #8a8f99;
}
.hero-divider {
width: 1rpx;
height: 56rpx;
background: rgba(17, 17, 17, 0.08);
}
.filter-bar {
margin-top: 24rpx;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
}
.filter-tabs {
padding: 8rpx;
border-radius: 999rpx;
background: #ffffff;
display: inline-flex;
align-items: center;
gap: 8rpx;
box-shadow: 0 10rpx 24rpx rgba(15, 23, 42, 0.04);
}
.filter-tab {
min-width: 112rpx;
height: 60rpx;
padding: 0 24rpx;
border-radius: 999rpx;
display: flex;
align-items: center;
justify-content: center;
}
.filter-tab.active {
background: #111111;
}
.filter-tab-text {
font-size: 24rpx;
font-weight: 600;
color: #8a8f99;
}
.filter-tab.active .filter-tab-text {
color: #ffffff;
}
.filter-hint {
font-size: 22rpx;
color: #9aa0aa;
}
.section-list {
margin-top: 20rpx;
}
.section-block + .section-block {
margin-top: 24rpx;
}
.section-header {
padding: 6rpx 8rpx 14rpx;
}
.section-title {
font-size: 24rpx;
font-weight: 600;
color: #8a8f99;
}
.message-card {
position: relative;
display: flex;
gap: 20rpx;
padding: 24rpx;
border-radius: 28rpx;
background: #ffffff;
box-shadow: 0 12rpx 28rpx rgba(15, 23, 42, 0.05);
}
.message-card + .message-card {
margin-top: 16rpx;
}
.message-card.unread {
background: linear-gradient(180deg, #ffffff 0%, #fff9f7 100%);
}
.message-avatar-wrap {
position: relative;
width: 84rpx;
height: 84rpx;
flex-shrink: 0;
}
.message-avatar {
width: 84rpx;
height: 84rpx;
border-radius: 50%;
background: #f4f5f7;
}
.message-dot {
position: absolute;
top: 0;
right: 2rpx;
width: 18rpx;
height: 18rpx;
border-radius: 50%;
background: #ff4d4f;
border: 4rpx solid #ffffff;
box-sizing: border-box;
}
.message-main {
flex: 1;
min-width: 0;
}
.message-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20rpx;
}
.message-head-left {
flex: 1;
min-width: 0;
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 10rpx;
}
.message-title {
font-size: 28rpx;
font-weight: 700;
color: #18181b;
}
.type-tag {
padding: 6rpx 14rpx;
border-radius: 999rpx;
}
.type-tag-text {
font-size: 20rpx;
font-weight: 600;
}
.message-time {
flex-shrink: 0;
font-size: 22rpx;
color: #a0a4ae;
}
.message-content {
display: block;
margin-top: 12rpx;
font-size: 26rpx;
line-height: 1.7;
color: #555b66;
}
.message-footer {
margin-top: 16rpx;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
}
.message-meta {
font-size: 22rpx;
color: #a0a4ae;
}
.message-link {
font-size: 22rpx;
font-weight: 600;
color: #ff5a36;
}
.state-card {
margin-top: 24rpx;
padding: 44rpx 32rpx;
border-radius: 32rpx;
background: #ffffff;
text-align: center;
box-shadow: 0 12rpx 28rpx rgba(15, 23, 42, 0.05);
}
.login-card {
margin-top: 28rpx;
}
.state-title {
display: block;
font-size: 30rpx;
font-weight: 700;
color: #18181b;
}
.state-desc {
display: block;
margin-top: 14rpx;
font-size: 24rpx;
line-height: 1.7;
color: #8a8f99;
}
.primary-btn {
margin-top: 24rpx;
height: 84rpx;
line-height: 84rpx;
border-radius: 999rpx;
background: linear-gradient(135deg, #ff7a45 0%, #ff4d4f 100%);
color: #ffffff;
font-size: 28rpx;
font-weight: 600;
}
.primary-btn::after {
border: none;
}
.list-status {
padding: 28rpx 0 12rpx;
text-align: center;
font-size: 22rpx;
color: #a0a4ae;
}
</style>

View File

@@ -26,12 +26,18 @@
<text class="nickname" @tap="handleUserClick">{{ userInfo.nickName }}</text>
<view class="tags-container" v-if="isLoggedIn && userStats.label && userStats.label.length">
<text class="sparkle-icon"></text>
<text class="tags-text">{{ userStats.label.join(' | ') }}</text>
<!-- <text class="sparkle-icon"></text>
<text class="tags-text">{{ userStats.label.join(' | ') }}</text> -->
</view>
<view class="tags-container" v-else-if="!isLoggedIn">
<text class="tags-text">点击登录解锁更多功能</text>
</view>
<view v-if="isLoggedIn && userStats.joinedDay > 0" class="joined-day-pill">
<text class="joined-day-label">已加入</text>
<text class="joined-day-value">{{ userStats.joinedDay }}</text>
<text class="joined-day-label"></text>
</view>
<view class="stats-container">
<view class="stat-item">
@@ -40,8 +46,8 @@
</view>
<view class="stat-divider"></view>
<view class="stat-item">
<text class="stat-num">{{ isLoggedIn ? userStats.comment : '-' }}</text>
<text class="stat-label">评论数</text>
<text class="stat-num">{{ isLoggedIn ? userStats.pkCount : '-' }}</text>
<text class="stat-label">参与PK</text>
</view>
<view class="stat-divider"></view>
<view class="stat-item">
@@ -65,14 +71,19 @@
<view class="menu-card">
<view class="menu-item" @tap="navTo('/pages/mine/topics')">
<view class="icon-left"><text class="icon-bookmark"></text></view>
<text class="menu-text">参与</text>
<text class="menu-text">我参与的话题</text>
<text class="menu-value" v-if="isLoggedIn">{{ userStats.participant }}</text>
</view>
<view class="menu-item" @tap="navTo('comments')">
<view class="menu-item" @tap="navTo('/pages/mine/pk')">
<view class="icon-left"><text class="icon-bookmark"></text></view>
<text class="menu-text">我参与的PK</text>
<text class="menu-value" v-if="isLoggedIn">{{ userStats.pkCount }}</text>
</view>
<!-- <view class="menu-item" @tap="navTo('comments')">
<view class="icon-left"><text class="icon-document"></text></view>
<text class="menu-text">我的评论</text>
<text class="menu-value" v-if="isLoggedIn">{{ userStats.comment }}</text>
</view>
</view> -->
</view>
<!-- Menu Group 2 -->
@@ -125,6 +136,8 @@ const userStats = ref({
participant: 0,
comment: 0,
liked: 0,
pkCount: 0,
joinedDay: 0,
label: []
});
@@ -134,6 +147,8 @@ const loadUserStats = async () => {
participant: 0,
comment: 0,
liked: 0,
pkCount: 0,
joinedDay: 0,
label: []
};
return;
@@ -145,6 +160,8 @@ const loadUserStats = async () => {
participant: data.participant || 0,
comment: data.comment || 0,
liked: data.liked || 0,
pkCount: data.pkCount || 0,
joinedDay: data.joinedDay || 0,
label: data.label || []
};
} catch (error) {
@@ -226,6 +243,11 @@ const navTo = (page) => {
return;
}
if (page === "/pages/mine/pk") {
uni.navigateTo({ url: page });
return;
}
uni.showToast({ title: "功能开发中", icon: "none" });
};
</script>
@@ -302,7 +324,7 @@ const navTo = (page) => {
.tags-container {
display: flex;
align-items: center;
margin-bottom: 48rpx;
margin-bottom: 24rpx;
}
.sparkle-icon {
@@ -316,6 +338,31 @@ const navTo = (page) => {
color: #666;
}
.joined-day-pill {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8rpx;
height: 52rpx;
padding: 0 20rpx;
margin-bottom: 36rpx;
border-radius: 999rpx;
background: linear-gradient(135deg, rgba(91, 84, 223, 0.12), rgba(69, 59, 199, 0.05));
box-shadow: inset 0 0 0 2rpx rgba(91, 84, 223, 0.08);
}
.joined-day-label {
font-size: 22rpx;
color: #7b8191;
font-weight: 500;
}
.joined-day-value {
font-size: 28rpx;
color: #453bc7;
font-weight: 800;
}
.stats-container {
display: flex;
align-items: center;

805
pages/mine/pk.vue Normal file
View File

@@ -0,0 +1,805 @@
<template>
<view class="page-container">
<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
<view class="nav-content">
<view class="nav-left" @tap="goBack">
<text class="back-icon"></text>
<text class="nav-title">我参与的PK</text>
</view>
</view>
</view>
<view :style="{ height: statusBarHeight + 44 + 'px' }"></view>
<view class="content-wrap">
<!-- <view class="hero-card">
<view class="hero-main">
<text class="hero-badge">PK Record</text>
<text class="hero-title">我的站队记录</text>
<text class="hero-desc">回看你参与过的每一场 PK对比风向看看你是大众派还是眼光独到</text>
</view>
<view class="hero-stats">
<view class="hero-stat">
<text class="hero-stat-num">{{ isLoggedIn ? totalCount : "-" }}</text>
<text class="hero-stat-label">参与场次</text>
</view>
<view class="hero-stat-divider"></view>
<view class="hero-stat">
<text class="hero-stat-num">{{ supportWinCount }}</text>
<text class="hero-stat-label">站队领先</text>
</view>
</view>
</view> -->
<view v-if="!isLoggedIn" class="state-card">
<text class="state-title">登录后查看你的 PK 足迹</text>
<text class="state-desc">你参与过的 PK 对局站队结果和支持率都会记录在这里</text>
<button class="state-btn" @tap="openLoginPopup('请先登录')">立即登录</button>
</view>
<view v-else-if="loading && !pkList.length" class="state-card">
<text class="state-title">正在加载 PK 记录...</text>
<text class="state-desc">马上为你整理最近参与过的对决</text>
</view>
<view v-else-if="loadError && !pkList.length" class="state-card">
<text class="state-title">加载失败</text>
<text class="state-desc">{{ loadError }}</text>
<button class="state-btn" @tap="refreshList">重新加载</button>
</view>
<view v-else-if="!pkList.length" class="state-card empty-card">
<view class="empty-orbit orbit-1"></view>
<view class="empty-orbit orbit-2"></view>
<view class="empty-core">
<text class="empty-emoji"></text>
</view>
<text class="state-title">你还没有参与任何 PK</text>
<text class="state-desc"> PK 站挑一场神仙打架留下你的第一条站队记录吧</text>
<button class="state-btn" @tap="goToPkStation"> PK 站看看</button>
</view>
<view v-else class="pk-list">
<view
v-for="item in pkList"
:key="item.id"
class="pk-card"
@tap="goToPkDetail(item)"
>
<view class="pk-card-top">
<view class="pk-topic-wrap">
<text class="pk-topic-label">话题</text>
<text class="pk-topic-title">{{ item.topicTitle }}</text>
</view>
<view class="pk-time-chip">
<text class="pk-time-text">{{ item.votedAtText }}</text>
</view>
</view>
<view class="battle-stage">
<view class="fighter-card left" :class="{ selected: item.selfChooseSide === 'left' }">
<view class="fighter-badge" v-if="item.selfChooseSide === 'left'">我站这边</view>
<image class="fighter-avatar" :src="item.left.avatar" mode="aspectFill" />
<text class="fighter-name">{{ item.left.name }}</text>
<text class="fighter-votes">{{ item.leftVoteCount }} </text>
</view>
<view class="battle-middle">
<view class="vs-badge">VS</view>
<text class="battle-middle-text">{{ item.voteCount }} 人参与</text>
</view>
<view class="fighter-card right" :class="{ selected: item.selfChooseSide === 'right' }">
<view class="fighter-badge fighter-badge--right" v-if="item.selfChooseSide === 'right'">我站这边</view>
<image class="fighter-avatar" :src="item.right.avatar" mode="aspectFill" />
<text class="fighter-name">{{ item.right.name }}</text>
<text class="fighter-votes">{{ item.rightVoteCount }} </text>
</view>
</view>
<view class="support-bar">
<view class="support-fill left-fill" :style="{ width: `${item.leftPercent}%` }"></view>
<view class="support-fill right-fill" :style="{ width: `${item.rightPercent}%` }"></view>
</view>
<view class="support-row">
<text class="support-text left-text">{{ item.leftPercent }}% 支持 {{ item.left.name }}</text>
<text class="support-text right-text">{{ item.rightPercent }}% 支持 {{ item.right.name }}</text>
</view>
<view class="pk-card-footer">
<view class="choice-pill" :class="item.selfChooseSide === 'right' ? 'choice-pill--right' : ''">
<text class="choice-pill-label">我的选择</text>
<text class="choice-pill-value">{{ item.selfChooseName }}</text>
</view>
<text class="footer-link">继续查看</text>
</view>
</view>
</view>
</view>
<view v-if="pkList.length" class="load-more">
<text>{{ loading ? '加载中...' : (hasMore ? '上拉加载更多' : '没有更多了') }}</text>
</view>
<LoginPopup ref="loginPopupRef" @logind="handleLoginSuccess" />
</view>
</template>
<script setup>
import { computed, ref } from "vue";
import { onLoad, onPullDownRefresh, onReachBottom, onShow } from "@dcloudio/uni-app";
import { getStatusBarHeight } from "@/utils/system";
import { fetchUserPkList } from "@/api/pk";
import { FILE_BASE_URL } from "@/utils/constants";
import { formatDate } from "@/utils/date";
import LoginPopup from "@/components/LoginPopup/LoginPopup.vue";
import { useUserStore } from "@/stores/user";
const userStore = useUserStore();
const loginPopupRef = ref(null);
const statusBarHeight = ref(getStatusBarHeight() || 44);
const pkList = ref([]);
const loading = ref(false);
const hasMore = ref(true);
const page = ref(1);
const totalCount = ref(0);
const loadError = ref("");
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
const supportWinCount = computed(() => {
return pkList.value.filter((item) => {
if (item.selfChooseSide === "left") return item.leftPercent >= item.rightPercent;
if (item.selfChooseSide === "right") return item.rightPercent >= item.leftPercent;
return false;
}).length;
});
const buildAvatarUrl = (avatar) => {
if (!avatar) return "https://api.dicebear.com/7.x/avataaars/svg?seed=fallback";
const normalizedAvatar = String(avatar).trim();
return normalizedAvatar.startsWith("http") ? normalizedAvatar : `${FILE_BASE_URL}${normalizedAvatar}`;
};
const clampPercent = (value) => {
const number = Number(value);
if (!Number.isFinite(number)) return 0;
return Math.max(0, Math.min(100, Math.round(number)));
};
const normalizePercentPair = (item) => {
let leftPercent = clampPercent(item.leftPercent);
let rightPercent = clampPercent(item.rightPercent);
if (!leftPercent && !rightPercent) {
const leftVotes = Number(item.leftVoteCount || 0);
const rightVotes = Number(item.rightVoteCount || 0);
const totalVotes = leftVotes + rightVotes;
if (totalVotes > 0) {
leftPercent = Math.round((leftVotes / totalVotes) * 100);
rightPercent = 100 - leftPercent;
} else {
leftPercent = 50;
rightPercent = 50;
}
} else if (!leftPercent) {
leftPercent = 100 - rightPercent;
} else if (!rightPercent) {
rightPercent = 100 - leftPercent;
}
const total = leftPercent + rightPercent;
if (total && total !== 100) {
leftPercent = Math.round((leftPercent / total) * 100);
rightPercent = 100 - leftPercent;
}
return { leftPercent, rightPercent };
};
const normalizePkItem = (item = {}) => {
const { leftPercent, rightPercent } = normalizePercentPair(item);
const selfChooseSide =
item.selfChooseSide ||
(item.selfChooseItemId && item.selfChooseItemId === item.leftItemId
? "left"
: item.selfChooseItemId && item.selfChooseItemId === item.rightItemId
? "right"
: "");
const selfChooseName =
selfChooseSide === "right"
? (item.rightItemName || "右侧对象")
: selfChooseSide === "left"
? (item.leftItemName || "左侧对象")
: "未记录";
return {
id: item.id || "",
topicId: item.topicId || "",
topicTitle: item.topicTitle || "未知话题",
voteCount: Number(item.voteCount || 0),
leftVoteCount: Number(item.leftVoteCount || 0),
rightVoteCount: Number(item.rightVoteCount || 0),
selfChooseItemId: item.selfChooseItemId || "",
selfChooseSide,
selfChooseName,
leftPercent,
rightPercent,
votedAt: item.votedAt || "",
votedAtText: formatDate(item.votedAt, "YYYY-MM-DD HH:mm") || "刚刚参与",
left: {
id: item.leftItemId || "",
name: item.leftItemName || "左侧对象",
avatar: buildAvatarUrl(item.leftItemAvatar),
},
right: {
id: item.rightItemId || "",
name: item.rightItemName || "右侧对象",
avatar: buildAvatarUrl(item.rightItemAvatar),
},
};
};
const goBack = () => {
uni.navigateBack();
};
const openLoginPopup = (message = "请先登录") => {
loginPopupRef.value?.open();
if (message) {
uni.showToast({ title: message, icon: "none" });
}
};
const goToPkStation = () => {
uni.switchTab({
url: "/pages/pk/index",
});
};
const goToPkDetail = (item) => {
if (!item?.id) return;
const cacheKey = `pk_detail_cache_${item.id}`;
uni.setStorageSync(cacheKey, {
id: item.id,
topicId: item.topicId,
topicTitle: item.topicTitle,
leftItemId: item.left.id,
leftItemName: item.left.name,
leftItemAvatar: item.left.avatar,
rightItemId: item.right.id,
rightItemName: item.right.name,
rightItemAvatar: item.right.avatar,
voteCount: item.voteCount,
leftVoteCount: item.leftVoteCount,
rightVoteCount: item.rightVoteCount,
selfChooseItemId: item.selfChooseItemId,
selfChooseSide: item.selfChooseSide,
leftPercent: item.leftPercent,
rightPercent: item.rightPercent,
votedAt: item.votedAt,
});
uni.navigateTo({
url: `/pages/pk/detail?pkId=${item.id}&cacheKey=${cacheKey}`,
});
};
const loadList = async (targetPage = 1) => {
if (!isLoggedIn.value || loading.value) return;
try {
loading.value = true;
loadError.value = "";
const res = await fetchUserPkList({ page: targetPage });
const data = res?.data || res || {};
const rawList = data.list || res?.list || [];
const list = Array.isArray(rawList) ? rawList : [];
const formattedList = list.map(normalizePkItem);
const nextTotalCount = Number(data.totalCount || res?.totalCount || 0);
totalCount.value = nextTotalCount;
pkList.value = targetPage === 1 ? formattedList : [...pkList.value, ...formattedList];
if (nextTotalCount > 0) {
hasMore.value = pkList.value.length < nextTotalCount;
} else {
hasMore.value = list.length > 0;
}
} catch (error) {
loadError.value = "网络开了点小差,稍后重试";
if (targetPage > 1) {
page.value -= 1;
}
} finally {
loading.value = false;
}
};
const refreshList = async () => {
page.value = 1;
hasMore.value = true;
await loadList(1);
};
const handleLoginSuccess = async () => {
await refreshList();
};
onLoad(() => {});
onShow(() => {
if (!isLoggedIn.value) {
pkList.value = [];
totalCount.value = 0;
hasMore.value = false;
return;
}
refreshList();
});
onPullDownRefresh(async () => {
if (isLoggedIn.value) {
await refreshList();
}
uni.stopPullDownRefresh();
});
onReachBottom(() => {
if (!isLoggedIn.value || !hasMore.value || loading.value) return;
page.value += 1;
loadList(page.value);
});
</script>
<style lang="scss" scoped>
.page-container {
min-height: 100vh;
background:
radial-gradient(circle at top, rgba(111, 90, 255, 0.12), transparent 30%),
linear-gradient(180deg, #f7f8ff 0%, #f6f7fb 42%, #ffffff 100%);
padding-bottom: 56rpx;
}
.nav-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
background: rgba(247, 248, 255, 0.88);
backdrop-filter: blur(24rpx);
}
.nav-content {
height: 44px;
display: flex;
align-items: center;
padding: 0 32rpx;
}
.nav-left {
display: flex;
align-items: center;
}
.back-icon {
width: 40rpx;
height: 40rpx;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23202433' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='19' y1='12' x2='5' y2='12'%3E%3C/line%3E%3Cpolyline points='12 19 5 12 12 5'%3E%3C/polyline%3E%3C/svg%3E");
background-size: cover;
margin-right: 16rpx;
}
.nav-title {
font-size: 34rpx;
font-weight: 800;
color: #202433;
}
.content-wrap {
padding: 24rpx 32rpx 0;
}
.hero-card,
.pk-card,
.state-card {
background: rgba(255, 255, 255, 0.94);
border: 2rpx solid rgba(229, 232, 246, 0.92);
border-radius: 32rpx;
box-shadow: 0 18rpx 42rpx rgba(87, 92, 145, 0.08);
}
.hero-card {
padding: 32rpx;
margin-bottom: 24rpx;
}
.hero-main {
margin-bottom: 24rpx;
}
.hero-badge {
display: inline-flex;
align-items: center;
height: 44rpx;
padding: 0 18rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.1);
color: #5c43f5;
font-size: 22rpx;
font-weight: 700;
margin-bottom: 18rpx;
}
.hero-title {
display: block;
font-size: 42rpx;
font-weight: 800;
color: #1f2432;
margin-bottom: 12rpx;
}
.hero-desc {
font-size: 25rpx;
color: #7b8191;
line-height: 1.7;
}
.hero-stats {
display: flex;
align-items: center;
justify-content: space-between;
padding: 24rpx 20rpx 8rpx;
border-radius: 24rpx;
background: linear-gradient(135deg, rgba(92, 67, 245, 0.08), rgba(92, 67, 245, 0.02));
}
.hero-stat {
flex: 1;
text-align: center;
}
.hero-stat-num {
display: block;
font-size: 38rpx;
font-weight: 800;
color: #202433;
margin-bottom: 8rpx;
}
.hero-stat-label {
font-size: 22rpx;
color: #8a90a0;
}
.hero-stat-divider {
width: 2rpx;
height: 52rpx;
background: rgba(206, 211, 229, 0.8);
}
.state-card {
position: relative;
overflow: hidden;
padding: 64rpx 40rpx;
text-align: center;
}
.state-title {
display: block;
font-size: 32rpx;
color: #232734;
font-weight: 800;
margin-bottom: 14rpx;
}
.state-desc {
display: block;
font-size: 25rpx;
color: #8a90a0;
line-height: 1.7;
margin-bottom: 28rpx;
}
.state-btn {
width: 240rpx;
height: 84rpx;
line-height: 84rpx;
border-radius: 999rpx;
background: #111827;
color: #fff;
font-size: 28rpx;
font-weight: 700;
}
.state-btn::after {
border: none;
}
.empty-card {
padding-top: 92rpx;
}
.empty-orbit {
position: absolute;
border-radius: 50%;
border: 2rpx dashed rgba(99, 102, 241, 0.16);
}
.orbit-1 {
width: 200rpx;
height: 200rpx;
top: 26rpx;
left: 50%;
transform: translateX(-50%);
}
.orbit-2 {
width: 140rpx;
height: 140rpx;
top: 56rpx;
left: 50%;
transform: translateX(-50%);
border-style: solid;
}
.empty-core {
width: 112rpx;
height: 112rpx;
margin: 0 auto 130rpx;
border-radius: 50%;
background: linear-gradient(135deg, #ffffff 0%, #eef2ff 100%);
box-shadow: 0 16rpx 34rpx rgba(99, 102, 241, 0.14);
display: flex;
align-items: center;
justify-content: center;
position: relative;
z-index: 2;
}
.empty-emoji {
font-size: 52rpx;
}
.pk-list {
display: flex;
flex-direction: column;
gap: 24rpx;
}
.pk-card {
padding: 28rpx;
}
.pk-card-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20rpx;
margin-bottom: 24rpx;
}
.pk-topic-wrap {
flex: 1;
min-width: 0;
}
.pk-topic-label {
display: block;
font-size: 22rpx;
color: #8a90a0;
margin-bottom: 8rpx;
}
.pk-topic-title {
display: block;
font-size: 30rpx;
line-height: 1.45;
color: #202433;
font-weight: 800;
}
.pk-time-chip {
flex-shrink: 0;
padding: 10rpx 16rpx;
border-radius: 999rpx;
background: rgba(17, 24, 39, 0.06);
}
.pk-time-text {
font-size: 22rpx;
color: #6b7280;
}
.battle-stage {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12rpx;
margin-bottom: 22rpx;
}
.fighter-card {
position: relative;
width: 224rpx;
min-height: 276rpx;
border-radius: 28rpx;
padding: 30rpx 18rpx 24rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: linear-gradient(180deg, #fafbff 0%, #ffffff 100%);
border: 2rpx solid transparent;
box-sizing: border-box;
}
.fighter-card.left {
border-color: rgba(92, 67, 245, 0.18);
}
.fighter-card.right {
border-color: rgba(255, 153, 102, 0.2);
}
.fighter-card.selected {
transform: translateY(-4rpx);
box-shadow: 0 14rpx 28rpx rgba(92, 67, 245, 0.12);
}
.fighter-badge {
position: absolute;
top: 16rpx;
left: 16rpx;
padding: 8rpx 14rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.12);
color: #5c43f5;
font-size: 20rpx;
font-weight: 700;
}
.fighter-badge--right {
background: rgba(255, 141, 95, 0.14);
color: #ff7a45;
}
.fighter-avatar {
width: 120rpx;
height: 120rpx;
border-radius: 50%;
border: 6rpx solid #fff;
box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.08);
margin-bottom: 18rpx;
}
.fighter-name {
font-size: 30rpx;
color: #1f2432;
font-weight: 800;
text-align: center;
margin-bottom: 10rpx;
}
.fighter-votes {
font-size: 22rpx;
color: #858b9b;
}
.battle-middle {
display: flex;
flex-direction: column;
align-items: center;
flex-shrink: 0;
}
.vs-badge {
width: 90rpx;
height: 90rpx;
border-radius: 50%;
background: linear-gradient(135deg, #6a5bff, #4d44f1);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 28rpx;
font-weight: 900;
box-shadow: 0 16rpx 30rpx rgba(92, 67, 245, 0.24);
margin-bottom: 16rpx;
}
.battle-middle-text {
font-size: 22rpx;
color: #8a90a0;
}
.support-bar {
height: 18rpx;
border-radius: 999rpx;
overflow: hidden;
display: flex;
background: #eceff8;
margin-bottom: 16rpx;
}
.support-fill {
height: 100%;
}
.left-fill {
background: linear-gradient(90deg, #6557ff, #4d44f1);
}
.right-fill {
background: linear-gradient(90deg, #ffb15f, #ff8d5f);
}
.support-row {
display: flex;
justify-content: space-between;
gap: 20rpx;
margin-bottom: 22rpx;
}
.support-text {
font-size: 24rpx;
font-weight: 700;
}
.left-text {
color: #5c43f5;
}
.right-text {
color: #ff8d5f;
text-align: right;
}
.pk-card-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
}
.choice-pill {
display: inline-flex;
align-items: center;
gap: 8rpx;
padding: 12rpx 18rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.1);
}
.choice-pill--right {
background: rgba(255, 141, 95, 0.12);
}
.choice-pill-label {
font-size: 22rpx;
color: #8a90a0;
}
.choice-pill-value {
font-size: 24rpx;
font-weight: 700;
color: #4d44f1;
}
.choice-pill--right .choice-pill-value {
color: #ff7a45;
}
.footer-link {
font-size: 24rpx;
font-weight: 700;
color: #111827;
}
.load-more {
text-align: center;
padding: 24rpx 0 40rpx;
color: #999;
font-size: 24rpx;
}
</style>

View File

@@ -20,7 +20,7 @@
v-for="(item, index) in topics"
:key="item.id || index"
:topic="item"
@card-click="goToTopicDetail(item.id)"
@item-click="goToTopicDetail(item.id)"
/>
</view>
@@ -75,7 +75,7 @@ const loadData = async (page = 1) => {
try {
loading.value = true;
const res = await fetchUserTopics(page);
const list = res?.data?.records || res?.records || res?.list || [];
const list = res?.list || [];
// 如果是第一页,先清空原有数据
if (page === 1) {
@@ -83,15 +83,15 @@ const loadData = async (page = 1) => {
}
// 构造适合 TopicCard 渲染的数据格式
const formattedList = list.map(item => ({
id: item.topicId || item.id,
title: item.topicName || item.title || '未知话题',
image: item.image || item.coverUrl || 'https://api.dicebear.com/7.x/shapes/svg?seed=' + (item.topicId || item.id), // 给个默认图防止报错
views: item.participantCount || item.views || 0,
items: item.items || item.ratingItems || []
}));
// const formattedList = list.map(item => ({
// id: item.topicId || item.id,
// title: item.topicName || item.title || '未知话题',
// image: item.image || item.coverUrl || 'https://api.dicebear.com/7.x/shapes/svg?seed=' + (item.topicId || item.id), // 给个默认图防止报错
// views: item.participantCount || item.views || 0,
// items: item.items || item.ratingItems || []
// }));
topics.value = [...topics.value, ...formattedList];
topics.value = [...topics.value, ...list];
// 判断是否有下一页
hasMore.value = list.length >= pageSize;

712
pages/pk/detail.vue Normal file
View File

@@ -0,0 +1,712 @@
<template>
<view class="page-container">
<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
<view class="nav-content">
<view class="nav-left" @tap="goBack">
<text class="back-icon"></text>
</view>
<text class="nav-title">PK详情</text>
<view class="nav-right" @tap="goToPkStation">
<text class="nav-right-text">PK站</text>
</view>
</view>
</view>
<view :style="{ height: statusBarHeight + 44 + 'px' }"></view>
<view class="content-wrap">
<view v-if="detailData" class="hero-card">
<view class="hero-badge">PK Detail</view>
<text class="hero-title">{{ detailData.topicTitle }}</text>
<text class="hero-desc">{{ insightText }}</text>
<view class="summary-row">
<view class="summary-pill" :class="selectedTone ? `summary-pill--${selectedTone}` : ''">
<text class="summary-pill-label">我的选择</text>
<text class="summary-pill-value">{{ selectedItemLabel }}</text>
</view>
<view class="summary-meta">
<text class="summary-meta-item">{{ detailData.voteCount }} 人参与</text>
<text class="summary-dot"></text>
<text class="summary-meta-item">{{ detailData.votedAtText }}</text>
</view>
</view>
</view>
<view v-if="detailData" class="battle-card">
<view class="battle-stage">
<view class="fighter-card left" :class="{ selected: detailData.selfChooseSide === 'left' }">
<view class="fighter-badge" v-if="detailData.selfChooseSide === 'left'">我支持</view>
<image class="fighter-avatar" :src="detailData.left.avatar" mode="aspectFill" />
<text class="fighter-name">{{ detailData.left.name }}</text>
<text class="fighter-votes">{{ detailData.leftVoteCount }} </text>
</view>
<view class="battle-middle">
<view class="vs-badge">VS</view>
<text class="battle-middle-text">{{ leadText }}</text>
</view>
<view class="fighter-card right" :class="{ selected: detailData.selfChooseSide === 'right' }">
<view class="fighter-badge fighter-badge--right" v-if="detailData.selfChooseSide === 'right'">我支持</view>
<image class="fighter-avatar" :src="detailData.right.avatar" mode="aspectFill" />
<text class="fighter-name">{{ detailData.right.name }}</text>
<text class="fighter-votes">{{ detailData.rightVoteCount }} </text>
</view>
</view>
<view class="support-bar">
<view class="support-fill left-fill" :style="{ width: `${detailData.leftPercent}%` }"></view>
<view class="support-fill right-fill" :style="{ width: `${detailData.rightPercent}%` }"></view>
</view>
<view class="support-row">
<text class="support-text left-text">{{ detailData.leftPercent }}% 支持 {{ detailData.left.name }}</text>
<text class="support-text right-text">{{ detailData.rightPercent }}% 支持 {{ detailData.right.name }}</text>
</view>
</view>
<view v-else class="state-card">
<text class="state-title">暂时无法查看这场 PK</text>
<text class="state-desc">这场对决的信息不存在或已失效请返回列表重新进入</text>
<button class="state-btn" @tap="goBack">返回上一页</button>
</view>
<CommentSection
v-if="detailData"
ref="commentSectionRef"
variant="pk"
:request-key="commentRequestKey"
:visible="!!commentRequestKey"
:allow-compose="true"
compose-title="说说你的站队理由"
:compose-desc="`支持 ${selectedItemLabel} 的观点会更容易引发讨论`"
compose-placeholder="写下你的 PK 观点..."
page-name="pk_detail"
pill-label="我站"
:pill-value="selectedItemLabel"
:pill-tone="selectedTone"
loading-desc="看看大家如何评价这场对决"
empty-title="还没有评论"
empty-desc="来写下你的判断成为这场 PK 的第一位评论官"
footer-more-text="查看更多评论"
:list-handler="fetchPkCommentList"
:replies-handler="fetchPkCommentReplies"
:publish-handler="publishPkComment"
:normalize-comment="normalizePkComment"
:like-handler="likeComment"
@need-login="openLoginPopup('请先登录后再参与评论')"
/>
</view>
<LoginPopup ref="loginPopupRef" @logind="handleLoginSuccess" />
</view>
</template>
<script setup>
import { computed, ref } from "vue";
import { onLoad, onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
import { getStatusBarHeight } from "@/utils/system";
import { fetchCommentList, fetchCommentReplyList, publishComment, likeComment } from "@/api/pk";
import { FILE_BASE_URL } from "@/utils/constants";
import { formatDate } from "@/utils/date";
import CommentSection from "@/components/CommentSection/CommentSection.vue";
import LoginPopup from "@/components/LoginPopup/LoginPopup.vue";
import { useUserStore } from "@/stores/user";
const DETAIL_CACHE_PREFIX = "pk_detail_cache_";
const userStore = useUserStore();
const statusBarHeight = ref(getStatusBarHeight() || 44);
const loginPopupRef = ref(null);
const commentSectionRef = ref(null);
const detailData = ref(null);
const pkId = ref("");
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
const selectedTone = computed(() => {
if (!detailData.value?.selfChooseSide) return "";
return detailData.value.selfChooseSide === "left" ? "left" : "right";
});
const selectedItemLabel = computed(() => {
if (!detailData.value) return "";
return detailData.value.selfChooseName || "未记录";
});
const commentRequestKey = computed(() => {
if (!detailData.value?.id || !detailData.value?.selfChooseItemId) return "";
return `${detailData.value.id}_${detailData.value.selfChooseItemId}`;
});
const leadText = computed(() => {
if (!detailData.value) return "";
const leftPercent = Number(detailData.value.leftPercent || 0);
const rightPercent = Number(detailData.value.rightPercent || 0);
const leader = leftPercent >= rightPercent ? detailData.value.left.name : detailData.value.right.name;
const leaderPercent = leftPercent >= rightPercent ? leftPercent : rightPercent;
return `${leader} 当前 ${leaderPercent}% 领先`;
});
const insightText = computed(() => {
if (!detailData.value) return "这是一场你参与过的 PK 对决";
const myChoice = detailData.value.selfChooseName || "你的选择";
const myLeading =
(detailData.value.selfChooseSide === "left" && detailData.value.leftPercent >= detailData.value.rightPercent) ||
(detailData.value.selfChooseSide === "right" && detailData.value.rightPercent >= detailData.value.leftPercent);
return myLeading
? `你支持的 ${myChoice} 当前占优,来看看评论区大家的判断是否和你一致。`
: `你支持的 ${myChoice} 暂时落后,看看评论区里有没有和你同样坚定的声音。`;
});
const buildAvatarUrl = (avatar) => {
if (!avatar) return "https://api.dicebear.com/7.x/avataaars/svg?seed=fallback";
const normalizedAvatar = String(avatar).trim();
return normalizedAvatar.startsWith("http") ? normalizedAvatar : `${FILE_BASE_URL}${normalizedAvatar}`;
};
const clampPercent = (value) => {
const number = Number(value);
if (!Number.isFinite(number)) return 0;
return Math.max(0, Math.min(100, Math.round(number)));
};
const normalizePercentPair = (item = {}) => {
let leftPercent = clampPercent(item.leftPercent);
let rightPercent = clampPercent(item.rightPercent);
if (!leftPercent && !rightPercent) {
const leftVotes = Number(item.leftVoteCount || 0);
const rightVotes = Number(item.rightVoteCount || 0);
const totalVotes = leftVotes + rightVotes;
if (totalVotes > 0) {
leftPercent = Math.round((leftVotes / totalVotes) * 100);
rightPercent = 100 - leftPercent;
} else {
leftPercent = 50;
rightPercent = 50;
}
} else if (!leftPercent) {
leftPercent = 100 - rightPercent;
} else if (!rightPercent) {
rightPercent = 100 - leftPercent;
}
const total = leftPercent + rightPercent;
if (total && total !== 100) {
leftPercent = Math.round((leftPercent / total) * 100);
rightPercent = 100 - leftPercent;
}
return { leftPercent, rightPercent };
};
const normalizeDetail = (item = {}) => {
const { leftPercent, rightPercent } = normalizePercentPair(item);
const selfChooseSide =
item.selfChooseSide ||
(item.selfChooseItemId && item.selfChooseItemId === item.leftItemId
? "left"
: item.selfChooseItemId && item.selfChooseItemId === item.rightItemId
? "right"
: "");
const selfChooseName =
selfChooseSide === "right"
? (item.rightItemName || "右侧对象")
: selfChooseSide === "left"
? (item.leftItemName || "左侧对象")
: "未记录";
return {
id: item.id || "",
topicId: item.topicId || "",
topicTitle: item.topicTitle || "未知话题",
voteCount: Number(item.voteCount || 0),
leftVoteCount: Number(item.leftVoteCount || 0),
rightVoteCount: Number(item.rightVoteCount || 0),
selfChooseItemId: item.selfChooseItemId || "",
selfChooseSide,
selfChooseName,
leftPercent,
rightPercent,
votedAt: item.votedAt || "",
votedAtText: formatDate(item.votedAt, "YYYY-MM-DD HH:mm") || "刚刚参与",
left: {
id: item.leftItemId || "",
name: item.leftItemName || "左侧对象",
avatar: buildAvatarUrl(item.leftItemAvatar),
},
right: {
id: item.rightItemId || "",
name: item.rightItemName || "右侧对象",
avatar: buildAvatarUrl(item.rightItemAvatar),
},
};
};
const resolveChoiceMeta = (chooseItemId) => {
if (!chooseItemId || !detailData.value) {
return { choiceLabel: "", choiceTone: "" };
}
if (chooseItemId === detailData.value.left.id) {
return { choiceLabel: `支持 ${detailData.value.left.name}`, choiceTone: "left" };
}
if (chooseItemId === detailData.value.right.id) {
return { choiceLabel: `支持 ${detailData.value.right.name}`, choiceTone: "right" };
}
return { choiceLabel: "已站队", choiceTone: "" };
};
const normalizePkComment = (item = {}, rootComment = null) => {
const user = item.user || {};
const replyToUser = item.replyToUser || {};
const choiceMeta = resolveChoiceMeta(item.chooseItemId);
return {
id: item.id,
rootCommentId: item.rootCommentId || rootComment?.id || null,
parentCommentId: item.parentCommentId || rootComment?.id || null,
replyToCommentId: item.replyToCommentId || null,
name: user.nickname || "匿名用户",
avatar: buildAvatarUrl(user.avatar),
time: formatDate(item.createdAt) || "刚刚",
content: item.content || "",
likes: Number(item.likeCount || 0),
isLiked: !!item.isLiked,
userAction: "",
replyCount: Number(item.replyCount || 0),
replyToName: replyToUser.nickname || "",
choiceLabel: choiceMeta.choiceLabel,
choiceTone: choiceMeta.choiceTone,
chooseItemId: item.chooseItemId || "",
repliesLoading: false,
repliesLoaded: false,
repliesHasMore: false,
repliesPage: 1,
replies: [],
};
};
const openLoginPopup = (message = "请先登录后再操作") => {
loginPopupRef.value?.open();
if (message) {
uni.showToast({ title: message, icon: "none" });
}
};
const goBack = () => {
uni.navigateBack();
};
const goToPkStation = () => {
uni.switchTab({
url: "/pages/pk/index",
});
};
const fetchPkCommentList = ({ page, orderBy }) => {
return fetchCommentList({
pkId: pkId.value,
page,
orderBy,
});
};
const fetchPkCommentReplies = ({ rootCommentId, page }) => {
return fetchCommentReplyList({
pkId: pkId.value,
rootCommentId,
page,
});
};
const publishPkComment = ({ content, replyTarget, mode }) => {
return publishComment({
pkId: pkId.value,
chooseItemId: detailData.value?.selfChooseItemId,
content,
parentCommentId: mode === "reply" ? replyTarget?.parentCommentId : undefined,
replyToCommentId: mode === "reply" ? replyTarget?.replyToCommentId : undefined,
});
};
const hydrateDetail = (options = {}) => {
const cacheKey = options.cacheKey || `${DETAIL_CACHE_PREFIX}${options.pkId || ""}`;
const cached = cacheKey ? uni.getStorageSync(cacheKey) : null;
if (cached && typeof cached === "object") {
detailData.value = normalizeDetail(cached);
pkId.value = detailData.value.id;
return;
}
if (options.payload) {
try {
const parsed = JSON.parse(decodeURIComponent(options.payload));
detailData.value = normalizeDetail(parsed);
pkId.value = detailData.value.id;
} catch (error) {
detailData.value = null;
}
}
};
const handleLoginSuccess = async () => {
await commentSectionRef.value?.refreshComments?.();
};
onLoad((options) => {
pkId.value = options.pkId || "";
hydrateDetail(options);
});
onPullDownRefresh(async () => {
await commentSectionRef.value?.refreshComments?.();
uni.stopPullDownRefresh();
});
onReachBottom(() => {
commentSectionRef.value?.loadMoreComments?.();
});
</script>
<style lang="scss" scoped>
.page-container {
min-height: 100vh;
background:
radial-gradient(circle at top, rgba(111, 90, 255, 0.14), transparent 32%),
linear-gradient(180deg, #f7f8ff 0%, #f7f8fc 44%, #ffffff 100%);
}
.nav-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
background: rgba(247, 248, 255, 0.88);
backdrop-filter: blur(24rpx);
}
.nav-content {
height: 44px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 28rpx 0 24rpx;
}
.nav-left,
.nav-right {
min-width: 84rpx;
display: flex;
align-items: center;
}
.nav-right {
justify-content: flex-end;
}
.back-icon {
width: 40rpx;
height: 40rpx;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23202433' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='19' y1='12' x2='5' y2='12'%3E%3C/line%3E%3Cpolyline points='12 19 5 12 12 5'%3E%3C/polyline%3E%3C/svg%3E");
background-size: cover;
}
.nav-title {
font-size: 34rpx;
font-weight: 800;
color: #202433;
}
.nav-right-text {
font-size: 24rpx;
color: #5c43f5;
font-weight: 700;
}
.content-wrap {
padding: 24rpx 32rpx 40rpx;
}
.hero-card,
.battle-card,
.state-card {
background: rgba(255, 255, 255, 0.94);
border: 2rpx solid rgba(229, 232, 246, 0.92);
border-radius: 32rpx;
box-shadow: 0 18rpx 42rpx rgba(87, 92, 145, 0.08);
}
.hero-card {
padding: 32rpx;
margin-bottom: 24rpx;
}
.hero-badge {
display: inline-flex;
align-items: center;
height: 44rpx;
padding: 0 18rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.1);
color: #5c43f5;
font-size: 22rpx;
font-weight: 700;
margin-bottom: 18rpx;
}
.hero-title {
display: block;
font-size: 40rpx;
font-weight: 800;
color: #1f2432;
margin-bottom: 12rpx;
line-height: 1.4;
}
.hero-desc {
display: block;
font-size: 25rpx;
color: #7b8191;
line-height: 1.7;
margin-bottom: 24rpx;
}
.summary-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
}
.summary-pill {
display: inline-flex;
align-items: center;
gap: 8rpx;
padding: 12rpx 18rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.1);
}
.summary-pill--right {
background: rgba(255, 141, 95, 0.12);
}
.summary-pill-label {
font-size: 22rpx;
color: #8a90a0;
}
.summary-pill-value {
font-size: 24rpx;
font-weight: 700;
color: #4d44f1;
}
.summary-pill--right .summary-pill-value {
color: #ff7a45;
}
.summary-meta {
display: flex;
align-items: center;
gap: 12rpx;
flex-wrap: wrap;
justify-content: flex-end;
}
.summary-meta-item {
font-size: 22rpx;
color: #8a90a0;
}
.summary-dot {
width: 8rpx;
height: 8rpx;
border-radius: 50%;
background: #d1d5db;
}
.battle-card {
padding: 28rpx;
margin-bottom: 24rpx;
}
.battle-stage {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12rpx;
margin-bottom: 22rpx;
}
.fighter-card {
position: relative;
width: 224rpx;
min-height: 276rpx;
border-radius: 28rpx;
padding: 30rpx 18rpx 24rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: linear-gradient(180deg, #fafbff 0%, #ffffff 100%);
border: 2rpx solid transparent;
box-sizing: border-box;
}
.fighter-card.left {
border-color: rgba(92, 67, 245, 0.18);
}
.fighter-card.right {
border-color: rgba(255, 153, 102, 0.2);
}
.fighter-card.selected {
transform: translateY(-4rpx);
box-shadow: 0 14rpx 28rpx rgba(92, 67, 245, 0.12);
}
.fighter-badge {
position: absolute;
top: 16rpx;
left: 16rpx;
padding: 8rpx 14rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.12);
color: #5c43f5;
font-size: 20rpx;
font-weight: 700;
}
.fighter-badge--right {
background: rgba(255, 141, 95, 0.14);
color: #ff7a45;
}
.fighter-avatar {
width: 120rpx;
height: 120rpx;
border-radius: 50%;
border: 6rpx solid #fff;
box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.08);
margin-bottom: 18rpx;
}
.fighter-name {
font-size: 30rpx;
color: #1f2432;
font-weight: 800;
text-align: center;
margin-bottom: 10rpx;
}
.fighter-votes {
font-size: 22rpx;
color: #858b9b;
}
.battle-middle {
display: flex;
flex-direction: column;
align-items: center;
flex-shrink: 0;
}
.vs-badge {
width: 90rpx;
height: 90rpx;
border-radius: 50%;
background: linear-gradient(135deg, #6a5bff, #4d44f1);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 28rpx;
font-weight: 900;
box-shadow: 0 16rpx 30rpx rgba(92, 67, 245, 0.24);
margin-bottom: 16rpx;
}
.battle-middle-text {
font-size: 22rpx;
color: #8a90a0;
text-align: center;
}
.support-bar {
height: 18rpx;
border-radius: 999rpx;
overflow: hidden;
display: flex;
background: #eceff8;
margin-bottom: 16rpx;
}
.support-fill {
height: 100%;
}
.left-fill {
background: linear-gradient(90deg, #6557ff, #4d44f1);
}
.right-fill {
background: linear-gradient(90deg, #ffb15f, #ff8d5f);
}
.support-row {
display: flex;
justify-content: space-between;
gap: 20rpx;
}
.support-text {
font-size: 24rpx;
font-weight: 700;
}
.left-text {
color: #5c43f5;
}
.right-text {
color: #ff8d5f;
text-align: right;
}
.state-card {
padding: 64rpx 40rpx;
text-align: center;
margin-bottom: 24rpx;
}
.state-title {
display: block;
font-size: 32rpx;
color: #232734;
font-weight: 800;
margin-bottom: 14rpx;
}
.state-desc {
display: block;
font-size: 25rpx;
color: #8a90a0;
line-height: 1.7;
margin-bottom: 28rpx;
}
.state-btn {
width: 240rpx;
height: 84rpx;
line-height: 84rpx;
border-radius: 999rpx;
background: #111827;
color: #fff;
font-size: 28rpx;
font-weight: 700;
}
.state-btn::after {
border: none;
}
</style>

782
pages/pk/index.vue Normal file
View File

@@ -0,0 +1,782 @@
<template>
<view class="page-container">
<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
<view class="nav-content">
<text class="nav-title">PK站</text>
<view class="nav-action" @tap="refreshPool">
<text class="nav-action-text">刷新</text>
</view>
</view>
</view>
<view :style="{ height: statusBarHeight + 44 + 'px' }"></view>
<view class="content-wrap">
<view class="hero-card">
<view class="hero-badge">Battle Arena</view>
<text class="hero-title">随机PK</text>
<text class="hero-desc">系统会从当前可用话题中随机生成对局只给你还没参与过的 PK </text>
</view>
<view v-if="loading" class="state-card">
<text class="state-title">正在生成 PK 对局...</text>
<text class="state-desc">马上就给你安排一场新的神仙打架</text>
</view>
<view v-else-if="currentPair" class="pk-card">
<view class="topic-pill">
<text class="topic-pill-label">当前话题</text>
<text class="topic-pill-title">{{ currentPair.topicTitle }}</text>
</view>
<view class="battle-stage">
<view class="fighter-card left" :class="{ chosen: selectedSide === 'left' }">
<image class="fighter-avatar" :src="currentPair.left.avatar" mode="aspectFill" />
<text class="fighter-name">{{ currentPair.left.name }}</text>
<!-- <text class="fighter-meta">{{ currentPair.left.scoreText }}</text> -->
</view>
<view class="battle-middle">
<view class="vs-badge">VS</view>
<text class="battle-middle-text">你更支持谁</text>
</view>
<view class="fighter-card right" :class="{ chosen: selectedSide === 'right' }">
<image class="fighter-avatar" :src="currentPair.right.avatar" mode="aspectFill" />
<text class="fighter-name">{{ currentPair.right.name }}</text>
<!-- <text class="fighter-meta">{{ currentPair.right.scoreText }}</text> -->
</view>
</view>
<view v-if="hasVoted" class="result-card">
<view class="result-header">
<text class="result-title">PK 结果</text>
<text class="result-choice">你的选择{{ selectedSide === 'left' ? currentPair.left.name : currentPair.right.name }}</text>
</view>
<view class="support-bar">
<view class="support-fill left-fill" :style="{ width: `${supportStats.leftPercent}%` }"></view>
<view class="support-fill right-fill" :style="{ width: `${supportStats.rightPercent}%` }"></view>
</view>
<view class="support-text">
<text class="left-text">{{ supportStats.leftPercent }}% 支持 {{ currentPair.left.name }}</text>
<text class="right-text">{{ supportStats.rightPercent }}% 支持 {{ currentPair.right.name }}</text>
</view>
</view>
<view v-else class="action-row">
<button class="vote-btn left-btn" :disabled="voting" @tap="handleVote('left')">支持 {{ currentPair.left.name }}</button>
<button class="vote-btn right-btn" :disabled="voting" @tap="handleVote('right')">支持 {{ currentPair.right.name }}</button>
</view>
<view class="footer-row">
<!-- <view class="battle-tip-card">
<image class="battle-tip-icon" src="/static/images/icon/robot.png" mode="aspectFit"></image>
<text class="battle-tip-text">{{ battleInsight }}</text>
</view> -->
<button class="next-btn" :disabled="loading" @tap="loadNextPair">下一条 PK</button>
</view>
</view>
<CommentSection
v-if="currentPair && hasVoted"
ref="commentSectionRef"
variant="pk"
:request-key="commentRequestKey"
:visible="hasVoted"
:allow-compose="true"
compose-title="说说你的理由"
:compose-desc="`支持 ${selectedItemLabel} 的观点会更容易引发讨论`"
compose-placeholder="写下你的 PK 观点..."
page-name="pk_station"
pill-label="我站"
:pill-value="selectedItemLabel"
:pill-tone="selectedTone"
loading-desc="看看大家都站哪边"
empty-title="还没有人发言"
empty-desc="你可以成为这场 PK 的第一位评论官"
footer-more-text="查看更多评论"
:list-handler="fetchPkCommentList"
:replies-handler="fetchPkCommentReplies"
:publish-handler="publishPkComment"
:normalize-comment="normalizePkComment"
:like-handler="likeComment"
@need-login="openLoginPopup('请先登录后再参与评论')"
@published="handleCommentPublished"
/>
<view v-else-if="!currentPair" class="state-card">
<text class="state-title">暂时没有新的 PK 对局</text>
<text class="state-desc">当前没有可参与的随机对局稍后再来看看有没有新的组合</text>
<button class="state-btn" @tap="loadNextPair">重新获取</button>
</view>
</view>
<LoginPopup ref="loginPopupRef" @logind="handleLoginSuccess" />
</view>
</template>
<script setup>
import { computed, ref } from "vue";
import { onLoad, onPullDownRefresh, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";
import { getStatusBarHeight } from "@/utils/system";
import { fetchRandomTopicList, vote, fetchCommentList, fetchCommentReplyList, publishComment, likeComment } from "@/api/pk";
import { FILE_BASE_URL } from "@/utils/constants";
import { formatDate } from "@/utils/date";
import CommentSection from "@/components/CommentSection/CommentSection.vue";
import LoginPopup from "@/components/LoginPopup/LoginPopup.vue";
import { useUserStore } from "@/stores/user";
import { getShareReward } from "@/api/system";
import { getShareToken } from "@/utils/common";
const userStore = useUserStore();
const statusBarHeight = ref(getStatusBarHeight() || 44);
const loginPopupRef = ref(null);
const commentSectionRef = ref(null);
const loading = ref(false);
const voting = ref(false);
const currentPair = ref(null);
const selectedSide = ref("");
const voteStats = ref({ leftPercent: 50, rightPercent: 50 });
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
const hasVoted = computed(() => !!selectedSide.value);
const selectedItemId = computed(() => {
if (!currentPair.value || !selectedSide.value) return "";
return selectedSide.value === "left" ? currentPair.value.left.id : currentPair.value.right.id;
});
const selectedItemLabel = computed(() => {
if (!currentPair.value || !selectedSide.value) return "";
return selectedSide.value === "left" ? currentPair.value.left.name : currentPair.value.right.name;
});
const selectedTone = computed(() => {
if (!selectedSide.value) return "";
return selectedSide.value === "left" ? "left" : "right";
});
const commentRequestKey = computed(() => {
if (!currentPair.value?.id || !selectedItemId.value) return "";
return `${currentPair.value.id}_${selectedItemId.value}`;
});
const buildAvatarUrl = (avatar) => {
if (!avatar) return "https://api.dicebear.com/7.x/avataaars/svg?seed=fallback";
const normalizedAvatar = String(avatar).trim();
return normalizedAvatar.startsWith("http") ? normalizedAvatar : `${FILE_BASE_URL}${normalizedAvatar}`;
};
const resolveChoiceMeta = (chooseItemId) => {
if (!chooseItemId || !currentPair.value) {
return { choiceLabel: "", choiceTone: "" };
}
if (chooseItemId === currentPair.value.left?.id) {
return { choiceLabel: `支持 ${currentPair.value.left.name}`, choiceTone: "left" };
}
if (chooseItemId === currentPair.value.right?.id) {
return { choiceLabel: `支持 ${currentPair.value.right.name}`, choiceTone: "right" };
}
return { choiceLabel: "已站队", choiceTone: "" };
};
const normalizePkComment = (item = {}, rootComment = null) => {
const user = item.user || {};
const replyToUser = item.replyToUser || {};
const choiceMeta = resolveChoiceMeta(item.chooseItemId);
return {
id: item.id,
rootCommentId: item.rootCommentId || rootComment?.id || null,
parentCommentId: item.parentCommentId || rootComment?.id || null,
replyToCommentId: item.replyToCommentId || null,
name: user.nickname || "匿名用户",
avatar: buildAvatarUrl(user.avatar),
time: formatDate(item.createdAt) || "刚刚",
content: item.content || "",
likes: Number(item.likeCount || 0),
isLiked: !!item.isLiked,
userAction: "",
replyCount: Number(item.replyCount || 0),
replyToName: replyToUser.nickname || "",
choiceLabel: choiceMeta.choiceLabel,
choiceTone: choiceMeta.choiceTone,
chooseItemId: item.chooseItemId || "",
repliesLoading: false,
repliesLoaded: false,
repliesHasMore: false,
repliesPage: 1,
replies: [],
};
};
const fetchPkCommentList = ({ page, orderBy }) => {
return fetchCommentList({
pkId: currentPair.value?.id,
page,
orderBy,
});
};
const fetchPkCommentReplies = ({ rootCommentId, page }) => {
return fetchCommentReplyList({
pkId: currentPair.value?.id,
rootCommentId,
page,
});
};
const publishPkComment = ({ content, replyTarget, mode }) => {
return publishComment({
pkId: currentPair.value?.id,
chooseItemId: selectedItemId.value,
content,
parentCommentId: mode === "reply" ? replyTarget?.parentCommentId : undefined,
replyToCommentId: mode === "reply" ? replyTarget?.replyToCommentId : undefined,
});
};
const handleCommentPublished = () => {};
const normalizePercent = (value) => {
const number = Number(value);
if (!Number.isFinite(number)) return null;
return number <= 1 ? Math.round(number * 100) : Math.round(number);
};
const normalizeStats = (data = {}) => {
const source = data?.data || data || {};
const leftRaw =
source.leftPercent ??
source.leftSupportRate ??
source.leftItemSupportRate ??
source.leftRate ??
source.leftItemRate ??
source.leftRatio ??
source.leftItemRatio ??
source.leftSupportPercent ??
source.leftItemSupportPercent ??
source.leftItemPercent;
const rightRaw =
source.rightPercent ??
source.rightSupportRate ??
source.rightItemSupportRate ??
source.rightRate ??
source.rightItemRate ??
source.rightRatio ??
source.rightItemRatio ??
source.rightSupportPercent ??
source.rightItemSupportPercent ??
source.rightItemPercent;
let leftPercent = normalizePercent(leftRaw);
let rightPercent = normalizePercent(rightRaw);
if (leftPercent === null && rightPercent === null) {
const leftCount = Number(source.leftCount ?? source.leftVotes ?? source.leftSupportCount ?? source.leftItemSupportCount ?? 0);
const rightCount = Number(source.rightCount ?? source.rightVotes ?? source.rightSupportCount ?? source.rightItemSupportCount ?? 0);
const total = leftCount + rightCount;
if (total > 0) {
leftPercent = Math.round((leftCount / total) * 100);
rightPercent = 100 - leftPercent;
}
} else if (leftPercent === null) {
leftPercent = 100 - rightPercent;
} else if (rightPercent === null) {
rightPercent = 100 - leftPercent;
}
if (leftPercent === null || rightPercent === null) {
return { leftPercent: 50, rightPercent: 50 };
}
leftPercent = Math.max(0, Math.min(100, leftPercent));
rightPercent = Math.max(0, Math.min(100, rightPercent));
const totalPercent = leftPercent + rightPercent;
if (totalPercent && totalPercent !== 100) {
leftPercent = Math.round((leftPercent / totalPercent) * 100);
rightPercent = 100 - leftPercent;
}
return { leftPercent, rightPercent };
};
const supportStats = computed(() => voteStats.value);
const battleInsight = computed(() => {
if (!currentPair.value) return "随机生成新对局中...";
const left = currentPair.value.left;
const right = currentPair.value.right;
if (!hasVoted.value) {
return `${left.name}${right.name} 正在同台 PK先站队再看大家的支持率。`;
}
const leader = supportStats.value.leftPercent >= supportStats.value.rightPercent ? left : right;
const percent = leader.id === left.id ? supportStats.value.leftPercent : supportStats.value.rightPercent;
return `${leader.name} 当前获得 ${percent}% 支持,下一条 PK 可能又是完全不同的风向。`;
});
const openLoginPopup = (message = "请先登录后再操作") => {
loginPopupRef.value?.open();
if (message) {
uni.showToast({ title: message, icon: "none" });
}
};
const normalizePair = (item) => {
if (!item?.id) return null;
return {
id: item.id,
topicId: item.topicId,
topicTitle: item.topicTitle || "未知话题",
left: {
id: item.leftItemId,
name: item.leftItemName || "未知对象",
avatar: buildAvatarUrl(item.leftItemAvatar),
scoreText: "等待站队",
},
right: {
id: item.rightItemId,
name: item.rightItemName || "未知对象",
avatar: buildAvatarUrl(item.rightItemAvatar),
scoreText: "等待站队",
},
};
};
const loadRandomPair = async () => {
const pair = await fetchRandomTopicList();
currentPair.value = normalizePair(pair);
selectedSide.value = "";
voteStats.value = normalizeStats(pair);
};
const loadNextPair = async () => {
if (loading.value) return;
try {
loading.value = true;
await loadRandomPair();
} catch (error) {
currentPair.value = null;
} finally {
loading.value = false;
uni.stopPullDownRefresh();
}
};
const handleVote = async (side) => {
if (!isLoggedIn.value) {
openLoginPopup("请先登录后再参与 PK");
return;
}
if (!currentPair.value || hasVoted.value || voting.value) return;
const itemId = side === "left" ? currentPair.value.left.id : currentPair.value.right.id;
if (!itemId) return;
try {
voting.value = true;
const result = await vote({
pkId: currentPair.value.id,
itemId,
});
selectedSide.value = side;
voteStats.value = normalizeStats(result);
} finally {
voting.value = false;
}
};
const refreshPool = () => {
loadNextPair();
};
const handleLoginSuccess = async () => {
if (hasVoted.value) {
await commentSectionRef.value?.refreshComments?.();
}
};
onLoad(() => {
loadNextPair();
});
onPullDownRefresh(() => {
loadNextPair();
});
onShareAppMessage(async () => {
const shareToken = await getShareToken("pk_station");
getShareReward({ scene: "pk_station" });
return {
title: currentPair.value
? `${currentPair.value.left.name} VS ${currentPair.value.right.name},你站谁?`
: "夯拉评分 PK站来看看下一场神仙打架",
path: `/pages/pk/index?shareToken=${shareToken}`,
};
});
onShareTimeline(async () => {
const shareToken = await getShareToken("pk_station");
getShareReward({ scene: "pk_station_timeline" });
return {
title: currentPair.value
? `${currentPair.value.left.name}${currentPair.value.right.name} 正在 PK快来站队`
: "夯拉评分 PK站已开启来刷下一场对决",
query: `shareToken=${shareToken}`,
};
});
</script>
<style lang="scss" scoped>
.page-container {
min-height: 100vh;
background:
radial-gradient(circle at top, rgba(111, 90, 255, 0.14), transparent 32%),
linear-gradient(180deg, #f7f8ff 0%, #f7f8fc 44%, #ffffff 100%);
}
.nav-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
background: rgba(247, 248, 255, 0.88);
backdrop-filter: blur(24rpx);
}
.nav-content {
height: 44px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 28rpx 0 32rpx;
}
.nav-title {
font-size: 34rpx;
font-weight: 800;
color: #202433;
}
.nav-action {
min-width: 72rpx;
display: flex;
justify-content: flex-end;
}
.nav-action-text {
font-size: 26rpx;
color: #5c43f5;
font-weight: 700;
}
.content-wrap {
padding: 24rpx 32rpx 40rpx;
}
.hero-card,
.pk-card,
.state-card {
background: rgba(255, 255, 255, 0.94);
border: 2rpx solid rgba(229, 232, 246, 0.92);
border-radius: 32rpx;
box-shadow: 0 18rpx 42rpx rgba(87, 92, 145, 0.08);
}
.hero-card {
padding: 32rpx;
margin-bottom: 24rpx;
}
.hero-badge {
display: inline-flex;
align-items: center;
height: 44rpx;
padding: 0 18rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.1);
color: #5c43f5;
font-size: 22rpx;
font-weight: 700;
margin-bottom: 18rpx;
}
.hero-title {
display: block;
font-size: 42rpx;
font-weight: 800;
color: #1f2432;
margin-bottom: 12rpx;
}
.hero-desc {
font-size: 25rpx;
color: #7b8191;
line-height: 1.7;
}
.pk-card {
padding: 30rpx;
}
.topic-pill {
display: flex;
flex-direction: column;
padding: 22rpx 24rpx;
border-radius: 24rpx;
background: linear-gradient(135deg, rgba(92, 67, 245, 0.08), rgba(92, 67, 245, 0.03));
margin-bottom: 28rpx;
}
.topic-pill-label {
font-size: 22rpx;
color: #8a90a0;
margin-bottom: 8rpx;
}
.topic-pill-title {
font-size: 30rpx;
color: #222736;
font-weight: 800;
line-height: 1.45;
}
.battle-stage {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 28rpx;
}
.fighter-card {
width: 230rpx;
min-height: 304rpx;
border-radius: 28rpx;
padding: 28rpx 18rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: linear-gradient(180deg, #fafbff 0%, #ffffff 100%);
box-sizing: border-box;
border: 2rpx solid transparent;
}
.fighter-card.left {
border-color: rgba(92, 67, 245, 0.18);
}
.fighter-card.right {
border-color: rgba(255, 153, 102, 0.18);
}
.fighter-card.chosen {
transform: translateY(-4rpx);
box-shadow: 0 14rpx 28rpx rgba(92, 67, 245, 0.14);
}
.fighter-avatar {
width: 128rpx;
height: 128rpx;
border-radius: 50%;
border: 6rpx solid #fff;
box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.08);
margin-bottom: 18rpx;
}
.fighter-name {
font-size: 30rpx;
color: #1f2432;
font-weight: 800;
text-align: center;
margin-bottom: 10rpx;
}
.fighter-meta {
font-size: 24rpx;
color: #858b9b;
}
.battle-middle {
display: flex;
flex-direction: column;
align-items: center;
}
.vs-badge {
width: 96rpx;
height: 96rpx;
border-radius: 50%;
background: linear-gradient(135deg, #6a5bff, #4d44f1);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 30rpx;
font-weight: 900;
box-shadow: 0 16rpx 30rpx rgba(92, 67, 245, 0.24);
margin-bottom: 16rpx;
}
.battle-middle-text {
font-size: 22rpx;
color: #8a90a0;
}
.action-row {
display: flex;
gap: 18rpx;
margin-bottom: 22rpx;
}
.vote-btn,
.next-btn,
.state-btn {
border-radius: 999rpx;
font-size: 28rpx;
font-weight: 700;
}
.vote-btn::after,
.next-btn::after,
.state-btn::after {
border: none;
}
.vote-btn {
flex: 1;
height: 88rpx;
line-height: 88rpx;
}
.left-btn {
background: linear-gradient(135deg, #6a5bff, #4d44f1);
color: #fff;
}
.right-btn {
background: linear-gradient(135deg, #ffb25c, #ff8e63);
color: #fff;
}
.result-card {
margin-bottom: 22rpx;
padding: 24rpx;
border-radius: 24rpx;
background: #f8f9ff;
}
.result-header {
display: flex;
justify-content: space-between;
gap: 20rpx;
margin-bottom: 16rpx;
}
.result-title {
font-size: 28rpx;
color: #222736;
font-weight: 800;
}
.result-choice {
font-size: 24rpx;
color: #5c43f5;
font-weight: 700;
text-align: right;
}
.support-bar {
height: 18rpx;
border-radius: 999rpx;
overflow: hidden;
display: flex;
background: #eceff8;
margin-bottom: 16rpx;
}
.support-fill {
height: 100%;
}
.left-fill {
background: linear-gradient(90deg, #6557ff, #4d44f1);
}
.right-fill {
background: linear-gradient(90deg, #ffb15f, #ff8d5f);
}
.support-text {
display: flex;
justify-content: space-between;
gap: 20rpx;
font-size: 24rpx;
font-weight: 700;
}
.left-text {
color: #5c43f5;
}
.right-text {
color: #ff8d5f;
text-align: right;
}
.footer-row {
display: flex;
flex-direction: column;
gap: 20rpx;
}
.battle-tip-card {
display: flex;
align-items: flex-start;
padding: 22rpx 24rpx;
border-radius: 24rpx;
background: linear-gradient(135deg, rgba(92, 67, 245, 0.06), rgba(92, 67, 245, 0.02));
}
.battle-tip-icon {
width: 30rpx;
height: 30rpx;
margin-right: 12rpx;
margin-top: 4rpx;
flex-shrink: 0;
}
.battle-tip-text {
font-size: 24rpx;
color: #596071;
line-height: 1.65;
}
.next-btn,
.state-btn {
width: 100%;
height: 88rpx;
line-height: 88rpx;
background: #111827;
color: #fff;
}
.state-card {
padding: 56rpx 40rpx;
text-align: center;
}
.state-title {
display: block;
font-size: 32rpx;
color: #232734;
font-weight: 800;
margin-bottom: 14rpx;
}
.state-desc {
display: block;
font-size: 25rpx;
color: #8a90a0;
line-height: 1.7;
margin-bottom: 28rpx;
}
</style>

View File

@@ -55,78 +55,58 @@
/>
<!-- 大家都在 PK -->
<!-- <view class="pk-panel card">
<!-- <view v-if="pkPreview" class="pk-panel card" @tap="goToPk()">
<view class="pk-header">
<text class="section-title">大家都在 PK</text>
<view class="go-compare">去对比<text class="arrow-right"></text></view>
</view>
<view class="pk-content">
<view class="pk-person">
<image class="pk-avatar blue-border" :src="detailData.avatar" mode="aspectFill" />
<text class="pk-name">{{ detailData.name }}</text>
<image class="pk-avatar blue-border" :src="pkPreview.left.avatar" mode="aspectFill" />
<text class="pk-name">{{ pkPreview.left.name }}</text>
<text class="pk-score blue-text">{{ pkPreview.left.scoreText }}</text>
</view>
<text class="vs-text">VS</text>
<view class="pk-person">
<image class="pk-avatar yellow-border" src="https://api.dicebear.com/7.x/avataaars/svg?seed=13" mode="aspectFill" />
<text class="pk-name">康熙</text>
<image class="pk-avatar yellow-border" :src="pkPreview.right.avatar" mode="aspectFill" />
<text class="pk-name">{{ pkPreview.right.name }}</text>
<text class="pk-score yellow-text">{{ pkPreview.right.scoreText }}</text>
</view>
</view>
<view class="pk-bar-wrap">
<view class="pk-bar blue-bar" style="width: 63%;"></view>
<view class="pk-bar yellow-bar" style="width: 37%;"></view>
<view class="pk-bar blue-bar" :style="{ width: `${pkPreview.leftPercent}%` }"></view>
<view class="pk-bar yellow-bar" :style="{ width: `${pkPreview.rightPercent}%` }"></view>
</view>
<view class="pk-data-text">
<text class="blue-text">63% 支持</text>
<text class="yellow-text">37% 支持</text>
<text class="blue-text">{{ pkPreview.leftPercent }}% 看好</text>
<text class="yellow-text">{{ pkPreview.rightPercent }}% 看好</text>
</view>
</view> -->
<!-- 评论区 -->
<view class="comments-section card">
<view class="tabs-header">
<view class="tabs">
<text class="tab" :class="{ active: currentTab === 'hot' }" @tap="switchTab('hot')">最热</text>
<text class="tab" :class="{ active: currentTab === 'new' }" @tap="switchTab('new')">最新</text>
</view>
<!-- <text class="filter-icon"></text> -->
</view>
<view class="comment-list">
<view v-if="commentLoading && !comments.length" class="comment-state">
<text class="comment-state-title">评论加载中...</text>
<text class="comment-state-desc">正在拉取大家的锐评</text>
</view>
<view v-else-if="commentError && !comments.length" class="comment-state">
<text class="comment-state-title">评论加载失败</text>
<text class="comment-state-desc">{{ commentError }}</text>
<button class="comment-state-btn" @tap="retryLoadComments">重新加载</button>
</view>
<view v-else-if="!comments.length" class="comment-state">
<view class="comment-empty-visual">
<view class="comment-empty-orbit orbit-1"></view>
<view class="comment-empty-orbit orbit-2"></view>
<view class="comment-empty-core">
<text class="comment-empty-emoji">💬</text>
</view>
</view>
<text class="comment-state-title">还没有评论</text>
<text class="comment-state-desc">来发第一条锐评带动一下讨论气氛</text>
<text class="comment-state-tip">你的观点可能就是这条榜单最有意思的开始</text>
</view>
<CommentItem
v-for="(comment, index) in comments"
:key="comment.id"
:comment="comment"
@need-login="openLoginPopup"
@like-change="handleCommentLikeChange"
/>
</view>
<!-- 加载更多 -->
<view v-if="comments.length" class="load-more">
<text>{{ commentLoadText }}</text>
</view>
</view>
<CommentSection
ref="commentSectionRef"
variant="topic"
section-title="评论区"
:request-key="commentRequestKey"
:visible="!!commentRequestKey"
:allow-compose="false"
page-name="rating_detail"
loading-desc="正在拉取大家的锐评"
empty-title="还没有评论"
empty-desc="来发第一条锐评带动一下讨论气氛"
empty-tip="你的观点可能就是这条榜单最有意思的开始"
:show-empty-visual="true"
load-more-mode="scroll"
footer-hint-text="上拉加载更多"
:show-success-toast="false"
:list-handler="fetchTopicComments"
:replies-handler="fetchTopicCommentReplies"
:publish-handler="publishTopicComment"
:normalize-comment="normalizeCommentItem"
@need-login="openLoginPopup"
@published="handleCommentPublished"
/>
</view>
<!-- 评分成功特效弹窗 -->
@@ -141,31 +121,29 @@ import { getStatusBarHeight } from "@/utils/system";
import { onLoad, onPullDownRefresh, onReachBottom, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";
import AttitudePanel from "@/components/AttitudePanel/AttitudePanel.vue";
import SuccessPopup from "@/components/SuccessPopup/SuccessPopup.vue";
import CommentItem from "@/components/CommentItem/CommentItem.vue";
import CommentSection from "@/components/CommentSection/CommentSection.vue";
import LoginPopup from "@/components/LoginPopup/LoginPopup.vue";
import { FILE_BASE_URL } from "@/utils/constants";
import { fetchTopicRatingItems, topicItemScore, topicItemComment, topicItemCommentList } from "@/api/topicItem";
import { fetchTopicRatingItems as fetchTopicItemList } from "@/api/topic";
import { fetchTopicRatingItems, topicItemScore, topicItemComment, topicItemCommentList, topicItemCommentReplyList } from "@/api/topicItem";
import { formatDate } from "@/utils/date";
import { getShareToken } from "@/utils/common";
import { getShareReward } from "@/api/system";
import { useUserStore } from "@/stores/user";
const userStore = useUserStore();
const statusBarHeight = ref(getStatusBarHeight() || 44);
const hasMore = ref(true);
const currentPage = ref(1);
const currentAction = ref('');
const successPopupRef = ref(null);
const loginPopupRef = ref(null);
const attitudePanelRef = ref(null);
const currentTab = ref('hot'); // 'hot' 或 'new'
const commentSectionRef = ref(null);
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
const commentLoading = ref(false);
const isSendingComment = ref(false);
const commentError = ref('');
const commentLoadText = computed(() => {
if (commentLoading.value && comments.value.length) return '加载中...';
if (!hasMore.value) return '没有更多了';
return '上拉加载更多';
const pkPreview = ref(null);
const pendingAnchorCommentId = ref('');
const commentRequestKey = computed(() => {
if (!detailData.value.topicId || !itemId.value) return '';
return `${detailData.value.topicId}_${itemId.value}`;
});
const goBack = () => {
@@ -191,10 +169,110 @@ const detailData = ref({
userAction: "",
topicId: ''
});
const comments = ref([]);
const itemId = ref('');
const buildAvatarUrl = (avatar) => {
if (!avatar) return 'https://api.dicebear.com/7.x/avataaars/svg?seed=fallback';
const normalizedAvatar = String(avatar).trim();
return normalizedAvatar.startsWith('http') ? normalizedAvatar : `${FILE_BASE_URL}${normalizedAvatar}`;
};
const normalizeCommentItem = (item = {}, rootComment = null) => {
const user = item.user || {};
const replyToUser = item.replyToUser || {};
return {
id: item.id,
rootCommentId: item.rootCommentId || rootComment?.id || null,
parentCommentId: item.parentCommentId || rootComment?.id || null,
replyToCommentId: item.replyToCommentId || null,
name: user.nickname || '匿名用户',
avatar: buildAvatarUrl(user.avatar),
time: formatDate(item.createdAt) || '刚刚',
content: item.content || '',
likes: Number(item.likeCount || 0),
isLiked: !!item.isLiked,
badge: '',
badgeIcon: '',
userAction: item.userAction || '',
replyCount: Number(item.replyCount || 0),
replyToName: replyToUser.nickname || '',
repliesLoading: false,
repliesLoaded: !!rootComment,
repliesHasMore: false,
repliesPage: 1,
replies: rootComment ? [] : []
};
};
const calcPkPercent = (leftScore, rightScore) => {
const left = Number(leftScore) || 0;
const right = Number(rightScore) || 0;
if (left <= 0 && right <= 0) {
return { leftPercent: 50, rightPercent: 50 };
}
const total = left + right || 1;
let leftPercent = Math.round((left / total) * 100);
leftPercent = Math.min(82, Math.max(18, leftPercent));
return {
leftPercent,
rightPercent: 100 - leftPercent
};
};
const buildPkPreview = (items) => {
const sortedItems = [...items].sort((a, b) => (Number(b.scoreAvg) || 0) - (Number(a.scoreAvg) || 0));
const currentIndex = sortedItems.findIndex((item) => String(item.id) === String(itemId.value));
if (currentIndex === -1) return null;
const currentItem = sortedItems[currentIndex];
const rival =
sortedItems[currentIndex === 0 ? 1 : currentIndex - 1] ||
sortedItems.find((item) => String(item.id) !== String(itemId.value));
if (!currentItem || !rival) return null;
const { leftPercent, rightPercent } = calcPkPercent(currentItem.scoreAvg, rival.scoreAvg);
return {
left: {
id: currentItem.id,
name: currentItem.name || '当前对象',
avatar: buildAvatarUrl(currentItem.avatarUrl),
scoreText: `${Number(currentItem.scoreAvg || 0).toFixed(1)}`
},
right: {
id: rival.id,
name: rival.name || '对手',
avatar: buildAvatarUrl(rival.avatarUrl),
scoreText: `${Number(rival.scoreAvg || 0).toFixed(1)}`
},
leftPercent,
rightPercent
};
};
const getPkPreviewData = async () => {
if (!detailData.value.topicId || !itemId.value) {
pkPreview.value = null;
return;
}
try {
const res = await fetchTopicItemList(detailData.value.topicId, 1);
const rawList = res?.data?.records || res?.records || res?.data?.list || res?.list || [];
const list = Array.isArray(rawList) ? rawList : [];
pkPreview.value = list.length >= 2 ? buildPkPreview(list) : null;
} catch (error) {
pkPreview.value = null;
}
};
const goToPk = () => {
if (!detailData.value.topicId || !itemId.value) return;
uni.navigateTo({
url: `/pages/rating/pk?topicId=${detailData.value.topicId}&itemId=${itemId.value}`
});
};
const handleActionChange = async (action) => {
if (!isLoggedIn.value) {
openLoginPopup();
@@ -241,6 +319,56 @@ const handleActionChange = async (action) => {
}
};
const publishTopicComment = ({ content, replyTarget, mode }) => {
return topicItemComment({
topicId: detailData.value.topicId,
itemId: itemId.value,
content,
parentCommentId: mode === 'reply' ? replyTarget?.parentCommentId : undefined,
replyToCommentId: mode === 'reply' ? replyTarget?.replyToCommentId : undefined
});
};
const fetchTopicComments = ({ page, orderBy }) => {
return topicItemCommentList(detailData.value.topicId, itemId.value, page, orderBy);
};
const fetchTopicCommentReplies = ({ rootCommentId, page }) => {
return topicItemCommentReplyList({
rootCommentId,
page
});
};
const handleCommentPublished = ({ mode, content, replyTarget }) => {
if (successPopupRef.value) {
successPopupRef.value.show({
emoji: '💬',
label: mode === 'reply' ? '回复已发送' : '你的锐评已发布'
}, 'comment');
}
uni.$trackRecord({
eventName: 'comment',
eventType: 'comment',
elementId: `item_${itemId.value}`,
elementContent: `评论项_${detailData.value.name}`,
customParams: {
comment: content,
replyToCommentId: replyTarget?.replyToCommentId || '',
page: 'rating_detail'
}
});
};
const scrollToPendingComment = async () => {
if (!pendingAnchorCommentId.value) return;
const found = await commentSectionRef.value?.scrollToComment?.(pendingAnchorCommentId.value);
if (found) {
pendingAnchorCommentId.value = '';
}
};
const handleSendComment = async (payload) => {
if (!isLoggedIn.value) {
openLoginPopup();
@@ -252,36 +380,26 @@ const handleSendComment = async (payload) => {
try {
isSendingComment.value = true;
uni.showLoading({ title: '发送中...' });
await topicItemComment({
topicId: detailData.value.topicId,
itemId: itemId.value,
content: payload.content
const res = await publishTopicComment({
content: payload.content,
mode: 'comment',
replyTarget: null
});
uni.hideLoading();
// 弹出成功弹窗
if (successPopupRef.value) {
successPopupRef.value.show({
emoji: '💬',
label: '你的锐评已发布'
}, 'comment');
if(res?.success === false) {
uni.showToast({
title: res?.message || '发送失败',
icon: 'none'
});
return;
}
// 刷新页面数据以获取最新评论
currentPage.value = 1;
hasMore.value = true;
uni.$trackRecord({
eventName: 'comment',
eventType: 'comment',
elementId: `item_${itemId.value}`,
elementContent: `评论项_${detailData.value.name}`,
customParams: {
comment: payload.content,
page: 'rating_detail'
}
handleCommentPublished({
mode: 'comment',
content: payload.content,
replyTarget: null
});
attitudePanelRef.value?.resetComment?.();
await getCommentsData({ replace: true });
await commentSectionRef.value?.refreshComments?.();
} catch (error) {
uni.hideLoading();
uni.showToast({
@@ -293,99 +411,6 @@ const handleSendComment = async (payload) => {
}
};
const switchTab = (tab) => {
if (currentTab.value === tab) return;
currentTab.value = tab;
currentPage.value = 1;
hasMore.value = true;
commentError.value = '';
getCommentsData({ replace: true });
};
const resolveCommentHasMore = (res, list) => {
const data = res?.data || res || {};
const hasNext = data?.hasNext || res?.hasNext;
if (typeof hasNext === 'boolean') return hasNext;
const pages = Number(data?.pages || res?.pages || 0);
if (pages > 0) return currentPage.value < pages;
const total = Number(data?.total || res?.total || 0);
if (total > 0) {
const size = Number(data?.size || data?.pageSize || res?.size || res?.pageSize || list.length || 1);
return currentPage.value * size < total;
}
return list.length > 0;
};
const getCommentsData = async ({ replace = false } = {}) => {
if (!detailData.value.topicId || !itemId.value || commentLoading.value) return;
try {
commentLoading.value = true;
commentError.value = '';
const orderBy = currentTab.value === 'hot' ? 1 : 2;
const res = await topicItemCommentList(detailData.value.topicId, itemId.value, currentPage.value, orderBy);
const rawList =
res?.data?.records ||
res?.data?.list ||
res?.records ||
res?.list ||
(Array.isArray(res?.data) ? res.data : []);
const list = Array.isArray(rawList) ? rawList : [];
const formattedList = list.map(item => ({
id: item.id,
name: item.user?.nickname || '匿名用户',
avatar: item.user?.avatar
? (String(item.user.avatar).startsWith('http') ? item.user.avatar : `${FILE_BASE_URL}${item.user.avatar}`)
: 'https://api.dicebear.com/7.x/avataaars/svg?seed=fallback',
time: formatDate(item.createdAt) || '刚刚', // 如果后端返回了时间字段可以替换
content: item.content,
likes: item.likeCount || 0,
isLiked: item.isLiked || false,
badge: '', // 预留徽章字段
badgeIcon: '',
userAction: item.userAction || "" // 夯/顶级/人上人/NPC/拉
}));
if (currentPage.value === 1 || replace) {
comments.value = formattedList;
} else {
comments.value = [...comments.value, ...formattedList];
}
hasMore.value = resolveCommentHasMore(res, list);
} catch (error) {
console.error('获取评论列表失败:', error);
commentError.value = '请检查网络后重试';
if (currentPage.value > 1) {
currentPage.value -= 1;
}
} finally {
commentLoading.value = false;
}
};
const retryLoadComments = () => {
if (!comments.value.length) {
currentPage.value = 1;
}
getCommentsData({ replace: currentPage.value === 1 });
};
const handleCommentLikeChange = ({ id, isLiked, likes }) => {
comments.value = comments.value.map((item) => {
if (item.id !== id) return item;
return {
...item,
isLiked,
likes
};
});
};
const getDetailData = async () => {
if (!itemId.value) return;
try {
@@ -405,21 +430,11 @@ const getDetailData = async () => {
userAction: data.userAction || ""
};
// 如果接口返回了当前用户的态度,初始化它
if (data.userAction) {
currentAction.value = data.userAction;
}
currentAction.value = data.userAction || '';
// 如果有评论数据,更新评论 (兼容老接口一并返回的情况,但推荐通过 getCommentsData 单独获取)
if (data.comments && Array.isArray(data.comments) && currentPage.value === 1) {
// 如果后端在详情里依然返回了一部分评论
// comments.value = data.comments;
}
// 拉取真实的评论列表
if (currentPage.value === 1) {
getCommentsData({ replace: true });
}
await commentSectionRef.value?.refreshComments?.();
await scrollToPendingComment();
getPkPreviewData();
} catch (error) {
console.error('获取详情失败:', error);
uni.showToast({
@@ -441,6 +456,9 @@ onLoad((options) => {
if (options.topicId) {
detailData.value.topicId = options.topicId;
}
if (options.commentId) {
pendingAnchorCommentId.value = options.commentId;
}
if (options.itemId) {
itemId.value = options.itemId;
getDetailData();
@@ -448,10 +466,6 @@ onLoad((options) => {
});
const handleLoginSuccess = () => {
currentPage.value = 1;
hasMore.value = true;
comments.value = [];
commentError.value = '';
getDetailData();
};
@@ -477,17 +491,11 @@ onShareTimeline(async () => {
});
onPullDownRefresh(() => {
currentPage.value = 1;
hasMore.value = true;
comments.value = [];
commentError.value = '';
getDetailData();
});
onReachBottom(() => {
if (!hasMore.value || commentLoading.value) return;
currentPage.value += 1;
getCommentsData();
commentSectionRef.value?.loadMoreComments?.();
});
</script>
@@ -737,6 +745,12 @@ onReachBottom(() => {
color: #333;
}
.pk-score {
margin-top: 8rpx;
font-size: 22rpx;
font-weight: 700;
}
.vs-text {
font-size: 40rpx;
font-weight: 900;
@@ -765,166 +779,4 @@ onReachBottom(() => {
.blue-text { color: #4d44f1; }
.yellow-text { color: #333; }
/* 评论区 */
.comments-section {
padding: 32rpx;
}
.tabs-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 32rpx;
}
.tabs {
display: flex;
gap: 40rpx;
}
.tab {
font-size: 32rpx;
color: #999;
font-weight: 500;
position: relative;
padding-bottom: 12rpx;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 'Microsoft Yahei', sans-serif;
}
.tab.active {
color: #111;
font-size: 36rpx;
font-weight: 700; /* 稍微减轻字重从800改为700显得不那么生硬 */
}
.tab.active::after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 32rpx;
height: 8rpx;
background: linear-gradient(90deg, #4d44f1, #8a78ff);
border-radius: 4rpx;
}
.filter-icon {
width: 40rpx;
height: 40rpx;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23666' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='4' y1='21' x2='4' y2='14'%3E%3C/line%3E%3Cline x1='4' y1='10' x2='4' y2='3'%3E%3C/line%3E%3Cline x1='12' y1='21' x2='12' y2='12'%3E%3C/line%3E%3Cline x1='12' y1='8' x2='12' y2='3'%3E%3C/line%3E%3Cline x1='20' y1='21' x2='20' y2='16'%3E%3C/line%3E%3Cline x1='20' y1='12' x2='20' y2='3'%3E%3C/line%3E%3Cline x1='1' y1='14' x2='7' y2='14'%3E%3C/line%3E%3Cline x1='9' y1='8' x2='15' y2='8'%3E%3C/line%3E%3Cline x1='17' y1='16' x2='23' y2='16'%3E%3C/line%3E%3C/svg%3E");
background-size: cover;
transition: opacity 0.2s;
}
.filter-icon:active {
opacity: 0.7;
}
.comment-list {
display: flex;
flex-direction: column;
min-height: 400rpx;
}
.comment-state {
min-height: 400rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
color: #999;
padding: 56rpx 40rpx;
box-sizing: border-box;
background: linear-gradient(180deg, rgba(247, 248, 255, 0.92) 0%, rgba(255, 255, 255, 0.98) 100%);
border-radius: 28rpx;
border: 2rpx solid #f1f2fb;
}
.comment-empty-visual {
position: relative;
width: 180rpx;
height: 180rpx;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 28rpx;
}
.comment-empty-orbit {
position: absolute;
border-radius: 50%;
border: 2rpx solid rgba(123, 70, 241, 0.12);
}
.orbit-1 {
width: 180rpx;
height: 180rpx;
}
.orbit-2 {
width: 132rpx;
height: 132rpx;
border-color: rgba(77, 68, 241, 0.18);
}
.comment-empty-core {
width: 96rpx;
height: 96rpx;
border-radius: 32rpx;
background: linear-gradient(135deg, #7668ff, #4d44f1);
box-shadow: 0 18rpx 36rpx rgba(94, 83, 255, 0.2);
display: flex;
align-items: center;
justify-content: center;
}
.comment-empty-emoji {
font-size: 44rpx;
}
.comment-state-title {
font-size: 32rpx;
color: #1f2430;
font-weight: 700;
margin-bottom: 14rpx;
letter-spacing: 0.5rpx;
}
.comment-state-desc {
font-size: 25rpx;
color: #70778a;
line-height: 1.7;
}
.comment-state-tip {
margin-top: 14rpx;
font-size: 22rpx;
color: #a7adbc;
line-height: 1.6;
}
.comment-state-btn {
margin-top: 24rpx;
height: 72rpx;
line-height: 72rpx;
padding: 0 32rpx;
border-radius: 999rpx;
background: #4d44f1;
color: #fff;
font-size: 26rpx;
}
.comment-state-btn::after {
border: none;
}
.load-more {
text-align: center;
padding: 40rpx 0 0;
color: #999;
font-size: 24rpx;
}
</style>

View File

@@ -37,6 +37,30 @@
<text class="ai-content">{{ topicData.aiSummary }}</text>
</view>
<!-- <view v-if="pkPair.left && pkPair.right" class="pk-entry-card" @tap="goToPk()">
<view class="pk-entry-header">
<view class="pk-entry-title-wrap">
<text class="pk-entry-title">PK 对决模式</text>
<text class="pk-entry-subtitle">快速站队看谁更能打</text>
</view>
<view class="pk-entry-action">
<text>进入 PK</text>
<text class="pk-entry-arrow"></text>
</view>
</view>
<view class="pk-entry-battle">
<view class="pk-entry-user left">
<image class="pk-entry-avatar" :src="FILE_BASE_URL + pkPair.left.avatarUrl" mode="aspectFill" />
<text class="pk-entry-name">{{ pkPair.left.name }}</text>
</view>
<view class="pk-entry-vs">VS</view>
<view class="pk-entry-user right">
<image class="pk-entry-avatar" :src="FILE_BASE_URL + pkPair.right.avatarUrl" mode="aspectFill" />
<text class="pk-entry-name">{{ pkPair.right.name }}</text>
</view>
</view>
</view> -->
<!-- 领奖台 (Top 3) -->
<view class="podium-section" v-if="ratingItems && ratingItems.length >= 3">
<!-- 第二名 -->
@@ -180,6 +204,10 @@ const currentTopicId = ref(null);
const successPopupRef = ref(null);
const loginPopupRef = ref(null);
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
const pkPair = computed(() => ({
left: ratingItems.value?.[0] || null,
right: ratingItems.value?.[1] || null
}));
const goBack = () => {
uni.navigateBack({
@@ -267,7 +295,13 @@ const confirmAddItem = async () => {
});
uni.hideLoading();
uni.showToast({ title: '添加成功', icon: 'success' });
// uni.showToast({ title: '添加成功', icon: 'success' });
if (successPopupRef.value) {
successPopupRef.value.show({
emoji: '🎉',
label: '请耐心等待管理员审核'
}, 'release');
}
// 重置表单并关闭弹窗
newItem.value = { name: '', avatar: '' };
@@ -373,6 +407,13 @@ const goToDetail = (itemId) => {
});
};
const goToPk = (itemId = '') => {
if (!currentTopicId.value) return;
uni.navigateTo({
url: `/pages/rating/pk?topicId=${currentTopicId.value}${itemId ? `&itemId=${itemId}` : ''}`
});
};
const handleAddClick = () => {
if (!isLoggedIn.value) {
openLoginPopup();
@@ -582,6 +623,103 @@ onReachBottom(() => {
line-height: 1.6;
}
.pk-entry-card {
background: linear-gradient(135deg, #ffffff 0%, #f7f4ff 52%, #eef1ff 100%);
border: 2rpx solid rgba(99, 88, 255, 0.14);
border-radius: 28rpx;
padding: 28rpx;
margin-bottom: 42rpx;
box-shadow: 0 12rpx 30rpx rgba(91, 84, 223, 0.08);
}
.pk-entry-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 24rpx;
}
.pk-entry-title {
display: block;
font-size: 32rpx;
font-weight: 800;
color: #1d2233;
margin-bottom: 8rpx;
}
.pk-entry-subtitle {
font-size: 24rpx;
color: #7d8495;
}
.pk-entry-action {
display: flex;
align-items: center;
color: #5c43f5;
font-size: 24rpx;
font-weight: 700;
}
.pk-entry-arrow {
width: 24rpx;
height: 24rpx;
margin-left: 6rpx;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235c43f5' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='9 18 15 12 9 6'%3E%3C/polyline%3E%3C/svg%3E");
background-size: cover;
}
.pk-entry-battle {
display: flex;
align-items: center;
justify-content: space-between;
}
.pk-entry-user {
width: 220rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.pk-entry-avatar {
width: 104rpx;
height: 104rpx;
border-radius: 50%;
border: 4rpx solid #fff;
box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.08);
margin-bottom: 14rpx;
}
.pk-entry-user.left .pk-entry-avatar {
border-color: rgba(92, 67, 245, 0.22);
}
.pk-entry-user.right .pk-entry-avatar {
border-color: rgba(255, 154, 76, 0.22);
}
.pk-entry-name {
font-size: 26rpx;
color: #2a2f3c;
font-weight: 700;
text-align: center;
}
.pk-entry-vs {
width: 88rpx;
height: 88rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, #6f5aff, #4d44f1);
color: #fff;
font-size: 30rpx;
font-weight: 900;
letter-spacing: 1rpx;
box-shadow: 0 14rpx 28rpx rgba(92, 67, 245, 0.22);
}
/* 领奖台 (Top 3) */
.podium-section {
display: flex;

643
pages/rating/pk.vue Normal file
View File

@@ -0,0 +1,643 @@
<template>
<view class="page-container">
<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
<view class="nav-content">
<view class="nav-back" @tap="goBack">
<text class="back-icon"></text>
</view>
<text class="nav-title">PK 对决</text>
<view class="nav-placeholder"></view>
</view>
</view>
<view :style="{ height: statusBarHeight + 44 + 'px' }"></view>
<view class="content-wrap" v-if="leftItem && rightItem">
<view class="hero-card">
<text class="hero-badge">Battle Mode</text>
<text class="hero-title">{{ topicTitle || '夯拉评分 PK 对决' }}</text>
<text class="hero-subtitle">你更看好谁快速站队感受榜单碰撞的爽感</text>
</view>
<view class="battle-card">
<view class="battle-arena">
<view class="fighter-card left" :class="{ selected: selectedSide === 'left' }">
<image class="fighter-avatar" :src="leftItem.avatar" mode="aspectFill" />
<text class="fighter-name">{{ leftItem.name }}</text>
<text class="fighter-score">{{ leftItem.scoreText }}</text>
</view>
<view class="battle-center">
<view class="vs-badge">VS</view>
<text class="battle-tip">{{ scoreGapText }}</text>
</view>
<view class="fighter-card right" :class="{ selected: selectedSide === 'right' }">
<image class="fighter-avatar" :src="rightItem.avatar" mode="aspectFill" />
<text class="fighter-name">{{ rightItem.name }}</text>
<text class="fighter-score">{{ rightItem.scoreText }}</text>
</view>
</view>
<view class="support-bar">
<view class="support-fill left-fill" :style="{ width: `${supportData.leftPercent}%` }"></view>
<view class="support-fill right-fill" :style="{ width: `${supportData.rightPercent}%` }"></view>
</view>
<view class="support-text">
<text class="left-text">{{ supportData.leftPercent }}% 看好 {{ leftItem.name }}</text>
<text class="right-text">{{ supportData.rightPercent }}% 看好 {{ rightItem.name }}</text>
</view>
<view class="action-row">
<button class="support-btn left-btn" @tap="handleSupport('left')">我站 {{ leftItem.name }}</button>
<button class="support-btn right-btn" @tap="handleSupport('right')">我站 {{ rightItem.name }}</button>
</view>
</view>
<view class="insight-card">
<view class="insight-header">
<image class="insight-icon" src="/static/images/icon/robot.png" mode="aspectFit"></image>
<text class="insight-title">AI PK 分析</text>
</view>
<text class="insight-content">{{ battleInsight }}</text>
</view>
<view class="rivals-card" v-if="rivalOptions.length">
<view class="rivals-header">
<text class="rivals-title">换个对手继续 PK</text>
<text class="random-action" @tap="randomPickRival">随机换人</text>
</view>
<scroll-view scroll-x class="rivals-scroll" :show-scrollbar="false">
<view class="rivals-list">
<view
v-for="item in rivalOptions"
:key="item.id"
class="rival-chip"
:class="{ active: String(item.id) === String(rightItem.id) }"
@tap="changeRival(item.id)"
>
<image class="rival-avatar" :src="item.avatar" mode="aspectFill" />
<text class="rival-name">{{ item.name }}</text>
</view>
</view>
</scroll-view>
</view>
</view>
<view v-else class="empty-wrap">
<text class="empty-title">暂时还无法开启 PK</text>
<text class="empty-desc">至少需要两个评分对象快去为榜单补充更多角色吧</text>
</view>
<LoginPopup ref="loginPopupRef" @logind="handleLoginSuccess" />
</view>
</template>
<script setup>
import { computed, ref } from "vue";
import { onLoad, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";
import { getStatusBarHeight } from "@/utils/system";
import { fetchTopicDetail, fetchTopicRatingItems } from "@/api/topic";
import { FILE_BASE_URL } from "@/utils/constants";
import LoginPopup from "@/components/LoginPopup/LoginPopup.vue";
import { useUserStore } from "@/stores/user";
import { getShareReward } from "@/api/system";
import { getShareToken } from "@/utils/common";
const userStore = useUserStore();
const statusBarHeight = ref(getStatusBarHeight() || 44);
const loginPopupRef = ref(null);
const topicId = ref("");
const focusItemId = ref("");
const topicTitle = ref("");
const items = ref([]);
const leftItemId = ref("");
const rightItemId = ref("");
const selectedSide = ref("");
const isLoggedIn = computed(() => !!userStore.token || !!userStore.userInfo?.nickName);
const buildAvatarUrl = (avatar) => {
if (!avatar) return "https://api.dicebear.com/7.x/avataaars/svg?seed=fallback";
return String(avatar).startsWith("http") ? avatar : `${FILE_BASE_URL}${avatar}`;
};
const normalizeItem = (item) => ({
...item,
avatar: buildAvatarUrl(item.avatarUrl),
score: Number(item.scoreAvg || 0),
scoreText: `${Number(item.scoreAvg || 0).toFixed(1)}`,
});
const sortedItems = computed(() => [...items.value].sort((a, b) => b.score - a.score));
const leftItem = computed(() => sortedItems.value.find((item) => String(item.id) === String(leftItemId.value)) || null);
const rightItem = computed(() => sortedItems.value.find((item) => String(item.id) === String(rightItemId.value)) || null);
const calcPercent = (leftScore, rightScore) => {
const total = leftScore + rightScore;
if (!total) return { leftPercent: 50, rightPercent: 50 };
let leftPercent = Math.round((leftScore / total) * 100);
leftPercent = Math.max(18, Math.min(82, leftPercent));
if (selectedSide.value === "left") {
leftPercent = Math.min(88, leftPercent + 4);
} else if (selectedSide.value === "right") {
leftPercent = Math.max(12, leftPercent - 4);
}
return {
leftPercent,
rightPercent: 100 - leftPercent,
};
};
const supportData = computed(() => {
if (!leftItem.value || !rightItem.value) {
return { leftPercent: 50, rightPercent: 50 };
}
return calcPercent(leftItem.value.score, rightItem.value.score);
});
const scoreGapText = computed(() => {
if (!leftItem.value || !rightItem.value) return "火热对决";
const gap = Math.abs(leftItem.value.score - rightItem.value.score).toFixed(1);
if (gap === "0.0") return "势均力敌";
return `分差 ${gap}`;
});
const battleInsight = computed(() => {
if (!leftItem.value || !rightItem.value) return "当前榜单对象不足,暂时无法生成 PK 分析。";
if (leftItem.value.score === rightItem.value.score) {
return `${leftItem.value.name}${rightItem.value.name} 当前分数持平,这会是一场纯主观审美的大混战。`;
}
const leader = leftItem.value.score > rightItem.value.score ? leftItem.value : rightItem.value;
const follower = leader.id === leftItem.value.id ? rightItem.value : leftItem.value;
const gap = Math.abs(leftItem.value.score - rightItem.value.score).toFixed(1);
return `${leader.name} 目前在榜单分数上领先 ${gap} 分,但 ${follower.name} 更容易在 PK 模式里制造反转,适合拉好友一起站队。`;
});
const rivalOptions = computed(() => {
if (!leftItem.value) return [];
return sortedItems.value.filter((item) => String(item.id) !== String(leftItem.value.id));
});
const openLoginPopup = (message = "请先登录后再操作") => {
loginPopupRef.value?.open();
if (message) {
uni.showToast({ title: message, icon: "none" });
}
};
const initBattle = () => {
if (sortedItems.value.length < 2) {
leftItemId.value = "";
rightItemId.value = "";
return;
}
const currentIndex = sortedItems.value.findIndex((item) => String(item.id) === String(focusItemId.value));
const primary = currentIndex >= 0 ? sortedItems.value[currentIndex] : sortedItems.value[0];
const rival =
sortedItems.value[currentIndex > 0 ? currentIndex - 1 : 1] ||
sortedItems.value.find((item) => String(item.id) !== String(primary.id));
leftItemId.value = primary?.id || "";
rightItemId.value = rival?.id || "";
};
const loadPageData = async () => {
if (!topicId.value) return;
try {
const [detailRes, itemsRes] = await Promise.all([
fetchTopicDetail(topicId.value),
fetchTopicRatingItems(topicId.value, 1),
]);
const detail = detailRes?.data || detailRes || {};
topicTitle.value = detail.title || "";
const rawList = itemsRes?.data?.records || itemsRes?.records || itemsRes?.data?.list || itemsRes?.list || [];
items.value = (Array.isArray(rawList) ? rawList : []).map(normalizeItem);
initBattle();
} catch (error) {
items.value = [];
}
};
const goBack = () => {
uni.navigateBack({ delta: 1 });
};
const changeRival = (id) => {
if (String(id) === String(leftItemId.value)) return;
rightItemId.value = id;
selectedSide.value = "";
};
const randomPickRival = () => {
if (!rivalOptions.value.length) return;
const pool = rivalOptions.value.filter((item) => String(item.id) !== String(rightItemId.value));
const targetPool = pool.length ? pool : rivalOptions.value;
const index = Math.floor(Math.random() * targetPool.length);
changeRival(targetPool[index].id);
};
const handleSupport = (side) => {
if (!isLoggedIn.value) {
openLoginPopup("请先登录后再站队");
return;
}
selectedSide.value = side;
const targetName = side === "left" ? leftItem.value?.name : rightItem.value?.name;
uni.showToast({
title: `已站队 ${targetName}`,
icon: "success",
});
};
const handleLoginSuccess = () => {};
onLoad((options) => {
topicId.value = options.topicId || "";
focusItemId.value = options.itemId || "";
loadPageData();
});
onShareAppMessage(async () => {
const shareToken = await getShareToken("rating_pk");
getShareReward({ scene: "rating_pk" });
return {
title: leftItem.value && rightItem.value
? `你更站 ${leftItem.value.name} 还是 ${rightItem.value.name}`
: `来夯拉评分看看这场 PK 谁会赢`,
path: `/pages/rating/pk?topicId=${topicId.value}&itemId=${focusItemId.value}&shareToken=${shareToken}`,
};
});
onShareTimeline(async () => {
const shareToken = await getShareToken("rating_pk");
getShareReward({ scene: "rating_pk_timeline" });
return {
title: leftItem.value && rightItem.value
? `${leftItem.value.name} VS ${rightItem.value.name},你站谁?`
: `夯拉评分 PK 模式已开启,快来站队`,
query: `topicId=${topicId.value}&itemId=${focusItemId.value}&shareToken=${shareToken}`,
};
});
</script>
<style lang="scss" scoped>
.page-container {
min-height: 100vh;
background:
radial-gradient(circle at top, rgba(108, 92, 231, 0.16), transparent 32%),
linear-gradient(180deg, #f7f8ff 0%, #f8f9fe 38%, #ffffff 100%);
padding-bottom: 40rpx;
}
.nav-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
background: rgba(247, 248, 255, 0.84);
backdrop-filter: blur(22rpx);
}
.nav-content {
height: 44px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 32rpx;
}
.nav-back,
.nav-placeholder {
width: 88rpx;
}
.back-icon {
width: 40rpx;
height: 40rpx;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='15 18 9 12 15 6'%3E%3C/polyline%3E%3C/svg%3E");
background-size: cover;
}
.nav-title {
font-size: 32rpx;
font-weight: 800;
color: #262b37;
}
.content-wrap {
padding: 24rpx 32rpx;
}
.hero-card,
.battle-card,
.insight-card,
.rivals-card {
background: rgba(255, 255, 255, 0.92);
border: 2rpx solid rgba(229, 232, 246, 0.9);
border-radius: 32rpx;
box-shadow: 0 18rpx 40rpx rgba(88, 93, 146, 0.08);
margin-bottom: 24rpx;
}
.hero-card {
padding: 32rpx;
}
.hero-badge {
display: inline-flex;
height: 44rpx;
align-items: center;
padding: 0 16rpx;
border-radius: 999rpx;
background: rgba(92, 67, 245, 0.1);
color: #5c43f5;
font-size: 22rpx;
font-weight: 700;
margin-bottom: 18rpx;
}
.hero-title {
display: block;
font-size: 42rpx;
color: #1e2230;
font-weight: 800;
line-height: 1.35;
margin-bottom: 12rpx;
}
.hero-subtitle {
font-size: 26rpx;
color: #747b8c;
line-height: 1.7;
}
.battle-card {
padding: 32rpx 28rpx;
}
.battle-arena {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 28rpx;
}
.fighter-card {
width: 240rpx;
min-height: 300rpx;
padding: 28rpx 20rpx;
border-radius: 28rpx;
background: linear-gradient(180deg, #f8f9ff 0%, #ffffff 100%);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
box-sizing: border-box;
border: 2rpx solid transparent;
}
.fighter-card.left {
border-color: rgba(92, 67, 245, 0.18);
}
.fighter-card.right {
border-color: rgba(255, 168, 92, 0.2);
}
.fighter-card.selected {
box-shadow: 0 16rpx 30rpx rgba(92, 67, 245, 0.14);
transform: translateY(-4rpx);
}
.fighter-avatar {
width: 128rpx;
height: 128rpx;
border-radius: 50%;
border: 6rpx solid #fff;
box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.08);
margin-bottom: 18rpx;
}
.fighter-name {
font-size: 30rpx;
color: #202433;
font-weight: 800;
text-align: center;
margin-bottom: 10rpx;
}
.fighter-score {
font-size: 24rpx;
color: #81889b;
}
.battle-center {
display: flex;
flex-direction: column;
align-items: center;
}
.vs-badge {
width: 96rpx;
height: 96rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, #715eff, #4d44f1);
color: #fff;
font-size: 32rpx;
font-weight: 900;
letter-spacing: 1rpx;
box-shadow: 0 18rpx 32rpx rgba(92, 67, 245, 0.25);
margin-bottom: 16rpx;
}
.battle-tip {
font-size: 22rpx;
color: #81889b;
}
.support-bar {
height: 18rpx;
border-radius: 999rpx;
overflow: hidden;
display: flex;
margin-bottom: 16rpx;
background: #eef1fb;
}
.support-fill {
height: 100%;
}
.left-fill {
background: linear-gradient(90deg, #5f56f4, #7a71ff);
}
.right-fill {
background: linear-gradient(90deg, #ffb056, #ff8d5f);
}
.support-text {
display: flex;
justify-content: space-between;
gap: 20rpx;
font-size: 24rpx;
font-weight: 700;
margin-bottom: 28rpx;
}
.left-text {
color: #5c43f5;
}
.right-text {
color: #ff8d5f;
text-align: right;
}
.action-row {
display: flex;
gap: 18rpx;
}
.support-btn {
flex: 1;
height: 88rpx;
line-height: 88rpx;
border-radius: 999rpx;
font-size: 28rpx;
font-weight: 700;
}
.support-btn::after {
border: none;
}
.left-btn {
background: linear-gradient(135deg, #6556ff, #4d44f1);
color: #fff;
}
.right-btn {
background: linear-gradient(135deg, #ffb15f, #ff8d5f);
color: #fff;
}
.insight-card {
padding: 28rpx;
}
.insight-header {
display: flex;
align-items: center;
margin-bottom: 16rpx;
}
.insight-icon {
width: 32rpx;
height: 32rpx;
margin-right: 12rpx;
}
.insight-title {
font-size: 28rpx;
font-weight: 800;
color: #4d44f1;
}
.insight-content {
font-size: 26rpx;
color: #4b5161;
line-height: 1.7;
}
.rivals-card {
padding: 28rpx;
}
.rivals-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20rpx;
}
.rivals-title {
font-size: 30rpx;
font-weight: 800;
color: #212533;
}
.random-action {
font-size: 24rpx;
color: #5c43f5;
font-weight: 700;
}
.rivals-scroll {
white-space: nowrap;
}
.rivals-list {
display: inline-flex;
gap: 18rpx;
}
.rival-chip {
width: 176rpx;
padding: 18rpx 16rpx;
border-radius: 24rpx;
background: #f7f8fd;
border: 2rpx solid transparent;
display: flex;
flex-direction: column;
align-items: center;
}
.rival-chip.active {
background: #f1efff;
border-color: rgba(92, 67, 245, 0.18);
}
.rival-avatar {
width: 76rpx;
height: 76rpx;
border-radius: 50%;
margin-bottom: 12rpx;
}
.rival-name {
font-size: 24rpx;
color: #3a3f4d;
font-weight: 700;
text-align: center;
}
.empty-wrap {
min-height: 60vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 0 56rpx;
}
.empty-title {
font-size: 34rpx;
color: #232733;
font-weight: 800;
margin-bottom: 16rpx;
}
.empty-desc {
font-size: 26rpx;
color: #81889b;
line-height: 1.7;
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

BIN
static/images/tabBar/pk.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

@@ -34,6 +34,8 @@ export const useUserStore = defineStore("user", {
this.userInfo = {};
uni.removeStorageSync("token");
uni.removeStorageSync("userInfo");
uni.removeTabBarBadge({ index: 3 });
uni.hideTabBarRedDot({ index: 3 });
},
loadUserInfo() {
const userStr = uni.getStorageSync("userInfo");
@@ -83,6 +85,8 @@ export const useUserStore = defineStore("user", {
this.token = "";
uni.removeStorageSync("userInfo");
uni.removeStorageSync("token");
uni.removeTabBarBadge({ index: 3 });
uni.hideTabBarRedDot({ index: 3 });
},
},
});

65
utils/tabBar.js Normal file
View File

@@ -0,0 +1,65 @@
import { fetchUnreadCount } from "@/api/notification";
const MESSAGE_TAB_INDEX = 3;
function parseStoredUserInfo() {
const raw = uni.getStorageSync("userInfo");
if (!raw) return {};
try {
return typeof raw === "string" ? JSON.parse(raw) : raw;
} catch (error) {
return {};
}
}
function hasLoginState() {
const token = uni.getStorageSync("token");
const userInfo = parseStoredUserInfo();
return !!token || !!userInfo?.nickName;
}
export function syncMessageTabBarBadge(unreadCount = 0) {
const count = Number(unreadCount) || 0;
if (count > 0) {
const text = count > 99 ? "99+" : String(count);
uni.setTabBarBadge({
index: MESSAGE_TAB_INDEX,
text,
});
return;
}
uni.removeTabBarBadge({
index: MESSAGE_TAB_INDEX,
});
uni.hideTabBarRedDot({
index: MESSAGE_TAB_INDEX,
});
}
export async function refreshMessageTabBarBadge() {
if (!hasLoginState()) {
syncMessageTabBarBadge(0);
return 0;
}
try {
const res = await fetchUnreadCount();
const count =
Number(
res?.data?.count ??
res?.data?.unreadCount ??
res?.count ??
res?.unreadCount ??
res?.data ??
0
) || 0;
syncMessageTabBarBadge(count);
return count;
} catch (error) {
return 0;
}
}