members.vue 16.7 KB
Newer Older
1 2 3 4
<template>
  <view class="group-members-box">
    <uni-search-bar v-model="keyword" class="search-bar" radius="5" placeholder="输入昵称搜索" clearButton="auto"
      cancelButton="none"></uni-search-bar>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
5
    <view v-if="!leave_group" class="members-list-container" >
6 7 8 9 10 11 12 13 14 15 16 17 18
      <view class="invite-box item" v-if="isAdmin">
        <view class="invite-icon">
          <uni-icons @click="invite" color="#989898" size="20px" type="plusempty"></uni-icons>
        </view>
        <text class="invite-text">邀请</text>
      </view>
      <template v-for="(member,index) in memberList" :key="index">
        <view class="item" :title="member.users.nickname"
          :class="{'pointer': canPrivateChat,'focus':member.focus}" @click="toChat(member.users._id)"
          @longpress.prevent="openConversationMenu($event,index)"
          @contextmenu.prevent="openConversationMenu($event,index)"
          @mousemove="hoverUserId = member.users._id"
          >
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
19
          <image class="avatar" :src="(member.users.avatar_file && member.users.avatar_file.url) ? member.users.avatar_file.url:'/uni_modules/uni-im/static/avatarUrl.png'"
20 21 22 23 24 25 26
            mode="widthFix"></image>
          <text class="nickname">{{member.users.nickname||'匿名用户'}}</text>
          <text v-if="member.role.includes('admin')" class="group-admin"></text>
          <text v-if="!mute_all_members && member.mute_type" class="mute-type-1">已被禁言</text>
        </view>
      </template>
    </view>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
27 28 29 30 31 32 33 34 35 36 37
    <view v-if="showLoadMoreBtn" class="set-index-box">
      <text v-if="laterRenderIndex > 0" class="item" @click="laterRenderIndex = 0" space="emsp">收起</text>  
      <view class="look-more-box">
        <text v-if="member.loading" class="item">加载中...</text>
        <text v-else-if="!isAllShow" class="item" @click="lookMore">查看更多</text>
      </view>
    </view>
    <template v-else>
      <text v-if="member.loading" class="tip">加载中...</text>
      <text v-else-if="!member.hasMore" class="tip">没有更多了</text>
    </template>
38
    <uni-im-contextmenu ref="uni-im-contextmenu"></uni-im-contextmenu>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
39 40 41 42 43
    <!-- #ifdef H5 -->
    <uni-popup v-if="isWidescreen" ref="invitePagePopup" type="center">
      <invitePage class="invite-page" ref="invitePage" @done="$refs.invitePagePopup.close()"></invitePage>
    </uni-popup>
    <!-- #endif -->
44 45 46 47 48 49
  </view>
</template>

<script>
  const db = uniCloud.database()
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
50 51 52
  // #ifdef H5
  import invitePage from '@/uni_modules/uni-im/pages/contacts/createGroup/createGroup.vue'
  // #endif
