-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapp.js
446 lines (437 loc) · 12.9 KB
/
app.js
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
//app.js
import Dialog from '@vant/weapp/dialog/dialog';
import Notify from '@vant/weapp/notify/notify';
import { hostUrl, appInfo } from './config';
App({
onLaunch: function () {
let that = this;
//初始化tabbar状态
that.updateNotices({read: 0, type: undefined});
},
globalData: {
status: {
tabbar: [
{
dot: false,
number: 0
},
{
dot: false,
number: 0
},
{
dot: false,
number: 0
},
{
dot: false,
number: 0
}
]
}
},
/*******************************
* 公共方法区
*******************************/
/**
* 设置tabbar状态
* index: 第几个tab,0~3
* value: { dot:boolean, number:number }
*/
setTabbar: function(index, value){
if(value.number==0 || value.number==null || value.number==undefined){//取消数字,设置红点
wx.removeTabBarBadge({
index: index,
})
if(value.dot){
wx.showTabBarRedDot({
index: index,
})
}else{
wx.hideTabBarRedDot({
index: index,
})
}
}else{ //设置数字
wx.setTabBarBadge({
index: index,
text: value.number+'',
})
}
},
/**
* 获取基础配置
*/
config: {
//获取请求环境
getHostUrl: function(){
return hostUrl;
}
},
/**
* 微信获取用户openid
*/
getOpenid: function(){
let that = this;
return new Promise(
(resolve, reject) => {
const openid = wx.getStorageSync('openid');
if (openid && openid.length > 10) {
resolve(openid); // 本地有openid缓存,直接返回
}
const isMyHost = !/nunet\.cn/.test(hostUrl);
if (!isMyHost) {
if (!appInfo.appid || !appInfo.secret) {
throw new Error('请在 config.js 中配置自己的appid和secret');
}
}
const api = !isMyHost
? `/api/main/getOpenid?appid=${appInfo.appid}&secret=${appInfo.secret}`
: '/api/main/getOpenid';
wx.login({
success (res) {
if (res.code) {
wx.request({
url: that.config.getHostUrl()+api,
method: 'post',
data: {
code: res.code
},
success: function(res){
if(res.statusCode == 200 && res.data.isSuccess){
resolve(res.data.data.openid);
wx.setStorageSync('openid', res.data.data.openid); // 缓存openid
}else{
// 服务器故障 标识:500
reject({error: 500, errMsg: "服务器故障", data: res});
}
},
fail: function(res){
// 请求错误
reject({error: 400, errMsg:"请求错误", data: res});
}
})
} else {
// 登录失败
reject({error: 400, errMsg:res.errMsg, data: res});
}
}
})
}
);
},
/**
* 微信授权注册
*/
getUserInfo: function(){
let that = this;
//前提需要通过open-type="getUserInfo"按钮进行用户信息授权
wx.getSetting({
complete: (res) => {
if(res.authSetting['scope.userInfo']){ //如果授权过了,获取用户信息
wx.getUserInfo({
complete: (res) => {
that.getOpenid().then( //获取openid,不需要授权
(data) => {
let userData = {
openid: data, //openid
nickname: res.userInfo.nickName, //昵称
sex: res.userInfo.gender, //头像
img: res.userInfo.avatarUrl //性别
};
//跑鸭注册
wx.request({
url: that.config.getHostUrl()+'/api/main/wxAuth',
method: 'post',
data: userData,
success: (res) => {
if(res.data.isSuccess){
//注册成功处理逻辑
console.log(res.data)
//用户信息本地缓存
wx.setStorageSync('user', JSON.stringify(res.data.data));
}else{
// 注册失败
console.log(res.data.msg)
}
},
fail: (res) => {
// 请求失败
}
})
}
).catch(
(data) => {
// 错误处理 data: {error, errMsg, data}
console.log(data)
}
)
},
})
}else{
//用户未授权,提示框用户授权
}
},
})
},
/**
* 获取当前用户数据、判断是否注册
*/
getUser: function(){
let that = this;
let user = wx.getStorageSync('user');
if(user){
if(user.constructor != Object) user = JSON.parse(user);
return user;
}else{
// 判断是否注册
that.getOpenid().then(
(openid)=>{
wx.request({
url: that.config.getHostUrl()+'/api/user/getUser',
method: 'post',
data: {
openid: openid
},
success: (res)=>{
if(res.data.data != null){ //已注册,未登录
wx.showModal({
title: '提示',
content: '当前操作需要登录才能进行,请登录',
success (r) {
if (r.confirm) {
wx.showToast({
title: '登录成功',
icon: 'success',
})
wx.setStorageSync('user', JSON.stringify(res.data.data));
user = res.data.data;
} else if (r.cancel) {
//点击取消,啥也不干
wx.showToast({
title: '取消登录',
icon: 'error',
})
}
}
})
}else{ //未注册
wx.showModal({
title: '提示',
content: '您还未授权注册,是否立即授权注册?',
success (res) {
if (res.confirm) {
that.getUserInfo();
} else if (res.cancel) {
//点击取消,啥也不干
wx.showToast({
title: '取消注册',
icon: 'error',
})
}
}
})
}
}
})
}
).catch(
(err)=>{
console.log(err);
wx.showToast({
title: err,
icon: 'none',
})
}
)
return user;
}
},
/**
* 作用:检测用户是否注册,阻止未注册用户进行某些事件交互
* 需要:在页面加 <van-dialog id="van-dialog" />
* 不传user时: 会先获得openid,然后在线判断是否注册
*/
checkUser: function(user){
let that = this;
if(!user){
that.showNoticToTraveler();
return false;
}else{
that.getOpenid().then(res => {
let openid = res;
wx.request({
url: that.config.getHostUrl() + '/api/user/getUser',
method: 'post',
data: {
openid: openid
},
success: function (res) {
if (res.statusCode == 200) {
if (res.data.isSuccess) {
wx.setStorageSync('user', JSON.stringify(res.data.data));
} else {
// 未注册情况
that.showNoticToTraveler();
}
} else {
// 服务器故障
}
},
fail: function (res) {
// 请求错误
}
})
})
}
},
// 未注册提示
showNoticToTraveler: function(){
wx.showModal({
title: '提示',
content: '您还未注册,为了保障你的良好体验,请在个人中心点击授权注册',
showCancel: false,
})
},
/**
* 结束下拉刷新:当使用了下拉刷新,请求结束时调用
*/
stopRefresh: function(){
//停止刷新
wx.stopPullDownRefresh();
// 隐藏顶部刷新图标
wx.hideNavigationBarLoading();
},
/**
* 获取通知
* type: 1点赞,2评论,0系统通知
* read: 0未读,1已读
*/
getNotices: function(rid, read, type){
if(!rid) return false;
let data = { rid };
if(type==0 || type) data.type = type;
if(read==0 || read) data.read = read;
return new Promise((resolve, reject)=>{
wx.request({
url: this.config.getHostUrl()+'/api/main/getNotice',
data: data,
method: 'POST',
success: (result)=>{
if(result.data.isSuccess){
resolve(result.data.data)
}else{
reject(result.data.msg)
}
},
fail: ()=>{},
complete: ()=>{}
});
})
},
// 更新通知
updateNotices: function({read, type}){
let that = this;
return new Promise((resolved, rejected)=>{
let user = wx.getStorageSync('user');
if(user){
if(user.constructor != Object) user = JSON.parse(user);
let setting = wx.getStorageSync('setting');
if(!setting){
setting = {
power: true,
voice: true,
shake: true,
screen: true,
method: '1'
};
wx.setStorageSync('setting', setting);
}
that.getNotices(user.rid, read, type).then((res)=>{
let moment = 0, system = 0;
let tabbar = that.globalData.status.tabbar;
for(let i=0; i<res.length; i++){
if(res[i].type !=0 && res[i].read==0 ) moment++;
if(res[i].type ==0 && res[i].read==0 ) system++;
}
if(setting.method != '2'){ //非免打扰
if(setting.method == '1'){ //数字提示
tabbar[1] = { dot: false, number: moment}; //动态圈子
tabbar[3] = { dot: false, number: system}; //个人中心
}else if(setting.method == '0'){ //红点
tabbar[1] = { dot: true }; //动态圈子
tabbar[3] = { dot: true }; //动态圈子
}
if(type == 0){
that.setTabbar(3, tabbar[3]); //设置个人中心tabbar数字
}else { //全部设置tabbar数字
that.setTabbar(1, tabbar[1]);
that.setTabbar(3, tabbar[3]);
}
}else{ //免打扰
that.setTabbar(1, { dot: false });
that.setTabbar(3, { dot: false });
}
// that.globalData.status.tabbar.forEach(function(item,index){
// that.setTabbar(index, item)
// })
resolved({moment, system}); //返回数据页面内使用
})
}
})
},
// 阅读消息
doRead(noids) {
if(!noids) return;
if(!(noids instanceof Array)){
noids = [ noids ];
}
return new Promise((resolve, reject)=>{
wx.request({
url: this.config.getHostUrl()+'/api/main/readNotice',
data: { noids },
method: 'POST',
success: (result)=>{
if(result.data.isSuccess){
resolve(result)
}else{
reject(result.data.msg)
}
},
fail: ()=>{},
complete: ()=>{}
});
})
},
// 删除消息
doDelete(noids) {
if(!noids) return;
if(!(noids instanceof Array)){
noids = [ noids ];
}
return new Promise((resolve, reject)=>{
wx.request({
url: this.config.getHostUrl()+'/api/main/delNotice',
data: { noids },
method: 'POST',
success: (result)=>{
if(result.data.isSuccess){
resolve(result)
}else{
reject(result.data.msg)
}
},
fail: ()=>{},
complete: ()=>{}
});
})
},
/**
* 查看他人个人中心
*/
goToUserPage: function(rid){
wx.navigateTo({
url: '/pages/user/userPage/userPage?rid='+rid
});
}
})