info.vue 15.5 KB
Newer Older
1 2 3 4
<template>
  <view class="group-info-box" v-if="conversation_id">
    <uni-im-group-members :conversation_id="conversation_id"></uni-im-group-members>
    <uni-list>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
5 6
      <uni-list-item v-if="!leave_group && !isTmpGroup" title="消息免打扰" @switchChange="changeConversationMute"
        :switchChecked="conversation.mute" :showSwitch="true" switchColor="#22b832"></uni-list-item>
7 8 9 10
      <uni-list-item v-for="(val,key) in editorFields" :key="key" @click.native="openPopupInfo(key)" :title="val"
        :showArrow="isAdmin" :clickable="isAdmin">
        <template v-slot:footer>
          <text
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
11
            class="group-info-text">{{ (key == "notification" ? conversation.group[key]?.content : conversation.group[key]) || '未设置' }}</text>
12 13 14 15
        </template>
      </uni-list-item>
      <uni-list-item @click.native="setAvatar" title="群头像" :clickable="isAdmin">
        <template v-slot:footer>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
16
          <uni-im-img width="50px" height="50px" :src="logoUrl||'/uni_modules/uni-im/static/avatarUrl.png'" mode=""></uni-im-img>
17 18 19 20 21 22 23 24 25
        </template>
      </uni-list-item>
      <template v-if="isAdmin">
        <uni-list-item @click.native="setAddGroupType" title="加群方式" note="申请加入本群的验证规则" :clickable="isAdmin">
          <template v-slot:footer>
            <text class="join_option">{{join_option}}</text>
          </template>
        </uni-list-item>
        <uni-list-item title="全员禁言" @switchChange="setMuteAllMembers"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
26
          :switchChecked="conversation.group.mute_all_members" :showSwitch="true"></uni-list-item>
27 28
      </template>
      <!-- #ifdef H5 -->
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
29
      <uni-list-item v-if="!isTmpGroup" @click="share" :clickable="true" title="分享此群">
30 31 32 33 34 35 36 37 38 39
        <template v-slot:footer>
          <uni-icons size="25px" color="#666" type="redo"></uni-icons>
        </template>
      </uni-list-item>
      <!-- #endif -->
    </uni-list>
    <view v-if="leave_group">
      <text style="padding: 15px;text-align: center;color: #666;">- 你不是此群成员 -</text>
      <!-- <text @click="joinGroup" style="padding: 15px;text-align: center;color: #005eca;cursor: pointer;">申请加入</text> -->
    </view>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
40
    <text v-else-if="isTmpGroup" style="padding: 15px;text-align: center;color: #666;">- 临时群聊 -</text>
41 42 43
    <text v-else class="exitGroup" @click="exitGroup">{{isGroupCreator?'解散群聊':'退出群聊'}}</text>
    <uni-popup ref="popupInfo" type="dialog">
      <uni-popup-dialog mode="input" :title="editorFields[editorType]" :placeholder="'请输入'+editorFields[editorType]"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
44
        :duration="2000" :before-close="true" :value="editorDefaultValue" @close="closePopupInfo" :maxlength="500"
45 46
        @confirm="confirmPopupInfo"></uni-popup-dialog>
    </uni-popup>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
47 48 49 50 51
    <!-- #ifdef H5 -->
    <uni-popup v-if="isWidescreen" ref="popup-qr-code" type="center">
      <qrCodePage ref="popup-qr-code-page"></qrCodePage>
    </uni-popup>
    <!-- #endif -->
52 53 54 55 56 57 58
  </view>
</template>

<script>
  const db = uniCloud.database()
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  import members from '@/uni_modules/uni-im/pages/group/members';
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
59 60 61
  // #ifdef H5
  import qrCodePage from './qrcode.vue'
  // #endif
