-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
228 lines (201 loc) · 5.84 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//app.js
var message = require('component/message/message.js')
var api = require('comm/script/fetch.js')
var config = require('comm/script/config.js')
App({
api: api,
message: message,
config: config,
globalData: {
userInfo: null
},
onLaunch: function () {
//初始化缓存
this.initStorage()
},
onShow: function (options) {
console.log('onShow')
console.log(options)
this.init()
},
init: function () {
// 获取用户信息
this.getUserInfo()
},
auth: function (cb) {
// var sessionId = wx.getStorageSync('thirdSessionId')
// if (sessionId){
// typeof cb == "function" && cb(sessionId)
// return;
// }
wx.login({
success: function (res) {
//调用登录接口,获取 code
api.auth(res.code, function (sessionId) {
if (sessionId) {
typeof cb == "function" && cb(sessionId)
}
})
}
})
},
refreshUser: function (cb) {
var that = this
this.auth(function (sessionId) {
wx.getUserInfo({
success: function (userRes) {
wx.setStorage({
key: 'userInfo',
data: userRes.userInfo
})
that.globalData.userInfo = userRes.userInfo;
api.login(sessionId, userRes.encryptedData, userRes.iv, function (data) {
var personInfo = {}
personInfo.nickName = userRes.userInfo.nickName
personInfo.avatarUrl = userRes.userInfo.avatarUrl
if (data.errcode == 1) {
personInfo.account = data.data.account
personInfo.money = data.data.money
personInfo.isPhone = data.data.isPhone
personInfo.id = data.data.userId
personInfo.name = data.data.name
personInfo.imgUrl = data.data.imgUrl
personInfo.phone = data.data.phone
personInfo.email = data.data.email
that.setPersonInfo(personInfo)
typeof cb == "function" && cb(userRes.userInfo)
}
})
}, fail: function () {
that.getUserInfoFail(function () {
that.refreshUser(function (personInfo) {
typeof cb == "function" && cb(personInfo)
})
})
}
})
})
},
getUserInfo: function (cb) {
var that = this
wx.getStorage({
key: 'userInfo',
success: function (res) {
that.globalData.userInfo = res.data;
}
})
this.auth(function (sessionId) {
that.getPersonInfo(function (personInfo) {
console.log('getUserInfo-auth-getPersonInfo')
console.log(personInfo)
if (personInfo && personInfo.id > 0) {
if (that.loginCallback) {
that.loginCallback()
return
}
}
wx.getUserInfo({
success: function (userRes) {
wx.setStorage({
key: 'userInfo',
data: userRes.userInfo
})
that.globalData.userInfo = userRes.userInfo;
api.login(sessionId, userRes.encryptedData, userRes.iv, function (data) {
console.log('api.login')
if (!personInfo)
personInfo = {}
personInfo.nickName = userRes.userInfo.nickName
personInfo.avatarUrl = userRes.userInfo.avatarUrl
if (data.errcode == 1) {
personInfo.account = data.data.account
personInfo.money = data.data.money
personInfo.isPhone = data.data.isPhone
personInfo.id = data.data.userId
personInfo.name = data.data.name
personInfo.imgUrl = data.data.imgUrl
personInfo.phone = data.data.phone
personInfo.email = data.data.email
that.setPersonInfo(personInfo)
typeof cb == "function" && cb(userRes.userInfo)
console.log('loginCallback')
if (that.loginCallback) {
that.loginCallback()
}
if (that.dataCallback) {
that.dataCallback()
}
}
})
}, fail: function () {
that.getUserInfoFail(function () {
that.getUserInfo()
})
}
})
})
})
},
initStorage: function () {
wx.getStorageInfo({
success: function (res) {
// 个人信息默认数据
var personInfo = {
id: 0,
name: '',
nickName: '',
age: '',
birthday: '',
phone: '',
email: '',
intro: ''
}
// 判断个人信息是否存在,没有则创建
if (!('person_info' in res.keys)) {
wx.setStorage({
key: 'person_info',
data: personInfo
})
}
}
})
},
getPersonInfo: function (cb) {
var personInfo = wx.getStorage({
key: 'person_info',
complete: function (res) {
typeof cb == "function" && cb(res.data)
}
})
},
setPersonInfo: function (personInfo, cb) {
wx.setStorage({
key: 'person_info',
data: personInfo
})
},
getUserInfoFail: function (cb) {
var that = this
wx.navigateTo({
url: '/pages/wxauth/wxauth',
})
return
wx.showModal({
title: '警告',
content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
success: function (res) {
if (res.confirm) {
wx.openSetting({
success: (res) => {
typeof cb == "function" && cb()
that.getUserInfo()
// if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
// }
}, fail: function (res) {
}
})
}
}
})
}
})