demo.vue 9.9 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1 2 3 4 5 6 7 8 9 10 11
<template>
	<view class="page">
		<uniNoticeBar v-if="noticeData.data" showIcon="true" :text="noticeData.data"></uniNoticeBar>
		<text class="view_count">阅读量:{{noticeData.view_count}}</text>
		<view class="hader-box">
			<view class="let-box">
				<text>体验说明</text>
				<uni-icons size="14" color="#aaaaaa" @click="$refs.helpPopup.open()" type="info"></uni-icons>
			</view>
			<text @click="options.selfId?$refs.dialog.open():tipLogin()" class="comment-btn">写留言</text>
		</view>
12 13
		<unicloud-db v-if="noticeData._id" ref="udb" v-slot:default="{data, loading, error, options}" :options="options" page-data="replace"
			collection="opendb-notice-comment,uni-id-users" field="user_id{nickname,_id},text,_id,state" :where="options.where">
study夏羽's avatar
study夏羽 已提交
14 15
			<scroll-view :show-scrollbar="true" scroll-y v-if="data.length" class="comment-list">
				<view class="comment-item" v-for="(item,index) in data" :key="item._id">
16
					<image class="userImg" :src="'../../../static/userImg/'+item.user_id[0].nickname+'.png'" mode=""></image>
study夏羽's avatar
study夏羽 已提交
17 18
					<view class="content">
						<view style="flex-direction: column;">
19
							<text style="color: #666;font-size: 14px;font-weight:700;">{{item.user_id[0].nickname}}</text>
study夏羽's avatar
study夏羽 已提交
20 21 22 23 24
							<text style="color: #888;font-size: 14px;">{{item.text}}</text>
						</view>
						<view style="flex-direction: row;">
							<switch v-if="options.role.index>1" class="switch" :checked="item.state==1"
								@change="updateState($event,item._id)" />
25
							<template v-if="options.selfId == item.user_id[0]._id || options.role.index>1">
study夏羽's avatar
study夏羽 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
								<text class="in-review" v-if="options.role.index===1&&item.state==0">审核中</text>
								<uni-icons v-else color="#cdcfd4" class="ico" size="16" type="compose"
									@click="clickIcon(0,item)"></uni-icons>
								<uni-icons color="#cdcfd4" class="ico" size="16" type="trash"
									@click="clickIcon(1,item)"></uni-icons>
							</template>
						</view>
					</view>
				</view>
			</scroll-view>
		</unicloud-db>
		<uni-popup ref="dialog" type="dialog">
			<uni-popup-dialog mode="input" @confirm="submitComment" title="提交留言" placeholder="留言内容不能含单词test">
			</uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="upDataDialog" type="dialog">
			<uni-popup-dialog mode="input" :value="defaultText" @confirm="updateComment" title="更新留言"
				placeholder="留言内容不能含单词test"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="helpPopup" type="center">
			<uni-section title="demo说明" type="line"></uni-section>
			<uni-list-item title="发表留言" note="未登陆用户不能发表留言,你可以尝试切换账号类型为未登陆,发表留言后会被拒绝请求" />
			<uni-list-item title="敏感词过滤" note="发表留言内容含test会被拦截请求" />
			<uni-list-item title="编辑/删除留言" note="限用户编辑或删除自己发表的留言.管理员可以删除/编辑任何人的留言" />
			<uni-list-item title="公告的阅读量" note="1.读取,限登陆用户查看文章阅读量; \n 2.自增,阅读量自动增加由特殊的云函数add_view_count完成" />
52
			<uni-list-item title="数据查询" note="完整留言数据,由opendb-notice-comment,uni-id-users连张表,通过foreignKey联查获取" />
study夏羽's avatar
study夏羽 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65
		</uni-popup>
		<set-permission @change="changePermission"></set-permission>
	</view>
