chat.vue 32.5 KB
Newer Older
1
<template>
2
  <view id="uni-im-chat" class="uni-im-chat" :class="{'pc':isWidescreen}">
3 4
    <!-- #ifdef H5 -->
    <!-- web-pc端会话标题 -->
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
5 6 7 8 9 10
    <view v-if="isWidescreen" id="web-pc-chat-title">
      <text :selectable="true">{{navTitle}}</text>
      <template v-for="(item,index) in extChatTitle" :key="index">
        <component :is="item.component" v-bind="item.props"></component>
      </template>
    </view>
11 12 13 14 15 16 17 18 19 20 21 22
    <!-- H5端 左上角显示未读消息数 ,app端使用setTitleNViewButtonBadge设置,小程序端暂未支持-->
    <view @click="tapUnreadCount" class="unread_count" v-if="unread_count != 0">
      {{ unread_count > 99 ? '99+' : unread_count }}
    </view>
    <!-- #endif -->

    <!-- 消息列表 -->
    <uni-im-msg-list :conversationId="conversation.id" ref="msg-list" 
      @showControl="showControl" @longpressMsgAvatar="onLongpressMsgAvatar"
      @retriesSendMsg="retriesSendMsg" class="msg-list"
      @clickItem="onclickMsgList" @putChatInputContent="putChatInputContent" :chooseMore="chooseMoreMsg"
      :checkedMsgList.sync="checkedMsgList"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
23
      @showMenberList="showMenberList"
24 25 26 27 28
    ></uni-im-msg-list>
    
    <!-- 聊天数据输入框 键盘弹出后要抬高底部内边距 全面屏的安全距离 -->
    <text v-if="conversation.leave" class="disable-chat-foot">- 你不是此群成员 -</text>
    <text v-else-if="conversation.isMuteAllMembers" class="disable-chat-foot">-  全群禁言禁止输入 -</text>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
29
    <view v-else class="chat-foot" @touchmove.prevent>
30 31 32 33 34 35 36 37 38
      <uni-im-chat-input
        ref="chat-input"
        v-model="chatInputContent"
        :keyboardMaxHeight="keyboardMaxHeight"
        :keyboardHeight="keyboardHeight"
        @confirm="chatInputConfirm"
        @input="onInput"
        @sendSoundMsg="sendSoundMsg"
        @sendCodeMsg="beforeSendMsg"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
39
        @showMenberList="showMenberList"
40 41 42
      >
        <template #about-msg>
          <view class="answer-msg" v-if="answerMsg !== false">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
43
            <text class="answer-msg-text">{{answerMsgNickname}}{{answerMsgNote}}</text>
44 45 46 47
            <uni-icons class="close-icon" @click="answerMsg = false" type="clear" color="#ccc" size="18px"></uni-icons>
          </view>
        </template>
      </uni-im-chat-input>
48
      
49
      <toolbar v-model="chooseMoreMsg" @shareMsg="shareMsg(checkedMsgList,$event)"></toolbar>
50 51
    </view>

DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
52
    <msg-popup-control ref="msg-popup-control" @answer="setAnswerMsg" @intoTopic="intoTopic" @share="shareMsg" @chooseMore="chooseMoreMsg = true;checkedMsgList = $event"></msg-popup-control>
53

DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
54 55
    
    <uni-im-member-list ref="member-list" :conversationId="conversation.id"></uni-im-member-list>
56

DCloud_JSON's avatar
DCloud_JSON 已提交
57
    <!-- #ifdef H5 -->
58
    <uni-im-share-msg v-if="isWidescreen" id="uni-im-share-msg" ref="share-msg"></uni-im-share-msg>
DCloud_JSON's avatar
DCloud_JSON 已提交
59
    <!-- #endif -->
60 61
    
    <view style="position: fixed;top: 200px;left: 0;background-color: #FFFFFF;z-index: 9999;">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
62
      <!-- keyboardMaxHeight:{{keyboardMaxHeight}}
63 64 65 66 67 68 69 70 71 72 73 74 75
			conversation.leave:{{conversation.leave}}
      chatInputContent:{{chatInputContent}}
			keyboardHeight:{{keyboardHeight}}
			systemInfo.osName:{{systemInfo.osName}}
      chooseMoreMsg:{{chooseMoreMsg}}
      checkedMsgList:{{checkedMsgList}} -->
		</view>
  </view>
</template>

<script>
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  import msgPopupControl from '@/uni_modules/uni-im/components/uni-im-msg/popup-control.vue';
76
  import toolbar from './toolbar.vue';
