uni-list-chat.vue 13.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9
<template>
	<!-- #ifdef APP-NVUE -->
	<cell>
		<!-- #endif -->
		<view :hover-class="!clickable && !link ? '' : 'uni-list-chat--hover'" class="uni-list-chat" @click.stop="onClick">
			<view :class="{ 'uni-list--border': border, 'uni-list-chat--first': isFirstChild }"></view>
			<view class="uni-list-chat__container">
				<view class="uni-list-chat__header-warp">
					<view v-if="avatarCircle || avatarList.length === 0" class="uni-list-chat__header" :class="{ 'header--circle': avatarCircle }">
study夏羽's avatar
study夏羽 已提交
10
						<image class="uni-list-chat__header-image" :class="{ 'header--circle': avatarCircle }" :src="avatarUrl" mode="aspectFill"></image>
DCloud_JSON's avatar
DCloud_JSON 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
					</view>
					<!-- 头像组 -->
					<view v-else class="uni-list-chat__header">
						<view v-for="(item, index) in avatarList" :key="index" class="uni-list-chat__header-box" :class="computedAvatar"
						 :style="{ width: imageWidth + 'px', height: imageWidth + 'px' }">
							<image class="uni-list-chat__header-image" :style="{ width: imageWidth + 'px', height: imageWidth + 'px' }" :src="item.url"
							 mode="aspectFill"></image>
						</view>
					</view>
				</view>
				<view v-if="badgeText && badgePositon === 'left'" class="uni-list-chat__badge uni-list-chat__badge-pos" :class="[isSingle]">
					<text class="uni-list-chat__badge-text">{{ badgeText === 'dot' ? '' : badgeText }}</text>
				</view>
				<view class="uni-list-chat__content">
					<view class="uni-list-chat__content-main">
study夏羽's avatar
study夏羽 已提交
26 27 28 29 30
						<text class="uni-list-chat__content-title uni-ellipsis">{{ title }}</text>
						<view style="flex-direction: row;">
							<text class="draft" v-if="isDraft">[草稿]</text>
							<text class="uni-list-chat__content-note uni-ellipsis">{{isDraft?note.slice(14,-1):note}}</text>
						</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
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
					</view>
					<view class="uni-list-chat__content-extra">
						<slot>
							<text class="uni-list-chat__content-extra-text">{{ time }}</text>
							<view v-if="badgeText && badgePositon === 'right'" class="uni-list-chat__badge" :class="[isSingle, badgePositon === 'right' ? 'uni-list-chat--right' : '']">
								<text class="uni-list-chat__badge-text">{{ badgeText === 'dot' ? '' : badgeText }}</text>
							</view>
						</slot>
					</view>
				</view>
			</view>
		</view>
		<!-- #ifdef APP-NVUE -->
	</cell>
	<!-- #endif -->
</template>

