forked from labirin/pickabook-wechatminiprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (110 loc) · 3 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
//app.js
App({
onLaunch: function () {
this.getUserInfo()
},
getUserInfo:function(cb){
var that = this
//调用登录接口
wx.login({
success: res => {
console.log('the code is ', res.code)
this.globalData.code = res.code
wx.request({
url:'https://api.pickabook.xyz/api/v1/users/auth',
// url:'http://202.182.124.93/api/v1/users/auth',
// url: 'http://localhost:3002/api/v1/users/auth',
method:'POST',
data:{
code: this.globalData.code
},
success:function(res){
// that.globalData.token=res.data.token
wx.setStorage({
key: 'token',
data: res.data.token,
success: function(res){
console.log('successfully stored token')
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
var token=wx.getStorageSync('token')
console.log('login token new---',token)
wx.request({
url: 'https://api.pickabook.xyz/api/v1/users/login',
// url:'http://202.182.124.93/api/v1/users/login',
// url: 'http://localhost:3002/api/v1/users/login',
method:'POST',
header:{
'content-Type':'application/json'
},
data:{
wechat:{
openid:token
}
},
success:function(res)
{
console.log(res.data)
var mytoken = res.data.token
if(res.data.code===0)
{
wx.setStorage({
key: 'auth',
data: res.data.token,
success: function(res){
// success
console.log('auth stored')
},
fail: function() {
// fail
console.log('could not retrive auth')
},
complete: function() {
// complete
}
})
}
}
})
},
fail: function() {
console.log('could not store the token')
},
complete: function() {
// complete
}
})
console.log('token in storage---',wx.getStorageSync('token'))
// that.login_in()
return
}
})
}
})
},
login_in:function(){
var that=this
console.log('login called')
var token=wx.getStorageSync('token')
console.log('token in login---',token)
console.log('token sending for login ',token)
},
globalData:{
userInfo: null,
version: '1.0', //app version
mytoken:{}
},
//my utils
showLoadToast:function(title,duration){
wx.showToast({
title: title || 'Loading...',
icon:'loading',
mask:true,
duration:duration || 10000
})
}
})