77
  import {markRaw} from "vue";
DCloud_JSON's avatar
DCloud_JSON 已提交
78 79 80
  // #ifdef H5
  import uniImShareMsg from '@/uni_modules/uni-im/pages/share-msg/share-msg.vue';
  // #endif
81
  
82 83 84 85 86 87 88 89 90 91

/**
 * chat 组件,渲染一个完整的会话,包括头部、消息列表、输入区。
 * 
 * @module
 * 
 * @see 用于渲染消息列表的组件 {@link module:uni-im-msg-list}
 */
  export default {
    components: {
DCloud_JSON's avatar
DCloud_JSON 已提交
92
      // #ifdef H5
93
      uniImShareMsg,
DCloud_JSON's avatar
DCloud_JSON 已提交
94 95
      // #endif
      msgPopupControl,
96
      toolbar
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
    },
    data() {
      return {
        // 当前会话对象
        conversation: {
          id: false,
          leave:false,
          title: ''
        },
        navTitle:"",//导航栏标题
        keyboardHeight: 0,
        keyboardMaxHeight: 0,
        answerMsg: false,
        chooseMoreMsg: false,
        checkedMsgList: [],
        // 聊天输入框内容
        chatInputContent: '',
      };
    },
    props: {
      // #ifdef VUE3
      conversation_id: {
        default: ''
      }
      // #endif
    },
    computed: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
124
      ...uniIm.mapState(['currentConversationId', 'isWidescreen', 'systemInfo']),
125 126 127 128 129 130 131 132 133 134 135 136 137 138
      unread_count() {
        // 所有会话的未读消息数
        const unreadCount = uniIm.conversation.unreadCount()

        // #ifdef APP-PLUS
        // 给标题栏返回按钮添加数字角标,表示有几条其他会话的未读消息数
        plus.webview.currentWebview().setTitleNViewButtonBadge({
          index: 0,
          text: unreadCount
        })
        // #endif

        return unreadCount
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
139 140 141
      //当前用户自己的uid
      current_uid() {
        return uniIm.currentUser._id;
142
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
143 144 145 146 147 148 149 150 151 152 153 154
      extChatTitle(){
        if(this.conversation){
          return uniIm.extensions
            .invokeExts("chat-title",this.conversation)
            .filter((result) => result && result.component)
            .map((result) => {
              return {
                component: markRaw(result.component),
                props: result.props||{}
              };
            });
        }else{
155 156 157
          return []
        }
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
158 159 160 161 162 163
      answerMsgNote() {
        return uniIm.utils.getMsgNote(this.answerMsg)
      },
      answerMsgNickname() {
        // 兼容旧版本,消息中未包含用户最新的昵称,需要通过用户id获取
        return this.answerMsg.nickname || uniIm.users.getNickname(this.answerMsg.from_uid)
164 165 166 167 168 169 170 171 172 173
      }
    },
    created() {
      // console.log('chat created',this.systemInfo)
      // 监听推送消息
      this.onImMsg = (res) => {
        if(uniIm.isDisabled){
          return console.log('uniIm isDisabled')
        }
        //获取透传内容
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
174
        const { data } = res.data.payload;
175 176
        //判断消息类型是否为im,且为当前页面对应会话消息
        if (
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
177
          data.conversation_id == this.currentConversationId 
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
          && data.from_uid != this.current_uid 
          && uniIm.utils.isReadableMsg(data)
        ) {
          // 已经打开相应会话时,收到消息,则设置此会话为已读。注意无需判断,本地会话未读数是否为0,因为云端可能不为0
          this.conversation.clearUnreadCount();
          console.log('聊天页面-收到消息: ', JSON.stringify(res));
          // 需要重新设置滚动条的高,以至于用户可以立即看到(即:滚动到最后一条消息)
          // console.log(66666);

          // 注:为兼容web-PC端这里必须使用setTimeout 0
          setTimeout(() => {
            this.$refs['msg-list']?.notifyNewMsg()
          }, 0);
        }
      }
      uniIm.onMsg(this.onImMsg);
      
      // 优化 提前拿到键盘高度,防止第一次在会话点击输入框时,输入框抬起慢。(缺省值300,是为了解决模拟器调试没有键盘高度而设置)
      this.keyboardMaxHeight = uniIm.keyboardMaxHeight || 300
      this.onKeyboardHeightChange = ({
        height
      }) => {
        this.keyboardHeight = height
        // console.log('height',height)
        if (height > this.keyboardMaxHeight) {
          this.keyboardMaxHeight = height
        }
        this.$nextTick(() => {
          this.showLast();
        });
      }
      // #ifdef H5
      const oldWindowHeight = window.innerHeight;
      window.onresize = ()=>{
        this.onKeyboardHeightChange({"height": oldWindowHeight - window.innerHeight})
      }
      // #endif
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
215 216 217 218
      // #ifndef H5
      // 监听键盘高度变化显示最后一条消息
      uni.onKeyboardHeightChange(this.onKeyboardHeightChange);
      // #endif
219 220 221
    },
    mounted() {
      // #ifdef H5
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
      if(this.isWidescreen){
        // 以下为实现拖拽或粘贴图片至聊天页面,直接发送的逻辑
        const chatBodyDom = document.getElementById("uni-im-chat")
        // 阻止默认事件
        chatBodyDom.addEventListener(
          'dragover',
          function(event) {
            event.preventDefault();
          },
          false
        );
        // 拖拽结束时触发
        chatBodyDom.addEventListener(
          'drop',
          e => {
            //取消默认浏览器拖拽效果
            e.preventDefault();
            //获取文件对象
            let fileList = e.dataTransfer.files;
            if (fileList.length == 0) {
              return false;
243
            }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
244 245 246 247 248 249 250 251
            const [file] = fileList
            let fileType = file.type.split('/')[0] || 'file'
            if (fileType === 'image') {
              let blobUrl = window.URL.createObjectURL(file);
              if(this.chatInputContent === ''){
                this.$refs["chat-input"].setContent({html:`<img src="${blobUrl}" />`})
              }else{
                this.$refs["chat-input"].addHtmlToCursor(`<img src="${blobUrl}" />`)
252
              }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
253
              return false; // 拖拽图片内容进入输入框先不发送
254
            }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
            // 其他文件类型,直接发送
            let {
              name,
              size
            } = file
            // console.log(78979798,fileList);
            const blobUrl = window.URL.createObjectURL(file);
            this.uploadFileAndSendMsg({
                tempFiles:[{
                  size,
                  name,
                  path:blobUrl,
                  fileType
                }]
            })
          },
          false
        );
        const chatInput = document.querySelector('.pc .uni-im-editor');
        chatInput.addEventListener('keydown',async e => {
          this.$refs['member-list'].onChatInputKeydown(e)
276 277 278 279 280
        })
      }
      // #endif
    },
    onShow() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
281 282 283 284
      // TODO:解决 previewImage 误触发onShow的问题
      if(uniIm.ext._previewImageIsOpen){
        return
      }
285 286 287 288
      if (this.conversation.id) {
        // 用于打开会话窗口后,切换到后台,再切回时设置当前会话id。
        uniIm.currentConversationId = this.conversation.id
        // 用于从后台切到前台时,设置当前会话为已读
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
289 290
        this.clearUnreadCount();
        this.updateNavTitle()
291 292 293 294 295 296 297 298 299 300 301 302 303
      }
    },
    onUnload() {
      // console.log('onUnload');
      
      // 关闭监听消息推送事件
      uniIm.offMsg(this.onImMsg);
      
      // #ifndef H5
      uni.offKeyboardHeightChange(this.onKeyboardHeightChange)
      // #endif
      
      //页面销毁之前销毁 全局变量 正在聊天的用户的id
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
304
      uniIm.currentConversationId = ''
305 306 307 308 309 310
      // console.log('beforeDestroy');
      // 关闭sound播放
      uniIm.audioContext.stop()
    },
    beforeDestroy() {
      //页面销毁之前销毁 全局变量 正在聊天的用户的id
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
311
      uniIm.currentConversationId = ''
312 313 314 315 316
      // console.log('beforeDestroy');
      // 关闭sound播放
      uniIm.audioContext.stop()
    },
    onHide() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
317 318 319 320 321
      // TODO:解决 previewImage 误触发onHide的问题
      if(uniIm.ext._previewImageIsOpen){
        return
      }
      uniIm.currentConversationId = ''
322 323 324
      // 关闭sound播放
      uniIm.audioContext.stop()
    },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
325
    onLoad(param) {
326 327 328 329 330 331
      for (const key in param) {
        try{
          param[key] = JSON.parse(param[key])
        }catch(_){}
      }
      //调用load方法,因为pc宽屏时本页面是以组件形式展示。如$refs.chatView.loadconversation_id()执行
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
332
      this.load(param)
333 334
    },
    onBackPress(e) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
335 336 337 338
      // console.log('onBackPress',e);
      const memberListRef = this.$refs['member-list']
      if(memberListRef.isShow){
        memberListRef.hide()
339 340 341 342 343
        return true
      }
    },
    watch: {
      // 监听群昵称变化
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
344 345
      'conversation.title'(){
        this.updateNavTitle()
346 347
      },
      // 监听群成员数变化
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
348 349
      'conversation.group.member_count'(){
        this.updateNavTitle()
350 351 352 353 354 355
      }
    },
    methods: {
      async load(param) {
        this.answerMsg = false
        if(this.conversation.id){
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
356
          // 设置上一个会话的chatInputContent 实现多个会话之间的草稿功能
357 358 359 360 361 362 363 364 365 366 367 368
          this.conversation.chatInputContent = this.chatInputContent
        }

        this.conversation = await uniIm.conversation.get(param)
        // 初始化会话的chatInputContent
        this.chatInputContent = this.conversation.chatInputContent
        // this.conversation.call_list = []
        // console.log('this.conversation',this.conversation)
        

        //设置全局的app当前正在聊天的会话id(注:本页面可能是直达页)
        uniIm.currentConversationId = this.conversation.id
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
369
        setTimeout(() => {
370
          this.$refs['msg-list'].init()
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
371
        }, 0);
372 373 374 375 376 377 378 379 380 381

        // console.log('this.conversation',this.conversation);
        
        //debug用模拟一次性自动发送100条数据
        // for (var i = 0; i < 20; i++) {
        // 	this.chatInputContent = '这是第'+i+'条消息'
        // 	this.beforeSendMsg()
        // }*/
        
        // 清除当前会话未读数
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
382
        this.clearUnreadCount();
383 384 385 386 387 388 389
        
        // #ifdef H5
        if(this.isWidescreen){
          // 切换到新的会话后把输入焦点设置到输入框(考虑到可能有草稿文字,需延迟设置)
          setTimeout(() => {
            this.$refs["chat-input"]?.focus()
          }, 100)
DCloud_JSON's avatar
DCloud_JSON 已提交
390 391 392 393 394 395 396 397 398 399 400 401
        }else{
          const {is_temp,friend_uid} = this.conversation
          if(is_temp && friend_uid){
            // 为了避免在web端刷新页面之后,本地单聊临时会话丢失,将地址栏中的会话id参数替换为好友的uid,以此来确保在刷新页面之后,本地能够再次创建临时会话。
            const {route:path,options} = getCurrentPages().pop()
            delete options.conversation_id
            options.user_id = friend_uid
            let paramSrt
            for (const key in options) {
              paramSrt = `${key}=${options[key]}`
            }
            history.replaceState(null, '', `/#/${path}?${paramSrt}`)
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
        // #endif
      },
      onclickMsgList(){
        this.$refs["chat-input"]?.setShowMore(false)
        uni.hideKeyboard()
      },
      putChatInputContent(value){
        this.$refs["chat-input"]?.setContent(value)
      },
      uploadFileAndSendMsg({tempFiles}){
        // console.log(res, 'image');
        // console.log('this.uploadFileAndSendMsg res',res);
        tempFiles.forEach(async tempFile => {
          // console.log('tempFile~',tempFile);
          const {
            path:url,
            name,
            size
          } = tempFile;
          let {fileType} = tempFile
          if (!['image', 'video'].includes(fileType)) {
            fileType = 'file'
          }
          // console.log('fileType===>', fileType);
          // console.error('tempFile~~~~~~~~~', tempFile,size/1000/1024+'mb');
          const sizeMB = size/1000/1024
          if(fileType == 'image' && sizeMB > 2){
            return uni.showToast({
              title: '图片大小不能超过2mb',
              icon: 'none'
            });
          } else if(sizeMB > 100){
            return uni.showToast({
              title: '文件大小不能超过100mb',
              icon: 'none'
            });
          }
          
          const data = {};
          const fileInfo = {
            url,
            size,
            name
          };
          if(fileType == 'image'){
            const {width,height} = await uni.getImageInfo({src:url});
            fileInfo.width = width
            fileInfo.height = height
          }
          data[fileType] = fileInfo
          let msg = await this.beforeSendMsg(data,false)
          // console.log('~~~beforeSendMsg',msg);
456
          const uploadFileFn = async ()=>{
457 458
            const result = await uniCloud.uploadFile({
              filePath: tempFile.path,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
459
              cloudPath: Date.now() + this.current_uid + '.' + name.split('.').pop(),
460 461 462
            });
            // console.log('result.fileID',result.fileID);
            msg.body.url = result.fileID
463 464 465
          }
          try{
            await uploadFileFn()
466 467 468
            this.sendMsg(msg)
          }catch(e){
            console.error('uploadFile error:',e)
469 470 471
            // 重发之前增加先上传图片的逻辑
            msg.__beforeRetriesAction = uploadFileFn
            msg.state = -200
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 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
          }
        });
      },
      async chooseFileSendMsg(type,_config={}) {
        // console.log('type',type);
        //先创建发送消息的
        let objFn = {
          'image':()=>{
            uni.chooseImage({
            	// count:9,
            	// sourceType,
            	// extension,
            	success:res=> beforeUploadFileAndSendMsg(res,'image'),
            	"fail":alertFail
            });
          },
          'video':()=>{
            uni.chooseVideo({
              sourceType: ['camera', 'album'],
              success:res=> beforeUploadFileAndSendMsg(res,'video'),
              "fail":alertFail
            });
          },
          'all':()=>{
            let chooseFile = uni.chooseFile;
            // #ifdef MP-WEIXIN
            chooseFile = wx.chooseMedia;
            // #endif
            chooseFile({
            	type: 'all',
            	// count:10,
              sourceType:['album','camera'],
            	"success":this.uploadFileAndSendMsg,
            	"fail":alertFail
            })
          }
        };
        objFn[type]();
        
        const _this = this;
        function beforeUploadFileAndSendMsg(res,fileType){
          // console.log(111,res)
          // 视频只能选择单文件,为了参数统一,这里转成数组
          if(fileType == 'video'){
            // #ifndef H5
            res.tempFile = {
              size: res.size,
              width: res.width,
              height: res.height
            }
            // #endif
            res.tempFile.path = res.tempFilePath
            res.tempFiles = [res.tempFile]
          }
          res.tempFiles.forEach(item=>{
            //如果没有type,默认为:用户选择的类型
            if(!item.fileType){
              item.fileType = fileType
            }
            // 如果没有name,默认为:用户id+随机数+时间戳生成一个
            if(!item.name){
             item.name = _this.current_uid + Math.random().toString(36).substr(2) + Date.now()
            }
          })
          // console.log(222,res)
          _this.uploadFileAndSendMsg(res)
        }
        function alertFail(res){
          console.error('res',res);
          // uni.showModal({
          //   content: JSON.stringify(res),
          //   showCancel: false
          // });
        }
        
      },
      sendSoundMsg(sound){
        this.beforeSendMsg({sound})
      },
      onInput(e) {
        // console.log('onInput',e.data);
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
553 554 555 556 557 558 559 560 561 562
        // #ifndef MP
        if(this.conversation.group_id && e.data == "@"){
          // 非小程序的群聊才支持@功能
          uni.hideKeyboard()
          this.showMenberList({
            confirm:userId=> {
              this.setCallAboutUid(userId)
            },
            filter:member=>!this.getCallUid().includes(member.users._id)
          })
563
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
564 565 566 567 568 569
        // #endif
        // 成员列表内实现:在被显示时,监听输入框的输入,以实现搜索功能等
        this.$refs['member-list'].onChatInput(e)
      },
      intoTopic(msgId){
        this.$refs['msg-list'].intoTopic(msgId)
570 571
      },
      async setAnswerMsg(msgId) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
572
        this.answerMsg = this.conversation.msg.find(msgId)
573
        this.$refs["chat-input"]?.focus()
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
574 575 576 577
        const {from_uid} = this.answerMsg
        if (this.conversation.group && from_uid != this.current_uid) {
          this.setCallAboutUid(from_uid,false)
        }
578 579
      },
      async chatInputConfirm() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
580 581
        const $mr = this.$refs['member-list']
        if($mr.isShow && $mr.memberList.length){
582 583 584 585 586
          console.log('正在执行选中要@的人,不发送数据')
          return
        }
        if (typeof this.chatInputContent == 'object'){
          // 富文本(图文混排、@某人)消息
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
          const htmlStr = this.chatInputContent.html
          const HtmlArr = uniIm.utils.parseHtml(htmlStr)
          // 把用户手敲的http链接转为a标签包裹,再转为富文本数组
          HtmlArr.forEach((item,index)=>{
            // 如果文字中有http链接,需要把链接的地址提取出来
            if(item.type === "text"){
              if(item.text.includes('http')){
                const newStr = uniIm.utils.replaceUrlToLink(item.text)
                const newItems = uniIm.utils.parseHtml(newStr)
                // 删除原来的text,替换成新的
                HtmlArr.splice(index,1,...newItems)
              }
              //转义:&gt; 转换为 > $lt; 转换为 < &amp; 转换为 & &quot; 转换为 " &nbsp; 转换为空格 &ensp; 转换为空格 &emsp; 转换为空格
              item.text = item.text.replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&amp;/g,'&').replace(/&nbsp;/g,' ').replace(/&ensp;/g,' ').replace(/&emsp;/g,' ')
            }
          })
          // 如果最后一项是text
          const lastItem = HtmlArr[HtmlArr.length - 1]
          if(lastItem.type === 'text'){
            // 删除末尾的空格或者换行
            lastItem.text = lastItem.text.replace(/[\s\n]*$/,'')
            if(lastItem.text === ''){
              HtmlArr.pop()
            }  
          }
612 613
          // 先插到消息列表
          let msg = await this.beforeSendMsg({
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
614
            "rich-text":HtmlArr
615 616
          },false)
          
617 618 619 620 621
          const uploadFileFn = async ()=>{
            // 上传消息中的图片
            let promiseArr = []
            msg.body.forEach(async item=>{
              if(item.name === 'img'){
622
                promiseArr.push(new Promise((resolve,reject)=>{
623 624 625 626 627 628 629 630 631
                  uni.getImageInfo({
                    src:item.attrs.src,
                    success:res=>{
                      item.attrs.width = res.width
                      item.attrs.height = res.height
                      resolve()
                    },
                    fail:reject
                  });
632
                }))
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
                
                if(item.attrs.src.indexOf('blob:http') === 0){
                  promiseArr.push(new Promise((resolve,reject)=>{
                    uniCloud.uploadFile({
                      filePath: item.attrs.src,
                      cloudPath: Date.now() + this.current_uid + '.' + name.split('.').pop(),
                    }).then(res=>{
                      item.attrs.src = res.fileID
                      // console.log('上传成功',res);
                      resolve()
                    }).catch(e=>{
                      reject()
                    })
                  }))
                }
648
              }
649 650 651 652 653 654 655 656 657 658 659 660 661
            })
            await Promise.all(promiseArr)
          }
          try{
            await uploadFileFn()
            // 执行发送
            this.sendMsg(msg)
          }catch(e){
            // 重发之前增加先上传图片的逻辑
            msg.__beforeRetriesAction = uploadFileFn
            msg.state = -200
          }
          
662 663 664 665 666 667 668
        }else{
          // 把this.chatInputContent中的&nbsp;变成空格,再把头尾的空格去掉
          this.chatInputContent = this.chatInputContent.replace(/&nbsp;/g, ' ').trim()
          // 普通消息
          await this.beforeSendMsg()
        }
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
669 670
      showMenberList(){
        this.$refs['member-list'].show(arguments[0])
671 672 673 674 675 676 677 678 679
      },
      async beforeSendMsg(param = {},_continue = true) {
        console.log('beforeSendMsg',{param});
        let msg = {
          type: 'text',
          to_uid: this.conversation.friend_uid,
          conversation_id: this.conversation.id,
          group_id: this.conversation.group_id,
          client_create_time: Date.now(),
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
680
          from_uid: this.current_uid,
681
          state: 0,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
682 683 684
          body: this.chatInputContent,
          // 接收消息的appId,默认为当前应用的appId。如果你是2个不同appId的应用相互发,请修改此值为相对的appId
          appId: this.systemInfo.appId
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
        }
        // 根据传入的参数设置消息类型和内容
        for (let key in param) {
          if (param[key]) {
            msg.type = key
            msg.body = JSON.parse(JSON.stringify(param[key]))
          }
        }
        // 如果是文本类型需要做一些处理
        if (msg.type === 'text') {
          //清除空格
          msg.body = msg.body.trim();
          // 阻止发送空消息
          if (!msg.body.length) {
            this.resetChatInput()
            return uni.showToast({
              title: '不能发送空消息',
              icon: 'none'
            });
          }
        }

        //如果是回复某一条消息,需要带上相关id
        if (this.answerMsg !== false) {
          msg.about_msg_id = this.answerMsg._id
        }

        // 消息列表追加此消息。此时消息状态值为0,表示发送中
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
713
        msg = this.conversation.msg.add(msg)
714
        
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
715
        // 代码在,弹出的代码输入框,无需清空消息输入框内容
716 717 718 719 720 721 722 723 724
        if(msg.type !== 'code'){
          this.resetChatInput()
        }

        this.$nextTick(() => {
          this.showLast()
        })
        // console.error('sendMsg-sendMsg-sendMsg', msg);
        // 存到本地数据库
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
725
        // await this.conversation.msgManager.localMsg.add(msg)
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        // console.log('msg被localMsg.add引用 会新增一个unique_id',msg)
        if(_continue){
          this.sendMsg(msg);
        }else{
          return msg;
        }
      },
      resetChatInput() {
        this.chatInputContent = ''
        // 关闭引用的消息
        this.answerMsg = false
      },
      getCallUid(){
        return this.chatInputContent?.aboutUserIds || []
      },
      sendMsg(msg, callback) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
742
        
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
        if(this.conversation.source){
          msg.chat_source = this.conversation.source
        }
        
        // console.log('sendMsg-sendMsg-sendMsg', msg);
        const uniImCo = uniCloud.importObject('uni-im-co', {
          customUI: true
        });
        
        // 检查内容不是包含 个推 两个字,有则 改成 个 + 零宽字符 + 推
        let tmpBody = JSON.stringify(msg.body)
        if(tmpBody.includes('个推')){
          msg.body = JSON.parse(tmpBody.replace(/个推/g,'个\u200b推'))
        }
        
        uniImCo.sendMsg(msg)
          .then(async e => {
            // console.log('uniImCo.sendMsg',{e,msg});
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
761 762 763
            msg.state = e.errCode === 0 ? 100 : -100
            msg.create_time = e.data.create_time
            msg._id = e.data._id
764 765 766 767 768 769 770 771
          })
          .catch(async e => {
            uni.showModal({
              content: e.message,
              showCancel: false,
              confirmText: '关闭',
            });
            console.error('uniImCo.sendMsg error:', e.errCode, e.message);
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
772 773 774
            // todo:必须要有create_time的值,否则indexDB通过创建时间索引找不到数据。后续会优化这个问题
            msg.state = -200,
            msg.create_time = Date.now()
775 776 777 778 779 780 781
          })
          .finally(e => {
            if (callback) {
              callback(e);
            }
          });
      },
782
      async retriesSendMsg(msg) {
783 784 785
        uni.showLoading({
          mask: true
        });
786 787 788 789 790 791
         msg.state = 0
        // 检查文件是否上传成功
        if(msg.__beforeRetriesAction){
          await msg.__beforeRetriesAction()
        }
        delete msg.__beforeRetriesAction
792 793 794 795 796 797 798 799 800 801 802 803
        // console.log('retriesSendMsg', msg);
        msg.isRetries = true
        this.sendMsg(msg, e => {
          uni.hideLoading();
        });
      },
      showLast(duration = 300) {
        let msgListRef = this.$refs['msg-list']
        if (msgListRef) {
          msgListRef.showLast(duration)
        }
      },
804
      onLongpressMsgAvatar(user_id){
805 806
        // 当前输入框已经@了的用户id 要过滤掉
        let callUidList = this.getCallUid()
807
        if(callUidList.includes(user_id)){
808 809 810 811 812 813 814 815
          console.log('此用户id已经@过');
          uni.showToast({
            title: '此用户已经@过',
            icon: 'none'
          });
        }else{
          this.$refs['chat-input'].raiseEditor = true
          this.$nextTick(()=>{
816
            this.setCallAboutUid(user_id,false)
817 818 819
          })
        }
      },
820
      setCallAboutUid(user_id,needDeleteLeftART = true) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
821 822
        // 拿到@之后为搜索用户的关键字长度,以便插入@的用户后删除关键字 (移动端搜索的关键词并不在消息输入栏)
        const keywordLength = this.isWidescreen ? this.$refs['member-list'].keyword.length : 0
823
        this.$refs['chat-input'].addCallUser({
824
          user_id,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
825 826
          nickname: user_id == '__ALL' ? '所有人' : uniIm.users[user_id]?.nickname || '未知用户'
        },needDeleteLeftART,keywordLength)
827 828 829 830 831
      },
      async showControl({
        msgId,
        msgContentDomInfo
      }) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