62 63
  export default {
    components: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
64 65 66 67
      'uni-im-group-members':members,
      // #ifdef H5
      qrCodePage
      // #endif
68 69 70 71
    },
    data() {
      return {
        conversation: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
72
          group: {
73
            user_id: "",
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
74 75 76 77 78
            mute_all_members: false,
            member: {
              dataList: [],
              count:() => 0
            },
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
          },
          mute: false
        },
        leave_group: false,
        editorFields: {
          "name": " 群聊名称",
          "introduction": "群简介",
          "notification": "群公告"
        },
        editorType: '',
        editorDefaultValue: '',
        groupType: '',
        isAdmin: false,
        keyword: '',
        mute_all_members: false,
        showAllMember: false,
        conversation_id:'',
        // 鼠标在哪个用户id上
        hoverUserId: ''
      };
    },
    computed: {
      ...uniIm.mapState(['isWidescreen']),
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
102 103 104
      currentUid() {
        return uniIm.currentUser._id
      },
105
      logoUrl() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
106
        return this.conversation.group.avatar_file ? this.conversation.group.avatar_file.url : false
107 108
      },
      join_option() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
109
        let val = this.conversation.group.join_option
110 111 112 113 114 115 116
        return {
          needPermission: "需要验证权限",
          freeAccess: "自由加入",
          disableApply: "禁止加入"
        } [val]
      },
      isGroupCreator() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
117
        return this.conversation.group.user_id == this.currentUid
118 119 120
      },
      canPrivateChat(){
        // 当前登录的账号是管理员,或者当前消息是群管理员发的
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
121 122 123 124
        return this.uniIDHasRole('staff') || this.hoverUserId && this.conversation.group.member.find(this.hoverUserId).role.includes('admin')
      },
      isTmpGroup(){
        return this.conversation?.id?.indexOf('__tmp') === 0
125 126 127
      }
    },
    watch: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
128 129
      "conversation.group.member_count"(){
        this.updateNavTitle()
130 131 132 133 134
      },
      // (后续)通过监听实现实时切换管理员实时刷新权限
      // console.log('isAdmin',isAdmin);
      conversation: {
        handler(conversation, oldValue) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
135 136
          // 当前用户是群的创建者或者管理员(在群成员中找到当前用户的角色包含admin)
          this.isAdmin = this.isGroupCreator || this.conversation.group.member.find(this.currentUid)?.role.includes('admin')
137
          this.leave_group = conversation.leave
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
138
          this.mute_all_members = conversation.group.mute_all_members
139 140 141 142 143 144 145 146 147 148 149
        },
        deep: true
      },
    },
    async onLoad(e) {
      if (!e.conversation_id) {
        throw new Error("会话id不能为空")
      }
      this.load(e.conversation_id)
    },
    onShow() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
150 151
      // console.log("this.conversation: ", this.conversation.group.member);
      this.updateNavTitle()
152 153
    },
    methods: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
154 155 156 157 158 159 160
      updateNavTitle(){
        const {group:{member_count},title} = this.conversation
        if (!uniIm.isWidescreen && typeof member_count == 'number') { // pc宽屏不需要
          const newTitle = title + "" + member_count + "人)-  群信息"
          uni.setNavigationBarTitle({"title":newTitle});
        }
      },
161 162 163 164 165 166 167 168 169 170
      navToMembers(conversation_id){
        uni.navigateTo({
          url:"/uni_modules/uni-im/pages/group/members?conversation_id=" + conversation_id
        })
      },
      async load(conversation_id) {
        this.conversation_id = conversation_id
        this.conversation = await uniIm.conversation.get(conversation_id)
      },
      async exitGroup() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
