-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
200 lines (197 loc) · 6.21 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
//app.js
const serverUrl = 'https://werehunter.com/mini-goals';
// const serverUrl = 'https://aliyun.alumik.cn:5180/mini-goals';
// const serverUrl = 'http://localhost';
App({
globalData: {
serverUrl: serverUrl,
userInfo: {
id: 0
},
userId: 2,
taskAddTaskListUrl: serverUrl + '/api/task/add-task-list',
taskGetTaskListUrl: serverUrl + '/api/task/get-task-list',
taskAddTaskUrl: serverUrl + '/api/task/add-task',
taskCompleteTaskUrl: serverUrl + '/api/task/complete-task',
taskRemoveTaskUrl: serverUrl + '/api/task/remove-task',
taskRemoveTaskListUrl: serverUrl + '/api/task/remove-task-list',
taskTaskListUpUrl: serverUrl + '/api/task/task-list-up',
taskTaskUpUrl: serverUrl + '/api/task/task-up',
},
onLaunch: function() {
var that = this
// 展示本地存储能力
// var logs = wx.getStorageSync('logs') || []
// logs.unshift(Date.now())
// wx.setStorageSync('logs', logs)
wx.getStorage({
key: 'userInfo',
success(res) {
console.log('获取缓存用户为👇')
console.log(res.data)
that.globalData.userInfo = res.data
},
fail(res) {
console.log('未缓存用户')
}
})
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
if (res.code) {
// 发起网络请求
wx.request({
url: getApp().globalData.serverUrl + '/api/user/login',
data: {
code: res.code
},
success(res) {
console.log(res.data)
that.globalData.userInfo.id = res.data
// console.log('用户id为' + that.globalData.userInfo.id)
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
console.log('成功授权')
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
res.userInfo.id = that.globalData.userInfo.id
// if (JSON.stringify(that.globalData.userInfo)
// != JSON.stringify(res.userInfo)) {
that.globalData.userInfo = res.userInfo
wx.setStorage({
key: 'userInfo',
data: that.globalData.userInfo,
success(res) {
console.log('成功缓存👇')
console.log(that.globalData.userInfo)
}
})
wx.request({
url: getApp().globalData.serverUrl + '/api/user/update',
data: {
openid: that.globalData.userInfo.id,
name: that.globalData.userInfo.nickName,
avatar: that.globalData.userInfo.avatarUrl
},
success(res) {
console.log(res.data)
}
})
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res)
}
}
})
}
},
fail(res) {
console.log(res.data);
}
})
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
},
requestAsync: function(url, data, successFunc, that) {
wx.request({
url: url,
data: data,
method: 'POST',
success: function(res) {
console.log(res);
if (res.statusCode == 200 && res.data.success) {
successFunc(res.data.data, that);
} else {
wx.showToast({
title: '请求失败',
icon: 'none'
});
}
},
fail: function() {
wx.showToast({
title: '服务器错误',
icon: 'none'
})
}
});
},
requestSync: function(url, data, successFunc, that) {
wx.showLoading({
title: '加载中',
mask: true
});
wx.request({
url: url,
data: data,
method: 'POST',
success: function(res) {
wx.hideLoading();
// console.log(res);
if (res.statusCode == 200 && res.data.success) {
successFunc(res.data.data, that);
} else {
wx.showToast({
title: '请求失败',
icon: 'none'
});
}
},
fail: function() {
wx.hideLoading();
wx.showToast({
title: '服务器错误',
icon: 'none'
})
}
});
},
jsonToObject: function(jsonString) {
return JSON.parse(jsonString);
},
jsonArrayToObjectArray: function(jsonArray) {
let arr = [];
for (let i = 0; i < jsonArray.length; i++) {
let o = this.jsonToObject(jsonArray[i]);
arr = arr.concat(o);
}
return arr;
},
request: function(url, data, successFunc, that) {
wx.request({
url: url,
header: {
"Content-Type": 'application/x-www-form-urlencoded'
},
data: data,
method: 'POST',
success: function(res) {
if (res.statusCode == 200) {
successFunc(res.data, that);
} else {
wx.showToast({
title: '请求失败',
icon: 'none'
})
}
},
fail: function() {
wx.showToast({
title: '服务器错误',
icon: 'none'
})
}
});
}
})