-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
93 lines (84 loc) · 2.35 KB
/
App.tsx
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
import React from 'react';
import Turntable from '../lib/turntable';
import './App.css';
const prizeBackgrounds: string[] = [
'#62CDD8', '#FFFFFF', '#FEB446', '#FFFFFF',
'#62CDD8', '#FFFFFF', '#FEB446', '#FFFFFF',
];
const prizes = Array(8).fill(0).map((_, index) => ({
texts: index === 0 ? [
{
text: '谢谢', fontStyle: '13px Arial', fontColor: 'rgba(70, 47, 47, 1)', fromCenter: 0.8,
},
{
text: '参与', fontStyle: '13px Arial', fontColor: 'rgba(70, 47, 47, 1)', fromCenter: 0.68,
},
] : [
{
text: '奖品价值', fontStyle: '13px Arial', fontColor: 'rgba(70, 47, 47, 1)', fromCenter: 0.8,
},
{
text: `${index * 1000}元`, fontStyle: '13px Arial', fontColor: 'rgba(255, 40, 40, 1)', fromCenter: 0.68,
},
],
background: prizeBackgrounds[index],
images: index === 0 ? undefined : [
{
src: '../sample/gift.png',
width: 25,
height: 25,
fromCenter: 0.65,
},
],
}));
function App() {
const canStart = true;
const toast = (msg: string) => {
console.log(msg);
};
const fetchPrizeResult = (abort: () => void) => {
if (!canStart) { // 未达条件不启动抽奖
toast('no times!');
return false;
}
return new Promise<number>((resolve, reject) => {
// setTimeout 模拟接口请求抽奖结果
setTimeout(() => {
const resultPrizeIndex = Math.floor(Math.random() * 8);
if (resultPrizeIndex < 0) { // 未达条件不启动抽奖
reject();
toast('something is wrong!');
} else {
resolve(resultPrizeIndex);
}
}, 90);
});
};
const complete = (index: number) => {
console.log(`恭喜你抽中 - ${[index]} `, prizes[index]);
};
const timeout = () => {
console.log('已超时');
};
const stateChange = (drawing: boolean) => {
console.log(drawing ? '进入抽奖' : '离开抽奖');
};
return (
<div className="turntable">
<Turntable
size={268}
prizes={prizes}
onStart={fetchPrizeResult}
onComplete={complete}
onTimeout={timeout}
onStateChange={stateChange}
>
{/* 转盘指针 点击按钮 */}
<div className="turntable-pointer">
<img className="pointer-img" src={require('./pointer.png')} alt="" />
</div>
</Turntable>
</div>
);
}
export default App;