171
        const group_id = this.conversation.group._id
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
        if (this.isGroupCreator) {
          uni.showModal({
            title: '确认要解散群聊吗?',
            content: '不能撤销,请谨慎操作',
            cancelText: '取消',
            confirmText: '确认',
            complete: async (e) => {
              // console.log(e);
              if (e.confirm) {
                uni.showLoading({
                  mask: true
                });
                let res = await db.collection('uni-im-group')
                  .where({
                    _id: group_id
                  })
                  .remove()
                  .finally((res) => {
                    // uni.navigateBack({ // 收到离群的推送通知会自动关闭当前页面
                    // 	delta: 2
                    // })
                    uni.hideLoading()
                  })
                // console.log('exitGroup', res);
              }
            }
          });
        } else {
          uni.showModal({
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
201 202
            title: '确认退出当前群聊?',
            content: `退出后聊天记录将会被清空,此操作无法撤销,请谨慎操作。`,
203 204 205 206 207 208 209 210 211 212 213 214
            cancelText: '取消',
            confirmText: '确认',
            complete: async (e) => {
              // console.log(e);
              if (e.confirm) {
                // uni.navigateBack({ // 收到离群的推送通知会自动关闭当前页面
                // 	delta: 2
                // })
                uni.showLoading({
                  mask: true
                });
                let res = await db.collection('uni-im-group-member').where({
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
215
                    user_id: this.currentUid,
216 217 218 219 220 221 222 223 224
                    group_id
                  })
                  .remove()
                // console.log(res.result);
                if (res.result.deleted) {
                  uni.showToast({
                    title: '成功退出',
                    icon: 'none'
                  });
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
225
                  uniIm.conversation.remove(this.conversation_id)
226 227 228 229 230 231 232 233 234 235 236 237
                }
                uni.hideLoading()
                // console.log('exitGroup', res);
              }
            }
          });
        }
      },
      openPopupInfo(type) {
        // console.log(type);
        if (!this.isAdmin) return
        this.editorType = type
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
238
        this.editorDefaultValue = this.conversation.group[type]
239 240 241 242 243 244 245 246 247
        if (this.editorType == "notification") {
          this.editorDefaultValue = this.editorDefaultValue?.content || ""
        }
        this.$refs.popupInfo.open()
      },
      closePopupInfo() {
        this.$refs.popupInfo.close()
      },
      confirmPopupInfo(value) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
248
        value = value.trim()
