const app = getApp()
let {
config,
Comm
} = require('../../utils/util.js');
let {
AJAX,
} = require('../../utils/ajax.js');
var timer;
var n = 1;
var which = 1;
var drawCode = 0;
var deg;
Page({
data: {
animationData: {},
isEnd: false,
detail: '',
noLuck: false,
btnShow: false,
drawNum: 0,
orderAmount: 0,
drawUserList: [],
},
onLoad: function (options) {
},
onReady: function () {
},
onShow: function () {
this.getUserDraw();
this.getUserDrawRecodeList();
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
return app.shareFn();
},
toDrawDetail() {
wx.navigateTo({
url: '/pages/luckydrawdetail/luckydrawdetail',
})
},
getUserDraw() {
let _this = this;
AJAX.POST('/drawRecode/getCustomerDrawCount', {}, function (res) {
if (res && res.code == 1) {
_this.setData({
drawNum: res.data.count,
orderAmount: res.data.orderAmount,
})
} else {
Comm.message(res.msg)
}
})
},
getUserDrawRecodeList() {
let _this = this;
AJAX.POST('/drawRecode/getLastDrawRecodeList', {}, function (res) {
if (res && res.code == 1) {
_this.setData({
drawUserList: res.data
})
} else {
Comm.message(res.msg)
}
})
},
start() {
var _this = this;
this.setData({
noLuck: false,
btnShow: true,
})
timer = setInterval(function () {
var animation = wx.createAnimation({
transformOrigin: "50% 50%",
duration: 1000,
timingFunction: "linear"
});
animation.rotate(360 * n).step();
_this.setData({
animationData: animation.export()
})
n++;
}, 300);
AJAX.POST('/luckDraw/invoke', {}, function (res) {
if (res && res.code == 1) {
drawCode = res.data;
} else {
Comm.message(res.msg);
}
})
setTimeout(function () {
_this.stop()
}, 1000)
},
stop() {
var _this = this;
clearInterval(timer);
timer = null;
sto.call(_this);
function sto() {
var animation = wx.createAnimation({
transformOrigin: "50% 50%",
duration: 1.4 * (2000 - (which - 1) * 60),
timingFunction: "ease-out"
});
let arr5 = [22.5, 202.5, 337.5];
let arr6 = [112.5, 247.5];
var angle = 0;
var useAngle = 0;
switch (drawCode) {
case '555':
angle = _this.shuffle(arr5);
useAngle = _this.shuffle(arr5);
break;
case '666':
angle = _this.shuffle(arr6);
useAngle = _this.shuffle(arr6);
break;
case '777':
angle = 67.5;
useAngle = 67.5;
break;
case '888':
angle = 157.5;
useAngle = 157.5;
break;
case '999':
angle = 292.5;
useAngle = 292.5;
break;
default:
break;
}
if (angle > 180) {
angle = angle - 360;
}
animation.rotate(angle).step();
let drawNum = _this.data.drawNum;
drawNum--;
this.setData({
animationData: animation.export(),
drawNum: drawNum,
})
setTimeout(function () {
_this.setData({
isEnd: true,
btnShow: false,
})
_this.getDrawResult(useAngle);
}, 3000)
}
},
shuffle(array) {
var return_array = '';
var arrIndex = Math.floor(Math.random() * array.length);
return_array = array[arrIndex];
return return_array
},
getDrawResult: function (deg) {
var prize = "";
switch (deg) {
case 22.5:
case 202.5:
case 337.5:
prize = "五等奖";
break;
case 112.5:
case 247.5:
prize = "四等奖";
break;
case 67.5:
prize = "三等奖";
break;
case 157.5:
prize = "二等奖";
break;
case 292.5:
prize = "一等奖";
break;
default:
this.setData({
noLuck: true
})
break;
}
if (!this.data.noLuck) {
this.setData({
detail: prize
})
}
},
showForm(e) {
this.setData({
isEnd: false
})
this.getUserDrawRecodeList();
}
})
<view class="container drawBox">
<view class="container f0 drawBG">
<image src="../../static/images/draw_bg.jpg" class="imgCover"></image>
</view>
<view class="drawDetail w100 flex-right pad5">
<view class="colorfff f12 w20 center radius5 h25 lh25" bindtap="toDrawDetail">活动规则</view>
</view>
<view class='turntable w100'>
<view class="drawBox w100">
<view class="flex-center w100">
<view class="turntableImg f0">
<image class="imgCover" animation="{{animationData}}" src="../../static/images/turntable.png"></image>
</view>
</view>
<view class='draw-btn w100'>
<view class="draw-m" wx:if="{{btnShow}}"></view>
<view class="f0 w100 flex-center">
<image bindtap="start" src="../../static/images/draw_btn.png" wx:if="{{drawNum > 0}}"></image>
<image src="../../static/images/draw_btn_d.png" wx:else></image>
</view>
</view>
</view>
</view>
<view class='drawing-text w100'>
<view class="f16 colorFE6 ">消费金额{{orderAmount/100 || 0}}元,剩余次数{{drawNum}}次</view>
</view>
<view class="swiperBox w100">
<swiper autoplay circular vertical interval="3000" class="w100 h50 lh50 f14 bold">
<block wx:for="{{drawUserList}}" wx:key="index">
<swiper-item>
<text>{{item.customerName}}抽中了</text><text class="colorFFB">{{item.grade}}</text>
</swiper-item>
</block>
</swiper>
</view>
<view wx:if="{{isEnd}}" class='container modal-result flex-center'>
<view class='modal-result-content flex-center'>
<image mode="widthFix" src="../../static/images/modal_bg_1.png"></image>
<view class='modal-result-text'>
<view class="mart20 f14">亲,恭喜您获得</view>
<view class='colorFF3 f18 bold mart5'>{{detail}}</view>
<view class='mart20'>
<view class='colorfff bold bgFF9 f14 center radius15 h30 lh30' bindtap="showForm">确认领取</view>
</view>
</view>
</view>
</view>
</view>
.drawBox {
position: relative;
}
.drawBG {
position: absolute;
z-index: 1;
left: 0;
top: 0;
}
.drawDetail {
position: absolute;
top: 17%;
z-index: 3;
}
.drawDetail>view {
background-color: rgba(51, 51, 51, 0.3);
}
.turntable {
position: absolute;
top: 30%;
z-index: 2;
}
.turntableImg {
width: 520rpx;
height: 520rpx;
}
.draw-btn {
position: absolute;
top: calc(50% - 80rpx);
z-index: 3;
}
.draw-m {
position: absolute;
width: 100%;
height: 100%;
z-index: 10;
}
.draw-btn>view>image {
width: 112rpx;
height: 140rpx;
}
.drawing-text {
position: absolute;
z-index: 4;
text-align: center;
font-size: 24rpx;
top: 76%;
font-weight: bold;
color: #333;
}
.swiperBox{
position: absolute;
z-index: 5;
text-align: center;
top: 90%;
}
.modal-result {
background: rgba(0, 0, 0, 0.5);
position: absolute;
z-index: 5;
}
.modal-result-content {
width: 80%;
height: 30%;
position: relative;
}
.modal-result-text {
position: absolute;
font-size: 32rpx;
text-align: center;
padding: 20rpx;
}