feat: comment like and Dislike

This commit is contained in:
KUN1007 2023-10-09 02:04:26 +08:00
parent 1f33d3b765
commit e2e1580d5b
9 changed files with 349 additions and 38 deletions

View file

@ -1,4 +1,6 @@
import { fetchGet, fetchPost } from '@/utils/request' import { fetchGet, fetchPut, fetchPost } from '@/utils/request'
// 将对象转为请求参数的函数
import objectToQueryParams from '@/utils/objectToQueryParams'
import * as Comment from './types/comment' import * as Comment from './types/comment'
@ -19,6 +21,30 @@ export async function getCommentsByReplyRidApi(
} }
} }
// 点赞评论
export async function updateCommentLikeApi(
request: Comment.TopicLikeCommentRequestData
): Promise<Comment.TopicLikeCommentResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/comment/like?${queryParams}`
const response = fetchPut<Comment.TopicLikeCommentResponseData>(url)
return response
}
// 点踩评论
export async function updateCommentDislikeApi(
request: Comment.TopicDislikeCommentRequestData
): Promise<Comment.TopicDislikeCommentResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/comment/dislike?${queryParams}`
const response = fetchPut<Comment.TopicDislikeCommentResponseData>(url)
return response
}
// 根据 tid 和 rid 创建一个评论 // 根据 tid 和 rid 创建一个评论
export async function postCommentByPidAndRidApi( export async function postCommentByPidAndRidApi(
request: Comment.TopicCreateCommentRequestData request: Comment.TopicCreateCommentRequestData

View file

@ -2,6 +2,7 @@ import { TopicUserInfo, TopicToUserInfo } from './topic'
// 评论的数据 // 评论的数据
export interface TopicComment { export interface TopicComment {
cid: number
rid: number rid: number
tid: number tid: number
c_user: Omit<TopicUserInfo, 'moemoepoint'> c_user: Omit<TopicUserInfo, 'moemoepoint'>
@ -11,6 +12,20 @@ export interface TopicComment {
dislikes: number[] dislikes: number[]
} }
// 点赞评论的请求数据
export interface TopicLikeCommentRequestData {
tid: number
cid: number
to_uid: number
}
// 点踩评论的请求数据
export interface TopicDislikeCommentRequestData {
tid: number
cid: number
to_uid: number
}
// 发表评论的请求数据 // 发表评论的请求数据
export interface TopicCreateCommentRequestData { export interface TopicCreateCommentRequestData {
tid: number tid: number
@ -22,6 +37,12 @@ export interface TopicCreateCommentRequestData {
// 单个回复底下所有的评论数据,是一个数组 // 单个回复底下所有的评论数据,是一个数组
export type TopicCommentResponseData = KUNGalgameResponseData<TopicComment[]> export type TopicCommentResponseData = KUNGalgameResponseData<TopicComment[]>
// 点赞评论的响应数据
export type TopicLikeCommentResponseData = KUNGalgameResponseData<{}>
// 点踩评论的响应数据
export type TopicDislikeCommentResponseData = KUNGalgameResponseData<{}>
// 创建单个评论后返回的评论数据 // 创建单个评论后返回的评论数据
export type TopicCreateCommentResponseData = export type TopicCreateCommentResponseData =
KUNGalgameResponseData<TopicComment> KUNGalgameResponseData<TopicComment>

View file

@ -27,7 +27,7 @@ export interface TopicReply {
dislikes: number[] dislikes: number[]
tags: string[] tags: string[]
time: number time: number
cid: number[] comment: number[]
} }
// 发表回复的请求数据 // 发表回复的请求数据

View file

@ -48,10 +48,19 @@ import type {
} from '@/api' } from '@/api'
// 评论 // 评论
import { getCommentsByReplyRidApi, postCommentByPidAndRidApi } from '@/api' import {
getCommentsByReplyRidApi,
updateCommentLikeApi,
updateCommentDislikeApi,
postCommentByPidAndRidApi,
} from '@/api'
import type { import type {
TopicCommentResponseData, TopicCommentResponseData,
TopicLikeCommentRequestData,
TopicLikeCommentResponseData,
TopicDislikeCommentRequestData,
TopicDislikeCommentResponseData,
TopicCreateCommentRequestData, TopicCreateCommentRequestData,
TopicCreateCommentResponseData, TopicCreateCommentResponseData,
} from '@/api' } from '@/api'
@ -254,6 +263,34 @@ export const useKUNGalgameTopicStore = defineStore({
return await getCommentsByReplyRidApi(tid, rid) return await getCommentsByReplyRidApi(tid, rid)
}, },
// 点赞评论
async updateCommentLike(
tid: number,
cid: number,
toUid: number
): Promise<TopicLikeCommentResponseData> {
const requestData: TopicLikeCommentRequestData = {
tid: tid,
cid: cid,
to_uid: toUid,
}
return await updateCommentLikeApi(requestData)
},
// 点踩评论
async updateCommentDislike(
tid: number,
cid: number,
toUid: number
): Promise<TopicDislikeCommentResponseData> {
const requestData: TopicDislikeCommentRequestData = {
tid: tid,
cid: cid,
to_uid: toUid,
}
return await updateCommentDislikeApi(requestData)
},
// 创建一个评论 // 创建一个评论
async postNewComment( async postNewComment(
tid: number, tid: number,

View file

@ -12,6 +12,8 @@ import {
} from 'vue' } from 'vue'
import { onBeforeRouteLeave, useRoute } from 'vue-router' import { onBeforeRouteLeave, useRoute } from 'vue-router'
import message from '@/components/alert/Message' import message from '@/components/alert/Message'
//
import { useKUNGalgameMessageStore } from '@/store/modules/message'
import { TopicDetail, TopicReply } from '@/api' import { TopicDetail, TopicReply } from '@/api'
@ -44,6 +46,7 @@ const {
isShowAdvance, isShowAdvance,
isEdit, isEdit,
replyRequest, replyRequest,
replyRewrite,
isScrollToTop, isScrollToTop,
isLoading, isLoading,
scrollToReplyId, scrollToReplyId,
@ -236,31 +239,27 @@ const resetPanelStatus = () => {
// useKUNGalgameTopicStore().resetRewriteTopicData() // useKUNGalgameTopicStore().resetRewriteTopicData()
} }
// //
onBeforeRouteLeave(() => { onBeforeRouteLeave(async (to, from, next) => {
//
if (replyRewrite.value.isReplyRewriting) {
//
const res = await useKUNGalgameMessageStore().alert(
'AlertInfo.edit.leave',
true
)
if (res) {
resetPanelStatus() resetPanelStatus()
//
next()
} else {
//
next(false)
}
} else {
next()
}
}) })
// onBeforeRouteLeave(async (to, from, next) => {
// //
// if (replyRewrite.value.isReplyRewriting) {
// //
// const res = await useKUNGalgameMessageStore().alert(
// 'AlertInfo.edit.leave',
// true
// )
// if (res) {
// //
// useKUNGalgameEditStore().resetRewriteTopicData()
// //
// next()
// } else {
// //
// next(false)
// }
// } else {
// next()
// }
// })
// //
onBeforeMount(() => { onBeforeMount(() => {

View file

@ -32,7 +32,7 @@ const handleInputComment = () => {
// //
const debouncedUpdateContent = debounce(() => { const debouncedUpdateContent = debounce(() => {
content.value = commentValue.value content.value = commentValue.value
}, 1007) }, 300)
// //
debouncedUpdateContent() debouncedUpdateContent()
@ -79,8 +79,6 @@ const handlePublishComment = async () => {
message('Comment publish successfully!', '评论发布成功', 'success') message('Comment publish successfully!', '评论发布成功', 'success')
handleCloseCommentPanel() handleCloseCommentPanel()
} else {
message('Comment publish failed!', '评论发布失败', 'success')
} }
} }

View file

@ -4,12 +4,19 @@
<script setup lang="ts"> <script setup lang="ts">
import { defineAsyncComponent, onMounted, reactive, ref, watch } from 'vue' import { defineAsyncComponent, onMounted, reactive, ref, watch } from 'vue'
import { Icon } from '@iconify/vue' import { Icon } from '@iconify/vue'
//
const CommentPanel = defineAsyncComponent(() => import('./CommentPanel.vue')) const CommentPanel = defineAsyncComponent(() => import('./CommentPanel.vue'))
//
import Like from './Like.vue'
//
import Dislike from './Dislike.vue'
import { TopicComment } from '@/api/index' import { TopicComment } from '@/api/index'
// store // store
import { useKUNGalgameTopicStore } from '@/store/modules/topic' import { useKUNGalgameTopicStore } from '@/store/modules/topic'
// store
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
// 使 store // 使 store
import { useTempCommentStore } from '@/store/temp/comment' import { useTempCommentStore } from '@/store/temp/comment'
@ -31,6 +38,8 @@ const props = defineProps<{
const tidRef = ref(props.tid) const tidRef = ref(props.tid)
const ridRef = ref(props.rid) const ridRef = ref(props.rid)
const toUser = ref(props.toUser) const toUser = ref(props.toUser)
// uid
const currentUserUid = useKUNGalgameUserStore().uid
// //
watch( watch(
@ -110,15 +119,21 @@ const handleClickComment = (
<div class="operate"> <div class="operate">
<ul> <ul>
<!-- 点赞 --> <!-- 点赞 -->
<li> <Like
<Icon class="icon" icon="line-md:thumbs-up-twotone" /> :tid="props.tid"
{{ comment.likes.length }} :cid="comment.cid"
</li> :uid="currentUserUid"
:to-uid="comment.c_user.uid"
:likes="comment.likes"
/>
<!-- --> <!-- -->
<li> <Dislike
<Icon class="icon" icon="line-md:thumbs-down-twotone" /> :tid="props.tid"
{{ comment.dislikes.length }} :cid="comment.cid"
</li> :uid="currentUserUid"
:to-uid="comment.c_user.uid"
:dislikes="comment.dislikes"
/>
<li <li
@click=" @click="
handleClickComment( handleClickComment(

View file

@ -0,0 +1,111 @@
<!--
这是回复话题下方的评论区包含了所有的评论是一个单独的组件它的子组件是单个评论
-->
<script setup lang="ts">
import { ref, watch } from 'vue'
import { Icon } from '@iconify/vue'
//
import message from '@/components/alert/Message'
// throttle
import { throttle } from '@/utils/throttle'
// store
import { useKUNGalgameTopicStore } from '@/store/modules/topic'
const props = defineProps<{
tid: number
cid: number
uid: number
toUid: number
dislikes: number[]
}>()
const isDisliked = ref(props.dislikes.includes(props.uid))
const dislikesCount = ref(props.dislikes.length)
//
watch(
() => props.dislikes,
(newLikes) => {
isDisliked.value = newLikes.includes(props.uid)
dislikesCount.value = newLikes.length
}
)
// throttle
const throttleCallback = () => {
message(
'You can only perform one operation within 1007 milliseconds',
'您在 1007 毫秒内只能进行一次操作',
'warn'
)
}
const dislikeComment = async () => {
//
if (isDisliked.value) {
message(`You've already disliked it`, '您已经点过踩了', 'warn')
return
}
//
if (props.uid === props.toUid) {
message('You cannot dislike yourself', '您不可以给自己点踩', 'warn')
return
}
const { tid, cid, toUid } = props
const res = await useKUNGalgameTopicStore().updateCommentDislike(
tid,
cid,
toUid
)
if (res.code === 200) {
dislikesCount.value++
isDisliked.value = true
message('Dislike successfully!', '点踩成功', 'success')
} else {
message('Dislike failed!', '点踩失败', 'error')
}
}
// throttle 1007
const handleClickDislikeThrottled = throttle(
dislikeComment,
1007,
throttleCallback
)
//
const handleClickDislike = () => {
handleClickDislikeThrottled()
}
</script>
<template>
<li :class="isDisliked ? 'active' : ''" @click="handleClickDislike">
<Icon class="icon" icon="line-md:thumbs-down-twotone" />
{{ dislikesCount }}
</li>
</template>
<style lang="scss" scoped>
li {
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;
.icon {
cursor: pointer;
color: var(--kungalgame-font-color-2);
margin-right: 2px;
}
}
/* 激活后的样式 */
.active .icon {
color: var(--kungalgame-blue-4);
}
</style>

View file

@ -0,0 +1,104 @@
<!--
这是回复话题下方的评论区包含了所有的评论是一个单独的组件它的子组件是单个评论
-->
<script setup lang="ts">
import { ref, watch } from 'vue'
import { Icon } from '@iconify/vue'
//
import message from '@/components/alert/Message'
// throttle
import { throttle } from '@/utils/throttle'
// store
import { useKUNGalgameTopicStore } from '@/store/modules/topic'
const props = defineProps<{
tid: number
cid: number
uid: number
toUid: number
likes: number[]
}>()
const isLiked = ref(props.likes.includes(props.uid))
const likesCount = ref(props.likes.length)
//
watch(
() => props.likes,
(newLikes) => {
// isLiked likesCount
isLiked.value = newLikes.includes(props.uid)
likesCount.value = newLikes.length
}
)
// throttle
const throttleCallback = () => {
message(
'You can only perform one operation within 1007 milliseconds',
'您在 1007 毫秒内只能进行一次操作',
'warn'
)
}
const likeComment = async () => {
//
if (isLiked.value) {
message(`You've already liked it`, '您已经点过赞了', 'warn')
return
}
//
if (props.uid === props.toUid) {
message('You cannot like yourself', '您不可以给自己点赞', 'warn')
return
}
const { tid, cid, toUid } = props
const res = await useKUNGalgameTopicStore().updateCommentLike(tid, cid, toUid)
if (res.code === 200) {
likesCount.value++
isLiked.value = true
message('Like successfully!', '点赞成功', 'success')
} else {
message('Like failed!', '点赞失败', 'error')
}
}
// throttle 1007
const handleClickLikeThrottled = throttle(likeComment, 1007, throttleCallback)
//
const handleClickLike = () => {
handleClickLikeThrottled()
}
</script>
<template>
<li :class="isLiked ? 'active' : ''" @click="handleClickLike">
<Icon class="icon" icon="line-md:thumbs-up-twotone" />
{{ likesCount }}
</li>
</template>
<style lang="scss" scoped>
li {
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;
.icon {
cursor: pointer;
color: var(--kungalgame-font-color-2);
margin-right: 2px;
}
}
/* 激活后的样式 */
.active .icon {
color: var(--kungalgame-blue-4);
}
</style>