Files
t/pages/test/test.vue
T

119 lines
3.0 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>
import { onMounted } from 'vue'
export default {
setup() {
onMounted(() => {
// 模拟 onLoad
})
async function get() {
let query = { article_id: "1" }
const res = await uniCloud.importObject("comments").getComments(query)
console.log("成功获取评论:", res)
}
async function del() {
let id = "map[$oid:67b45c598a5c78c37f605029]"
const res = await uniCloud.importObject("comments").deleteComment(id)
console.log(res)
}
async function 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 || '一级评论'
})
}
} 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 function update() {
let comment_id = "67bdf451dab53f685048dccc"
let comment = { content: "修改后" }
const res = await uniCloud.importObject("comments").updateComment(comment_id, comment)
console.log(res)
}
async function relikeComment() {
let comment_like_id = "67bdd9cd5a6f8bd7032c949a"
let comment_id = "67b466c9ce5ec9e5aa41a25d"
const res = await uniCloud.importObject("comments").relikeComment(comment_like_id, comment_id)
}
async function likeComment() {
let user_id = "67b466c9ce5ec9e5aa41a25d"
let comment_id = "67b466c9ce5ec9e5aa41a25d"
const res = await uniCloud.importObject("comments").likeComment(user_id, comment_id)
}
function to() {
uni.navigateTo({ url: "/uni_modules/uni-cms-article/pages/list/list" })
}
function to1() {
uni.navigateTo({ url: "/uni_modules/uni-cms-article/pages/detail/detail" })
}
return {
get,
del,
add,
update,
likeComment,
relikeComment,
to,
to1
}
}
}
</script>
<style>
</style>