const urlPrefix = getUrlPrefix()
const browser = checkVersion();
if(EnvCheck() === 'test') new VConsole
// 封装layer消息提醒框
let layerIndex
const showLoading = () => {
layer.open({
type: 2,
shadeClose: false,
content: '加載中...',
success(e){
layerIndex = $(e).attr('index')
}
})
}
const hideLoading = (index) => {
// layer.closeAll()
layer.close(index)
}
const toastMsg = (content = '操作完成', time = 2) => {
layer.open({
content,
time,
skin: 'msg'
})
}
let leftBattery = 0
let batteryCanBuy = 0
let chargeAmount = 0
let t //抽奖动画定时器
let num //点亮次数
let isAnimateOver = true //动画结束才可进行下次抽奖
let tenList = [] //点亮10次 奖品列表
//获取用户活动数据
const getData = () => {
showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/bulbActivity/query',
success(res) {
hideLoading(layerIndex)
if (res.code === 200) {
let data = res.data
leftBattery = data.batteryBought //剩余电池
batteryCanBuy = data.batteryCanBuy //可领取电池
chargeAmount = data.chargeAmount //累充
$('.leftBulb').html(leftBattery) //剩余电池
$('.accumulated > span').html(chargeAmount) //累充
$('.canget > span').html(batteryCanBuy) //可领取
data.batteryCanBuy ? $('.btn-right').html('') : $('.btn-right').html('
')
} else {
toastMsg(res.message)
}
},
error(err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤')
}
})
}
//领取电池
const getBattery = () => {
showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/bulbActivity/buyBattery',
async: false,
success(res) {
hideLoading(layerIndex)
if (res.code === 200) {
toastMsg('领取成功')
} else {
toastMsg(res.message)
}
},
error(err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤')
}
})
}
//点亮灯泡
const lightBulb = (num) => {
showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/bulbActivity/lightBulb',
async: false, //这里需要同步
data:{
num
},
success(res) {
hideLoading(layerIndex)
if (res.code === 200) {
let index = res.data.indexList[0]
let isTen = res.data.indexList.length > 1 ? true : false
if(isTen) {
tenList = res.data.indexList
operateTenList()
}
if (index >= 4) {
switch (index) {
case 4: index = 7; break;
case 5: index = 6; break;
case 6: index = 5; break;
case 7: index = 4; break;
}
}
renderAnimatiton(index, isTen)
isAnimateOver = !isAnimateOver
} else {
toastMsg(res.message)
}
},
error(err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤')
}
})
}
//处理 抽奖10次 返回的数据
let tenAward = [
{name: '幸运铭牌', num: 0},
{name: '星光头饰', num: 0},
{name: '幸运头饰', num: 0},
{name: '度假头饰', num: 0},
{name: '机车座驾', num: 0},
{name: '宝马跑车', num: 0}
]
const operateTenList = () => {
tenList.map((item)=>{
switch(item){
case 0:
tenAward[2].num++
break
case 1: case 6:
tenAward[0].num++
break
case 2:
tenAward[5].num++
break
case 3:
tenAward[4].num++
break
case 4:
tenAward[3].num++
break
case 5: case 7:
tenAward[1].num++
break
}
})
tenAward = tenAward.filter((item) => {
return item.num > 0
})
// console.log(tenAward);
}
//抽奖动画
let x = 30, count = 0, speed = 500, stepToChangeSpeed = 2
let awardList = [
{name:'幸运头饰x1天', img: './images/0.png'},
{name:'幸运铭牌x1天', img: './images/1.png'},
{name:'宝马跑车x1天', img: './images/2.png'},
{name:'机车座驾x1天', img: './images/3.png'},
{name:'星光头饰x1天', img: './images/4.png'},
{name:'幸运铭牌x1天', img: './images/1.png'},
{name:'星光头饰x1天', img: './images/4.png'},
{name:'度假头饰x1天', img: './images/5.png'}
]
const renderAnimatiton = (index, isTen) => {
$('.circle').css('display', 'block')
t = setTimeout(() => {
count++
if (Math.floor(count / 4) % 2 === 0) {
x += 77
$('.circle').css('transform', `translate(${x * 2 / 75}rem, 15.6rem)`)
} else {
$('.circle').css('transform', `translate(${x * 2 / 75}rem, ${680 * 2 / 75}rem)`)
x -= 77
}
if (count === stepToChangeSpeed || count === stepToChangeSpeed * 14) speed = 400
if (count === stepToChangeSpeed * 2 || count === stepToChangeSpeed * 13) speed = 300
if (count === stepToChangeSpeed * 3 || count === stepToChangeSpeed * 12) speed = 200
if (count === stepToChangeSpeed * 4) speed = 100
if (count >= stepToChangeSpeed * 16) {
speed = 550
if (count === stepToChangeSpeed * 16 + index) {
let contentHtml = ''
if(isTen){
let str = tenAward.map((item)=>{
return `
连续点击会坏的哟~
点亮结束后才可以再次点击') } }) //点击 知道啦 $(document).on('click', '.know', function(){ $('.shade-mask').fadeOut(200) //还原抽10次的数组 tenAward = [ {name: '幸运铭牌', num: 0}, {name: '星光头饰', num: 0}, {name: '幸运头饰', num: 0}, {name: '度假头饰', num: 0}, {name: '机车座驾', num: 0}, {name: '宝马跑车', num: 0} ] isAnimateOver = !isAnimateOver }) //点击获奖记录 $('.record').on('click', function () { getRecordList() $('.record-shade-mask').fadeIn(200) $('body').css('overflow', 'hidden') }) //关闭弹窗 $('.close,.record-shade-mask').click(()=>{ $('.record-shade-mask').fadeOut(200) $('body').css('overflow', 'auto') }) $('.record-shade-mask .mask-content').click((e)=>{ e.stopPropagation() }) })