quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
<template>
    <view class="im_index">
        <view class="im_item" v-for="(item,index) in imList" :key="index"
            @click="jumpPage(item.supplier_user_id,item.shop_supplier_id,item.supplier.name)">
            <view class="im_item_left">
                <image :src="item.supplier.logo.file_path" mode=""></image>
            </view>
            <view class="im_item_right">
                <view class="im_item_right_item">
                    <view class="title text-ellipsis">{{item.supplier.name}}</view>
                    <view class="time">{{item.newMessage.create_time}}</view>
                </view>
                <view class="im_item_right_item">
                    <view class="content" v-if="item.newMessage.type==0">{{item.newMessage.content}}</view>
                    <view class="content" v-if="item.newMessage.type==1">[图片]</view>
                    <view class="content" v-if="item.newMessage.type==2">[商品]</view>
                    <view v-if="item.num>0" class="message">{{item.num}}</view>
                </view>
            </view>
        </view>
        <view class="hint" v-if="imList.length==0">
            您当前并没有与任何人聊天哦!
        </view>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                imList: [],
                user_id: '',
                official: '', //站内信数据
                logistic: '', //物流数据
                socketTask: null,
                // 确保websocket是打开状态
                is_open_socket: false,
                url: '',
                // 心跳定时器
                intervalId: null,
                /* 初次进入 */
                is_live: false,
                my_user_id: ''
            }
        },
        onShow() {
            this.get_im_list();
        },
        onLoad() {
            this.my_user_id = uni.getStorageSync('user_id');
        },
        beforeDestroy() {
            console.log('beforeDestroy');
            // 销毁监听
            this.closeSocket();
            this.is_live = true;
        },
        methods: {
            send: function(data) {
                let self = this;
                if (self.is_open_socket) {
                    self.socketTask.send({
                        data: data,
                        success() {
 
                        }
                    });
                } else {
                    console.log("处于离线状态");
                    self.socketTask = null;
                    self.is_open_socket = false;
                    clearInterval(self.intervalId);
                    self.socketInit();
                }
            },
            socketInit() {
                let self = this;
                if (self.is_open_socket) {
                    return;
                }
                self.socketTask = null;
                self.socketTask = uni.connectSocket({
                    url: self.url + '/socket?user_id=' + self.getUserId() + '&usertype=user' + '&to=0',
                    success() {
                        console.log('Socket连接成功!');
                    }
                });
                // 消息的发送和接收必须在正常连接打开中,才能发送或接收【否则会失败】
                self.socketTask.onOpen((res) => {
                    console.log("WebSocket连接正常打开中...!");
                    self.is_open_socket = true;
                    // 开始发送心跳
                    self.startHeart();
                    // 注:只有连接正常打开中 ,才能正常收到消息
                    self.socketTask.onMessage(function(res) {
                        console.log("收到服务器内容:");
                        console.log(res);
                        self.getNewcontent(res);
                    });
                });
                // 这里仅是事件监听【如果socket关闭了会执行】
                self.socketTask.onClose(() => {
                    console.log("已经被关闭了");
                    //重连机制
                    self.socketTask = null;
                    self.is_open_socket = false;
                    clearInterval(self.intervalId);
                    !self.is_live && self.socketInit();
                });
            },
            getNewcontent(res) {
                let newdata = JSON.parse(res.data);
                //绑定用户
                if (newdata.type == 'init') {
                    let self = this;
                    self._post(
                        'plus.chat.chat/bindClient', {
                            client_id: newdata.client_id,
                            supplier_user_id: 0,
                        },
                        function(res) {
 
                        }
                    );
                }
            },
            //获取聊天列表
            get_im_list() {
                let self = this;
                self._post('plus.chat.chat/index', {
 
                }, (res) => {
                    console.log(res)
                    self.imList = res.data.list;
                    self.official = res.data.official;
                    self.logistic = res.data.logistic;
                    if(self.url==''){
                        self.url = res.data.url;
                        self.$nextTick(function() {
                            self.socketInit();
                        });
                    }
                })
            },
            closeSocket: function() {
                let self = this;
                let data = JSON.stringify({
                    type: 'close',
                    app_id: self.getAppId(),
                    supplier_user_id: 0,
                    user_id: self.my_user_id,
                    shop_supplier_id: 0,
                    msg_type: 2,
                });
                self.send(data);
                self.socketTask.close({
                    success(res) {
                        console.log("关闭成功", res)
                    },
                    fail(err) {
                        console.log("关闭失败", err)
                    }
                });
                self.socketTask = null;
                self.is_open_socket = false;
                clearInterval(self.intervalId);
            },
            jumpPage(user_id, shop_supplier_id, nickName) {
                if (shop_supplier_id == uni.getStorageInfoSync('user_id')) {
                    return false
                }
                this.gotoPage('/pages/plus/chat/chat?user_id=' + user_id + '&shop_supplier_id=' + shop_supplier_id +
                    '&nickName=' + nickName);
            },
            gotoMessage(val) {
                this.gotoPage('/pages/im/message?type=' + val);
            },
 
        }
    }
</script>
 
<style>
    .im_index {
        width: 100%;
    }
 
    .im_item {
        width: 90%;
        height: 120rpx;
        margin: 0 auto;
        display: flex;
        align-items: center;
    }
 
    .im_item_left {
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
    }
 
    .im_item_left image {
        width: 80rpx;
        height: 80rpx;
        margin: 0 auto;
        margin-top: 5%;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, 0.1);
    }
 
    .im_item_right {
        width: 555rpx;
        padding: 10rpx;
        border-bottom: 1px #dcdcdc solid;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
    }
 
    .message {
        width: 30rpx;
        height: 30rpx;
        border-radius: 50%;
        color: white;
        background-color: red;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 15rpx;
    }
 
    .im_item_right_item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 10rpx;
    }
 
    .title {
        font-size: 32rpx;
    }
 
 
    .guangfang {
        font-size: 16rpx;
        color: #F36A24;
        border: 1rpx #F36A24 solid;
        border-radius: 10rpx;
        padding: 5rpx 10rpx;
        box-sizing: border-box;
        position: relative;
        left: -65rpx;
    }
 
    .time {
        font-size: 26rpx;
        color: #999999;
        flex-shrink: 0;
    }
 
    .content {
        font-size: 26rpx;
        color: #999999;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
        word-break: break-all;
    }
 
    .hint {
        width: 750rpx;
        text-align: center;
        font-size: 32rpx;
        color: #585858;
        margin-top: 20rpx;
    }
</style>