uniapp使用DB Schema方式实现一级评论


首先我们在项目uniCloud/database中新建一个schema数据库文件

创建一个文章表

在该文件下写入以下内容 (也可根据自己要求进行添加)

{
	"bsonType": "object",
	"required": ["articleTitle","articleContent"],
	"permission": {
		"read": false,
		"create": false,
		"update": false,
		"delete": false
	},
	"properties": {
		"_id": {
			"description": "ID,系统自动生成"
		},
		"user_id":{
			"bsonType": "string",
			"title": "绑定用户",
			"description": "绑定用户id 这里必须设置,不然不清楚发表文章的是谁",
			"defaultValue":{
				"$env": "uid"
			}
		},
		"articleTitle":{
			"bsonType": "string",
			"title": "文章标题",
			"description": "文章标题"
		},
		"articleContent":{
			"bsonType": "string",
			"title": "文章内容",
			"description": "文章内容"
		},
		"ArticleReleaseTime":{
			"bsonType": "timestamp",
			"title": "发布时间",
			"description": "文章发布时间",
			"defaultValue":{
				"$env": "now"
			}
		},
		"publishingAddress":{
			"bsonType": "string",
			"title": "发布地址",
			"defaultValue":{
				"$env": "clientIP"
			}
		}
		
	}
}

 注意:这里我们使用了uid,所以我们需要提前安装uniapp的uni-id-pages进行配置,如果不会请前往官网进行配置概述 | uni-app官网 后期我也会出一个uni-id-pages的使用教程

 创建一个schema表,注意.schema.json不能更改,选择评论模板,点击创建

此时我们可以看见表中的字段
required是必填项,不填写会提示错误信息,我们可以把 "reply_user_id",,"reply_comment_id"去除了,这两个属性可以设置为不是必填,每个字段都有相应的description描述自己看下
permission 是对数据库的权限操作,分别是 读取,写入,更新,删除
auth.uid !=null 表示用户不能为空,也就是用户必须登录
doc.user_id == auth.uid 表示必须是当前发布的用户才能进行操作

{
	"bsonType": "object",
	"required": [
		"article_id",
		"user_id",
		"comment_content",
		"comment_type"
	],
	"permission": {
		"read": true,
		"create": "auth.uid != null",
		"update": "doc.user_id == auth.uid",
		"delete": "doc.user_id == auth.uid"
	},
	"properties": {
		"_id": {
			"description": "存储文档 ID(文章 ID),系统自动生成"
		},
		"article_id": {
			"bsonType": "string",
			"description": "文章ID,opendb-news-posts 表中的`_id`字段",
			"foreignKey": "article._id"
		},
		"user_id": {
			"bsonType": "string",
			"description": "评论者ID,参考`uni-id-users` 表",
			"forceDefaultValue": {
				"$env": "uid"
			},
			"foreignKey": "uni-id-users._id"
		},
		"comment_content": {
			"bsonType": "string",
			"description": "评论内容",
			"title": "评论内容",
			"trim": "right"
		},
		"comment_type": {
			"bsonType": "int",
			"description": "回复类型: 0 针对文章的回复  1 针对评论的回复"
		},
		"reply_user_id": {
			"bsonType": "string",
			"description": "被回复的评论用户ID,comment_type为1时有效",
			"foreignKey": "uni-id-users._id"
		},
		"reply_comment_id": {
			"bsonType": "string",
			"description": "被回复的评论ID,comment_type为1时有效",
			"parentKey": "_id"
		},
		"comment_date": {
			"bsonType": "timestamp",
			"description": "评论发表时间",
			"forceDefaultValue": {
				"$env": "now"
			}
		},
		"comment_ip": {
			"bsonType": "string",
			"description": "评论发表时 IP 地址",
			"forceDefaultValue": {
				"$env": "clientIP"
			}
		}
	},
	"version": "0.0.1"
}

 这时候我们的表已经建立好了,但是要记得需要上传所有的schema数据

接下来我们就可以在页面中去使用了

发布文章 在index.vue中 代码如下

<template>
	<view>
		<u--input placeholder="请输入内容" border="surround" v-model="newsTitle"></u--input>
		<u--textarea v-model="newsContent" placeholder="请输入内容"></u--textarea>
		<u-button type="primary" text="确定" @click="primary"></u-button>
	</view>
</template>

<script>
	let db = uniCloud.database()
	export default {
		data() {
			return {
				newsTitle: '',
				newsContent: ''
			}
		},
		methods: {
			// 发布文章
			primary() {
				let _this = this
				db.collection('article')
					.add({
						newsTitle: _this.newsTitle,
						newsContent: _this.newsContent
					})
					.then(res => {
						uni.showToast({
							title: "发表成功",
							icon: "none"
						})
					})
					.catch(err => {
						uni.showToast({
							title: err.message,
							icon: "none"
						})
					})
			}
		}
	}
</script>

<style>

</style>

给文章添加评论 

methods: {
    // 用户评论
	pinglun(){
		uniCloud.database().collection('comment').add({
			"article_id":this.uid,//文章id
			"comment_content":"用户评论内容",
			"comment_type":0 //0为文章 1为用户
		}).then(res=>{
			uni.showToast({
				title:'评论成功'
			})
		})
	}
}

展示文章和评论 代码如下

onLoad(options) {
			// 展示评论数据
			let uid = options.uid
			let comment= uniCloud.database().collection('comment').where(`article_id == "${uid}"`).getTemp()
			let users = uniCloud.database().collection('uni-id-users').field('_id,nickname').getTemp()
			uniCloud.database().collection(comment,users).get().then(res=>{
				console.log(res)
			})
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值