-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (68 loc) · 2.02 KB
/
app.js
File metadata and controls
70 lines (68 loc) · 2.02 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
//app.js
App({
// 全局变量
globalData: {
userInfo: null,
baseurl: 'http://localhost:8000/',
code: "",
hasAuthKey: false,
openid: '',
userId: '',
},
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
},
userAuth: function () {
let that = this
return new Promise((resolve, reject) => {
// 调用登录接口
wx.login({
success: (res) => {
if (res.code) {
console.log('用户登录授权code为:' + res.code)
// 传递code凭证换取用户openid,并获取后台用户信息
wx.request({
url: this.globalData.baseurl + 'user/wxLogin',
data: {
code: res.code // 后台请求凭证
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: (res) => {
console.log('app.js-response:')
console.log(res)
// if (res.statusCode == 200) {
// // 获取用户信息成功
// that.globalData.openid = res.data.openid
// // 存入session缓存
// wx.setStorageSync('openid', that.globalData.openid)
// console.log('存入缓存')
// console.log('openid=' + that.globalData.openid)
// // promise机制放回成功数据
// resolve(res.data)
// } else {
// reject('error')
// }
},
fail: (res) => {
reject(res)
wx.showToast({
title: '系统错误',
icon: 'none',
duration: 2000
})
}
})
} else {
reject('error')
}
},
})
})
}
})