mobile.js
16.3 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// var baseURL = 'http://dsc-b.vchangyi.com/party/phpapi';
// var baseURL = 'https://yq.vchangyi.com/party/phpapi';
var baseURL = 'http://party.vchangyi.com/phpapi';
var socketUrl = '//106.54.64.163:3000';
var defaultAvatar = './img/mobile/default_avatar.png';
var toastTimer = null;
var displayTimer = null;
// 页面数据绑定操作
var action = {
isRun : 0, // 是否开始签到
userInfo: null, // 用户信息
mediaId: '',
userAvatar: '', // 用户头像
programList: [], // 节目列表
init: function() {
// 本地获取用户信息,判断跳转页面
var userInfo = localStorage.getItem('userInfo');
if (userInfo) {
this.userInfo = JSON.parse(userInfo);
var userAvatar = localStorage.getItem('userAvatar');
if (userAvatar) {
this.userAvatar = userAvatar;
this.goHome();
} else {
this.goPic();
}
} else {
this.goSignIn();
}
this.wechatJsSign(); // 微信 SDK config
},
// 获取微信jsapi签名
wechatJsSign: function() {
var WECHAT_JS_SIGN_URL = baseURL + '/api.WechatJsSign.php';
var sign = function(data) {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appId, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名
jsApiList: ['chooseImage','uploadImage','getLocalImgData','downloadImage', 'hideAllNonBaseMenuItem'] // 必填,需要使用的JS接口列表
});
wx.ready(function() {
wx.hideAllNonBaseMenuItem(); // 隐藏所有非基础按钮
});
};
baseRquest.get(WECHAT_JS_SIGN_URL, {'url' : location.href}, sign);
},
// 获取年会开关设置
getSetting: function(callback) {
var url = baseURL + '/apicp.GetSetting.php';
baseRquest.get(url, '', callback);
},
// 判断是否可以签到
getSignInState: function() {
var callback = function(res) {
if (res.data.isStopSign == 2) { // 已开启签到
var url = baseURL + '/apicp.SignIn.php';
var realname = $('.sign-input').val().replace( /^\s*/, '');
var data = { realname: realname };
var callback = function(res) {
action.userInfo = res.data;
localStorage.setItem('userInfo', JSON.stringify(action.userInfo));
$('.sign-input').val('');
action.goPic();
};
baseRquest.post(url, data, callback);
} else { // 未开启签到
action.switchDialog('#stop_sign_dialog', true);
}
};
action.getSetting(callback);
},
// 签到
signIn: function() {
var realname = $('.sign-input').val().replace( /^\s*/, '');
if (!realname) {
return;
}
action.getSignInState();
},
// 进入签到页
goSignIn: function() {
$('.sign-in').show();
$('.pic').hide();
$('.home').hide();
$('.vote').hide();
$('.seat').hide();
},
// 进入拍照页
goPic: function() {
$('.pic').show();
$('.sign-in').hide();
$('.home').hide();
$('.vote').hide();
$('.seat').hide();
this.mediaId = '';
},
goHome: function() {
$('.home').show();
$('.sign-in').hide();
$('.pic').hide();
$('.vote').hide();
$('.seat').hide();
if (action.userInfo) {
var userAvatar = action.userAvatar || defaultAvatar;
$('#home_user_avatar').attr('src', userAvatar);
action.loadImage('.user-avatar .avatar-img');
$('#home_user_name').html(action.userInfo.realname);
$('#home_prize_no').html(action.userInfo.prize_no);
$('#home_table_no').html(action.userInfo.table_no);
}
this.initBarrage();
},
goSeat: function() {
$('.seat').show();
$('.sign-in').hide();
$('.pic').hide();
$('.home').hide();
$('.vote').hide();
this.getSeatList();
},
// 打开摄像头,type=1: 唤起摄像头,type=2: 打开相册
openCamera: function(type) {
console.log('type: ', type)
var sourceType = [];
if (type === 1) {
sourceType = ['camera'];
} else if (type === 2) {
sourceType = ['album'];
} else {
sourceType = ['album', 'camera'];
}
console.log('sourceType: ', sourceType);
wx.chooseImage({
count: 1, // 默认9
sizeType: ['compressed'], // 'original', 'compressed'指定是原图还是压缩图,默认都有
sourceType: sourceType, // 'album', 'camera'指定来源是相册还是相机,默认都有
success: function(res) {
var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
wx.uploadImage({
localId: localIds.toString(), // 需要上传的图片的ID,由chooseImage接口获得
isShowProgressTips: 1, // 进度提示
success: function (res) {
$('.pic-avatar').attr('src', localIds);
action.loadImage('.pic-avatar', localIds);
action.mediaId = res.serverId; // 返回图片的服务器端ID,即 mediaId
console.log('action.mediaId: ', action.mediaId);
$('#upload_unavailable').hide();
$('#upload_available').show();
},
fail: function(res) {
alert('图片上传失败,请重试');
}
});
},
cancel: function() {
console.log('cancel chooseImage');
var userAgent = navigator.userAgent.toLowerCase();
if (/android/.test(userAgent)) {
// window.location.reload();
}
}
});
},
loadImage: function(dom, src) {
var width = $(dom).width();
var img = new Image();
img.src = src; // 改变图片的src
if (img.complete) { // 判断是否有缓存
console.log('img complete');
console.log('width: ', width);
console.log('img-width: ' + img.width, 'img-height: ' + img.height);
var ratio = img.width / img.height;
if (ratio > 1) {
img.height = width;
img.width = width * ratio;
var margin = (img.width - width) / 2;
$(dom).css({
'margin-left': -margin + 'px'
});
} else if (ratio < 1) {
img.width = width;
img.height = width / ratio;
var margin = (img.height - width) / 2;
$(dom).css({
'margin-top': -margin + 'px'
});
} else {
img.width = width;
img.height = width;
}
} else {
img.onload = function() {
console.log('img onload');
console.log('width: ', width);
console.log('img-width: ' + img.width, 'img-height: ' + img.height);
var ratio = img.width / img.height;
if (ratio > 1) {
img.height = width;
img.width = width * ratio;
var margin = (img.width - width) / 2;
$(dom).css({
'margin-left': -margin + 'px'
});
} else if (ratio < 1) {
img.width = width;
img.height = width / ratio;
var margin = (img.height - width) / 2;
$(dom).css({
'margin-top': -margin + 'px'
});
} else {
img.width = width;
img.height = width;
}
}
}
},
// 上传头像
uploadImage: function() {
if (!action.mediaId) {
return;
}
var data = {
id: action.userInfo.id,
mediaId : action.mediaId
};
var url = baseURL + '/apicp.UploadHeadImg.php';
var callback = function(data) {
action.userAvatar = data.headimg;
localStorage.setItem('userAvatar', action.userAvatar);
action.goHome();
};
baseRquest.post(url, data, callback);
},
// 获取座位列表
getSeatList: function() {
var url = baseURL + '/apicp.TablePositionList.php';
var callback = function(data) {
var html = template('seatListTmpl', {
list: data.data,
userSeat: action.userInfo.table_no
});
$('.seat-list').html(html);
};
baseRquest.post(url, '', callback);
},
// 判断是否开启投票通道
getVoteState: function() {
var callback = function(res) {
if (res.data.isStartVote == 1) {
action.getProgramList();
} else {
action.switchDialog('#home_dialog', true);
}
};
action.getSetting(callback);
},
// 获取节目列表
getProgramList: function() {
var url = baseURL + '/apicp.ItemList.php';
var callback = function(data) {
var user_vote = data.data.user_vote;
var items = data.data.item;
if (user_vote && user_vote.length > 0) {
action.showToast('你已经投过票了哦');
} else {
$('.vote').show();
$('.home').hide();
items.map(function(item) {
item.checked = false;
});
var html = template('programListTmpl', { list: items });
$('.program-list').html(html);
}
action.programList = items;
};
baseRquest.get(url, {user_id: action.userInfo.id}, callback);
},
// 选择节目
selectProgram: function(index) {
if (this.programList[index].checked) {
$('.program-item').eq(index).removeClass('active');
$('.vote .tip').hide();
} else {
var selectedList = this.programList.filter(item => item.checked);
if (selectedList.length >= 5) {
$('.vote .tip').show();
return;
}
$('.program-item').eq(index).addClass('active');
}
this.programList[index].checked = !this.programList[index].checked;
},
// 点击确认投票
vote: function() {
var selectedList = this.programList.filter(item => item.checked);
console.log('selected: ', selectedList);
if (selectedList.length === 0) {
action.showToast('请先选择节目');
return;
}
this.switchDialog('#vote_dialog', true);
},
// 投票请求
submitVote: function() {
let VOTE_URL = '/apicp.ItemVote.php';
var itemIds = this.programList.filter(item => item.checked).map(item => item.id).join(',');
console.log('itemIds: ', itemIds);
var data = {
user_id: action.userInfo.id,
item_ids: itemIds
};
var callback = function() {
action.showToast('投票成功!');
action.switchDialog('#vote_dialog', false)
setTimeout(function() {
$('.home').show();
$('.vote').hide();
}, 1000);
};
baseRquest.post(VOTE_URL, data, callback);
},
// 发送弹幕
initBarrage: function() {
var socket = io(socketUrl);
$('.send-btn').on('click', function() {
if ($('.barrage-input').val() != '') {
socket.emit('barrage', $('.barrage-input').val());
$('.barrage-input').val('');
$('.send-btn').removeClass('active');
}
});
},
resetPosition: function () {
var currentPosition, timer;
var speed = 1; // 页面滚动距离
timer = setTimeout(function() {
currentPosition = document.documentElement.scrollTop || document.body.scrollTop;
currentPosition -= speed;
window.scrollTo(0, currentPosition); // 页面向上滚动
currentPosition += speed;
window.scrollTo(0, currentPosition); // 页面向下滚动
clearTimeout(timer);
}, 1);
},
// 改变发送按钮状态
toggleBtnState: function() {
if ($('.barrage-input').val()) {
$('.send-btn').addClass('active');
} else {
$('.send-btn').removeClass('active');
}
},
switchDialog: function(dom, isShow) {
if (isShow) {
$(dom).show();
$('body, .home, .vote').css({
height: '100vh',
overflow: 'hidden'
});
} else {
$(dom).hide();
$('body, .home, .vote').css({
'min-height': '100vh',
overflow: 'auto'
});
}
},
// toast提示
showToast: function(content, time = 1000) {
if (toastTimer != null) {
clearTimeout(toastTimer);
clearTimeout(displayTimer);
}
if ($('#toast').get().length == 0) {
$('body').append('<div id="toast" class="toast-tip"></div>');
}
$('#toast').show();
$('#toast').css('opacity', 1);
$('#toast').html(content);
toastTimer = setTimeout(function() {
$('#toast').css('opacity', 0);
displayTimer = setTimeout(
function() {
$('#toast').hide();
}, 1000);
}, time);
},
}
// api接口数据请求,数据交互引用的axios
var baseRquest = {
// get请求
get: function(url, params, callback) {
axios.defaults.baseURL = baseURL;
axios.get(url, {
params : params
}).then(function(response) {
if (response.status == 200) {
data = response.data;
if (!!data) {
// 过滤错误信息,统一抛出
if (data.errcode != 0 && data.errcode != 200) {
alert(data.errmsg);
} else {
callback(data.result);
}
}
} else {
alert('get无法访问接口');
}
}).catch(function(error) {
console.log(error)
});
},
// post请求
post: function(url, params, callback) {
axios.defaults.baseURL = baseURL;
let data = new URLSearchParams();
Object.keys(params).forEach(function(key){
data.append(key, params[key]);
});
axios.post(url, data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
if (response.status == 200) {
data = response.data;
if (!!data) {
// 过滤错误信息,统一抛出
if (data.errcode != 0 && data.errcode != 200) {
if (url.search('SignIn') !== -1) {
action.switchDialog('#sign_in_dialog', true);
} else {
alert(data.errmsg);
}
} else {
callback(data.result);
}
}
} else {
alert('post无法访问接口');
}
}).catch(function(error) {
console.log(error);
});
}
}