feat: 新增文章点赞功能

This commit is contained in:
小莫唐尼 2024-11-06 19:23:13 +08:00
parent 7c15c84dba
commit e19b107ed0
3 changed files with 1367 additions and 1336 deletions

View File

@ -205,12 +205,22 @@ export default {
* 获取二维码信息 * 获取二维码信息
*/ */
getQRCodeInfo: (key) => { getQRCodeInfo: (key) => {
return HttpHandler.Get(`/apis/api.uni.uhalo.pro/v1alpha1/plugins/plugin-uni-halo/getQRCodeInfo/${key}`, null,) return HttpHandler.Get(`/apis/api.uni.uhalo.pro/v1alpha1/plugins/plugin-uni-halo/getQRCodeInfo/${key}`,
null, {})
}, },
/** /**
* 获取二维码图片 * 获取二维码图片
*/ */
getQRCodeImg: (postId) => { getQRCodeImg: (postId) => {
return HttpHandler.Get(`/apis/api.uni.uhalo.pro/v1alpha1/plugins/plugin-uni-halo/getQRCodeImg/${postId}`, null,) return HttpHandler.Get(`/apis/api.uni.uhalo.pro/v1alpha1/plugins/plugin-uni-halo/getQRCodeImg/${postId}`,
null, {})
},
/**
* 点赞
* @param {*} data ={group, plural, name}
*/
submitUpvote(data) {
return HttpHandler.Post(`/apis/api.halo.run/v1alpha1/trackers/upvote`, data, {})
} }
} }

View File

@ -131,7 +131,7 @@
<!-- 返回顶部 --> <!-- 返回顶部 -->
<tm-flotbutton :offset="[16, 80]" icon="icon-angle-up" color="bg-gradient-light-blue-accent" <tm-flotbutton :offset="[16, 80]" icon="icon-angle-up" color="bg-gradient-light-blue-accent"
@click="fnToTopPage()"></tm-flotbutton> @click="fnToTopPage()"></tm-flotbutton>
<tm-flotbutton :actions="flotButtonActions" actions-pos="left" :show-text="true" <tm-flotbutton :actions="flotButtonActions" :click-actions-hiden="false" actions-pos="left" :show-text="true"
color="bg-gradient-orange-accent" @change="fnOnFlotButtonChange"></tm-flotbutton> color="bg-gradient-orange-accent" @change="fnOnFlotButtonChange"></tm-flotbutton>
</block> </block>
@ -232,9 +232,8 @@
import rCanvas from '@/components/r-canvas/r-canvas.vue'; import rCanvas from '@/components/r-canvas/r-canvas.vue';
import barrage from '@/components/barrage/barrage.vue'; import barrage from '@/components/barrage/barrage.vue';
import { import {getAppConfigs} from '@/config/index.js'
getAppConfigs import {upvote} from '@/utils/upvote.js'
} from '@/config/index.js'
export default { export default {
components: { components: {
@ -325,6 +324,9 @@
calcIsShowComment() { calcIsShowComment() {
return this.postDetailConfig.showComment return this.postDetailConfig.showComment
},
calcUpvoted() {
return upvote.has("post", this.result?.metadata?.name)
} }
}, },
watch: { watch: {
@ -410,6 +412,7 @@
this.fnSetPageTitle('文章详情'); this.fnSetPageTitle('文章详情');
this.loading = 'success'; this.loading = 'success';
this.fnHandleSetFlotButtonItems(this.haloConfigs);
}) })
.catch(err => { .catch(err => {
console.log("错误", err) console.log("错误", err)
@ -427,9 +430,9 @@
use: true, use: true,
}, },
{ {
icon: 'icon-like', icon: upvote.has("post", this.result?.metadata?.name) ? 'icon-heart-fill' : 'icon-like',
color: 'bg-gradient-orange-accent', color: upvote.has("post", this.result?.metadata?.name) ? 'bg-gradient-red-accent' : 'bg-gradient-orange-accent',
use: false, use: true,
}, },
{ {
icon: 'icon-commentdots-fill', icon: 'icon-commentdots-fill',
@ -443,13 +446,13 @@
// //
fnOnFlotButtonChange(index) { fnOnFlotButtonChange(index) {
switch (index) { switch (index) {
// case 0:
// this.fnDoLikes();
// break;
case 0: case 0:
this.fnShowShare(); this.fnShowShare();
break; break;
case 1: case 1:
this.fnDoLikes();
break;
case 2:
this.fnToComment(); this.fnToComment();
break; break;
} }
@ -483,24 +486,22 @@
this.commentModal.title = ""; this.commentModal.title = "";
}, },
fnDoLikes() { fnDoLikes() {
this.$httpApi if (upvote.has("post", this.result?.metadata?.name)) {
.postLikePost(this.result.id) uni.$tm.toast('已经点过赞啦!');
.then(res => { return;
if (res.status == 200) {
this.result.likes += 1;
uni.$tm.toast('\(^o^)/~点赞成功!');
} else {
uni.showToast({
icon: 'none',
title: res.message
});
} }
this.$httpApi.v2.submitUpvote({
group: "content.halo.run",
plural: "posts",
name: this.result?.metadata?.name
})
.then(res => {
uni.$tm.toast('点赞成功!');
upvote.set("post", this.result?.metadata?.name)
this.fnHandleSetFlotButtonItems(this.haloConfigs);
}) })
.catch(err => { .catch(err => {
uni.showToast({ uni.$tm.toast('点赞失败');
icon: 'none',
title: err.message
});
}); });
}, },
fnShowShare() { fnShowShare() {
@ -1017,7 +1018,8 @@
font-size: 24rpx; font-size: 24rpx;
color: #666; color: #666;
&-name {} &-name {
}
&-time { &-time {
margin-left: 36rpx; margin-left: 36rpx;

19
utils/upvote.js Normal file
View File

@ -0,0 +1,19 @@
export const upvote = {
get(key) {
const data = uni.getStorageSync(`upvote.${key}.halo.run`)
if (data) {
return JSON.parse(data)
} else {
return []
}
},
has(key, name) {
const list = this.get(key)
if (list.length == 0) return false;
return list.includes(name)
},
set(key, name) {
const list = this.get(key)
uni.setStorageSync(`upvote.${key}.halo.run`, JSON.stringify([...list, name]))
}
}