extension.js 2.6 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
import uniIm from '@/uni_modules/uni-im/sdk/index.js'
DCloud_JSON's avatar
DCloud_JSON 已提交
2
import UniImMsgReader from '@/uni_modules/uni-im-msg-reader/components/uni-im-msg-reader/uni-im-msg-reader.vue'
DCloud_JSON's avatar
DCloud_JSON 已提交
3
function install() {
DCloud_JSON's avatar
DCloud_JSON 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  
  // 配置在什么情况下启用已读功能
  uniIm.extensions.installExt('msg-extra', (msg, currentUser) => {
    // 仅限“我”发送的消息
    if (msg.from_uid !== uniIm.currentUser._id) return
    // 仅限内部用户
    if (!uniIm.currentUser.role.includes('staff')) return 
    // 排除特殊消息
    if (msg.type == 'system' || msg.is_revoke) return
    // 仅限私聊会话
    let conversation = uniIm.conversation.find(msg.conversation_id)
    if (!conversation?.friend_uid) return
    return {
      component: UniImMsgReader,
      props: {
        msg
      }
    }
  })
  
  
DCloud_JSON's avatar
DCloud_JSON 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  // 注册 read_msg 消息类型
  uniIm.extensions.installExt('msg-type-register', () => {
    return {
      type: 'read_msg',

      isReadable(msg) {
        return false
      },

      beforeLocalAdd(msgData, conversation) {
        let {
          body: {
            msgId
          },
          from_uid,
          create_time,
        } = msgData
DCloud_JSON's avatar
DCloud_JSON 已提交
42 43
        // debugger
        const msg = conversation.msg.find(msgId)
DCloud_JSON's avatar
DCloud_JSON 已提交
44 45 46 47 48 49 50 51 52 53 54 55
        if (msg) {
          let reader = {
            user_id: from_uid,
            create_time
          }
          msg.reader_list ? msg.reader_list.push(reader) : msg.reader_list = [reader]
        }
      },
    }
  })

  // 监听每条消息的显示状态,报送 read_msg 消息
DCloud_JSON's avatar
DCloud_JSON 已提交
56 57
  uniIm.extensions.installExt('msg-appear', msg => {
    const currentUid = uniIm.currentUser._id
DCloud_JSON's avatar
DCloud_JSON 已提交
58 59 60 61
    // 特殊消息不用处理
    if (msg.type === 'system' || msg.is_revoke) return

    // 如果是我发送的消息则不处理
DCloud_JSON's avatar
DCloud_JSON 已提交
62
    if (msg.from_uid == currentUid) return
DCloud_JSON's avatar
DCloud_JSON 已提交
63 64

    // 如果我已经在已读列表里则不再处理
DCloud_JSON's avatar
DCloud_JSON 已提交
65
    if (msg.reader_list?.some(u => u.user_id == currentUid)) return
DCloud_JSON's avatar
DCloud_JSON 已提交
66 67

    // 如果是群聊消息且没有 @我,则不用处理
DCloud_JSON's avatar
DCloud_JSON 已提交
68 69
    let conversation = uniIm.conversation.find(msg.conversation_id)
    if (conversation.group_id && !msg.call_uid?.includes(currentUid)) return
DCloud_JSON's avatar
DCloud_JSON 已提交
70 71 72 73

    // 把自己记入已读列表
    msg.reader_list = msg.reader_list || []
    msg.reader_list.push({
DCloud_JSON's avatar
DCloud_JSON 已提交
74
      user_id: currentUid,
DCloud_JSON's avatar
DCloud_JSON 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
      create_time: Date.now()
    })

    // 向云端提交 read_msg 消息
    const uniImCo = uniCloud.importObject('uni-im-co', {
      customUI: true
    })
    uniImCo.sendMsg({
      type: 'read_msg',
      body: {
        msgId: msg._id
      }
    }).catch(e => {
      // 提交失败,把自己从已读列表里去掉
DCloud_JSON's avatar
DCloud_JSON 已提交
89
      msg.reader_list = msg.reader_list.filter(u => u.user_id !== currentUid)
DCloud_JSON's avatar
DCloud_JSON 已提交
90 91 92 93 94 95 96
    })
  })
}

export default {
  install
}