demo.vue 10.0 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1

study夏羽's avatar
study夏羽 已提交
2 3 4 5 6 7 8 9 10 11 12
<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>
13 14
		<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夏羽 已提交
15 16
			<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">
17
					<image class="userImg" :src="'../../../static/userImg/'+item.user_id[0].nickname+'.png'" mode=""></image>
study夏羽's avatar
study夏羽 已提交
18 19
					<view class="content">
						<view style="flex-direction: column;">
20
							<text style="color: #666;font-size: 14px;font-weight:700;">{{item.user_id[0].nickname}}</text>
study夏羽's avatar
study夏羽 已提交
21 22 23 24 25
							<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)" />
26
							<template v-if="options.selfId == item.user_id[0]._id || options.role.index>1">
study夏羽's avatar
study夏羽 已提交
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 52
								<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完成" />
53
			<uni-list-item title="数据查询" note="完整留言数据,由opendb-notice-comment,uni-id-users连张表,通过foreignKey联查获取" />
study夏羽's avatar
study夏羽 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66
		</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 {
67 68 69
        noticeData: {
        	"_id": null
        },
study夏羽's avatar
study夏羽 已提交
70 71 72
				currentRole: 0,
				options: {
					"selfId": "",
73
					"where": "",// 默认为空,在查到公告内容后设置
study夏羽's avatar
study夏羽 已提交
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 112
					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:
113
						this.options.where = `state == 1 && notice_id == "${this.noticeData._id}"`
study夏羽's avatar
study夏羽 已提交
114 115
						break;
					case 1:
116
						this.options.where = `state == 1 && notice_id == "${this.noticeData._id}" || user_id._id==$env.uid`
study夏羽's avatar
study夏羽 已提交
117 118
						break;
					case 2:
119
						this.options.where = {"notice_id":this.noticeData._id}
study夏羽's avatar
study夏羽 已提交
120 121
						break;
					case 3:
122
						this.options.where = {"notice_id":this.noticeData._id}
study夏羽's avatar
study夏羽 已提交
123 124 125 126 127 128 129 130 131
						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 已提交
132
					.collection('opendb-notice')
A
Anne_LXM 已提交
133
					.doc("65365ac355b3379a66170144")
study夏羽's avatar
study夏羽 已提交
134 135
					.field('data,_id,update_time,view_count')
					.get();
136
					console.log("res: ",res);
study夏羽's avatar
study夏羽 已提交
137
				this.noticeData = res.result.data[0]
A
Anne_LXM 已提交
138
				this.options.where = `state == 1 && notice_id == "${this.noticeData._id}"`
study夏羽's avatar
study夏羽 已提交
139 140 141
			},
			async clickIcon(e, item) {
				if (e) {
142
					await this.$refs.udb.remove(item._id);
study夏羽's avatar
study夏羽 已提交
143 144 145 146 147 148
				} else {
					this.defaultText = item.text
					this.activeNoticeId = item._id
					this.$refs.upDataDialog.open()
				}
			},
A
Anne_LXM 已提交
149
			async updateState(e, _id) {
study夏羽's avatar
study夏羽 已提交
150 151 152 153
				console.log(e.detail.value, _id);
				uni.showLoading({
					mask: true
				});
154
				return await db.collection('opendb-notice-comment')
study夏羽's avatar
study夏羽 已提交
155 156 157 158
					.doc(_id)
					.update({
						"state": e.detail.value / 1
					})
159
					.then(({code,message}) => {
study夏羽's avatar
study夏羽 已提交
160 161 162 163 164 165
						uni.showToast({
							title: '已切换为:' + (e.detail.value ? '审核通过' : '审核中'),
							icon: 'none',
							duration: 3000
						});
						console.log(code, message);
A
Anne_LXM 已提交
166
						return message
167
					}).catch(({code,message}) => {
study夏羽's avatar
study夏羽 已提交
168
						console.log(code, message);
A
Anne_LXM 已提交
169
						return message
study夏羽's avatar
study夏羽 已提交
170 171 172
					}).finally(e => {
						uni.hideLoading()
						this.$refs.upDataDialog.close()
A
Anne_LXM 已提交
173
						return e
study夏羽's avatar
study夏羽 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
					})
			},
			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
				});
A
Anne_LXM 已提交
191
				return await this.$refs.udb.update(this.activeNoticeId, {text}, {
study夏羽's avatar
study夏羽 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
					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
								}
							}
						})
A
Anne_LXM 已提交
209
						return message
study夏羽's avatar
study夏羽 已提交
210 211
					},
					fail: (err) => { // 更新失败后的回调
212 213
						console.log("err: ",err);
						const {message} = err
A
Anne_LXM 已提交
214
						return message
study夏羽's avatar
study夏羽 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
					},
					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()
232
				return await db.collection('opendb-notice-comment').add({
A
Anne_LXM 已提交
233
					notice_id:this.noticeData._id,
study夏羽's avatar
study夏羽 已提交
234 235 236 237
					text
				}).then(res => {
					console.log(res);
					this.getNewData()
A
Anne_LXM 已提交
238
					return res.result
study夏羽's avatar
study夏羽 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
				}).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);
A
Anne_LXM 已提交
256
					return message
study夏羽's avatar
study夏羽 已提交
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 387 388 389 390 391 392 393 394
				})
			},
			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>