openIndex.js
2.0 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
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
},
getOpenId() {
let that = this
wx.login({
success: (res) => {
let url = 'wxapp/public/getSessionKey'
let param = {
code: res.code,
}
app.post(url, param).then((res) => {
console.log(res)
that.setData({
openid: res.data.openid,
session_key: res.data.session_key
})
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo'] === true) {
wx.getUserInfo({
success: reg => {
that.setData({
encrypted_data: reg.encryptedData,
iv: reg.iv
})
that.login()
}
})
}
}
})
}).catch((errMsg) => {
})
}
});
},
// 获取用户权限信息
getUserInfo(e) {
console.log(e)
let that = this;
if (e.detail.errMsg == "getUserInfo:ok") {
that.setData({
encrypted_data: e.detail.encryptedData,
iv: e.detail.iv
})
that.login()
}
},
// 登录
login() {
let that = this;
let url = "wxapp/public/login";
let data = {
openid: that.data.openid,
session_key: that.data.session_key,
encrypted_data: that.data.encrypted_data,
iv: that.data.iv,
}
app.post(url, data).then((res) => {
wx.setStorageSync("token", res.data.token);
wx.showLoading({
title: '正在进入',
})
let t = setInterval(function () {
clearInterval(t)
wx.switchTab({
url: '/pages/index/index',
})
wx.hideLoading()
}, 1500)
}).catch((err) => {
})
// }).catch((errMsg) => { })
},
// 111
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getOpenId();
},
})