<script>
	// 头像大小
	const avatarWidth = 45;

	/**
	 * ListChat 聊天列表
	 * @description 聊天列表,用于创建聊天类列表
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=24
	 * @property {String} 	title 							标题
	 * @property {String} 	note 							描述
	 * @property {Boolean} 	clickable = [true|false] 		是否开启点击反馈,默认为false
	 * @property {String} 	badgeText						数字角标内容
	 * @property {String}  	badgePositon = [left|right]		角标位置,默认为 right
	 * @property {String} 	link = [false|navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈,默认为false
	 *  @value false	 	不开启
	 *  @value navigateTo 	同 uni.navigateTo()
	 * 	@value redirectTo 	同 uni.redirectTo()
	 * 	@value reLaunch   	同 uni.reLaunch()
	 * 	@value switchTab  	同 uni.switchTab()
	 * @property {String | PageURIString} 	to  			跳转目标页面
	 * @property {String} 	time							右侧时间显示
	 * @property {Boolean} 	avatarCircle = [true|false]		是否显示圆形头像,默认为false
	 * @property {String} 	avatar							头像地址,avatarCircle 不填时生效
	 * @property {Array} 	avatarList 						头像组,格式为 [{url:''}]
	 * @event {Function} 	click 							点击 uniListChat 触发事件
	 */
	export default {
		name: 'UniListChat',
		emits:['click'],
		props: {
			title: {
				type: String,
				default: ''
			},
			note: {
				type: String,
				default: ''
			},
			clickable: {
				type: Boolean,
				default: false
			},
			link: {
				type: [Boolean, String],
				default: false
			},
			to: {
				type: String,
				default: ''
			},
			badgeText: {
				type: [String, Number],
				default: ''
			},
			badgePositon: {
				type: String,
				default: 'right'
			},
			time: {
				type: String,
				default: ''
			},
			avatarCircle: {
				type: Boolean,
				default: false
			},
			avatar: {
				type: String,
				default: ''
			},
			avatarList: {
				type: Array,
				default () {
					return [];
				}
			}
		},
		// inject: ['list'],
study夏羽's avatar
study夏羽 已提交
126 127 128 129
		computed: {
			isDraft(){
				return this.note.slice(0,14) == '[uni-im-draft]'
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
			isSingle() {
				if (this.badgeText === 'dot') {
					return 'uni-badge--dot';
				} else {
					const badgeText = this.badgeText.toString();
					if (badgeText.length > 1) {
						return 'uni-badge--complex';
					} else {
						return 'uni-badge--single';
					}
				}
			},
			computedAvatar() {
				if (this.avatarList.length > 4) {
					this.imageWidth = avatarWidth * 0.31;
					return 'avatarItem--3';
				} else if (this.avatarList.length > 1) {
					this.imageWidth = avatarWidth * 0.47;
					return 'avatarItem--2';
				} else {
					this.imageWidth = avatarWidth;
					return 'avatarItem--1';
				}
			}
study夏羽's avatar
study夏羽 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		},
		watch: {
			avatar:{
				handler(avatar) {
					if(avatar.includes('://')){
						uniCloud.getTempFileURL({
							fileList: [avatar]
						}).then(res=>{
							// console.log(res);
							// 兼容uniCloud私有化部署
							let fileList = res.fileList || res.result.fileList
							this.avatarUrl = fileList[0].tempFileURL
						})
					}else{
						this.avatarUrl = avatar
					}
				},
				immediate: true
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
173 174 175 176 177 178
		},
		data() {
			return {
				isFirstChild: false,
				border: true,
				// avatarList: 3,
study夏羽's avatar
study夏羽 已提交
179 180
				imageWidth: 50,
				avatarUrl:''
DCloud_JSON's avatar
DCloud_JSON 已提交
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
			};
		},
		mounted() {
			this.list = this.getForm()
			if (this.list) {
				if (!this.list.firstChildAppend) {
					this.list.firstChildAppend = true;
					this.isFirstChild = true;
				}
				this.border = this.list.border;
			}
		},
		methods: {
			/**
			 * 获取父元素实例
			 */
			getForm(name = 'uniList') {
				let parent = this.$parent;
				let parentName = parent.$options.name;
				while (parentName !== name) {
					parent = parent.$parent;
					if (!parent) return false
					parentName = parent.$options.name;
				}
				return parent;
			},
			onClick() {
				if (this.to !== '') {
					this.openPage();
					return;
				}

				if (this.clickable || this.link) {
					this.$emit('click', {
						data: {}
					});
				}
			},
			openPage() {
				if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
					this.pageApi(this.link);
				} else {
					this.pageApi('navigateTo');
				}
			},
			pageApi(api) {
				uni[api]({
					url: this.to,
					success: res => {
						this.$emit('click', {
							data: res
						});
					},
					fail: err => {
						this.$emit('click', {
							data: err
						});
						console.error(err.errMsg);
					}
				});
			}
		}
	};
</script>

246 247 248 249
<style lang="scss" >
	$uni-font-size-lg:16px;
	$uni-spacing-row-sm: 5px;
	$uni-spacing-row-base: 10px;
250
	$uni-spacing-row-lg: 15px;
DCloud_JSON's avatar
DCloud_JSON 已提交
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	$background-color: #fff;
	$divide-line-color: #e5e5e5;
	$avatar-width: 45px;
	$avatar-border-radius: 5px;
	$avatar-border-color: #eee;
	$avatar-border-width: 1px;
	$title-size: 16px;
	$title-color: #3b4144;
	$title-weight: normal;
	$note-size: 12px;
	$note-color: #999;
	$note-weight: normal;
	$right-text-size: 12px;
	$right-text-color: #999;
	$right-text-weight: normal;
	$badge-left: 0px;
	$badge-top: 0px;
	$dot-width: 10px;
	$dot-height: 10px;
	$badge-size: 18px;
	$badge-font: 12px;
	$badge-color: #fff;
	$badge-background-color: #ff5a5f;
	$badge-space: 6px;
	$hover: #f5f5f5;

	.uni-list-chat {
		font-size: $uni-font-size-lg;
		position: relative;
		flex-direction: column;
		justify-content: space-between;
		background-color: $background-color;
	}

	// .uni-list-chat--disabled {
	// 	opacity: 0.3;
	// }

	.uni-list-chat--hover {
		background-color: $hover;
	}

	.uni-list--border {
		position: relative;
		margin-left: $uni-spacing-row-lg;
		/* #ifdef APP-PLUS */
		border-top-color: $divide-line-color;
		border-top-style: solid;
		border-top-width: 0.5px;
		/* #endif */
	}

	/* #ifndef APP-NVUE */
	.uni-list--border:after {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		background-color: $divide-line-color;
	}

	.uni-list-item--first:after {
		height: 0px;
	}

	/* #endif */

	.uni-list-chat--first {
		border-top-width: 0px;
	}

	.uni-ellipsis {
		/* #ifndef APP-NVUE */
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		/* #endif */
		/* #ifdef APP-NVUE */
		lines: 1;
		/* #endif */
	}

	.uni-ellipsis-2 {
		/* #ifndef APP-NVUE */
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		/* #endif */

		/* #ifdef APP-NVUE */
		lines: 2;
		/* #endif */
	}

	.uni-list-chat__container {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex: 1;
		padding: $uni-spacing-row-base $uni-spacing-row-lg;
		position: relative;
		overflow: hidden;
	}

	.uni-list-chat__header-warp {
		position: relative;
	}

	.uni-list-chat__header {
		/* #ifndef APP-NVUE */
		display: flex;
		align-content: center;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		flex-wrap: wrap-reverse;
		/* #ifdef APP-NVUE */
		width: 50px;
		height: 50px;
		/* #endif */
		/* #ifndef APP-NVUE */
		width: $avatar-width;
		height: $avatar-width;
		/* #endif */

		border-radius: $avatar-border-radius;
		border-color: $avatar-border-color;
		border-width: $avatar-border-width;
		border-style: solid;
		overflow: hidden;
	}

	.uni-list-chat__header-box {
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		display: flex;
		width: $avatar-width;
		height: $avatar-width;
		/* #endif */
		/* #ifdef APP-NVUE */
		width: 50px;
		height: 50px;
		/* #endif */
		overflow: hidden;
		border-radius: 2px;
	}

	.uni-list-chat__header-image {
		margin: 1px;
		/* #ifdef APP-NVUE */
		width: 50px;
		height: 50px;
		/* #endif */
		/* #ifndef APP-NVUE */
		width: $avatar-width;
		height: $avatar-width;
		/* #endif */
	}

	/* #ifndef APP-NVUE */
	.uni-list-chat__header-image {
		display: block;
		width: 100%;
		height: 100%;
	}

	.avatarItem--1 {
		width: 100%;
		height: 100%;
	}

	.avatarItem--2 {
		width: 47%;
		height: 47%;
	}

	.avatarItem--3 {
		width: 32%;
		height: 32%;
	}

	/* #endif */
	.header--circle {
		border-radius: 50%;
	}

	.uni-list-chat__content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex: 1;
		overflow: hidden;
		padding: 2px 0;
	}

	.uni-list-chat__content-main {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		padding-left: $uni-spacing-row-base;
		flex: 1;
		overflow: hidden;
	}

	.uni-list-chat__content-title {
		font-size: $title-size;
		color: $title-color;
		font-weight: $title-weight;
		overflow: hidden;
	}

