Files
t/pages/test/test.vue
T
2025-03-12 20:11:03 +08:00

132 lines
3.6 KiB
Vue

<template>
<view>
<view class="">
<button @click="get()">get</button>
<button @click="del()">del</button>
<button @click="add()">add</button>
<button @click="update()">update</button>
<button @click="likeComment()">likeComment</button>
<button @click="relikeComment()">relikeComment</button>
<button @click="to()">to</button>
</view>
</view>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
async get() {
console.log();
let query ={article_id:"1"};
const res = await uniCloud.importObject("comments").getComments(query);
console.log("成功获取评论:", res);
},
async del() {
let id = "map[$oid:67b45c598a5c78c37f605029]";
const res = await uniCloud.importObject("comments").deleteComment(id);
console.log(res);
},
async add() {
try {
// 获取当前用户信息(需确保已登录)
const uniIdToken = uni.getStorageSync('uni_id_token');
if (!uniIdToken) {
uni.showToast({ title: '请先登录', icon: 'none' });
return;
}
// 构建符合后端接口规范的评论对象
const comment = {
article_id: "67bbac72c6de56f8256d76b1",
content: "测试评论",
parent_id: 0,
};
// 调用云对象方法
const commentObj = uniCloud.importObject("comments");
const res = await commentObj.addComment(comment);
// 处理响应结果
if (res.code === 200) {
uni.showToast({ title: '评论成功' });
console.log('评论详情:', {
id: res.data.comment_id,
depth: res.data.depth,
rootComment: res.data.root_id || '一级评论'
});
// 更新前端显示(示例)
// this.$refs.commentList.unshift({
// _id: res.data.comment_id,
// content: comment.content,
// create_time: new Date().toISOString(),
// depth: res.data.depth,
// reply_count: 0
// });
}
} catch (e) {
console.error('评论失败:', e);
// 错误分类处理(根据后端错误码映射)
const errorHandler = {
'PERMISSION_DENIED': '登录状态失效,请重新登录',
'INVALID_PARAMS': '评论内容不能为空',
'CONTENT_BLOCKED': '包含违禁词汇: ' + e.message.split(':')[1],
'PARENT_COMMENT_NOT_FOUND': '回复的评论不存在'
};
uni.showToast({
title: errorHandler[e.errorType] || '评论失败,请稍后重试',
icon: 'none'
});
}
},
async update() {
let comment_id = "67bdf451dab53f685048dccc";
let comment = {
content: "修改后"
}
const res = await uniCloud.importObject("comments").updateComment(comment_id, comment);
console.log(res);
},
async relikeComment() {
let comment_like_id="67bdd9cd5a6f8bd7032c949a";
let comment_id="67b466c9ce5ec9e5aa41a25d";
const res = await uniCloud.importObject("comments").relikeComment(comment_like_id, comment_id);
},
async likeComment() {
let user_id="67b466c9ce5ec9e5aa41a25d";
let comment_id = "67b466c9ce5ec9e5aa41a25d";
const res = await uniCloud.importObject("comments").likeComment(user_id, comment_id);
},
to() {
uni.navigateTo({
url:"/uni_modules/uni-cms-article/pages/list/list"
})
},
to1() {
uni.navigateTo({
url:"/uni_modules/uni-cms-article/pages/detail/detail"
})
}
}
}
</script>
<style>
</style>