-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
549 lines (488 loc) · 21.7 KB
/
app.js
File metadata and controls
549 lines (488 loc) · 21.7 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* @Descripttion:
* @version:
* @Author: Ghost
* @Date: 2020-08-14 17:50:12
* @LastEditors: Ghost
* @LastEditTime: 2020-10-27 11:48:25
*/
//app.js
App({
onLaunch: function () {
//----------------------!!!!!!!!!!!!!!!!清除本地存储!!!!!!!!!!!!!!
try {
wx.removeStorage({
key: 'userData',
success () {}
})
} catch(e) {
console.error(e)
}
//----------------------!!!!!!!!!!!!!!!!清除本地存储!!!!!!!!!!!!!!
this.globalData.userDataInitFinished = false;
const that = this;
wx.cloud.init({
env: "your-cloud-env-id" // 请替换为你的云环境ID
});
this.globalData.userDataInitFinished = false;
//0.缓存测试
//更新
const updateManager = wx.getUpdateManager()
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,不兼容旧版本,请问是否重启?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
});
// * @FileName: app
// * @FilePath: I:\WeiXin\ershougou\demo02\app.js
// * @Author: vancece
// * @Email: vancece@outlook.com
// * @CreatedTime: 2020/8/20 15:02
// * @LastChangeTime: 2020年8月21日19:03:22
// * @Declaration: 用户进入小程序,初始用户信息
//0.检测存储userData缓存
wx.getStorage({
key: 'userData',
success(res) {
//0.1将缓存的userData设为全局变量的userData
// console.log("app.js")
// console.log(res)
that.globalData.userData = res.data
that.globalData.userDataInitFinished = true;
//0.2调用云函数更新用户登录时间
wx
.cloud
.callFunction({
name: 'updateUserLastLoginTime',
data: {}
})
.then(res => {
// 获取用户的全部信息
wx
.cloud
.callFunction({
name: 'getUserAllData',
data: {}
})
.then(res => {
//0.3更新本地用户登录时间
that.globalData.userData = res.result.data[0];
wx.setStorage({
key: 'userData',
data: that.globalData.userData
});
})
.then(() => {
//1.2 查看用户是否已授权
if (that.globalData.userData.isAuthorize) {
// 2.1调用getUserInfo,更新本地数据
wx.getUserInfo({
lang: 'zh_CN',
success: function (res) {
const userInfo = res.userInfo;
//跳过
if (!that.globalData.userData.username) {
that.globalData.userData.username = userInfo.nickName;
}
that.globalData.userData.imageUrl = userInfo.avatarUrl;
that.globalData.userData.gender = userInfo.gender;
if (!that.globalData.userData.province) {
that.globalData.userData.province = userInfo.province;
}
if (!that.globalData.userData.city) {
that.globalData.userData.city = userInfo.city;
}
if (!that.globalData.userData.country) {
that.globalData.userData.country = userInfo.country;
}
//2.2更新缓存UserData
wx.setStorage({
key: 'userData',
data: that.globalData.userData
})
}
});
} else {
}
})
});
// 0.1将缓存的userData设为全局变量的userData
},
fail() {
//1.进入小程序,云调用云函数检测数据库是否存在该用户
wx.cloud.callFunction({
name: 'checkUserExit',
data: {}
}).then(res => {
if (res.result.exit) {
//1.1.云调用函数更新用户信息,,返回用户的全部信息
// 更新用户最后登录时间
wx
.cloud
.callFunction({
name: 'updateUserLastLoginTime',
data: {}
})
.then(res => {
// 获取用户的全部信息
wx
.cloud
.callFunction({
name: 'getUserAllData',
data: {}
})
.then(res => {
that.globalData.userData = res.result.data[0];
that.globalData.userDataInitFinished = true;
})
.then(res => {
//1.2 查看用户是否已授权
if (that.globalData.userData.isAuthorize) {
// 2.1调用getUserInfo,更新本地数据
wx.getUserInfo({
lang: 'zh_CN',
success: function (res) {
const userInfo = res.userInfo;
//跳过
if (!that.globalData.userData.username) {
that.globalData.userData.username = userInfo.nickName;
}
that.globalData.userData.imageUrl = userInfo.avatarUrl;
that.globalData.userData.gender = userInfo.gender;
if (!that.globalData.userData.province) {
that.globalData.userData.province = userInfo.province;
}
if (!that.globalData.userData.city) {
that.globalData.userData.city = userInfo.city;
}
if (!that.globalData.userData.country) {
that.globalData.userData.country = userInfo.country;
}
//2.2更新缓存UserData
wx.setStorage({
key: 'userData',
data: that.globalData.userData
})
},
fail: function () {
// console.log("getUserInfo失败")
}
});
} else {
}
})
.then(res => {
//2.3调用云函数更新数据库数据
wx.cloud.callFunction({
name: 'updateUserData',
data: {
userData: that.globalData.userData
}
})
.then(res => {
that.globalData.userData.lastLoginTime = res.result.lastLoginTime;
})
//设置已经初始化userData
.then(res => {
that.globalData.userDataInitFinished = true;
});
})
.catch(console.error);
})
} else {
//3.1记录库新增一条记录,设置OpenId,随机设置名字,设置未授权状态
wx
.cloud
.callFunction({
name: 'insertNewUserWithEmptyData',
data: {}
})
.then(res => {
})
.then(() => {
//3.2返回用户的全部信息
wx
.cloud
.callFunction({
name: 'getUserAllData',
data: {}
})
.then(res => {
//3.3本地将返回的信息设置全局变量
that.globalData.userData = res.result.data[0];
that.globalData.userDataInitFinished = true;
//3.4更新本地缓存userData
wx.setStorage({
key: 'userData',
data: that.globalData.userData
})
})
.catch(console.error);
});
}
}).catch(console.error);
}
})
//如果第一次进来,创建一个聊天记录的数组
var value = wx.getStorageSync('chatting')
if(!value){
wx.setStorageSync('chatting', [])
}
//初始化全局变量chatting
let tmp = wx.getStorageSync('chatting');
this.globalData.chatting=tmp;
// for(;;)
// {
// if(this.globalData.userDataInitFinished == true) break;
// }
if(this.globalData.userDataInitFinished == false)
{
setTimeout(()=>{
this.loadChat();
this.startChat();
},5000)
}else{
this.loadChat();
this.startChat();
}
},
onShow(){
// console.log(this.globalData.userData);
//如果第一次进来,创建一个聊天记录的数组
// var value = wx.getStorageSync('chatting')
// if(!value){
// wx.setStorageSync('chatting', [])
// }
//初始化全局变量chatting
// let tmp = wx.getStorageSync('chatting');
// this.globalData.chatting=tmp;
},
onHide(){
wx.setStorageSync('chatting', this.globalData.chatting)
// console.log("触发apponhide");
//全部改为已接收
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'loadMessage',
// 传递给云函数的event参数
data: {
status: 1,
}
})
},
async loadChat(){
let that =this
//接收未接收的聊天数据
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'loadMessage',
// 传递给云函数的event参数
data: {
status: 2,
}
}).then(async res => {
// console.log("未读信息",res);
//如果返回的信息不为0条
if(res.result.data.length!=0)
{
let flag=0;//查询是否存在该对象
for(let j=0;j< that.globalData.chatting.length;j++)
{
if(that.globalData.chatting[j].openid == res.result.data[0].sender)
{
flag=1;
break;
}
}
//不存在
if(flag==0)
{
const db = wx.cloud.database()
await db.collection('user').where({
openid:res.result.data[0].sender
}).get().then(res1 => {
// console.log(res1.data)
let tmp={
openid:res1.data[0].openid,
username:res1.data[0].username,
imageUrl:res1.data[0].imageUrl,
isRead:false,
chat:[]
}
that.globalData.chatting.push(tmp)
// console.log("qianmian");
})
}
//放进对应的人员中
for(let i=0;i<res.result.data.length;i++)
{
//把接收到的消息放进全局的聊天变量中
for(let j=0;j<that.globalData.chatting.length;j++)
{
if(that.globalData.chatting[j].openid==res.result.data[i].sender)
{
// console.log("app.js插入一条");
that.globalData.chatting[j].chat.push(res.result.data[i])
that.globalData.chatting[j].isRead = false
break;
}
}
//显示小红点
wx.showTabBarRedDot({
index: 3,
});
}
}
})
},
startChat()
{
// 监听聊天
let that = this;
// console.log(this.globalData);
// console.log(this.globalData.userData);
const db = wx.cloud.database()
this.globalData.chat = db.collection('chat')
.where({
receiver:that.globalData.userData.openid, // 填入当前用户 openid
//receiver:"o78Ah5ev393S0TztSYjY11HUYVpM", // 填入当前用户 openid
isRead:false
})
.orderBy('time', 'asc')
.watch({
onChange: function(snapshot) {
// console.log('snapshot', snapshot)
//全部改为已接收
wx.cloud.callFunction({
// 要调用的云函数名称
name: 'loadMessage',
// 传递给云函数的event参数
data: {
status: 1,
}
})
// console.log(snapshot.docChanges[0].doc);
if(snapshot.type != 'init' && snapshot.docChanges[0].dataType!= "update"){
//查询到的数据插入到全局的chatting
let flag=0;//查询是否存在该对象
for(let i=0;i< that.globalData.chatting.length;i++)
{
if(that.globalData.chatting[i].openid==snapshot.docChanges[0].doc.sender)
{
flag=1;
// console.log("已存在对象");
break;
}
}
if(flag==0){
// console.log("不存在对象");
//不存在就创建该对象
const db = wx.cloud.database()
db.collection('user').where({
openid:snapshot.docChanges[0].doc.sender
}).get().then(res => {
console.log(res.data)
let tmp={
openid:snapshot.docChanges[0].doc.sender,
username:res.data[0].username,
imageUrl:res.data[0].imageUrl,
isRead:false,
chat:[]
}
that.globalData.chatting.push(tmp)
console.log("houmian");
//把接收到的消息放进全局的聊天变量中
for(let i=0;i<that.globalData.chatting.length;i++)
{
if(that.globalData.chatting[i].openid==snapshot.docChanges[0].doc.sender)
{
console.log("app.js插入一条");
that.globalData.chatting[i].chat.push(snapshot.docChanges[0].doc)
that.globalData.chatting[i].isRead = false
break;
}
}
//显示小红点
wx.showTabBarRedDot({
index: 3,
});
if(that.globalData.indexChat!="")
{
that.globalData.indexChat.updateData();
}
if(that.globalData.indexChatting!="")
{
that.globalData.indexChatting.updateData();
}
})
}
else{
let fff=0;
console.log("hello1");
//把接收到的消息放进全局的聊天变量中
for(let i=0;i<that.globalData.chatting.length;i++)
{
if(that.globalData.chatting[i].openid==snapshot.docChanges[0].doc.sender)
{
console.log("app.js插入一条");
that.globalData.chatting[i].chat.push(snapshot.docChanges[0].doc)
that.globalData.chatting[i].isRead = false
fff=1
break;
}
if(fff) break;
}
//显示小红点
wx.showTabBarRedDot({
index: 3,
});
console.log("hello");
if(that.globalData.indexChat!="")
{
that.globalData.indexChat.updateData();
that.globalData.indexChat.updateData();
}
if(that.globalData.indexChatting!="")
{
that.globalData.indexChatting.updateData();
}
}
}
},
onError: function(err) {
console.error('the watch closed because of error', err)
}
})
},
closeChat()
{
this.globalData.chat.close()
},
// 给子页面调用,升级页面数据
updateUserData(obj) {
this.globalData.userData = obj;
},
// 给子页面调用,升级页面数据
updateChatting(obj) {
this.globalData.chatting = obj;
},
globalData: {
userData: {},
userDataInitFinished: false,
//全局变量的聊天记录
chatting:[],
indexChat:"",
indexChatting:"",
},
initData(res) {
this.globalData.userData = res.data;
this.globalData.userDataInitFinished = true;
}
})