createGroup.nvue 8.1 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 88 89 90 91 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 148 149 150 151 152 153 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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
<template>
  <view class="create-group-box" :class="{'join-grpop':group_id}">
    <view class="header-box">
      <uni-search-bar v-model="keyword" placeholder="搜索" bgColor="#fff" :radius="100" 
        @cancel="doClear();isFocus = true" @clear="doClear" :isFocus="isFocus" @focus="isFocus = true" @blur="isFocus = false" ></uni-search-bar>
    </view>
    <uni-list class="content-box">
      <uni-list-chat @click="checkboxChange(item._id)" v-for="(item,index) in friendList" :key="index" :avatar-circle="true" :title="item.nickname" :border="false" :clickable="true"
        :avatar="item.avatar_file && item.avatar_file.url ? item.avatar_file.url : '/uni_modules/uni-im/static/avatarUrl.png'">
        <template v-slot:left>
          <view class="checkbox">
            <uni-icons type="checkmarkempty" color="#007aff" v-if="checkFriendIds.includes(item._id)"></uni-icons>
          </view>
        </template>
      </uni-list-chat>
      <uni-im-load-state :status="loading?'loading':(hasMore?'hasMore':'noMore')"
        :contentText='{"contentnomore": friendList.length?"没有更多好友":"没有可以选择的好友"}'></uni-im-load-state>
      <uni-list-item style="height: 60px;" :border="false">
        <!-- 占位,用于此元素上方显示 操作按钮 -->
      </uni-list-item>
    </uni-list>
    <view class="foot-box">
      <!-- 创建新群可以不选择好友直接创建,邀请好友进群必须选择好友 -->
      <button :disabled="group_id? !checkFriendIds.length : false " class="btn" type="primary"
        @click="createGroup">{{btnText}}{{checkFriendNum}}</button>
    </view>
  </view>
</template>