832 833
        const msg = this.conversation.msg.find(msgId)
        let isSelf = msg.from_uid == this.current_uid
834 835 836
        this.$refs['msg-popup-control'].show({isSelf,msg,msgContentDomInfo})
      },
      shareMsg(msgList,merge = false) {
837 838 839 840 841 842 843 844 845 846 847
        console.error('msgList',msgList)
        if (this.isWidescreen) {
          this.$refs['share-msg'].open(msgList,merge)
        } else {
          uni.navigateTo({
            url: '/uni_modules/uni-im/pages/share-msg/share-msg',
            success: res => {
              res.eventChannel.emit('shareMsg', [msgList,merge])
            }
          })
        }
848 849 850 851
        this.chooseMoreMsg = false
      },
      tapUnreadCount() {
        //点击未读消息文字按钮事件
DCloud_JSON's avatar
DCloud_JSON 已提交
852
        uni.navigateBack();
853 854 855
      },
      updateNavTitle(){
        this.navTitle = this.conversation.title
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
856 857 858
        const group_id = this.conversation?.group_id
        if (group_id && group_id?.indexOf('__tmp') != 0) {
          this.navTitle += `(${this.conversation.group.member_count})`
859 860 861 862 863 864
        }
        if(this.navTitle && !this.isWidescreen){
          uni.setNavigationBarTitle({
          	title: this.navTitle
          });
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
865 866 867 868 869
      },
      clearUnreadCount(){
        if ( this.conversation.unread_count > 0) {
          this.conversation.clearUnreadCount();
        }
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
      }
    },
    onNavigationBarButtonTap(e) {
      if (e.index === 0) {
        if (this.conversation.group_id) {
          uni.navigateTo({
            url: "/uni_modules/uni-im/pages/group/info?conversation_id=" + this.conversation.id
          })
        } else {
          // console.log(this.conversation,6565);
          uni.navigateTo({
            url: `/uni_modules/uni-im/pages/chat/info?user_id=${this.conversation.friend_uid}&conversation_id=${this.conversation.id}`
          })
          // uni.showToast({
          // 	title: '仅群里可见,详细信息',
          // 	icon: 'none'
          // });
        }
      }
      // uni.navigateBack();
    }
  };
