notification.vue 7.0 KB
Newer Older
1 2 3 4 5 6
<template>
	<view class="notification-box">
		<text class="tips" v-if="tips">{{tips}}</text>
		<uni-list :border="false">
			<template v-if="notificationDatas && notificationDatas.length">
				<uni-im-info-card v-for="(item,index) in notificationDatas" :key="item.id" :avatarCircle="true"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
7 8 9
					:clickable="true" :badge="item.is_read?'':'dot'" badgePositon="left"
					:title="item.payload.title || item.title" :note="item.payload.content||item.content||'无'"
					:avatar="item.payload?.avatar_file?.url || '/uni_modules/uni-im/static/noticeIcon/notification2.png'"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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
					@click.native="clickHandle(index,item)" direction="column" :time="friendlyTime(item.create_time)">
					<view class="handle-box">
						<template v-if="item.payload.state">
							<text class="handle done">
								{{''+(item.payload.state == 'confirm'?item.payload.confirmText:item.payload.cancelText)}}
							</text>
						</template>
						<template v-else>
							<text class="handle" @click.stop="doAction(index,0)"
								v-if="item.payload.cancelText">{{item.payload.cancelText}}</text>
							<text class="handle" @click.stop="doAction(index,1)"
								v-if="item.payload.confirmText">{{item.payload.confirmText}}</text>
							<uni-icons v-if="!item.payload.cancelText && !item.payload.confirmText && item.path" type="right"
								color="#cccccc"></uni-icons>
						</template>
					</view>
				</uni-im-info-card>
			</template>
			<uni-list-item v-else class="load-more">
				<template v-slot:body>
					<uni-im-load-state :contentText="contentText" class="tip" :status="hasMore?'loading':'noMore'"></uni-im-load-state>
				</template>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	import uniIm from '@/uni_modules/uni-im/sdk/index.js';
	import action from './action.js';
	const db = uniCloud.database();
	export default {
		data() {
			return {
				contentText: {
					"contentrefresh": "加载中...",
					"contentnomore": "- 暂无相关数据 -"
				},
				filterNotice: {},
				tips: "",
				hasMore: true
				// notificationDatas:[]
			}
		},
		async onLoad({
			param
		}) {
			// console.log(param,decodeURIComponent(param))
			param = JSON.parse(decodeURIComponent(param))
			// console.log(param)
			this.setParam(param)
		},
		computed: {
			//是否为pc宽屏(width>960px)
			isWidescreen() {
				return uniIm.isWidescreen
			},
			notificationDatas() {
				let notificationDatas = uniIm.notification.get(this.filterNotice)
				if (notificationDatas.length == 0) {
					setTimeout(() => {
						this.hasMore = false
					}, 100);
				}
				return notificationDatas
			}
		},
		mounted() {
			this.hasMore = uniIm.notification.hasMore
		},
		methods: {
			setParam({
				filterNotice,
				title
			}) {
				if (typeof filterNotice == 'string') {
					filterNotice = JSON.parse(decodeURIComponent(filterNotice))
				}
				this.filterNotice = filterNotice
				console.log('filterNotice', filterNotice)
				uni.setNavigationBarTitle({
					title
				})

				if (title == '新朋友' && !this.isWidescreen) {
					this.tips = '好友请求通知'
				}
			},
			async setItem({
				_id
			}, param) {
				const datas = uniIm.notification.get(this.filterNotice)
				for (let i = 0; i < datas.length; i++) {
					if (datas[i]._id == _id) {
						datas[i] = deepAssign(datas[i], param)
						uniIm.notificationDatas = datas
						console.log('uniIm.notificationDatas', uniIm.notificationDatas)
						break;
					}
				}

				let ares = await db.collection('uni-im-notification')
					.where(`"_id" == "${_id}" && "user_id" == $cloudEnv_uid`)
					.get()
				// console.log(13231,ares);
				let res = await db.collection('uni-im-notification')
					.where(`"_id" == "${_id}" && "user_id" == $cloudEnv_uid`)
					.update(param)
				// console.log('res---66666',param,res.result.updated);

				/**
				 *判断对象是否是一个纯粹的对象
				 */
				function isPlainObject(obj) {
					return typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]'
				}
				/**
				 *深度合并多个对象的方法
				 */
				function deepAssign() {
					let len = arguments.length,
						target = arguments[0]
					if (!isPlainObject(target)) {
						target = {}
					}
					for (let i = 1; i < len; i++) {
						let source = arguments[i]
						if (isPlainObject(source)) {
							for (let s in source) {
								if (s === '__proto__' || target === source[s]) {
									continue
								}
								if (isPlainObject(source[s])) {
									target[s] = deepAssign(target[s], source[s])
								} else {
									target[s] = source[s]
								}
							}
						}
					}
					return target
				}
			},
			async clickHandle(index, item) {
				// console.log('index',index,item);
				//如果未读,设置为已读
				if (!item.is_read) {
					this.setItem(item, {
						is_read: true
					})
				}
				//存在链接就跳转
				let path = item.path || item.payload.path
				if (path) {
					uni.navigateTo({
						url: path,
						fail: (e) => {
							console.error(e);
						}
					})
				}
				// let item = this.notificationDatas[index]
				// item.data.is_read = true
				// this.notificationDatas[index] = Object.assign({},item)
				// console.log(this.notificationDatas);
			},
			doAction(index, type) {
				let item = this.notificationDatas[index]
				let e = {
					subType: item.payload.subType,
					confirm: type === 1,
					cancel: type === 0,
					item
				}
				action(e, data => {
					console.log('doAction', data)
					this.setItem(item, {
						is_read: true,
						payload: {
							state: type === 1 ? 'confirm' : 'cancel'
						}
					})
				})
				// console.log(index);
			},
			friendlyTime(timestamp) {
				return uniIm.utils.toFriendlyTime(timestamp)
			},
			handleText(state) {
				switch (state) {
					case 0:
						return '同意'
						break;
					case 100:
						return '已同意'
						break;
					case -100:
						return '已拒绝'
						break;
					default:
						return '其他'
						break;
				}
			}
		}
	}
</script>

218 219
<style lang="scss">
  @import "@/uni_modules/uni-im/common/baseStyle.scss";
220 221 222
	.notification-box {
		height: 100vh;
		background-color: #f5f5f5;
223 224 225 226 227 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
    .tips {
    	height: 40px;
    	line-height: 40px;
    	padding-left: 20rpx;
    	font-size: 26rpx;
    	color: #666;
    }
    
    .handle-box {
    	flex-direction: row;
    	height: 40px;
    	align-items: center;
    }
    
    .handle {
    	width: 50px;
    	text-align: center;
    	height: 25px;
    	line-height: 25px;
    	background-color: #efefef;
    	border-radius: 50px;
    	font-size: 12px;
    	margin: 0 5px;
    	/* #ifdef H5 */
    	cursor: pointer;
    	/* #endif */
    }
    
    .done {
    	width: 50px;
    	background-color: #FFF;
    	color: #aaa;
    	/* #ifdef H5 */
    	cursor: default;
    	/* #endif */
    }
    
    .load-more {
    	background-color: #f5f5f5 !important;
    	justify-content: center;
    }
    
    .tip {
    	position: relative;
    	left: -15px;
    	width: 750rpx;
    	/* pc宽屏时需要使用100vw */
    	width: 100%;
    }
    
    /* #ifdef MP-WEIXIN */
    .load-more ::v-deep .uni-list-item {
    	background-color: #f5f5f5 !important;
    }
    
    /* #endif */
279 280
	}
</style>