info.vue 5.8 KB
Newer Older
1
<template>
2
<view class="chat-info">
3 4
  <uni-list :border="false" class="list">
    <uni-im-info-card
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
5
      :avatarUrl="friend_info.avatar_file?.url || '/uni_modules/uni-im/static/avatarUrl.png'"
6 7 8 9 10 11 12 13 14 15 16 17
      :title="friend_info.nickname"
      :note="friend_info.nickname != friend_info.email ? friend_info.email : ''"
    />
    <uni-list-item
      title="消息免打扰"
      :switch-checked="conversation.mute"
      :show-switch="true"
      @switchChange="changeConversationMute"
    />
    <!-- #ifdef H5 -->
    <!-- 发送名片消息(仅内部人员可用) -->
    <uni-list-item
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
18
      v-if="isWidescreen && uniIDHasRole('staff')"
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 81 82 83 84 85 86 87
      :link="true"
      title="发送他(她)的名片"
      @click="sendNameCard"
    />
    <!-- #endif -->
    <!-- 选择某个用户创建群聊(仅dcloud员工可用) -->
    <uni-list-item
      v-if="uniIDHasRole('staff') && friend_uid != currentUid"
      :link="true"
      title="选此用户创建群聊"
      @click="createGroup"
    />
    <template v-for="item in UserinfoMenu" :key="item.component.name">
      <component :is="item.component" :conversation="conversation" cementing="UserinfoMenu" />
    </template>
  </uni-list>
  <button
    v-if="isFriend"
    class="btn"
    plain
    type="warn"
    @click="deteleFriend"
  >
    删除好友
  </button>

  <!-- #ifdef H5 -->
  <uni-im-share-msg
    ref="share-msg"
    no-msg-list
    no-comment
  />
  <!-- #endif -->
</view>
</template>

<script>
const db = uniCloud.database();
import uniIm from '@/uni_modules/uni-im/sdk/index.js';
import { ref,markRaw } from "vue"
export default {
  data() {
    // 调用扩展点,扩展程序可以在消息输入框新增一个工具类的项
    let UserinfoMenu = uniIm.extensions
      .invokeExts("userinfo-menu-extra",)
      .filter((result) => result && result.component)
      .map((result) => {
        return {
          component: markRaw(result.component),
          props: result.props
        };
      });
      
    return {
      UserinfoMenu,
      conversation: {},
      friend_uid: '',
      friend_info: {
        username: '',
        nickname: '',
        avatar_file: {
          url: ''
        }
      }
    }
  },
  computed: {
    ...uniIm.mapState(['isWidescreen']),
    isFriend() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
88
      return this.friend_uid ? uniIm.friend.find(this.friend_uid) : false
89 90
    },
    currentUid() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
91
      return uniIm.currentUser._id
92 93 94 95 96 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    }
  },
  async onLoad(options) {
    this.load(options)
  },
  methods: {
    async load(options) {
      console.log('options',options);
      let conversation_id = options.conversation_id || options.id
      // 如果只传了user_id,需要先获取conversation_id
      if(!conversation_id){
        if(!options.user_id){
          console.error('参数错误')
          return uni.showToast({
            title: '参数错误',
            icon: 'none'
          });
        }
        conversation_id = await uniIm.utils.getConversationId(options.user_id)
        console.log('conversation_id',conversation_id);
      }
      
      let conversation = await uniIm.conversation.get(conversation_id)
      this.conversation = conversation
      this.friend_uid = conversation.friend_uid
      let field = '_id,nickname,avatar_file'
      if (this.uniIDHasRole('staff')) {
        field += ',email'
      }
      let res = await db.collection('uni-id-users')
        .doc(this.friend_uid)
        .field(field)
        .get()
        // console.log("res: ",res);
      this.friend_info = res.result.data[0]
    },

    changeConversationMute() {
      console.log('changeConversationMute',this.conversation)
      this.conversation.changeMute()
    },
    async deteleFriend() {
      uni.showModal({
        title: '确认要删除好友吗',
        content: '此操作不可撤销',
        showCancel: true,
        cancelText: '取消',
        confirmText: '确认',
        complete: async (e) => {
          if (e.confirm) {
            uni.showLoading({
              mask: true
            });
            try {
              await db.collection('uni-im-friend').where({
                friend_uid: this.friend_uid,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
148
                user_id: this.currentUid
149
              }).remove()
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
150 151 152 153
              if (!uniIm.isWidescreen) {
                // 收到push消息后store会自动,将此用户从列表中移除
                uni.navigateBack({ delta: 2 })
              }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            } catch (e) {
              uni.showModal({
                content: JSON.stringify(e.message),
                showCancel: false
              });
            }
            uni.hideLoading()
          }
        }
      });
    },
    async createGroup(){
      console.log('createGroup')
      const user_ids = [this.friend_uid]
      const uniImCo = uniCloud.importObject("uni-im-co")
      let res = await uniImCo.chooseUserIntoGroup({
        user_ids
      })
      uni.$emit('uni-im-toChat','group_'+res.data.group_id)
    },
    // #ifdef H5
    async sendNameCard() {
      let msg = {
        type: 'userinfo-card',
        body: {
          user_id: this.conversation.friend_uid,
          name: this.conversation.title,
        },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
182
        from_uid: this.currentUid,
183 184 185 186 187 188 189 190 191
        create_time: Date.now(),
      }
      this.$refs['share-msg'].open([msg], false)
    }
    // #endif
  }
}
</script>

192 193 194
<style lang="scss">
@import "@/uni_modules/uni-im/common/baseStyle.scss";
.chat-info {
195 196 197 198
  width: 750rpx;
  height: 100vh;
  align-items: center;
  background-color: #fff;
199 200 201 202 203
  
  .list {
    width: 750rpx;
    border-bottom: 1px solid #ececec;
  }
204

205 206 207 208 209 210
  /* #ifdef H5 */
  .list ::v-deep .info-card {
    .title, .note {
      cursor: text;
      user-select: text;
    }
211
  }
212 213 214 215
  .list ::v-deep .uni-list-item + .uni-list-item{
    cursor: pointer;
  }
  /* #endif */
216

217 218 219 220 221 222 223 224
  .btn {
    margin-top: 15px;
    width: 600rpx;
    /* height: 45px; */
    text-align: center;
    line-height: 45px;
    border-radius: 20rpx;
  }
225 226
}
</style>