-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
307 lines (301 loc) · 8.58 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//app.js
App({
onLaunch: function () {
let that = this;
let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
console.log(extConfig);
if (extConfig.api_url && extConfig.appid) {
that.config.url = extConfig.api_url;
that.config.extAppid = extConfig.appid;
that.config.app_type = extConfig.app_type;
} else {
wx.showModal({
title: '警告',
content: '第三方数据异常',
})
}
//获取设备信息
this.getSystemInfo();
this.getSessionKey();
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo;
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
config: {
platform: 'wxxcx',
url: 'https://www.ruoyw.cn/api/new_mall/shop4/public/api/data/'
},
globalData: {
userInfo: null,
session_key: null
},
systemInfo:{
statusHeight:20,
system:'ios',
navHeight:44,
headerHeight:64,
winWidth:null,
winHeight:null
},
//获取设备信息
getSystemInfo:function(){
var that = this;
wx.getSystemInfo({
success: function(res) {
that.systemInfo.statusHeight = res.statusBarHeight;
that.systemInfo.winWidth = res.windowWidth;
that.systemInfo.winHeight = res.windowHeight;
that.systemInfo.system = res.platform.toLowerCase();
if (res.platform.toLowerCase() == "ios" || res.platform.toLowerCase() == "devtools"){
that.systemInfo.navHeight = 44;
that.systemInfo.headerHeight = 44 + res.statusBarHeight;
}else{
that.systemInfo.navHeight = 48;
that.systemInfo.headerHeight = 48 + res.statusBarHeight;
}
},
})
},
bezier: function (pots, amount, position) {
var pot;
var lines;
var ret = [];
var points;
for (var i = 0; i <= amount; i++) {
points = pots.slice(0);
lines = [];
while (pot = points.shift()) {
if (points.length) {
lines.push(pointLine([pot, points[0]], i / amount));
} else if (lines.length > 1) {
points = lines;
lines = [];
} else {
break;
}
}
ret.push(lines[0]);
}
function pointLine(points, rate) {
var pointA, pointB, pointDistance, xDistance, yDistance, tan, radian, tmpPointDistance;
var ret = [];
pointA = points[0];//点击
pointB = points[1];//中间
if (position == 'L'){
xDistance = pointB.x - pointA.x;
}else{
xDistance = -(pointB.x - pointA.x);
}
yDistance = pointB.y - pointA.y;
pointDistance = Math.pow(Math.pow(xDistance, 2) + Math.pow(yDistance, 2), 1 / 2);
tan = yDistance / xDistance;
radian = Math.atan(tan);
tmpPointDistance = pointDistance * rate;
if (position == 'L'){
ret = {
x: pointA.x + tmpPointDistance * Math.cos(radian),
y: pointA.y + tmpPointDistance * Math.sin(radian)
};
}else{
ret = {
x: pointA.x - tmpPointDistance * Math.cos(radian),
y: pointA.y + tmpPointDistance * Math.sin(radian)
};
}
return ret;
}
return {
'bezier_points': ret
};
},
POST: function (o) {
var self = this;
if (!o.fail || (typeof o.fail !== 'function')) {
o.fail = function (err) {
console.log(err);
wx.hideLoading();
wx.showToast({
title: '网络异常',
icon: 'none',
duration: 2000
})
}
}
wx.request({
url: o.url,
method: 'POST',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
data: o.data,
success: o.success,
fail: o.fail,
complete: o.complete
})
},
//升级版本
updateVersion: function () {
// 版本升级提醒
if (wx.getUpdateManager) {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log(res.hasUpdate)
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
})
} else {
// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
},
//获取session_key
getSessionKey: function () {
var that = this;
wx.showLoading({
title: '正在登录',
mask: true
})
wx.login({
success: function (res) {
that.POST({
url: that.config.url,
data: {
platform: that.config.platform,
request: 'public.weixin.user.openid.from.code.get',
code: res.code,
// userInfo: JSON.stringify(that.globalData.userInfo)
},
success: function (ret) {
// console.log(ret);
wx.hideLoading();
if (ret.data.code == 0) {
that.globalData.session_key = ret.data.session_key;
that.getUserData();
//获取session_key的回调,避免page身份信息获取不到
if (that.getSessionKeyCallback) {
that.getSessionKeyCallback(ret)
}
if (that.getSessionKeyCallbackIndex){
that.getSessionKeyCallbackIndex(ret)
}
} else {
wx.showToast({
title: ret.data.msg,
icon: 'none',
duration: 2000
})
}
},
fail: function () {
wx.hideLoading();
wx.showToast({
title: '获取登录信息失败',
icon: 'none',
duration: 2000
})
}
})
}
})
},
//同步用户信息
updateUserInfo: function (userInfo){
var that = this;
this.POST({
url: that.config.url,
data: {
platform: that.config.platform,
request: 'public.weixin.user.openid.from.code.get',
token: that.globalData.session_key,
userInfo: JSON.stringify(userInfo)
},
success: function (ret) {
// console.log(ret);
if (ret.data.code == 0) {
if (that.updateUserInfoCallback) {
that.updateUserInfoCallback(ret)
}
} else if(ret.data.code >= 999){
that.getSessionKeyCallback = function(res){
that.updateUserInfo(userInfo);
}
that.getSessionKey();
}else {
wx.showToast({
title: ret.data.msg,
icon: 'none',
duration: 2000
})
}
}
})
},
//获取用户信息
getUserData: function () {
var that = this;
this.POST({
url: that.config.url,
data: {
platform: that.config.platform,
request: 'private.weixin.user.info.get',
token: that.globalData.session_key
},
success: function (res) {
// console.log('getUserData',res);
if (res.data.code == 0) {
that.globalData.userData = res.data.data;
//获取用户信息回调,避免page的异步造成获取数据失败的问题
if (that.getUserDataCallback) {
that.getUserDataCallback(res);
}
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
})
}
},
fail: function () {
wx.showToast({
title: '获取用户信息失败',
icon: 'none',
duration: 2000
})
}
})
}
})