53
  export default {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
54 55 56 57 58
    components: {
      // #ifdef H5
      invitePage
      // #endif
    },
59 60 61
    data() {
      return {
        conversation: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
62
          group: {
63
            user_id: "",
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
64 65 66 67
            mute_all_members: false,
            member: {
              dataList: []
            }
68 69 70 71 72 73 74 75 76 77 78 79 80
          },
          mute: false
        },
        leave_group: false,
        editorType: '',
        editorDefaultValue: '',
        groupType: '',
        isAdmin: false,
        keyword: '',
        mute_all_members: false,
        // 鼠标在哪个用户id上
        hoverUserId: '',
        // 延迟渲染,避免页面卡顿
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
81 82
        laterRenderIndex: 0,
        showLoadMoreBtn: true
83 84 85 86
      };
    },
    computed: {
      ...uniIm.mapState(['isWidescreen']),
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
87 88 89
      member(){
        return this.conversation.group.member
      },
90
      memberList() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
91
        let memberList = this.member.dataList
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
          // 根据关键词搜索
          .filter(member => {
            // 忽略大小写
            return member.users.nickname.toLowerCase().includes(this.keyword.toLowerCase())
          })
          // 是管理员排序靠前
          .sort((a, b) => {
            if (a.role.includes('admin') && !b.role.includes('admin')) {
              return -1
            } else if (!a.role.includes('admin') && b.role.includes('admin')) {
              return 1
            } else {
              return 0
            }
          })
          .filter((item, index) => {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
108
            return index < this.canRenderCount
109 110 111
          })
        return memberList
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      // 允许被渲染的个数
      canRenderCount() {
        // 懒渲染的值*50,如果是管理员,减1(因为有一个“+邀请”用户加入群聊的占位)
        let n = this.laterRenderIndex * 50 - (this.isAdmin ? 1 : 0)
        // 如果是第一次渲染,则默认渲染10个,如果是管理员,则默认渲染9个(因为有一个“+邀请”用户加入群聊的占位)
        if(this.laterRenderIndex === 0){
          n = this.isAdmin ? 9 : 10
        }
        return n
      },
      // 剩余未渲染的个数
      notRenderCount() {
        return this.member.dataList.length - this.canRenderCount
      },
      // 是否全部显示(全部加载完毕,并全部被渲染)
      isAllShow() {
        return this.notRenderCount <= 0 && !this.member.hasMore
      },
      currentUid() {
        return uniIm.currentUser._id
      },
133
      isGroupCreator() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
134
        return this.conversation.group.user_id == this.currentUid
135 136 137 138
      },
      canPrivateChat(){
        // 当前登录的账号是管理员,或者是群管理员,或者要私信管理员
        return this.uniIDHasRole('staff') || 
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
139 140
              this.member.find(this.currentUid)?.role.includes('admin') ||
              this.hoverUserId && this.member.find(this.hoverUserId).role.includes('admin') 
141 142 143
      }
    },
    onReachBottom() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
144
      this.showMore()