</script>

894 895
<style lang="scss">
@import "@/uni_modules/uni-im/common/baseStyle.scss";
DCloud_JSON's avatar
DCloud_JSON 已提交
896 897 898
page {
  height: 100%;
}
899 900
.uni-im-chat {
  position: relative;
DCloud_JSON's avatar
DCloud_JSON 已提交
901
  height: 100%;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
902
  width: 100%;
DCloud_JSON's avatar
DCloud_JSON 已提交
903
  flex: 1;
904
  background-color: #efefef;
905 906 907 908 909
  .msg-list {
    /* height: 1px; 覆盖掉 组件内的height:100%,使得flex-grow: 1作用在容器内被撑开*/
    height: 1px !important;
    flex-grow: 1;
  }
910
  
911
  .chat-foot,.disable-chat-foot{
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
912
    position: relative;
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
    flex-direction: column;
    border-top: 1rpx solid #BBBBBB;
    background-color: #F7F7F7;
  }
  
  .disable-chat-foot{
    padding: 20px;
    text-align: center;
    justify-content: center;
    color: #777777;
  }
  
  .answer-msg {
    padding: 2px 10px;
    background-color: #eee;
    border-radius: 3px;
    margin-bottom: 10px;
    flex-direction: row;
    align-items: center;
    ::v-deep .uni-icons {
      margin-left: 5px;
    }
    /* #ifdef H5 */
    .close-icon{
      cursor: pointer;
    }
    @media screen and (min-device-width:960px){
      margin: 5px;
      margin-bottom: -18px;
      top: 0;
    }
    /* #endif */
    .answer-msg-text {
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-size: 12px;
      color: #333;
    }
  }
954
  
955 956 957 958 959 960 961
  /* #ifdef H5 */
  .vue-codemirror {
    position: fixed;
    top: 100px;
    left: 50%;
    width: 500px;
  }
962
  
963
  /* #endif */
964
  
965 966 967 968 969
  /* #ifdef H5 */
  .chat-foot {
    border: none;
  }
  /* #endif */
970
  
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
  /* #ifdef H5 */
  .unread_count {
    position: absolute;
    top: -30px;
    left: 70rpx;
    z-index: 10;
    background-color: #dfe2e9;
    padding: 0 14rpx;
    height: 14px;
    line-height: 14px;
    border-radius: 9px;
    color: #0c1013;
    font-size: 12px;
    margin-top: 3px;
  }
  /* #endif */
987
}
988
</style>