|
@@ -1,30 +1,71 @@
|
1
|
1
|
const app = getApp();
|
2
|
|
-
|
|
2
|
+const ImageType = 'images';
|
|
3
|
+const TextType = 'text';
|
3
|
4
|
Page({
|
4
|
5
|
|
5
|
6
|
/**
|
6
|
7
|
* 页面的初始数据
|
7
|
8
|
*/
|
8
|
9
|
data: {
|
9
|
|
- focus: false,
|
10
|
|
- // scrollTop: 100,
|
|
10
|
+ scrollTopVal: 1, //滚动条
|
11
|
11
|
menu: false, //菜单显示隐藏
|
12
|
|
- uploadImages: ["/image/smyz2.png","/image/smyz1.png"], //相册放大图集
|
|
12
|
+ Expression: false, // 表情显示隐藏
|
|
13
|
+ Sendoutshow: false, // 发送按钮显示隐藏
|
|
14
|
+ menushow: true, //菜单按钮显示隐藏
|
|
15
|
+ _width:500, //input高度
|
|
16
|
+ // uploadImages: ["/image/smyz2.png","/image/smyz1.png"], //相册放大图集
|
13
|
17
|
news:'',//input消息
|
14
|
|
- inputValue: null, //input 的 value
|
15
|
|
-
|
16
|
|
- messages:'',//接收消息数据
|
|
18
|
+ KeyboardHeight: 366.66666666666674, //键盘高度
|
|
19
|
+ ExpressionImg:'', //表情
|
|
20
|
+ inputValue:'', //input 的 value
|
17
|
21
|
photo:'', //相片
|
|
22
|
+
|
|
23
|
+ Receive_news:'', //历史消息
|
|
24
|
+ UserToken:'', //区分自己
|
|
25
|
+ limit_page:1,
|
|
26
|
+
|
|
27
|
+ myheadurl:'', //我的头像
|
|
28
|
+ headUrl:'', //他人头像
|
|
29
|
+
|
|
30
|
+ to_id:'', //他人
|
|
31
|
+ form_id:'', //自己
|
|
32
|
+
|
|
33
|
+ // scrollTop:'' //下拉更多时的位置
|
|
34
|
+ connectemojiO: ['😊','😅','😲', '😭', '😂', '😄', '😩', '😞', '😵', '😒', '😍', '😤', '😜', '😝', '😋', '😘', '😚', '😷', '😳', '😃', '😆', '😁', '😢', '😨', '😠', '😣', '😌', '😖', '😔', '😰', '😱', '😪', '😏', '😓'],
|
|
35
|
+
|
18
|
36
|
},
|
19
|
37
|
|
20
|
38
|
|
21
|
39
|
onLoad: function (options) {
|
22
|
40
|
var that = this;
|
23
|
|
- console.log(options);
|
|
41
|
+
|
|
42
|
+ that.ArrMsg = [];
|
|
43
|
+ // console.log(that.data.UserToken);
|
|
44
|
+ // console.log(options.headUrl);
|
|
45
|
+ // console.log(options.myheadurl);
|
|
46
|
+ // console.log(getApp().UserId(),'token');
|
|
47
|
+
|
|
48
|
+ // console.log(options);
|
24
|
49
|
const friend = options;
|
25
|
|
- this.SocketSend({ to_id: friend.to_id, form_id: getApp().UserId(), type: 'get_history', page: this.data.limit_page });
|
26
|
50
|
|
|
51
|
+ that.setData({
|
|
52
|
+ to_id: friend.to_id, // 他人
|
|
53
|
+ UserToken: getApp().UserToken(), //自己
|
|
54
|
+ myheadurl: options.myheadurl, //我的头像
|
|
55
|
+ headUrl: options.headUrl, //他人头像
|
|
56
|
+ })
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+ this.SocketSend({
|
|
60
|
+ // to_id: friend.to_id, // 他人
|
|
61
|
+ // form_id: getApp().UserId(), //自己
|
|
62
|
+ type: 'get_history',
|
|
63
|
+ page: this.data.limit_page
|
|
64
|
+ });
|
27
|
65
|
getApp().Imsocket().onSocketMessageCallback = this.onSocketMessageCallback;
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
28
|
69
|
},
|
29
|
70
|
|
30
|
71
|
|
|
@@ -33,18 +74,35 @@ Page({
|
33
|
74
|
var that = this;
|
34
|
75
|
that.setData({
|
35
|
76
|
menu: false, //关闭底部菜单
|
|
77
|
+ Expression: false, //关闭表情
|
36
|
78
|
})
|
37
|
79
|
},
|
38
|
80
|
|
|
81
|
+
|
39
|
82
|
// 底部菜单显示隐藏切换
|
40
|
83
|
chatInputExtraClickEvent:function(){
|
41
|
84
|
var that = this;
|
42
|
85
|
that.setData({
|
43
|
|
- menu: !that.data.menu
|
|
86
|
+ menu: !that.data.menu,
|
|
87
|
+ Expression: false, //关闭表情
|
44
|
88
|
})
|
45
|
89
|
},
|
46
|
90
|
|
47
|
91
|
|
|
92
|
+
|
|
93
|
+ // 关闭表情显示隐藏切换
|
|
94
|
+ExpressionClickEvent: function () {
|
|
95
|
+ var that = this;
|
|
96
|
+ that.setData({
|
|
97
|
+ Expression: !that.data.Expression,
|
|
98
|
+ menu: false, //关闭底部菜单
|
|
99
|
+ })
|
|
100
|
+ },
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
48
|
106
|
// 相册点击放大
|
49
|
107
|
enlarge: function (e) {
|
50
|
108
|
var that = this;
|
|
@@ -52,7 +110,7 @@ Page({
|
52
|
110
|
// console.log(e.currentTarget.dataset.src,)
|
53
|
111
|
wx.previewImage({
|
54
|
112
|
current: e.currentTarget.dataset.src, // 当前显示图片的http链接
|
55
|
|
- urls: that.data.uploadImages, // 需要预览的图片http链接列表
|
|
113
|
+ urls: [e.currentTarget.dataset.src], // 需要预览的图片http链接列表
|
56
|
114
|
})
|
57
|
115
|
|
58
|
116
|
|
|
@@ -60,32 +118,20 @@ Page({
|
60
|
118
|
|
61
|
119
|
|
62
|
120
|
|
63
|
|
-// 发送消息事件
|
64
|
|
- Sendout: function (e) {
|
65
|
|
- var that = this;
|
66
|
|
- console.log(that.data.news);
|
67
|
|
-
|
68
|
|
- that.setData({
|
69
|
|
- menu: false, //隐藏底部菜单
|
70
|
|
- inputValue: '', //清空 input
|
71
|
|
- messages: that.data.news, //显示信息
|
72
|
|
- })
|
73
|
|
- console.log('发送');
|
74
|
|
-
|
75
|
|
- },
|
76
|
|
-
|
77
|
|
- //输入框
|
|
121
|
+ //input输入框
|
78
|
122
|
Telephone: function (e) {
|
79
|
123
|
var that = this;
|
80
|
|
- // console.log(e.detail.value);
|
81
|
|
-
|
|
124
|
+ console.log(e.detail.value);
|
82
|
125
|
that.setData({
|
83
|
126
|
news: e.detail.value, //获取输入文字
|
84
|
127
|
})
|
|
128
|
+
|
85
|
129
|
|
|
130
|
+ that.Sendoutswitch(e.detail.value); // 发送键切换
|
86
|
131
|
},
|
87
|
132
|
|
88
|
133
|
|
|
134
|
+
|
89
|
135
|
// 上传图片
|
90
|
136
|
chooseImageTap: function () {
|
91
|
137
|
var that = this;
|
|
@@ -95,18 +141,52 @@ Page({
|
95
|
141
|
sourceType: ['album', 'camera'],
|
96
|
142
|
success(res) {
|
97
|
143
|
|
|
144
|
+ wx.uploadFile({
|
|
145
|
+ url: getApp().globalData.url + 'imsocket/images/chatimg',
|
|
146
|
+ filePath: res.tempFilePaths[0],
|
|
147
|
+ name: 'photo',
|
|
148
|
+ formData: {
|
|
149
|
+ key: wx.getStorageSync('access_key'),
|
|
150
|
+ token: wx.getStorageSync('access_token')
|
|
151
|
+ },
|
|
152
|
+ success: function (res) {
|
|
153
|
+ var data = JSON.parse(res.data);
|
|
154
|
+
|
|
155
|
+ console.log(data, '图片上传成功');
|
|
156
|
+
|
|
157
|
+ if (data.code == 201) {
|
|
158
|
+ let con = data.data;
|
|
159
|
+ //显示图片
|
|
160
|
+ console.log(con, 1);
|
|
161
|
+
|
|
162
|
+ // console.log(ImgRes.tempFilePaths[0], 1222222);
|
|
163
|
+ that.SocketSend({type: ImageType, content: con});
|
|
164
|
+
|
|
165
|
+ // that.msgManager.sendMsg({ type: IMOperator.ImageType, content: con })
|
|
166
|
+
|
|
167
|
+ } else if (data.code == 301) {
|
|
168
|
+ wx.showModal({
|
|
169
|
+ title: '提示',
|
|
170
|
+ content: data.msg,
|
|
171
|
+ showCancel: false,
|
|
172
|
+ })
|
|
173
|
+ }
|
|
174
|
+ }, fail: function (d) {
|
|
175
|
+ }
|
|
176
|
+ })
|
98
|
177
|
console.log(res.tempFilePaths[0]);
|
99
|
178
|
// tempFilePath可以作为img标签的src属性显示图片
|
100
|
179
|
that.setData({
|
101
|
180
|
photo: res.tempFilePaths[0],
|
|
181
|
+ menu: false, //菜单显示隐藏
|
102
|
182
|
});
|
103
|
183
|
|
104
|
184
|
}
|
105
|
185
|
})
|
106
|
|
-
|
107
|
|
-
|
108
|
186
|
},
|
109
|
187
|
|
|
188
|
+
|
|
189
|
+
|
110
|
190
|
// 重发消息
|
111
|
191
|
retransmission:function(){
|
112
|
192
|
wx.showModal({
|
|
@@ -119,30 +199,305 @@ Page({
|
119
|
199
|
}
|
120
|
200
|
}
|
121
|
201
|
})
|
122
|
|
-
|
123
|
202
|
},
|
124
|
|
- SocketSend: function (msg) {
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+// 发送消息
|
|
207
|
+ SocketSend: function (msg) {
|
|
208
|
+ var that =this;
|
|
209
|
+ msg.form_id = getApp().UserId();
|
|
210
|
+ msg.to_id = that.data.to_id;
|
|
211
|
+
|
|
212
|
+ // console.log(that.data.Receive_news);
|
|
213
|
+
|
125
|
214
|
// { to_id: friend.to_id, form_id: getApp().UserId(), type: 'get_history' }
|
126
|
215
|
// msg.type = 'get_history';
|
127
|
216
|
getApp().Imsocket().sendSocketMessage({
|
128
|
|
- msg: JSON.stringify(msg),
|
129
|
|
- success: function (res) {
|
|
217
|
+ msg: JSON.stringify(msg),
|
|
218
|
+
|
|
219
|
+ success: function (res) { //发送成功
|
|
220
|
+
|
|
221
|
+ // 我跟他人说话
|
|
222
|
+ switch(msg.type){
|
|
223
|
+
|
|
224
|
+ case "alone": // 发送文本
|
|
225
|
+
|
|
226
|
+ console.log(' 发送文本');
|
|
227
|
+ var obj = { //创建对象,用来合并历史消息对象
|
|
228
|
+ content: that.data.news, //发送消息内容
|
|
229
|
+ type: 1, //类型 1 : 文字类型 类型 2 : 图片
|
|
230
|
+ time: Date.now(), //时间
|
|
231
|
+ isMy: true, //自己
|
|
232
|
+ send: true, // 发送成功 为true 失败false
|
|
233
|
+ };
|
|
234
|
+ let _Receive_news = that.data.Receive_news;
|
|
235
|
+ _Receive_news.push(obj); //合并历史消息对象
|
|
236
|
+
|
|
237
|
+ console.log(_Receive_news,'aaa');
|
|
238
|
+ that.setData({
|
|
239
|
+ Receive_news: _Receive_news, //前台数据赋值
|
|
240
|
+ });
|
|
241
|
+
|
|
242
|
+ //调用页面滚动条处理
|
|
243
|
+ that.scrollbar(_Receive_news);
|
|
244
|
+
|
|
245
|
+ break;
|
|
246
|
+
|
|
247
|
+ case "images": // 发送图片
|
|
248
|
+
|
|
249
|
+ console.log(' 发送图片');
|
|
250
|
+
|
|
251
|
+ var obj = { //创建对象,用来合并历史消息对象
|
|
252
|
+ content: that.data.photo, //发送消息内容
|
|
253
|
+ type: 2, //类型 1 : 文字类型 类型 2 : 图片
|
|
254
|
+ time: Date.now(), //时间
|
|
255
|
+ isMy: true, //自己
|
|
256
|
+ send: true, // 发送成功 为true 失败false
|
|
257
|
+ };
|
|
258
|
+ let _Receive_img = that.data.Receive_news;
|
|
259
|
+ _Receive_img.push(obj); //合并历史消息对象
|
|
260
|
+
|
|
261
|
+ console.log(_Receive_img, '发送图片');
|
|
262
|
+ that.setData({
|
|
263
|
+ Receive_news: _Receive_img, //前台数据赋值
|
|
264
|
+ });
|
|
265
|
+
|
|
266
|
+ //调用页面滚动条处理
|
|
267
|
+ that.scrollbar(_Receive_img);
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+ break;
|
|
273
|
+ }
|
|
274
|
+
|
130
|
275
|
console.log(res, '获取历史消息成功');
|
131
|
|
- }, fail: function (res) {
|
|
276
|
+
|
|
277
|
+ }, fail: function (res) { //发送失败
|
|
278
|
+
|
132
|
279
|
console.log('获取历史消息失败')
|
|
280
|
+
|
133
|
281
|
}
|
134
|
282
|
})
|
|
283
|
+
|
135
|
284
|
},
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+// 接收消息
|
136
|
288
|
onSocketMessageCallback: function (e) {
|
|
289
|
+ var that = this;
|
137
|
290
|
let data = JSON.parse(e);
|
138
|
|
-
|
139
|
|
- console.log(data,'接受消息成功');
|
|
291
|
+
|
|
292
|
+ // console.log(data,'接受消息成功');
|
|
293
|
+
|
|
294
|
+ var arrData = data.data;
|
|
295
|
+
|
|
296
|
+ switch(data.type){
|
|
297
|
+ // 接收历史消息
|
|
298
|
+ case "get_history":
|
|
299
|
+ // console.log(arrData,111112222);
|
|
300
|
+ for (var index in arrData) { //二次赋值
|
|
301
|
+ if (arrData[index].form_id == getApp().UserToken()) { //判断 如果等于自己 true ,等于他人 false。
|
|
302
|
+ arrData[index].isMy = true; //自己
|
|
303
|
+ }else{
|
|
304
|
+ arrData[index].isMy = false; //他人
|
|
305
|
+ }
|
|
306
|
+ that.ArrMsg.push(arrData[index]);
|
|
307
|
+ }
|
|
308
|
+
|
|
309
|
+ // 滚动条处理
|
|
310
|
+ var scrollTopValLength = "";
|
|
311
|
+ if (that.data.limit_page == 1) {
|
|
312
|
+ scrollTopValLength = arrData.length * 999
|
|
313
|
+ } else {
|
|
314
|
+ scrollTopValLength = arrData.length * 100
|
|
315
|
+ }
|
|
316
|
+
|
|
317
|
+ console.log(that.ArrMsg, 111112222);
|
|
318
|
+ that.setData({
|
|
319
|
+ Receive_news: that.sortarr(that.ArrMsg), //前台数据赋值
|
|
320
|
+ scrollTopVal: scrollTopValLength, // 滚动条赋值
|
|
321
|
+ });
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+ // that.scrollbar(that.ArrMsg); //调用页面滚动条处理 这里不对
|
|
325
|
+ break;
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+ case "alone": // 接收他人文本
|
|
329
|
+ console.log(arrData,"aaasd");
|
|
330
|
+
|
|
331
|
+ var obj = { //创建对象,用来合并历史消息对象
|
|
332
|
+ content: arrData.content, //发送消息内容
|
|
333
|
+ type: 1, //类型 1 : 文字类型 类型 2 : 图片
|
|
334
|
+ time: Date.now(), //时间
|
|
335
|
+ isMy: false, //他人
|
|
336
|
+ };
|
|
337
|
+
|
|
338
|
+ let _Receive_news = that.data.Receive_news;
|
|
339
|
+ _Receive_news.push(obj); //合并历史消息对象
|
|
340
|
+
|
|
341
|
+ that.setData({
|
|
342
|
+ Receive_news: _Receive_news, //前台数据赋值
|
|
343
|
+ });
|
|
344
|
+
|
|
345
|
+ //调用页面滚动条处理
|
|
346
|
+ that.scrollbar(_Receive_news);
|
|
347
|
+ break;
|
|
348
|
+
|
|
349
|
+ case "images": // 接收他人图片
|
|
350
|
+ console.log(arrData, "接收图片");
|
|
351
|
+
|
|
352
|
+ var obj = { //创建对象,用来合并历史消息对象
|
|
353
|
+ content: arrData.content, //发送消息内容
|
|
354
|
+ type: 2, //类型 1 : 文字类型 类型 2 : 图片
|
|
355
|
+ time: Date.now(), //时间
|
|
356
|
+ isMy: false, //他人
|
|
357
|
+ };
|
|
358
|
+
|
|
359
|
+ let _Receive_img = that.data.Receive_news;
|
|
360
|
+ _Receive_img.push(obj); //合并历史消息对象
|
|
361
|
+
|
|
362
|
+ that.setData({
|
|
363
|
+ Receive_news: _Receive_img, //前台数据赋值
|
|
364
|
+ });
|
|
365
|
+
|
|
366
|
+ //调用页面滚动条处理
|
|
367
|
+ that.scrollbar(_Receive_img);
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+ break;
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+ }
|
|
375
|
+
|
|
376
|
+
|
140
|
377
|
|
141
|
378
|
},
|
142
|
379
|
|
|
380
|
+// 处理滚动条
|
|
381
|
+ scrollbar: function (data) {
|
|
382
|
+ var that =this;
|
|
383
|
+ if(data){
|
|
384
|
+ let lengNum = data.length;
|
|
385
|
+ //页面滚动条处理
|
143
|
386
|
|
|
387
|
+ that.setData({
|
|
388
|
+ scrollTopVal: lengNum * 999
|
|
389
|
+ })
|
|
390
|
+ console.log(lengNum * 999);
|
|
391
|
+ }
|
144
|
392
|
|
|
393
|
+ },
|
145
|
394
|
|
|
395
|
+ // 发送消息事件 用来获取input数据
|
|
396
|
+ Sendout: function (e) {
|
|
397
|
+ var that = this;
|
|
398
|
+ // console.log(that.data.news);
|
|
399
|
+
|
|
400
|
+ let msg = {
|
|
401
|
+ // to_id: that.data.to_id,
|
|
402
|
+ // form_id: getApp().UserId(),
|
|
403
|
+ type: 'alone',
|
|
404
|
+ content: that.data.news
|
|
405
|
+ };
|
|
406
|
+
|
|
407
|
+ // console.log(msg);
|
|
408
|
+ this.SocketSend(msg);
|
|
409
|
+
|
|
410
|
+ that.setData({
|
|
411
|
+ menu: false, //隐藏底部菜单
|
|
412
|
+ Expression: false,//隐藏表情栏
|
|
413
|
+ inputValue: '', //清空 input
|
|
414
|
+ news: '', //清空 input内容
|
|
415
|
+ })
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+ that.Sendoutswitch(that.data.news) // 发送键切换
|
|
419
|
+
|
|
420
|
+ // console.log(that.data.Receive_news)
|
|
421
|
+
|
|
422
|
+ },
|
|
423
|
+
|
|
424
|
+//冒泡排序
|
|
425
|
+ sortarr: function (arr) {
|
|
426
|
+ for (let i = 0; i < arr.length - 1; i++) {
|
|
427
|
+ for (let j = 0; j < arr.length - 1 - i; j++) {
|
|
428
|
+ if (arr[j].create_at > arr[j + 1].create_at) {
|
|
429
|
+ var temp = arr[j];
|
|
430
|
+ arr[j] = arr[j + 1];
|
|
431
|
+ arr[j + 1] = temp;
|
|
432
|
+ }
|
|
433
|
+ }
|
|
434
|
+ }
|
|
435
|
+ return arr;
|
|
436
|
+ },
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+ upper: function (e) {
|
|
440
|
+ // console.log(e,'加载')
|
|
441
|
+
|
|
442
|
+ let that = this;
|
|
443
|
+ that.setData({
|
|
444
|
+ limit_page: that.data.limit_page * 1 + 1
|
|
445
|
+ })
|
|
446
|
+ console.log(that.data.limit_page * 1 + 1)
|
|
447
|
+ that.SocketSend({type: 'get_history', page: that.data.limit_page })
|
146
|
448
|
|
147
|
449
|
|
|
450
|
+ },
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+// 点击表情
|
|
454
|
+ Expression:function(e){
|
|
455
|
+ var that = this;
|
|
456
|
+
|
|
457
|
+ var _news = that.data.news;
|
|
458
|
+ var _text = _news + e.currentTarget.dataset.id;
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+ console.log(that.data.news)
|
|
462
|
+ console.log(_text)
|
|
463
|
+ that.setData({
|
|
464
|
+ news: _text,
|
|
465
|
+ inputValue: _text,
|
|
466
|
+ })
|
|
467
|
+
|
|
468
|
+ that.Sendoutswitch(that.data.news) // 发送键切换
|
|
469
|
+ },
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+// 点击input
|
|
473
|
+ bindfocusclick: function(e) {
|
|
474
|
+ var that = this;
|
|
475
|
+ that.setData({
|
|
476
|
+ Expression: false,
|
|
477
|
+ menu: false,
|
|
478
|
+ KeyboardHeight: e.detail.height,
|
|
479
|
+ })
|
|
480
|
+ console.log(e.detail.height);
|
|
481
|
+ },
|
|
482
|
+
|
|
483
|
+ // 发送键切换
|
|
484
|
+ Sendoutswitch: function (data) {
|
|
485
|
+ var that = this;
|
|
486
|
+ if (data == '') {
|
|
487
|
+ that.setData({
|
|
488
|
+ menushow: true, //菜单按钮显示隐藏
|
|
489
|
+ Sendoutshow: false,// 发送按钮显示隐藏
|
|
490
|
+ _width: 500, // input宽度
|
|
491
|
+ })
|
|
492
|
+ } else {
|
|
493
|
+ that.setData({
|
|
494
|
+ menushow: false, //菜单按钮显示隐藏
|
|
495
|
+ Sendoutshow: true,// 发送按钮显示隐藏
|
|
496
|
+ _width: 447, // input宽度
|
|
497
|
+ })
|
|
498
|
+ }
|
|
499
|
+ },
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
148
|
503
|
})
|