guestbook.vue 5.1 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5
<template>
	<view class="page">
		<unicloud-db ref="udb" class="content" v-slot:default="{data,pagination,hasMore, loading, error}"
			collection="guestbook,uni-id-users" :where="udbWhere" field="user_id.nickname,user_id._id,user_id.avatar_file,text,_id,state">
			<view v-if="error">{{error.message}}</view>
雪洛's avatar
雪洛 已提交
6 7 8
			<view v-else>
				<view v-for="(item,index) in data" :key="index" class="item">
					<view class="main">
DCloud_JSON's avatar
DCloud_JSON 已提交
9 10 11 12 13 14
						<cloud-image :src="item.user_id[0].avatar_file.url"></cloud-image>
						<view>
							<text class="nickname">{{item.user_id[0].nickname}}</text>
							<text>{{item.text}}</text>
						</view>
					</view>
雪洛's avatar
雪洛 已提交
15
					<view class="handle">
DCloud_JSON's avatar
DCloud_JSON 已提交
16 17 18
						<switch :checked="item.state" @change="setState(item,$event)" />
						<uni-icons @click="deleteItem(item._id)" type="trash" size="18" color="#c9c3cd" />
					</view>
雪洛's avatar
雪洛 已提交
19 20
				</view>
				<uni-load-state :state="{data,pagination,hasMore,loading}"></uni-load-state>
DCloud_JSON's avatar
DCloud_JSON 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
			</view>
		</unicloud-db>
		<view class="submit-box">
			<cloud-image class="userImg" width="60rpx" height="60rpx" v-if="userInfo.avatar_file&&userInfo.avatar_file.url" :src="userInfo.avatar_file.url"></cloud-image>
			<image class="userImg" v-else src="/static/uni-center/grey.png" mode="widthFix"/>
			<input class="input-box" v-model="text" :disabled="!hasLogin" :placeholder="hasLogin?$t('guestbook.msgContent'):$t('guestbook.notAvailable')" />
			<button @click="text?send():''" class="btn" :class="{active:text}">{{$t('guestbook.send')}}</button>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const guestbookTable = db.collection('guestbook')
	import {
		mapMutations,mapGetters
	} from 'vuex';
	export default {
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				hasLogin: 'user/hasLogin'
雪洛's avatar
雪洛 已提交
43 44 45 46 47 48 49 50 51 52 53
			}),
			udbWhere(){
				if(this.hasLogin){
					if( this.uniIDHasRole('AUDITOR') ){
						return ''
					}else{
						return 'state==true || user_id._id==$cloudEnv_uid'
					}
				}else{
					return '"state"==true'
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
			}
		},
		data() {
			return {
				text: ""
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: this.$t('guestbook.navigationBarTitle')
			})
		},
		methods: {
			setState(item, e) {
				item.state = e.detail.value
雪洛's avatar
雪洛 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
				console.log(item, e);
				this.$refs.udb.update(item._id,{
					state: item.state
				},{
					needConfirm:false,
					toastTitle: this.$t('common').updateSucceeded, // toast提示语
					success: (res) => { // 新增成功后的回调
						let {code,message} = res
						console.log(code,message);
					},
					fail: (err) => { // 新增失败后的回调
						let {message} = err
						console.log(err);
						// 判断没有权限
						uni.showToast({
							title:this.$t('guestbook.noPermission'),
							icon: 'none'
						});
						this.$nextTick(() => {
							item.state = !e.detail.value
						})
					},
					complete: () => { // 完成后的回调 
					}
				})
DCloud_JSON's avatar
DCloud_JSON 已提交
94 95
			},
			deleteItem(id) {
雪洛's avatar
雪洛 已提交
96
				this.$refs.udb.remove(id, {
DCloud_JSON's avatar
DCloud_JSON 已提交
97 98 99 100 101 102 103
					complete: e => {
						console.log(e);
					}
				})
			},
			send() {
				this.$refs.udb.add({
雪洛's avatar
雪洛 已提交
104
					text: this.text
DCloud_JSON's avatar
DCloud_JSON 已提交
105 106 107
				},{
					toastTitle:this.$t('guestbook.addSucceeded'), // toast提示语
					success: (res) => { // 新增成功后的回调
雪洛's avatar
雪洛 已提交
108 109 110
						let {code,message} = res
						console.log(code,message);
						this.text = ''
DCloud_JSON's avatar
DCloud_JSON 已提交
111 112 113
						this.$refs.udb.refresh() //{clear:true}
					},
					fail: (err) => { // 新增失败后的回调
雪洛's avatar
雪洛 已提交
114 115
						let {message} = err
						console.log(err);
DCloud_JSON's avatar
DCloud_JSON 已提交
116 117 118 119
					},
					complete: () => { // 完成后的回调
					}
				})
雪洛's avatar
雪洛 已提交
120 121 122
			},
			...mapMutations({
				logout: 'user/logout'
DCloud_JSON's avatar
DCloud_JSON 已提交
123
			}),
雪洛's avatar
雪洛 已提交
124 125 126 127 128 129 130 131 132 133
		},
		onNavigationBarButtonTap(e) {
			console.log(e);
			if(e.index){
				this.logout()
			}else{
				uni.navigateTo({
					url:"/pages/ucenter/login-page/index/index"
				})
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		}
	}
</script>

<style>
	view {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
	}

	.content {
		padding-bottom: 110px;
	}

	.item {
		flex-direction: row;
		justify-content: space-between;
		padding: 10rpx;
		width: 730rpx;
		margin-left: 10rpx;
		border-radius: 10px;
		margin-top: 10px;
	}
	.item .main,
	.item .handle {
		flex-direction: row;
		align-items: center;
	}

	.item .main text {
		padding: 0 10rpx;
		color: #666666;
		font-size: 24rpx;
	}

	.item .main .nickname {
		font-weight: 600;
	}

	.item .handle switch {
		transform: scale(0.6);
	}

	.submit-box {
		position: fixed;
		flex-direction: row;
		align-items: center;
		bottom: 0;
		padding: 20rpx 15rpx;
		width: 750rpx;
		border-top: solid 1px #efecf2;
		background-color: #ffffff;
		height: 56px;
	}

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

	.submit-box .input-box {
		background-color: #f8f8f8;
		padding: 15rpx;
		flex-grow: 1;
		margin: 20rpx;
		border-radius: 6px;
		font-size: 24rpx;
	}

	.submit-box .btn {
		height: 30px;
		line-height: 30px;
		font-size: 24rpx;
		width: 80rpx;
		padding: 0;
		color: #888888;
	}

	.submit-box .btn::after {
		display: none;
	}

	.submit-box .btn.active {
		background-color: #007aff;
		color: #FFFFFF;
	}
雪洛's avatar
雪洛 已提交
223
</style>