145 146 147 148 149 150
    },
    watch: {
      // (后续)通过监听实现实时切换管理员实时刷新权限
      // console.log('isAdmin',isAdmin);
      conversation: {
        handler(conversation, oldValue) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
151 152
          // 当前用户是群的创建者或者管理员(在群成员中找到当前用户的角色包含admin)
          this.isAdmin = this.isGroupCreator || this.member.find(this.currentUid)?.role.includes('admin')
153
          this.leave_group = conversation.leave
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
154
          this.mute_all_members = conversation.group.mute_all_members
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        },
        deep: true
      },
    },
    props: {
      conversation_id: {
        default () {
          return false
        }
      }
    },
    async onLoad(e) {
      if (!e.conversation_id) {
        throw new Error("会话id不能为空")
      }
      this.load(e.conversation_id)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
171 172 173
      // 以页面模式加载时不需要显示加载更多按钮
      this.showLoadMoreBtn = false
      this.laterRenderIndex = 1
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    },
    mounted() { //pc端以组件模式加载时逻辑
      if (this.conversation_id) {
        this.load(this.conversation_id)
      }
    },
    methods: {
      async load(conversation_id) {
        this.conversation = await uniIm.conversation.get(conversation_id)
      },
      toChat(user_id) {
        if (this.canPrivateChat) {
          uniIm.toChat({
            user_id,
            source:{
              group_id: this.conversation.group_id
            }
          })
        }
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
194 195 196 197 198 199 200 201 202 203
      showMore(){
        if(this.notRenderCount < 5){
          this.member.loadMore().then(() => {
            this.laterRenderIndex++
          })
        }else{
          this.laterRenderIndex++
        }
      },
      lookMore() {
204
        if(this.isWidescreen){
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
205 206 207 208 209
          if(this.isAllShow){
            this.laterRenderIndex = 0
          }else{
            this.showMore()
          }
210 211 212 213 214 215 216 217
        }else{
          uni.navigateTo({
            url: '/uni_modules/uni-im/pages/group/members?conversation_id=' + this.conversation.id,
            animationType:"slide-in-right"
          })
        }
      },
      invite() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
218 219 220 221 222 223 224 225 226 227 228
        const group_id = this.conversation.group._id
        if (this.isWidescreen) {
          this.$refs.invitePagePopup.open()
          setTimeout(() => {
            this.$refs.invitePage.setParam({group_id})
          },0)
        } else {
          uni.navigateTo({
            url: '/uni_modules/uni-im/pages/contacts/createGroup/createGroup?group_id=' + group_id
          })
        }
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
      },
      async expel(item) {
        uni.showModal({
          title: '确定要将该用户移出本群吗?',
          content: '不能撤销,请谨慎操作',
          cancelText: '取消',
          confirmText: '确认',
          complete: async (e) => {
            // console.log(e);
            if (e.confirm) {
              uni.showLoading({
                mask: true
              });
              try {
                let res = await db.collection('uni-im-group-member').where({
                    user_id: item.users._id,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
245
                    group_id: this.conversation.group._id
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 279 280 281 282 283
                  })
                  .remove()
                if (res.result.deleted) {
                  uni.showToast({
                    title: '成功移除',
                    icon: 'none',
                    complete: () => {}
                  });
                  // console.log('exitGroup', res);
                }
              } catch (error) {
                uni.showToast({
                  title: error.message,
                  icon: 'error',
                  complete: () => {}
                });
              }
              uni.hideLoading()
            }
          }
        });
      },
      async expelAndToBlack(item) {
      
        uni.showModal({
          title: '确定要将该用户移出本群并拉黑吗?',
          content: '拉黑后此用户将不能再次加入本群,不能撤销,请谨慎操作',
          cancelText: '取消',
          confirmText: '确认',
          complete: async (e) => {
            // console.log(e);
            if (e.confirm) {
              uni.showLoading({
                mask: true
              });
              try {
                let res = await db.collection('uni-im-group-member').where({
                    user_id: item.users._id,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
284
                    group_id: this.conversation.group._id
285 286 287 288 289 290
                  })
                  .remove()
                console.log('expel', res);
                const uniImCo = uniCloud.importObject("uni-im-co")
                res = await uniImCo.addToGroupMenberBlackList({
                  user_id: item.users._id,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
291
                  group_id: this.conversation.group._id
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
                })
                console.log('expelAndToBlack', res);
              } catch (error) {
                uni.showToast({
                  title: error.message,
                  icon: 'error',
                  complete: () => {}
                });
              }
              uni.hideLoading()
            }
          }
        });
      },
      async changeMemberMute(item) {
        let nickname = item.users.nickname || '匿名用户'
        uni.showModal({
          title: '确定要' + (item.mute_type ? `为"${nickname}"解除禁言吗?` : `禁言"${nickname}"吗?`),
          cancelText: '取消',
          confirmText: '确认',
          complete: async (e) => {
            // console.log(e);
            if (e.confirm) {
              uni.showLoading({
                mask: true
              });
              try {
                let res = await db.collection('uni-im-group-member').where({
                    _id: item._id,
                    mute_type: item.mute_type // 防止此时云端已经变化
                  })
                  .update({
                    mute_type: item.mute_type ? 0 : 1
                  })
                // console.log('mute_type', res);
                if (res.result.updated) {
                  item.mute_type = item.mute_type ? 0 : 1
      
                  uni.showToast({
                    title: '设置成功',
                    icon: 'none',
                    complete: () => {}
                  });
                }
              } catch (error) {
                // console.log('error',merror)
                uni.showToast({
                  title: error.message,
                  icon: 'error',
                  complete: () => {}
                });
              }
              uni.hideLoading()
            }
          }
        });
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
349
      openConversationMenu(event, index) {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
        if (!this.isAdmin) {
          return
        }
        const member = this.memberList[index]
        const menuList = []
        menuList.unshift({
          "title": "移除",
          "action": () => {
            // console.log('移除');
            this.expel(member)
          }
        })
            
        menuList.unshift({
          "title": "移除并拉黑",
          "action": () => {
            console.log('移除并拉黑');
            this.expelAndToBlack(member)
          }
        })
            
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
371
        if (!this.conversation.group.mute_all_members) {
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
          menuList.unshift({
            "title": member.mute_type ? "解除禁言" : '设为禁言',
            "action": () => {
              // console.log('禁言');
              this.changeMemberMute(member)
            }
          })
        }
            
        const isAdmin = member.role.includes('admin')
        menuList.push({
          "title": isAdmin ? "取消管理员" : "设置管理员",
          "action": () => {
            let role = member.role;
            if (isAdmin) {
              // console.log('取消管理员');
              role = member.role.filter(item => item !== 'admin')
            } else {
              role.push('admin')
              // console.log('设置管理员');
            }
            uni.showLoading({
              mask: true
            });
            db.collection('uni-im-group-member').doc(member._id).update({
                "role": role
              }).then(res => {
                // console.log('res', res);
                member.role = role
              })
              .catch(err => {
                console.error(err)
                uni.showToast({
                  title: err.message,
                  icon: 'none'
                });
              })
              .finally(() => {
                uni.hideLoading()
              })
          }
        })
      
        if (menuList.length > 0) {
          member.focus = true
          const myContextmenu = this.$refs['uni-im-contextmenu']
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
418 419 420 421 422 423 424 425
          let position = {
            "top": event.touches[0].screenY || event.touches[0].clientY,
            "left": event.touches[0].screenX || event.touches[0].clientX
          }
          if(event.type === 'contextmenu'){
            position = {
              "top": event.clientY,
              "left": event.clientX,
426
            }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
427
          }
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
      
          // #ifdef H5
          position.top = position.top + 120
          // #endif
      
          myContextmenu.show(position, menuList)
          myContextmenu.onClose(() => {
            member.focus = false
          })
        }
      },
    }
  }
