Blame view

antis-ncc-admin/src/layout/components/userList/UserList.vue 9.88 KB
4aba9b59   wwk   1
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
  <template>
    <div>
      <el-drawer title="内部聊天" :visible.sync="drawer" direction="rtl"
        class="contacts-drawer NCC-common-drawer" size="280px" :modal="true" :wrapperClosable="false"
        :before-close="handleClose" ref="drawer">
        <el-tabs v-model="activeTab">
          <el-tab-pane label="消息" name="reply">
            <div class="userList replyList" v-loading="replyLoading && listQuery.currentPage==1">
              <div v-if="replyList.length">
                <div v-for="(item,i) in replyList" :key="i" class="userList-item"
                  @click="readInfo(item,true)">
                  <el-avatar :size="36" :src="define.comUrl+item.headIcon">
                  </el-avatar>
                  <div class="userList-txt">
                    <p class="title">
                      <span>{{item.realName}}/{{item.account}}</span>
                      <el-badge :value="item.unreadMessage" :hidden="!item.unreadMessage" />
                    </p>
                    <p class="name">
                      <span class="content">{{getMsgText(item.latestMessage,item.messageType)}}</span>
                      <span class="time">{{item.latestDate| toDateText()}}</span>
                    </p>
                  </div>
                  <!-- <el-badge :value="item.unreadMessage" :hidden="!item.unreadMessage"></el-badge> -->
                </div>
              </div>
              <p class="noData-txt" v-else>{{$t('common.noData')}}</p>
            </div>
          </el-tab-pane>
          <el-tab-pane label="联系人" name="contacts">
            <el-input v-model="listQuery.keyword" placeholder="搜索:请输入关键词" clearable
              class="search-input">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="search" title="搜索" />
            </el-input>
            <div class="userList" v-loading="loading && listQuery.currentPage==1" ref="userList">
              <div v-if="userList.length">
                <div v-for="(item,i) in userList" :key="i" class="userList-item"
                  @click="readInfo(item)">
                  <el-avatar :size="36" :src="define.comUrl+item.headIcon">
                  </el-avatar>
                  <div class="userList-txt">
                    <p class="title">{{item.realName}}/{{item.account}}</p>
                    <p class="name">
                      <span>{{item.department}}</span>
                    </p>
                  </div>
                </div>
              </div>
              <p class="noData-txt" v-else>{{$t('common.noData')}}</p>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-drawer>
      <Im ref="NCCIm" append-to-body />
    </div>
  </template>
  
  <script>
  import { mapGetters } from 'vuex'
  import { getImUser } from '@/api/permission/user'
  import { getIMReply } from '@/api/system/message'
  import Im from './Im'
  export default {
    name: 'UserList',
    components: { Im },
    data() {
      return {
        drawer: false,
        activeTab: 'reply',
        userList: [],
        replyList: [],
        loading: false,
        replyLoading: false,
        visible: false,
        finish: false,
        listQuery: {
          keyword: '',
          currentPage: 1,
          pageSize: 20
        }
      }
    },
    created() { },
    computed: {
      ...mapGetters(['userInfo']),
    },
    watch: {
      activeTab(val) {
        if (val === 'contacts' && !this.userList.length) {
          this.getUserList()
        }
      }
    },
    methods: {
      init() {
        this.finish = false
        this.drawer = true
        this.listQuery.currentPage = 1
        this.listQuery.keyword = ''
        this.activeTab = 'reply'
        this.userList = []
        this.replyList = []
        this.getReplyList()
        this.$nextTick(() => {
          this.bindScroll()
        })
      },
      bindScroll() {
        let _this = this,
          vBody = _this.$refs.userList;
        vBody.addEventListener("scroll", function () {
          if (vBody.scrollHeight - vBody.clientHeight - vBody.scrollTop <= 300 && !_this.loading && !_this.finish) {
            _this.listQuery.currentPage += 1
            _this.getUserList()
          }
        });
      },
      getReplyList() {
        this.replyLoading = true
        getIMReply().then(res => {
          this.replyList = res.data.list
          this.replyLoading = false
        })
      },
      getUserList() {
        this.loading = true
        getImUser(this.listQuery).then(res => {
          if (res.data.list.length < this.listQuery.pageSize) this.finish = true
          this.userList = [...this.userList, ...res.data.list]
          this.loading = false
        })
      },
      search() {
        this.userList = []
        this.finish = false
        this.listQuery.currentPage = 1
        this.listQuery.pageSize = 20
        this.getUserList()
      },
      updateReply(data, isAdd) {
        let boo = false
        const len = this.replyList.length
        for (let i = 0; i < len; i++) {
          if (data.formUserId === this.replyList[i].id) {
            if (isAdd) this.replyList[i].unreadMessage += 1
            this.replyList[i].latestMessage = data.formMessage
            this.replyList[i].messageType = data.messageType
            this.replyList[i].latestDate = data.latestDate
            boo = true
            break
          }
        }
        if (boo) return
        if (isAdd) data.unreadMessage = 1
        data.latestMessage = data.formMessage
        data.id = data.formUserId
        this.replyList.unshift(data)
      },
      updateLatestMessage(data) {
        let boo = false
        const len = this.replyList.length
        for (let i = 0; i < len; i++) {
          if (data.toUserId === this.replyList[i].id) {
            this.replyList[i].latestMessage = data.toMessage
            this.replyList[i].messageType = data.messageType
            this.replyList[i].latestDate = data.latestDate
            boo = true
            break
          }
        }
        if (boo) return
        const item = {
          account: data.toAccount,
          headIcon: data.toHeadIcon,
          id: data.toUserId,
          latestDate: data.latestDate,
          latestMessage: data.toMessage,
          messageType: data.messageType,
          realName: data.toRealName,
          unreadMessage: 0
        }
        this.replyList.unshift(item)
      },
      toggleBlink() {
        const boo = this.replyList.some(o => o.unreadMessage)
        this.$emit('changeTwinkle', boo)
      },
      readInfo(item, isReply) {
        let socket = this.$store.getters.socket
        let updateReadMessage = {
          method: "UpdateReadMessage",
          formUserId: item.id,
          token: this.$store.getters.token
        }
        socket.send(JSON.stringify(updateReadMessage));
        if (isReply) {
          item.unreadMessage = 0
        } else {
          for (let i = 0; i < this.replyList.length; i++) {
            if (item.id === this.replyList[i].id) {
              this.replyList[i].unreadMessage = 0
              break
            }
          }
        }
        this.toggleBlink()
        this.$refs.NCCIm.list = []
        //获取消息列表
        var messageList = {
          method: "MessageList",
          toUserId: item.id,
          formUserId: this.userInfo.userId,
          token: this.$store.getters.token,
          currentPage: 1,
          pageSize: 30,
          sord: "desc"
        }
        socket.send(JSON.stringify(messageList));
        this.$refs.NCCIm.init(item)
      },
      getMsgText(text, type) {
        let message = ''
        switch (type) {
          case 'voice':
            message = '[语音]'
            break;
          case 'image':
            message = '[图片]'
            break;
          default:
            message = text
            break;
        }
        return message
      },
      handleClose(done) {
        this.$refs.NCCIm.colseIM()
        let vBody = this.$refs.userList;
        vBody.removeEventListener("scroll", function () { });
        done();
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .contacts-drawer {
    >>> .el-tabs {
      height: 100%;
      .el-tabs__header {
        margin-bottom: 0 !important;
      }
  
      .el-tabs__item {
        width: 50%;
        text-align: center;
      }
  
      .el-tabs__nav {
        width: 100%;
      }
      .el-tabs__content {
        height: calc(100% - 40px);
      }
      .el-tab-pane {
        height: 100%;
        overflow: hidden;
      }
    }
    .noData-txt {
      font-size: 14px;
      color: #909399;
      line-height: 20px;
      text-align: center;
      padding-top: 20px;
    }
    .search-input {
      >>> .el-input__inner {
        border-radius: 0;
        border-right: none;
        border-left: none;
        border-top: none;
      }
      .el-input__icon {
        cursor: pointer;
        &:hover {
          color: #1890ff;
        }
      }
    }
    >>> .el-drawer__header {
      border: none;
    }
    >>> .el-drawer__body {
      overflow: hidden;
      .userList {
        overflow: auto;
        overflow-x: hidden;
        position: relative;
        height: calc(100% - 32px);
        &.replyList {
          height: 100%;
          .title {
            overflow: auto;
            display: flex;
            align-items: center;
            justify-content: space-between;
            height: 20px;
          }
        }
        .el-badge {
          margin-top: 10px;
        }
        >>> .el-loading-mask {
          top: 100px;
        }
      }
      .userList-item {
        position: relative;
        display: block;
        padding: 0 20px;
        background-color: #fff;
        border-bottom: 1px solid #f5f7f9;
        height: 60px;
        display: flex;
        align-items: center;
        cursor: pointer;
        .offLine {
          filter: grayscale(1);
        }
        &:hover {
          background-color: #f5f7f9;
        }
        .userList-txt {
          margin-left: 14px;
          overflow: hidden;
          flex: 1;
          padding-top: 1px;
          .title {
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
            font-size: 14px;
            color: #333;
            margin-bottom: 5px;
            line-height: 20px;
          }
          .name {
            font-size: 12px;
            color: #999;
            .content {
              display: inline-block;
              width: 120px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            .time {
              float: right;
            }
          }
        }
      }
    }
  }
  </style>