249 250 251 252 253 254 255
        if (!value) {
          uni.showToast({
            title: '内容不能为空!',
            icon: 'none'
          });
          return
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
256 257 258 259 260 261 262
        if (value.length < 5) {
          uni.showToast({
            title: '内容长度不能低于5',
            icon: 'none'
          });
          return
        }
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
        // console.log('value', value);
        const updateData = {};
        if (this.editorType == 'notification') {
          updateData[this.editorType] = {
            "content": value
          }; // 创建时间服务端生成
        } else {
          updateData[this.editorType] = value;
        }
        this.updateGroupInfo(updateData)
        this.$refs.popupInfo.close()
      },
      setAddGroupType() {
        if (!this.isAdmin) return
        uni.showActionSheet({
          itemList: ['自由加入', '需要验证权限', '禁止加入'],
          success: (e) => {
            let join_option = ['freeAccess', 'needPermission', 'disableApply'][e.tapIndex]
            this.updateGroupInfo({
              join_option
            })
          },
          fail: (err) => {
            console.error("err: ", err);
          }
        })
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
290 291 292
      async updateGroupInfo(group) {
        // console.log('group---------',group);
        this.conversation.group = Object.assign(this.conversation.group, group)
293 294
        let res = await db.collection('uni-im-group')
          .doc(this.conversation.group_id)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
295
          .update(group)
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
        // console.log('change group info', res.result.updated,this.conversation);
      },
      async setAvatar() {
        if (!this.isAdmin) return
        const crop = {
          quality: 100,
          width: 600,
          height: 600,
          resize: true
        };
        uni.chooseImage({
          count: 1,
          crop,
          success: async (res) => {
            let tempFile = res.tempFiles[0],
              avatar_file = {
                // #ifdef H5
                extname: tempFile.name.split('.')[tempFile.name.split('.').length - 1],
                // #endif
                // #ifndef H5
                extname: tempFile.path.split('.')[tempFile.path.split('.').length - 1]
                // #endif
              },
              filePath = res.tempFilePaths[0]
            // #ifndef APP-PLUS
            //非app端用前端组件剪裁头像,app端用内置的原生裁剪
            let isPC = false
            // #ifdef H5
            isPC = !['ios', 'android'].includes(uni.getSystemInfoSync().platform)
            // #endif
            if (!isPC) {
              filePath = await new Promise((callback) => {
                uni.navigateTo({
                  url: '/uni_modules/uni-id-pages/pages/users/cropImage/cropImage?path=' +
                    filePath + `&options=${JSON.stringify(crop)}`,
                  animationType: "fade-in",
                  events: {
                    success: url => {
                      callback(url)
                    }
                  },
                  complete(e) {
                    // console.log(e);
                  }
                });
              })
            }
            // #endif
            // console.log(this.users);
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
345
            let cloudPath = this.currentUid + '' + Date.now()
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
            avatar_file.name = cloudPath
            uni.showLoading({
              title: "更新中",
              mask: true
            });
            let {
              fileID
            } = await uniCloud.uploadFile({
              filePath,
              cloudPath,
              fileType: "image"
            });
            // console.log(result)
            avatar_file.url = fileID
            // console.log({avatar_file});
            uni.hideLoading()
            this.updateGroupInfo({
              avatar_file
            })
          }
        })
      },
      // #ifdef H5
      share() {
        // 获取当前域名
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
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
        // const data = location.origin + "/#/?joinGroup=" + this.conversation.group._id
        // uni.setClipboardData({
        //   data,
        //   showToast: false,
        //   success: () => {
        //     uni.showToast({
        //       title: "已成功复制分享链接",
        //       duration: 2000,
        //       icon: 'none'
        //     });
        //   }
        // })
        if(uniIm.isWidescreen){
          this.$refs['popup-qr-code'].open()
          setTimeout(() => {
            this.$refs['popup-qr-code-page'].load({"conversation_id":this.conversation.id})
          },0)
        }else{
          uni.navigateTo({
          	url: '/uni_modules/uni-im/pages/group/qrCode?conversation_id=' + this.conversation.id,
          	complete: (e) => {
          		console.log(e);
          	}
          });
        }
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
      },
      // #endif
      joinGroup() {
        db.collection('uni-im-group-join').add({
          "group_id": this.conversation.group_id,
          "message": ''
        }).then((res) => {
          // console.log("res: ", res);
          uni.showToast({
            icon: 'none',
            title: '已申请'
          })
        })
      },
      setMuteAllMembers(e) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
411
        this.conversation.group.member.dataList.forEach(member => {
412
          member.mute_type += (e.value ? 1 : -1)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
413
        })
414 415 416 417 418 419 420 421 422 423 424
        this.updateGroupInfo({
          "mute_all_members": e.value
        })
      },
      changeConversationMute(e) {
        this.conversation.changeMute()
      }
    }
  }
</script>

425 426
<style lang="scss">
  @import "@/uni_modules/uni-im/common/baseStyle.scss";
427 428
  page,
  .group-info-box {
DCloud_JSON's avatar
DCloud_JSON 已提交
429 430
    width: 100%;
    height: 100%;
431
    background-color: #f5f5f5;
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
    .logo {
      width: 50px;
      height: 50px;
    }
    
    .exitGroup {
      margin: 10px 0;
      background-color: #FFFFFF;
      padding: 6px 0;
      color: #e64141;
      border-radius: 0;
      font-size: 16px;
      text-align: center;
      padding: 15px 0;
      /* #ifdef H5 */
      cursor: pointer;
      /* #endif */
    }
    
    .exitGroup::after {
      display: none;
    }
    
    .group-info-text {
      color: #666;
      font-size: 14px;
      max-width: 560rpx;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
459 460
      text-align: left;
      word-break: break-all;
461 462 463 464 465 466
    }
    
    .join_option {
      color: #666;
      font-size: 14px;
    }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
467 468 469 470 471 472 473
    
    .group-QRCode {
      border-radius: 20px;
      height: auto;
      padding: 15px;
      padding-top: 55px;
    }
474 475
  }
</style>