authorize.js
1.58 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
let app = getApp();
let config = require('../../config');
Page({
data: {
authorized: true,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad(options) {},
onReady() {
wx.showLoading();
this.autoAuth();
},
bindGetUserInfo(e) {
wx.showLoading();
this.getUserInfo(e.detail);
},
// 点openType==userInfo 手动登陆
getUserInfo(e) {
let _this = this;
// 直接就能拿到 userInfo
app.globalData.userInfo = e.userInfo;
wx.login({
success: function (res) {
app.post({
url: app.api.decrypt,
data: {
encryptedData: e.encryptedData,
iv: e.iv,
code: res.code
}
}).then((res2) => {
_this.autoAuth();
}).catch((err) => {})
}
});
},
// 刷新首页数据
refreshIndexData() {
return new Promise((resolve, reject) => {
app.post({
url: app.api.index,
data: {}
}).then((res) => {
app.globalData.indexInfo = res;
resolve(res)
}).catch((err) => {
console.log("index err:", err);
});
});
},
// 判断结果去首页
autoAuth() {
// 调用自动登陆方法
let _this = this;
this.refreshIndexData().then((result) => {
let indexInfo = app.globalData.indexInfo;
// 自动登陆成功
let loginSuc = true;
if (loginSuc) {
// 登陆成功跳转到首页
setTimeout(() => {
app.router.push({
path: 'index',
query: {},
duration: 0,
openType: "redirect"
})
wx.hideLoading();
}, 3000);
} else {
// 登陆失败,隐藏loading自动登陆
wx.hideLoading();
this.setData({
authorized: false
})
}
});
}
})