</template>
<script>
	const db = uniCloud.database()
	import uniNoticeBar from '@/uni_modules/uni-notice-bar/components/uni-notice-bar/uni-notice-bar.vue'
	export default {
		components: {
			uniNoticeBar
		},
		data() {
			return {
66 67 68
        noticeData: {
        	"_id": null
        },
study夏羽's avatar
study夏羽 已提交
69 70 71
				currentRole: 0,
				options: {
					"selfId": "",
72
					"where": "",// 默认为空,在查到公告内容后设置
study夏羽's avatar
study夏羽 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
					role: {
						index: 0
					},
				},
				activeNoticeId: false,
				defaultText: "",
				fields: "data",
				swipeActionOptions: [{
						text: '编辑',
						style: {
							backgroundColor: '#007aff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				],
			}
		},
		onLoad() {
			this.getNoticeData()
		},
		onReady() {},
		methods: {
			tipLogin() {
				uni.showModal({
					content: '未登陆游客不能写留言!可在底部工具条切换成其他角色体验',
					showCancel: false,
					confirmText: "知道了"
				});
			},
			changePermission(role) {
				console.log("role: ", role);
				this.options.selfId = role.uid
				switch (role.index) {
					case 0:
112
						this.options.where = `state == 1 && notice_id == "${this.noticeData._id}"`
study夏羽's avatar
study夏羽 已提交
113 114
						break;
					case 1:
115
						this.options.where = `state == 1 && notice_id == "${this.noticeData._id}" || user_id._id==$env.uid`
study夏羽's avatar
study夏羽 已提交
116 117
						break;
					case 2:
118
						this.options.where = {"notice_id":this.noticeData._id}
study夏羽's avatar
study夏羽 已提交
119 120
						break;
					case 3:
121
						this.options.where = {"notice_id":this.noticeData._id}
study夏羽's avatar
study夏羽 已提交
122 123 124 125 126 127 128 129 130
						break;
					default:
						break;
				}
				this.options.role = role
				this.currentRole = role.role
			},
			async getNoticeData() {
				let res = await db.action('add_view_count')
DCloud_JSON's avatar
DCloud_JSON 已提交
131
					.collection('opendb-notice')
132
          .doc("65365ac355b3379a66170144")
study夏羽's avatar
study夏羽 已提交
133 134
					.field('data,_id,update_time,view_count')
					.get();
135
					console.log("res: ",res);
study夏羽's avatar
study夏羽 已提交
136
				this.noticeData = res.result.data[0]
137
        this.options.where = `state == 1 && notice_id == "${this.noticeData._id}"`
study夏羽's avatar
study夏羽 已提交
138 139 140
			},
			async clickIcon(e, item) {
				if (e) {
141
					await this.$refs.udb.remove(item._id);
study夏羽's avatar
study夏羽 已提交
142 143 144 145 146 147
				} else {
					this.defaultText = item.text
					this.activeNoticeId = item._id
					this.$refs.upDataDialog.open()
				}
			},
148
			updateState(e, _id) {
study夏羽's avatar
study夏羽 已提交
149 150 151 152
				console.log(e.detail.value, _id);
				uni.showLoading({
					mask: true
				});
153
				db.collection('opendb-notice-comment')
study夏羽's avatar
study夏羽 已提交
154 155 156 157
					.doc(_id)
					.update({
						"state": e.detail.value / 1
					})
158
					.then(({code,message}) => {
study夏羽's avatar
study夏羽 已提交
159 160 161 162 163 164
						uni.showToast({
							title: '已切换为:' + (e.detail.value ? '审核通过' : '审核中'),
							icon: 'none',
							duration: 3000
						});
						console.log(code, message);
165
					}).catch(({code,message}) => {
study夏羽's avatar
study夏羽 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
						console.log(code, message);
					}).finally(e => {
						uni.hideLoading()
						this.$refs.upDataDialog.close()
					})
			},
			async updateComment(text) {
				console.log(text);
				console.log(this.activeNoticeId);
				if (this.defaultText == text) {
					uni.showToast({
						title: '内容未被修改',
						icon: 'none'
					});
					this.$refs.upDataDialog.close()
					return false
				}

				uni.showLoading({
					mask: true
				});
187
				await this.$refs.udb.update(this.activeNoticeId, {text}, {
study夏羽's avatar
study夏羽 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
					action: "up_comment",
					toastTitle: '修改成功', // toast提示语
					success: (res) => { // 更新成功后的回调
						const {
							code,
							message
						} = res
						console.log(code, message);
						//过度,后续unicloudDb的api会自动更新对应的数据
						this.$refs.udb.dataList.forEach((item, index) => {
							if (item._id == this.activeNoticeId) {
								this.$refs.udb.dataList[index].text = text
								if (this.options.role.index === 1) {
									this.$refs.udb.dataList[index].state = 0
								}
							}
						})
					},
					fail: (err) => { // 更新失败后的回调
207 208
						console.log("err: ",err);
						const {message} = err
study夏羽's avatar
study夏羽 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
					},
					complete: () => { // 完成后的回调
						uni.hideLoading()
						this.$refs.upDataDialog.close()
					}
				})
			},
			async submitComment(text) {
				console.log(text);
				if (!text) {
					uni.showToast({
						title: '留言内容不能为空',
						icon: 'none'
					});
					return false
				}
				this.$refs.dialog.close()
226 227
				await db.collection('opendb-notice-comment').add({
          notice_id:this.noticeData._id,
study夏羽's avatar
study夏羽 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
					text
				}).then(res => {
					console.log(res);
					this.getNewData()
				}).catch(({
					code,
					message
				}) => {
					if (code == 'TOKEN_INVALID_ANONYMOUS_USER') {
						uni.showModal({
							content: '未登陆游客不能写留言',
							showCancel: false
						});
					}
					if (code == 'VALIDATION_ERROR') {
						uni.showModal({
							content: message,
							showCancel: false
						});
					}
					console.log(code, message);
				})
			},
			getNewData() {
				this.$refs.udb.refresh() //{clear:true}
			},
			getUserImg(e) {
				switch (e) {
					case "admin":
						return '../../../static/userImg/2.png';
						break;
					case "user":
						return '../../../static/userImg/0.png';
						break;
					case "auditor":
						return '../../../static/userImg/1.png';
						break;
					default:
						return '../../../static/userImg/0.png';
						break;
				}
			},
		}
	}
</script>
<style scoped>
	view {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
	}

	text {
		font-size: 14px;
	}

	.icon {
		font-size: 26rpx;
	}

	.page {
		height: 100vh;
		width: 750rpx;
		overflow-x: hidden;
	}

	.row {
		background-color: #FFFFFF;
		flex-direction: row;
	}

	.let-box {
		flex-direction: row;
		color: #999999;
	}

	.let-box text,
	.let-box icon {
		line-height: 22px;
	}

	.bottom-box {
		width: 650rpx;
		padding: 0 50rpx;
		bottom: 0;
		position: fixed;
		background-color: #FFFFFF;
		flex-direction: column;
		display: flex;
	}

	.ico {
		margin-right: 10px;
	}

	.hader-box {
		flex-direction: row;
		justify-content: space-between;
		padding: 10px;
	}

	.add-comment-box {
		flex-direction: row;
	}

	.comment-list {
		flex: 1;
		width: 750rpx;
		padding: 16rpx;
	}

	.comment-item {
		width: 750rpx;
		margin-bottom: 20px;
		flex-direction: row;
	}

	.comment-item .content {
		flex-direction: row;
		width: 660rpx;
		justify-content: space-between;
		align-items: center;
		padding-left: 16rpx;
	}

	.userImg {
		width: 70rpx;
		height: 70rpx;
		border-radius: 100px;
		background-color: #EFEFF4;
	}

	.comment-btn {
		color: #586b95;
		font-size: 16px;
	}

	.view_count {
		font-size: 14px;
		padding: 0 16rpx;
		color: #666666;
	}

	.set-permission-box {
		position: fixed;
		width: 750rpx;
		align-items: center;
		bottom: 100px;
	}

	.switch {
		transform: scale(0.7);
	}

	.in-review {
		color: #DD524D;
		margin-right: 15rpx;
	}
</style>