This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
200 lines (184 loc) · 4.93 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
wx.showShareMenu({
withShareTicket: true
})
//app.js
App({
_login: function () {
console.log("正在前往登录页面")
wx.reLaunch({
url: '/pages/login/index'
})
},
_getUserInfo: function () {
// console.log("_getUserInfo()")
let that = this;
wx.login({
success: res => {
wx.request({
url: that.globalData.apiUrl + "/weixin/get_open_id",
data: {
from: "_getUserInfo",
code: res.code
},
success: function (r) {
if (r.data.data.openid) {
that.globalData.open_id = r.data.data.openid;
that.globalData.session_key = r.data.data.session_key;
}
},
fail: function (res) {
wx.hideLoading();
wx.showModal({
showCancel: false,
title: 'get_openid错误',
content: res.errMsg
})
},
complete: function () {
wx.hideLoading();
}
});
// 发送 res.code 到后台换取 openId, sessionKey, unionId
},
fail: function (res) {
wx.hideLoading();
wx.showModal({
showCancel: false,
title: '微信login错误',
content: res.errMsg
})
},
complete: function () {
wx.hideLoading();
}
});
},
_get: function (url, data, success, error, showloading) {
console.log("[get]请求"+url);
if (showloading) {
wx.showLoading();
}
if (!this.globalData.open_id || this.globalData.open_id == '') {
this._getUserInfo();
}
if (!this.globalData.open_id || this.globalData.open_id == '') {
wx.showModal({
content: '很抱歉,无法获取open_id……',
showCancel: false
})
return false;
}
console.error("get--------------" + this.globalData.open_id); //null
console.log("正在请求" + this.globalData.apiUrl + url + ",参数为", data)
wx.request({
url: this.globalData.apiUrl + url,
data: data,
success: function (res) {
if (res.statusCode == 200) {
success && success(res.data);
return false;
}
if (res.statusCode == 404) {
return false;
}
if (res.statusCode !== 0) {
wx.showModal({
showCancel: false,
content: res.errMsg
})
return false;
}
},
fail: function (res) {
console.log('in fail' + res.errMsg)
wx.showModal({
showCancel: false,
content: "网络异常,请重试"
})
error && error(res);
},
complete: function () {
if (showloading) {
wx.hideLoading();
}
}
});
},
_post: function (url, data, success, error) {
wx.showLoading();
if (!this.globalData.open_id || this.globalData.open_id == '') {
this._getUserInfo();
}
wx.request({
url: this.globalData.apiUrl + url,
data: data,
method: "POST",
success: function (res) {
if (res.statusCode == 200) {
success && success(res.data);
return false;
}
if (res.statusCode !== 0) {
wx.showModal({
showCancel: false,
content: res.data.info
})
return false;
}
success && success(res);
},
fail: function (res) {
error && error(res);
},
complete: function () {
wx.hideLoading();
}
});
},
onLaunch: function () {
wx.showLoading();
console.log("---------onLaunch开始执行---------")
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
if (wx.getStorageSync('open_id') != "") {
this.globalData.open_id = wx.getStorageSync('open_id')
}
this._getUserInfo();
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
this.globalData.encryptedData = res.encryptedData;
this.globalData.iv = res.iv;
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
} else {
this._login()
}
}
})
},
globalData: {
apiUrl: "https://weather.crazyphper.com/api",
// apiUrl: "http://127.0.0.1:8000/api",
userInfo: null,
open_id: null,
session_key: null,
unionId: null,
encryptedData: null,
iv: null,
binding: false,
defaultListImg: '../../common/assets/icons/haoyunbuzai.jpeg',
netStatus: true,
},
})