|
|
import React, {PureComponent} from 'react';
|
|
|
import './index.scss'
|
|
|
import {conf, start} from '../../api'
|
|
|
import {getQueryObj, linkToMiniApp, invokeMethod, parseUrl} from '../../../../common/utils';
|
|
|
import wxshare from '../../../../common/wxshare'
|
|
|
import wx from 'weixin-js-sdk';
|
|
|
import yaSDK from 'yoho-activity-sdk';
|
|
|
import config from '../../config';
|
|
|
import cookie from 'react-cookies';
|
...
|
...
|
@@ -12,100 +9,296 @@ import { |
|
|
Link
|
|
|
} from 'react-router-dom'
|
|
|
|
|
|
import BottomButtons from '../components/bottom-buttons';
|
|
|
|
|
|
const {actId} = getQueryObj();
|
|
|
import Resource from '../components/resource';
|
|
|
import BottomButton from '../components/bottom-button';
|
|
|
|
|
|
export default class wheelSurf extends PureComponent {
|
|
|
constructor(props) {
|
|
|
document.title ='大转盘';
|
|
|
super(props);
|
|
|
this.state = {
|
|
|
initial: true,
|
|
|
uid: 0,
|
|
|
query: {},
|
|
|
canStart: true, // 可以抽奖
|
|
|
pending: false, // 抽奖中
|
|
|
startEnd: false,
|
|
|
isEnding: false, // 即将停止
|
|
|
slowEnding: false, // 减速结束
|
|
|
startEnded: false, // 已结束
|
|
|
|
|
|
tryAgainTip: '再来一次',
|
|
|
|
|
|
isLogin: false,
|
|
|
|
|
|
conf: {
|
|
|
loading: ''
|
|
|
}
|
|
|
},
|
|
|
totalParts: '',
|
|
|
remainCount: '',
|
|
|
|
|
|
angle: 0, // 当前旋转角度
|
|
|
speed: 0, // 旋转速度
|
|
|
stopAngle: 0, // 停止的角度
|
|
|
startSlowAngle: 0 // 起始减速的角度
|
|
|
};
|
|
|
|
|
|
this.init();
|
|
|
}
|
|
|
|
|
|
init = async () => {
|
|
|
let uid = cookie.load('app_uid') || getQueryObj().uid || 0;
|
|
|
if(!uid || !parseInt(uid)){
|
|
|
|
|
|
// 登录状态
|
|
|
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('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('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('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('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) {
|
|
|
this.setState({
|
|
|
conf: result.data.conf,
|
|
|
totalParts: result.data.prize,
|
|
|
remainCount: result.data.residueCount,
|
|
|
})
|
|
|
} else {
|
|
|
console.log(result.message)
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
begin = async () => {
|
|
|
// 抽奖前验证是否可以抽奖
|
|
|
if (!this.state.remainCount) {
|
|
|
console.log('抽奖机会已经用完了');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
let result = await conf({
|
|
|
actId,
|
|
|
uid: uid,
|
|
|
sessionKey: cookie.load('app_session_key') || getQueryObj().session_key || '',
|
|
|
sessionType: cookie.load('app_client_type') || '',
|
|
|
appVersion: cookie.load('app_version') || ''
|
|
|
console.log('开始抽奖');
|
|
|
this.state.canStart = false;
|
|
|
|
|
|
let 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) {
|
|
|
this.setState({
|
|
|
conf: result.data
|
|
|
})
|
|
|
if (result.code !== 200) {
|
|
|
console.log(result.message);
|
|
|
}
|
|
|
|
|
|
let timer = setTimeout(() => {
|
|
|
clearTimeout(timer);
|
|
|
this.state.stopAngle = this.calcStopAngle(result.data.prize_idx);
|
|
|
this.setState({
|
|
|
slowEnding: true
|
|
|
});
|
|
|
}, 2000);
|
|
|
};
|
|
|
|
|
|
componentDidMount = () => {
|
|
|
window.addEventListener('scroll', this.handleScroll);
|
|
|
calcStopAngle = idx => {
|
|
|
return parseInt(360 / this.state.totalParts) * (this.state.totalParts - idx + 1) || 359;
|
|
|
};
|
|
|
|
|
|
componentWillUnmount = () => {
|
|
|
window.removeEventListener('scroll', this.handleScroll);
|
|
|
};
|
|
|
|
|
|
start = async () => {
|
|
|
console.log('抽奖开始');
|
|
|
let uid = cookie.load('app_uid') || getQueryObj().uid || 0;
|
|
|
if(!uid || !parseInt(uid)){
|
|
|
const {isLogin, canStart} = this.state;
|
|
|
|
|
|
if (!isLogin) {
|
|
|
return yaSDK.goLogin();
|
|
|
}
|
|
|
|
|
|
if (!canStart) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
this.begin();
|
|
|
this.rotate();
|
|
|
};
|
|
|
|
|
|
let result = await start({
|
|
|
actId,
|
|
|
uid: uid,
|
|
|
sessionKey: cookie.load('app_session_key') || getQueryObj().session_key || '',
|
|
|
sessionType: cookie.load('app_client_type') || '',
|
|
|
appVersion: cookie.load('app_version') || ''
|
|
|
});
|
|
|
rotate = () => {
|
|
|
let {slowEnding, startEnded, remainCount} = this.state;
|
|
|
|
|
|
if (result.code === 200) {
|
|
|
if (startEnded || !remainCount) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (!slowEnding) {
|
|
|
this.setState({
|
|
|
conf: result.data
|
|
|
})
|
|
|
pending: true
|
|
|
});
|
|
|
|
|
|
if (this.state.speed < 20) {
|
|
|
this.state.speed += 0.2;
|
|
|
}
|
|
|
|
|
|
this.state.angle += this.state.speed;
|
|
|
if (this.state.angle >= 360) {
|
|
|
this.state.angle -= 360;
|
|
|
}
|
|
|
} else {
|
|
|
this.state.angle = parseInt(this.state.angle);
|
|
|
|
|
|
if (this.state.angle >= 360 ) {
|
|
|
this.state.angle -= 360;
|
|
|
}
|
|
|
if (!this.state.setStopAngle) {
|
|
|
this.state.angle += 3;
|
|
|
|
|
|
if (this.state.angle >= 359) {
|
|
|
this.state.angle = 0;
|
|
|
this.state.setStopAngle = true;
|
|
|
}
|
|
|
}
|
|
|
if (this.state.setStopAngle) {
|
|
|
let diff = this.state.stopAngle - this.state.angle;
|
|
|
|
|
|
switch (true) {
|
|
|
case (diff > 200):
|
|
|
this.state.angle += 4;
|
|
|
break;
|
|
|
case (diff > 100):
|
|
|
this.state.angle += 2;
|
|
|
break;
|
|
|
default:
|
|
|
this.state.angle += 1;
|
|
|
}
|
|
|
|
|
|
if (this.state.angle >= this.state.stopAngle) {
|
|
|
this.state.setStopAngle = false;
|
|
|
|
|
|
this.setState({
|
|
|
startEnded: true
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!this.state.startEnded) {
|
|
|
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
|
|
|
});
|
|
|
};
|
|
|
|
|
|
render() {
|
|
|
const {conf, isEnding} = this.state;
|
|
|
const {conf, slowEnding, isLogin, startEnded, remainCount, tryAgainTip} = this.state;
|
|
|
|
|
|
if (!conf.id) {
|
|
|
return (
|
|
|
<div className="home-wrap">
|
|
|
<img className="main-bg" src={this.state.conf.loading + '?imageslim'}/>
|
|
|
<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 + '?imageslim'}/>
|
|
|
{conf.rule_btn_bg ? (<Link to={`${config.routerPath}/rule.html`}><img className="rule-btn" src={conf.rule_btn_bg + '?imageslim'}/></Link>) : ''}
|
|
|
{conf.share_btn_bg ? (<img className="share-btn" src={conf.share_btn_bg + '?imageslim'}/>): ''}
|
|
|
<img className="wheel-bg" src={conf.wheel_bg + '?imageslim'}/>
|
|
|
<img className="wheel-bg-surf-layer" src={conf.wheel_bg}/>
|
|
|
<img className="main-bg" src={conf.main_bg}/>
|
|
|
{conf.rule_btn_bg ? (<Link to={`${config.routerPath}/rule.html`}><img className="rule-btn" src={conf.rule_btn_bg}/></Link>) : ''}
|
|
|
{conf.share_btn_bg ? (<img 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}/>
|
|
|
{isEnding ? (<img className="prize-hit-bg" src={conf.win_prize_bg}/>) : ''}
|
|
|
{isEnding ? (<img className="prize-hit-start-bg" src={conf.prize_btn_bg}/>) : ''}
|
|
|
<BottomButtons links={this.conf}/>
|
|
|
{
|
|
|
isLogin ?
|
|
|
(<div className="tips">
|
|
|
<p className="tip-1">今日剩余次数:{remainCount}次</p>
|
|
|
</div>) : ''
|
|
|
}
|
|
|
|
|
|
<BottomButton links={links}/>
|
|
|
<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">{'恭喜您!中奖了'}</div>
|
|
|
<span onClick={this.setCanStart} className="try-again">{tryAgainTip}</span>
|
|
|
</div>
|
|
|
) : ''}
|
|
|
{slowEnding ? (<span onClick={this.setCanStart} className="close-prize-hit-bg">关闭</span>) : ''}
|
|
|
{slowEnding ? (<img className="prize-hit-start-bg" src={conf.prize_btn_bg}/>) : ''}
|
|
|
</div>
|
|
|
)
|
|
|
}
|
...
|
...
|
|