authorize.js
1.88 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
let app = getApp();
let config = require('../../config');
Page({
data: {
authorized: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onShareAppMessage() {},
onReady() {
wx.showLoading();
this.autoAuth();
},
onLoad(options) {},
onShow() {},
bindGetUserInfo(e) {
wx.showLoading();
this.getUserInfo(e.detail);
},
getUserInfo(e) {
let _this = this;
app.globalData.userInfo = e.userInfo;
app.post({
url: app.api.register,
sid: false,
data: {
encryptedData: e.encryptedData,
iv: e.iv,
code: app.globalData.wxcode,
tlMemberCode: app.globalData.tlMemberCode
}
}).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) => {
if (result.isNeedAuth == 1) {
console.log("result.isNeedAuth:",result.isNeedAuth);
// 未授权
wx.login({
success: function (res) {
app.store.setItem("wxcode", res.code);
}
});
wx.hideLoading();
_this.setData({
authorized: false
})
} else {
console.log("result.isNeedAuth:",result.isNeedAuth);
// 已经授权
// setTimeout(() => {
_this.toIndex();
wx.hideLoading();
// }, 2000);
}
});
},
// 跳转到首页
toIndex() {
console.log("toIndex");
app.router.push({
path: "index",
openType: "redirect"
})
wx.hideLoading();
}
})