study夏羽's avatar
study夏羽 已提交
474
	.draft ,.uni-list-chat__content-note {
DCloud_JSON's avatar
DCloud_JSON 已提交
475 476 477 478 479
		margin-top: 3px;
		color: $note-color;
		font-size: $note-size;
		font-weight: $title-weight;
		overflow: hidden;
study夏羽's avatar
study夏羽 已提交
480 481 482 483 484 485 486
	}
	.draft{
		color: #eb3a41;
		/* #ifndef APP-NVUE */
		flex-shrink: 0;
		/* #endif */
		padding-right: 3px;
DCloud_JSON's avatar
DCloud_JSON 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	}

	.uni-list-chat__content-extra {
		/* #ifndef APP-NVUE */
		flex-shrink: 0;
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
		margin-left: 5px;
	}

	.uni-list-chat__content-extra-text {
		color: $right-text-color;
		font-size: $right-text-size;
		font-weight: $right-text-weight;
		overflow: hidden;
	}

	.uni-list-chat__badge-pos {
		position: absolute;
		/* #ifdef APP-NVUE */
		left: 55px;
		top: 3px;
		/* #endif */
		/* #ifndef APP-NVUE */
		left: calc(#{$avatar-width} + 10px - #{$badge-space} + #{$badge-left});
		top: calc(#{$uni-spacing-row-base}/ 2 + 1px + #{$badge-top});
		/* #endif */
	}

	.uni-list-chat__badge {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		border-radius: 100px;
		background-color: $badge-background-color;
	}

	.uni-list-chat__badge-text {
		color: $badge-color;
		font-size: $badge-font;
	}

	.uni-badge--single {
		/* #ifndef APP-NVUE */
		// left: calc(#{$avatar-width} + 7px + #{$badge-left});
		/* #endif */
		width: $badge-size;
		height: $badge-size;
	}

	.uni-badge--complex {
		/* #ifdef APP-NVUE */
		left: 50px;
		/* #endif */
		/* #ifndef APP-NVUE */
		width: auto;
		/* #endif */
		height: $badge-size;
		padding: 0 $badge-space;
	}

	.uni-badge--dot {
		/* #ifdef APP-NVUE */
		left: 60px;
		top: 6px;
		/* #endif */
		/* #ifndef APP-NVUE */
		left: calc(#{$avatar-width} + 15px - #{$dot-width}/ 2 + 1px + #{$badge-left});
		/* #endif */
		width: $dot-width;
		height: $dot-height;
		padding: 0;
	}

	.uni-list-chat--right {
		/* #ifdef APP-NVUE */
		left: 0;
		/* #endif */
	}
571
</style>