userList.vue 2.9 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7
<template>
	<view>
		<unicloud-db ref="udb" @load="handleLoad" v-slot:default="{ data, loading, pagination, error, options }"
			collection="uni-id-users" field="_id,nickname,avatar_file" :where="udbWhere">
			<view v-if="error" class="error">
				<text>{{ error.message }}</text>
			</view>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
8
			<uni-list v-else :border="false">
9
				<uni-im-info-card v-for="(item, index) in data" :key="item._id" link
DCloud_JSON's avatar
DCloud_JSON 已提交
10
					:title="item.nickname"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
11
					:avatar="item.avatar_file?.url || '/uni_modules/uni-im/static/avatarUrl.png'"
12
					@click="toChat(item._id)"></uni-im-info-card>
DCloud_JSON's avatar
DCloud_JSON 已提交
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
			</uni-list>
			<uni-im-load-state :status="loading ? 'loading' : loadMoreStatus"></uni-im-load-state>
		</unicloud-db>
	</view>
</template>
<script>
import uniIm from '@/uni_modules/uni-im/sdk/index.js';
	export default {
		onLoad() {
		},
		computed: {
			//是否为pc宽屏(width>960px)
			isWidescreen(){
				return uniIm.isWidescreen
			},
		},
		data() {
			return {
				loadMoreStatus: 'more',
				udbWhere: '', //'_id != $cloudEnv_uid',
			};
		},
		onPullDownRefresh() {
			this.$refs.udb.loadData({
					clear: true
				},
				() => {
					uni.stopPullDownRefresh();
				}
			);
		},
		onReachBottom() {
			this.$refs.udb.loadMore();
		},
		onNavigationBarButtonTap(e) {
			console.log(e);
			if (e.index) {
				let data = uni.getStorageInfoSync();
				console.log('data.keys', JSON.stringify(data.keys));
				data.keys.forEach(item => {
					if (item.includes('uni-im-msg:') || item.includes('uni-im-conversation')) {
						// console.log(item);
						uni.removeStorageSync(item);
						console.log(uni.getStorageSync(item));
					}
				});
				uni.showToast({
					title: 'clear storage ok',
					icon: 'none'
				});
			} else {
				uni.navigateTo({
					url: '/uni_modules/uni-id-pages/pages/login/login-withpwd',
					complete: e => {
						console.log(e);
					}
				});
			}
		},
		methods: {
			handleLoad(data, ended) {
				this.loadMoreStatus = ended ? 'noMore' :
				'more'; // oading 的状态,可选值:more(loading前)、loading(loading中)、noMore(没有更多了)
			},
			async toChat(user_id) {
				// 当前用户给对方发个消息
				if (this.isWidescreen) {
					//若为宽屏,则触发右侧详情页的自定义事件,通知右侧窗体刷新详情
81
          location.href = '/#/uni_modules/uni-im/pages/index/index?user_id=' + user_id
DCloud_JSON's avatar
DCloud_JSON 已提交
82 83 84
				} else {
					// 若为窄屏,则打开新窗体,在新窗体打开详情页面
					uni.navigateTo({
85
						url: '/uni_modules/uni-im/pages/chat/chat?user_id=' + user_id
DCloud_JSON's avatar
DCloud_JSON 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
					});
				}
			},
			toAdd() {
				uni.navigateTo({
					url: '../uni-id-users/add',
					events: {
						// 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
						refreshData: () => {
							this.$refs.udb.loadData({
								clear: true
							});
						}
					}
				});
			}
		}
	};
</script>
105 106
<style lang="scss">
  @import "@/uni_modules/uni-im/common/baseStyle.scss";
DCloud_JSON's avatar
DCloud_JSON 已提交
107
</style>