</script>

443 444 445 446 447
<style lang="scss">
@import "@/uni_modules/uni-im/common/baseStyle.scss";
.group-members-box {
  width: 750rpx;
  background-color: #f5f5f5;
448 449 450 451
  .members-list-container {
    width: 750rpx;
    flex-direction: row;
    flex-wrap: wrap;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    .item {
      position: relative;
      width: 150rpx;
      height: 140rpx;
      margin: 5px 0;
      align-items: center;
      justify-content: space-around;
    }
    .invite-icon {
      border: 1px dashed #ccc;
      border-radius: 10px;
      width: 90rpx;
      height: 90rpx;
      justify-content: center;
      cursor: pointer;
    }
468 469 470 471 472 473 474
  }
  
  /* #ifdef H5 */
  .windows .item {
    width: 146rpx;
  }
  /* #endif */
475
  
476 477 478
  .item.focus {
    border: 1px dashed #ccc;
  }
479
  
480 481 482 483 484 485 486 487 488 489
  .group-admin {
    position: absolute;
    top: 5px;
    right: 5px;
    padding: 1px 3px;
    border-radius: 6px;
    background-color: #e64141;
    color: #fff;
    font-size: 12px;
  }
490
  
491 492 493 494 495 496 497 498
  .mute-type-1 {
    position: absolute;
    padding: 1px 10px;
    background-color: #0b60ff;
    color: #fff;
    font-size: 10px;
    bottom: 28px;
  }
499
  
500 501 502 503 504
  /* #ifdef H5 */
  .pointer {
    cursor: pointer;
  }
  /* #endif */
505
  
506 507 508 509 510 511
  .avatar {
    width: 100rpx;
    height: 100rpx;
    border-radius: 10px;
    box-shadow: 0 0 1px #aaa;
  }
512
  
513 514 515 516 517 518 519 520 521 522 523
  .invite-text,.nickname {
    width: 140rpx;
    text-align: center;
    font-size: 12px;
    color: #666;
    padding: 0 16rpx;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
524
  .tip {
525 526 527
    text-align: center;
    font-size: 14px;
    color: #666;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
528 529
    height: 60px;
    line-height: 60px;
530
  }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  
  .set-index-box {
    flex-direction: row;
    justify-content: center;
    .look-more-box {
      flex-direction: row;
      justify-content: center;
    }
    .item {
      margin:0 10px;
      text-align: center;
      font-size: 14px;
      color: #666;
      height: 40px;
      line-height: 40px;
    }
    /* #ifdef H5 */
    .item {
      cursor: pointer;
    }
    .item:hover {
      color: #333;
    }
    /* #endif */
555
  }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
556 557 558 559 560 561 562
  .invite-page {
    border-radius: 10px;
    background-color: #fff;
    overflow: hidden;
    .header-box {
      padding-top: 5px;
    }
563
  }
564 565
}

566 567
  
</style>