createGroup.vue 8.3 KB
Newer Older
1 2 3 4 5 6
<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>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
7
    <uni-list class="content-box" :border="false">
8
      <uni-im-info-card @click="checkboxChange(item._id)" v-for="(item,index) in friendList" :key="index" :avatar-circle="true" :title="item.nickname" :border="false" :clickable="true"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
9
        :avatarUrl="item.avatar_file?.url || '/uni_modules/uni-im/static/avatarUrl.png'">
10
        <template v-slot:left>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
11 12 13 14 15
          <view class="checkbox" :class="{
            'disabled': groupMemberUid.includes(item._id),
            'checked': checkFriendIds.includes(item._id)}
          ">
            <uni-icons type="checkmarkempty" color="#FFF" v-if="groupMemberUid.includes(item._id) || checkFriendIds.includes(item._id)"></uni-icons>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
          </view>
        </template>
      </uni-im-info-card>
      <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 {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
37
    emits: ['done'],
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    data() {
      return {
        loading: true,
        hasMore: false,
        keyword: '',
        checkFriendIds: [],
        friendData: [],
        groupMemberUid: [], //选人进群时,已经在群里的人的id
        group_id: false,
        isFocus: false
      }
    },
    computed: {
      friendList() {
        return this.friendData.filter(item => {
          //转小写筛选
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
54
          return (this.keyword == '' || item.nickname.toLowerCase().includes(this.keyword.toLowerCase()))
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
        })
      },
      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
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
89 90 91 92 93
          if(!uniIm.isWidescreen){
            uni.setNavigationBarTitle({
              title: '邀请新成员'
            })
          }
94 95 96 97 98 99 100
          //查本群,成员,
          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)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
101
          // console.log('this.groupMemberUid', this.groupMemberUid);
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
        }
        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) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
139 140 141
        if (this.groupMemberUid.includes(user_id)){
          return console.log('已经在群里了');
        }
142 143 144 145 146 147 148 149 150 151 152 153 154 155
        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) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
156 157 158 159 160 161 162
          if (uniIm.isWidescreen) {
            this.$emit('done')
          } else {
            uni.navigateBack({
              delta: 1
            })
          }
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
        } 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>

193 194 195 196 197 198
<style lang="scss">
@import "@/uni_modules/uni-im/common/baseStyle.scss";
.create-group-box {
  position: relative;
  background-color: #F9F9F9;
  flex: 1;
199 200 201 202
  .header-box {
    flex-direction: column;
    background-color: #F9F9F9;
  }
203
  
204 205 206 207
  .content-box {
   width: 100%;
   overflow: auto;
  }
208
  
209 210 211 212 213 214
  .label-box {
    flex-direction: row;
    align-items: center;
    background-color: #FFF;
    padding: 5px 0;
  }
215
  
216 217 218 219 220 221 222
  .checkbox {
    margin: 12px 10px 0 0;
    border: 1px solid #DDD;
    width: 20px;
    height: 20px;
    justify-content: center;
    align-items: center;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
223 224 225 226 227 228 229 230 231 232 233
    border-radius: 100px;
    &.disabled {
      background-color: #1189e9;
      opacity: 0.5;
      border: none;
      cursor: not-allowed;
    }
    &.checked {
      background-color: #1189e9;
      border: none;
    }
234
  }
235
  
236 237
  .foot-box {
    position: fixed;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
238
    bottom: 15px;
239 240 241 242 243 244 245
    // 注意:此页面可能显示在pc端所以不能用750rpx,而用100%
    width: 100%;
    height: 60px;
    justify-content: center;
    align-items: center;
  }
  
246 247 248 249
  .foot-box .btn {
    width: 300px;
  }
  
250 251
  /* #ifdef H5 */
  @media screen and (min-device-width:960px){
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
252 253
    width: 100%;
    margin: 10px auto;
254
    &.join-grpop {
255 256 257
      width: 800px;
    }
    .content-box {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
258
      height: calc(100vh - 175px);
259 260 261 262 263 264 265 266 267
    }
    .content-box ::v-deep .info-card {
      cursor: pointer;
    }
    .foot-box {
      position: absolute;
    }
  }
  /* #endif */
268
}
269
</style>