<script>
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  const db = uniCloud.database();
  export default {
    data() {
      return {
        loading: true,
        hasMore: false,
        keyword: '',
        checkFriendIds: [],
        friendData: [],
        groupMemberUid: [], //选人进群时,已经在群里的人的id
        group_id: false,
        isFocus: false
      }
    },
    computed: {
      friendList() {
        return this.friendData.filter(item => {
          //转小写筛选
          return !this.groupMemberUid.includes(item._id)
           && 
          (this.keyword == '' || item.nickname.toLowerCase().includes(this.keyword.toLowerCase()))
        })
      },
      checkFriendNum() {
        return this.checkFriendIds.length > 0 ? '(' + this.checkFriendIds.length + ')' : ''
      },
      btnText() {
        return this.group_id ? '立即邀请' : '立即创建'
      },
      checkFriendsWidth() {
        return this.checkFriendIds.length > 6 ? '100%' : this.checkFriendIds.length * 80 + 'px'
      },
      // checkFriendsSearchWidth() {
      // 	return this.checkFriendIds.length > 6 ? '360' : 720 - (this.checkFriendIds.length * 60)
      // },
      translateXWidth() {
        return this.checkFriendIds.length > 6 ? this.checkFriendIds.length * 65 : '60'
      },
      checkFriendImg() {
        return this.friendList.reduce((sum, current) => {
          if (this.checkFriendIds.includes(current._id)) {
            sum.push(current)
          }
          return sum
        }, []).map(item => item.avatar_file)
      }
    },
    async onLoad(options) {
      this.setParam(options)
    },
    methods: {
      async setParam(options = {}) {
        console.log("group_id", options);
        if (options.group_id) {
          this.group_id = options.group_id
          uni.setNavigationBarTitle({
            title: '邀请新成员'
          })
          //查本群,成员,
          let res = await db.collection('uni-im-group-member').where({
              group_id: options.group_id
            })
            .get()
          console.log("res:查本群,成员 ", res);
          this.groupMemberUid = res.result.data.map(item => item.user_id)
          console.log('this.groupMemberUid', this.groupMemberUid);
        }
        this.getFriendsData()
      },
      async getFriendsData() {
        let whereString = {}
        if (this.keyword) {
          whereString = `
          	"_id"		== 	"${this.keyword}" ||
          	"username"	== 	"${this.keyword}" || 
          	"nickname"	== 	"${this.keyword}" || 
          	"email"		== 	"${this.keyword}" || 
          	"mobile"	== 	"${this.keyword}" 
          `
        }
        let res = await db.collection(
          db.collection('uni-im-friend').where('"user_id" == $cloudEnv_uid').field('friend_uid,mark,class_name')
          .getTemp(),
          db.collection('uni-id-users').where(whereString).field('_id,nickname,avatar_file').getTemp()
        ).get()
        // console.log(res);
        let data = res.result.data
        data.forEach((item, index) => {
          if (item.friend_uid[0]) {
            data[index] = item.friend_uid[0]
          } else {
            delete data[index]
          }
        })
        this.friendData = data
        this.loading = false
        this.hasMore = this.friendList.length != 0
      },
      doClear() {
        this.keyword = ''
        this.getFriendsData()
      },
      checkboxChange(user_id) {
        console.log("checkboxChange-value",user_id);
        this.checkFriendIds = this.checkFriendIds.includes(user_id) ? this.checkFriendIds.filter(item => item != user_id) : this.checkFriendIds.concat(user_id)
        console.log("checkboxChange",this.checkFriendIds);
      },
      async createGroup() {
        // console.log('创建', this.checkFriendIds.length)
        const uniImCo = uniCloud.importObject("uni-im-co")
        let res = await uniImCo.chooseUserIntoGroup({
          user_ids: this.checkFriendIds,
          group_id: this.group_id
        })
        this.checkFriendIds = []
        // console.log('createGroup',res);
        if (this.group_id) {
          uni.navigateBack({
            delta: 1
          })
        } else {
          // #ifdef H5
          if (uniIm.isWidescreen) {
            uni.$emit('uni-im-toChat', 'group_' + res.data.group_id)
          } else {
            uni.redirectTo({
              url: '/uni_modules/uni-im/pages/chat/chat?conversation_id=' + 'group_' + res.data.group_id,
              animationDuration: 300,
              fail: (e) => {
                console.log(e);
              }
            })
          }
          // #endif

          // #ifndef H5
          uni.redirectTo({
            url: '/uni_modules/uni-im/pages/chat/chat?conversation_id=' + 'group_' + res.data.group_id,
            animationDuration: 300,
            complete: (e) => {
              console.log(e);
            }
          })
          // #endif
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .create-group-box {
    position: relative;
    background-color: #F9F9F9;
    flex: 1;
  }

  .header-box {
    flex-direction: column;
    background-color: #F9F9F9;
  }

  .content-box {
   /* #ifndef APP-NVUE */
   width: 100%;
   overflow: auto;
   /* #endif */
   
   /* #ifdef APP-NVUE */
  flex: 1;
   /* #endif */
  }

  .label-box {
    flex-direction: row;
    align-items: center;
    background-color: #FFF;
    padding: 5px 0;
  }

  .checkbox {
    margin: 12px 10px 0 0;
    border: 1px solid #DDD;
    width: 20px;
    height: 20px;
    justify-content: center;
    align-items: center;
    border-radius: 3px;
  }

  .foot-box {
    position: fixed;
    bottom: 0;
    /* #ifdef APP-NVUE */
    width: 750rpx;
    /* #endif */
    // 注意:此页面可能显示在pc端所以不能用750rpx,而用100%
    /* #ifndef APP-NVUE */
    width: 100%;
    /* #endif */
    height: 60px;
    justify-content: center;
    align-items: center;
  }
  
  /* #ifdef H5 */
  @media screen and (min-device-width:960px){
    .create-group-box {
      width: 100%;
      margin: 10px auto;
    }
    .join-grpop {
      width: 800px;
    }
    .content-box {
      height: calc(95vh - 200px);
    }
    .content-box ::v-deep .uni-list--border {
      display: none;
    }
    .content-box ::v-deep .uni-list-chat {
      width: 100%;
    }
    .content-box ::v-deep .uni-list-chat__container {
      padding-left: 10px;
    }
    .foot-box {
      position: absolute;
    }
  }
  /* #endif */

  .foot-box .btn {
    width: 300px;
  }
</style>