index.js
12.7 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
import React, {PureComponent} from 'react';
import './index.scss'
import {conf, start} from '../../api'
import yaSDK from 'yoho-activity-sdk';
import cookie from 'react-cookies';
import queryString from '../../../../common/query-stringify'
import Resource from '../components/resource';
import BottomButton from '../components/bottom-button';
import TipModal from '../components/tip-modal';
export default class wheelSurf extends PureComponent {
constructor(props) {
document.title ='大转盘';
super(props);
this.state = {
uid: 0,
epPrize: {},
query: {},
canStart: true, // 可以抽奖
slowEnding: false, // 减速结束
startEnded: false, // 已结束
startType: true,
isLogin: false,
dayLeftTimesTip: '',
conf: {
loading: ''
},
totalParts: '',
remainCount: '',
showTipModal: false,
modalTip: '',
modalBtnText: '',
gotDesc: '',
angle: 0, // 当前旋转角度
speed: 0, // 旋转速度
stopAngle: 0, // 停止的角度
startSlowAngle: 0 // 起始减速的角度
};
this.init();
}
init = async () => {
// 登录状态
this.state.query = yaSDK.getQueryObj() || {};
this.state.uid = +(this.state.query.uid || cookie.load('uid') || 0);
yaSDK.getUid().then(async (uid) => {
if (uid && uid === this.state.uid) {
this.setState({
uid,
isLogin: !!uid
});
switch (yaSDK.env) {
case 'h5':
if (!cookie.load('uid')) {
cookie.save('uid', this.state.uid);
cookie.save('app_session_key', this.state.query.session_key, { path: '/' });
cookie.save('app_client_type', this.state.query.app_client_type, { path: '/' });
cookie.save('app_version', this.state.query.app_version, { path: '/' });
}
break;
case 'miniprogram':
cookie.save('uid', this.state.uid);
cookie.save('app_session_key', this.state.query.session_key, { path: '/' });
cookie.save('app_client_type', this.state.query.client_type, { path: '/' });
cookie.save('app_version', '6.6.0', { path: '/' });
break;
case 'app':
cookie.save('uid', this.state.uid);
cookie.save('app_session_key', this.state.query.session_key, { path: '/' });
cookie.save('app_client_type', this.state.query.client_type, { path: '/' });
cookie.save('app_version', this.state.query.app_version, { path: '/' });
break;
default:
break;
}
} else {
cookie.remove('uid');
cookie.remove('app_session_key');
cookie.remove('app_client_type');
cookie.remove('app_version');
}
let params = {act_id: +this.state.query.actId};
if (this.state.uid) {
params.uid = this.state.uid;
}
let result = await conf(params);
if (result.code === 200) {
if (yaSDK.env === 'miniprogram') {
this.simplifyUrl(result.data.conf);
}
this.setState({
conf: result.data.conf,
epPrize: result.data.emptyPrize,
totalParts: result.data.prize,
remainCount: result.data.residueCount,
dayLeftTimesTip: result.data.conf.btm_tip_one,
startType: result.data.startType !== false,
startErrorMsg: result.data.errorMsg || ''
});
this.setWxShare();
} else {
console.log(result.message)
}
});
};
simplifyUrl = (conf) => {
const spKeys = [
'rule_url',
'jump_btn_left_url',
'jump_btn_middle_url',
'jump_btn_right_url'
];
let url, query;
spKeys.map(key => {
if (!conf[key] || !conf[key].split('?')[1]) {
return;
}
url = conf[key].split('?')[0];
query = queryString.parse(conf[key].split('?')[1]);
delete query['openby:yohobuy'];
conf[key] = `${url}?${queryString.stringify(query)}`
console.log(conf[key])
});
};
setWxShare = () => {
const {conf} = this.state;
yaSDK.wxShare({
title: conf.share_title,
imgUrl: `${conf.share_img}?imageView2/1/w/200/h/200`,
desc: conf.share_desc,
link: conf.share_url
});
};
begin = async () => {
this.state.canStart = false;
this.setState({
remainCount: this.state.remainCount - 1
});
let result;
try {
result = await start({
act_id: +this.state.query.actId,
uid: cookie.load('uid'),
sessionKey: cookie.load('app_session_key'),
sessionType: cookie.load('app_client_type'),
appVersion: cookie.load('app_version')
});
if (result.code !== 200) {
result = {
data: {
type: 1,
prize_idx: this.state.epPrize.prize_idx,
desc: 'oops!'
}
}
}
} catch (e) {
result = {
data: {
type: 1,
prize_idx: this.state.epPrize.prize_idx,
desc: 'oops!'
}
}
}
let timer = setTimeout(() => {
clearTimeout(timer);
this.state.stopAngle = this.calcStopAngle(result.data.prize_idx);
this.state.gotDesc = result.data.type === 1 ? 'oops!' : '恭喜您!中奖了';
this.setState({
slowEnding: true
});
this.state.angle = this.state.stopAngle;
this.stop(this.state.stopAngle);
}, 2000);
};
calcStopAngle = idx => {
return parseInt(360 / this.state.totalParts) * (this.state.totalParts - idx + 1) || 359;
};
start = async () => {
const {isLogin, canStart} = this.state;
if (!isLogin) {
return yaSDK.goLogin();
}
// 抽奖前验证是否可以抽奖
if (!this.state.startType) {
return this.setState({
showTipModal: true,
modalTip: '活动尚未开始',
modalBtnText: '确定'
});
}
// 抽奖前验证是否可以抽奖
if (!this.state.remainCount) {
return this.setState({
showTipModal: true,
modalTip: '您今天的抽奖次数已经用完<br/>请明天再来',
modalBtnText: '确定'
});
}
if (!canStart) {
return;
}
this.begin();
this.rotate();
};
stop = angle => {
let rotateDeg = angle + 360 * 3;
let randCssName = 'stop_' + (new Date()).getTime();
let randCssContent = '{' +
'transform: rotateZ(' + rotateDeg + 'deg) !important;' +
'-webkit-transform: rotateZ(' + rotateDeg + 'deg) !important;' +
'-webkit-transition: transform 4s ease-out;' +
'}';
document.getElementsByTagName('style')[0].innerHTML += ('.' + randCssName + ' ' + randCssContent);
document.getElementById('rotateWheel').className = 'wheel-bg ' + randCssName;
document.getElementById('rotateWheel').style.webkitTransform = 'rotateZ(' + angle + 'deg)';
let timer = setTimeout(() => {
this.setState({
startEnded: true
});
clearTimeout(timer);
}, 4000);
};
rotate = () => {
let {slowEnding, startEnded} = this.state;
if (startEnded) {
return;
}
if (!slowEnding) {
if (this.state.speed < 20) {
this.state.speed += 0.3;
}
this.state.angle += this.state.speed;
if (this.state.angle >= 360) {
this.state.angle -= 360;
}
}
if (!this.state.slowEnding) {
document.getElementById('rotateWheel').style.webkitTransform = 'rotateZ(' + this.state.angle + 'deg)';
requestAnimationFrame(this.rotate)
}
};
links = conf => {
let links = [];
if (conf.jump_btn_left_url) {
links.push({
url: conf.jump_btn_left_url,
bg: conf.jump_btn_left_bg
});
}
if (conf.jump_btn_middle_url) {
links.push({
url: conf.jump_btn_middle_url,
bg: conf.jump_btn_middle_bg
});
}
if (conf.jump_btn_right_url) {
links.push({
url: conf.jump_btn_right_url,
bg: conf.jump_btn_right_bg
});
}
return links;
};
setCanStart = () => {
this.setState({
canStart: true,
slowEnding: false,
startEnded: false,
speed: 0
});
document.getElementById('rotateWheel').className = 'wheel-bg';
};
shareInApp = () => {
const {conf} = this.state;
let shareData = {
title: conf.share_title,
link: conf.share_url,
desc: conf.share_desc,
imgUrl: conf.share_img,
hideType: ['7', '8', '9']
};
yaSDK.invokeMethod({method: 'go.showshareaction', args: shareData});
};
confirm = () => {
this.setState({
showTipModal: false
});
};
jump = e => {
let $el = e.currentTarget;
let url = $el.getAttribute('data-url');
if (!url) {
return;
}
yaSDK.link(e)
};
render() {
let {conf, slowEnding, isLogin, startEnded, remainCount} = this.state;
if (!conf.id) {
return (
<div className="home-wrap">
<img className="main-bg" src={this.state.conf.loading}/>
</div>
)
}
let links = this.links(conf);
return (
<div className="home-wrap">
<img className="main-bg" src={conf.main_bg}/>
{conf.rule_btn_bg ? (<a data-type="other" data-url={conf.rule_url} onClick={this.jump}>
<img className="rule-btn" src={conf.rule_btn_bg}/>
</a>) : ''}
{conf.share_btn_bg && yaSDK.env === 'app' ? (<img onClick={this.shareInApp} className="share-btn" src={conf.share_btn_bg}/>): ''}
<img className="wheel-bg" id="rotateWheel" src={conf.wheel_bg}/>
<img onClick={()=>{this.start()}} className="start-btn-bg" src={conf.prize_btn_bg}/>
{
isLogin ?
(<div className="tips">
<p className="tip-1">{`今日剩余次数:${remainCount}次`}</p>
</div>) : ''
}
<BottomButton links={links} login={isLogin}/>
<Resource code={conf.bottomContentCode}/>
{slowEnding ? (<img className="prize-hit-bg transition" src={conf.win_prize_bg}/>) : ''}
{startEnded ? (
<div className="got-tip">
<div className="prize-tip">{this.state.gotDesc}</div>
<span onClick={this.setCanStart} className="try-again">确定</span>
</div>
) : ''}
{slowEnding ? (<img className="prize-hit-start-bg" src={conf.prize_btn_bg}/>) : ''}
{
this.state.showTipModal ? <TipModal tip={this.state.modalTip} btnText={this.state.modalBtnText} confirm={() => {this.confirm()}} /> : ''
}
</div>
)
}
}