Files
peko-h5/view/peko/modules/luckyTarot/js/index.js
2023-07-24 10:46:52 +08:00

354 lines
11 KiB
JavaScript

let urlPrefix = getUrlPrefix()
let browser = checkVersion()
let env = EnvCheck();
if (env == 'test') {
new VConsole();
}
// 封裝layer消息提醒框
let layerIndex
const showLoading = (content = '加載中...') => {
layer.open({
type: 2,
shadeClose: false,
content,
success (e) {
layerIndex = $(e).attr('index')
}
})
}
const hideLoading = (index) => {
layer.close(index)
}
const toastMsg = (content = '操作完成', time = 2) => {
layer.open({
content,
time,
skin: 'msg'
})
}
var cardArr = [];//记录选择卡片
var gearPosition = []//档位价值的钻石
var gearPositionActive = 0;//当前选泽的档位
var prodId = [];//档位类型
var prodIdActive;
var lock = true;//锁
// 初始化函数
$(function () {
setTimeout(function () {
getInfoFromClient()
setTimeout(function () {
// 页面全屏
if (browser.app) {
if (browser.android) {
window.androidJsObj.initShowNav(false)
} else {
window.webkit.messageHandlers.initShowNav.postMessage(0)
}
};
// 顶部返回事件
$('.back').click(() => {
if (browser.android) {
window.androidJsObj.closeWebView()
} else {
window.webkit.messageHandlers.closeWebView.postMessage(null)
}
})
getAdvertising();
productList();
getUserInfo();
}, 100)
})
})
// 公告接口
function getAdvertising () {
showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/seekElfin/draw/list',
data: {
count: 30
},
success (res) {
if (res.code === 200) {
let result = ''
res.data.forEach(res => {
result += `
<li>恭喜 <b>${res.nick.length > 5 ? res.nick.slice(0, 5) + '...' : res.nick}</b>翻開 <b>幸運塔羅</b> 獲得 <i>${res.receiveGoldNum}鉆石</i></li>
`
})
$('.ul1').append(result)
var num = $(".ul1").find("li").length;
if (num > 1) {
setInterval(function () {
$('.ul1').animate({
marginTop: "-0.4rem"
}, 200, function () {
$(this).css({
marginTop: "0"
}).find("li:first").appendTo(this);
});
}, 2000);
}
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 档位接口
function productList () {
showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/seekElfin/product/list',
data: {
uid: pubInfo.uid
},
success (res) {
if (res.code === 200) {
gearPositionActive = res.data[0].prodNeedPrice;
prodIdActive = res.data[0].prodId;
res.data.forEach((res, i) => {
gearPosition.push(res.prodNeedPrice);
prodId.push(res.prodId);
$(`.tarotBox .tabBox .tabBox${i + 1} b`).text(`${res.prodNeedPrice}鉆/張`)
})
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 獲取用戶信息
const getUserInfo = () => {
showLoading()
networkRequest({
type: 'POST',
url: urlPrefix + '/seekElfin/user/info',
data: {
uid: pubInfo.uid
},
success: function (res) {
if (res.code == 200) {
$('.tarotBox .diamond b').text(res.data.diamonds);
} else {
toastMsg(res.message);
}
hideLoading(layerIndex)
},
error: function (res) {
hideLoading(layerIndex)
console.log(res, '報錯啦');
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 抽奖接口
const draw = () => {
lock = false;
showLoading()
networkRequest({
type: 'POST',
url: urlPrefix + '/seekElfin/draw',
data: JSON.stringify({
prodId: prodIdActive,
selectedNo: cardArr,
// roomUid:pubInfo.roomUid,
roomUid: pubInfo.uid,
}),
headers: { 'Content-Type': 'application/json;charset=utf8' },
success: function (res) {
if (res.code == 200) {
kineticEffect(cardArr, res.data.win, res);
getUserInfo();
} else {
lock = true;
toastMsg(res.message);
}
hideLoading(layerIndex)
},
error: function (res) {
lock = true;
hideLoading(layerIndex)
console.log(res, '報錯啦');
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 恢复初始样式函数
function initialStytle () {
lock = true;
// 清空已选卡片
cardArr = [];
// 恢复卡片默认样式
$('.tarotBox .cardBox').removeClass('card1_active');
$('.tarotBox .cardBox').removeClass('card2_active');
$('.tarotBox .cardBox').removeClass('card3_active');
// 恢复默认选择文案
$('.tarotBox .cardBoxText').html(`
<p>供選擇 <b>${cardArr.length}</b> 張</p>
<p>翻到钻石卡的概率为<b>${cardArr.length * 10}%</b></p>
`);
// 清除中奖&未中奖样式
$('.tarotBox .cardBox').removeClass('card_not');
$('.tarotBox .cardBox').removeClass('card_winning1');
$('.tarotBox .cardBox').removeClass('card_winning2');
$('.tarotBox .cardBox').removeClass('card_winning3');
$('.tarotBox .cardBoxText_no').hide();
$('.tarotBox .cardBoxText_gx').hide()
$('.tarotBox .cardBoxText').show();
$('.tarotBox .cardBoxText_gift').show();
$('.cardBoxBut').show();
$('.butAgain').hide();
}
// 处理动效
function kineticEffect (arr, bool, data) {
arr.forEach((res, i) => {
$(`.tarotBox .cardBox${res}`).css({
transition: ' all 1s',
transform: 'rotateY(360deg)',
})
})
setTimeout(function () {
$('.tarotBox .cardBox').css({
transition: ' all 0s',
transform: 'rotateY(0deg)',
});
arr.forEach((res, i) => {
$(`.tarotBox .cardBox${res}`).addClass('card_not');
})
if (bool) {
//中奖
$(`.tarotBox .cardBox${data.data.drawNum}`).addClass(prodIdActive == 1 ? 'card_winning1' : prodIdActive == 2 ? 'card_winning2' : 'card_winning3')
$('.tarotBox .cardBoxText').hide();
$('.tarotBox .cardBoxText_gift').hide();
$('.cardBoxText_gx').html(`
<p>翻到钻石卡!</p>
<p>获得 <b>${data.data.receiveGoldNum}</b> 钻石</p>
`)
$('.cardBoxText_gx').show();
} else {
//未中奖
$('.tarotBox .cardBoxText').hide();
$('.tarotBox .cardBoxText_gift').hide();
$('.tarotBox .cardBoxText_no').show();
}
$('.cardBoxBut').hide();
$('.tarotBox .butAgain').attr('src', prodIdActive == 1 ? './images/butAgain1.png' : prodIdActive == 2 ? './images/butAgain2.png' : './images/butAgain3.png')
$('.butAgain').show();
}, 1000)
}
// 档位tab切换按钮
$('.tarotBox .tabBox div').click(function () {
var i = $(this).index() + 1;
// 切换tab样式
$('.tarotBox .tabBox div').removeClass('active1').removeClass('active2').removeClass('active3');
$(this).addClass(`active${i}`);
// 切换卡牌样式
$('.tarotBox .cardBox').hide();
$(`.tarotBox .card${i}`).show();
// 切换按钮样式
$('.tarotBox .cardBoxBut').attr('src', `./images/but${i}.png`)
// if (i == 1) {
gearPositionActive = gearPosition[i - 1];
prodIdActive = prodId[i - 1];
$('.tarotBox .gradeText b').text(gearPositionActive * 10);
// } else if (i == 2) {
// gearPosition = 1000;
// $('.tarotBox .gradeText b').text(gearPosition * 10);
// } else {
// gearPosition = 10000;
// $('.tarotBox .gradeText b').text(gearPosition * 10);
// }
// 清空默认样式
initialStytle();
})
// 选择初级卡片按钮
$('.tarotBox .card1').click(function () {
var i = $(this).index();
tarotBox($(this), i, 1);
})
// 选择中级卡片按钮
$('.tarotBox .card2').click(function () {
var i = $(this).index();
tarotBox($(this), i, 2);
})
// 选择高级卡片按钮
$('.tarotBox .card3').click(function () {
var i = $(this).index();
tarotBox($(this), i, 3);
})
// 选择卡片样式
function tarotBox (dom, i, type) {
if (lock) {
var i = type == 1 ? dom.index() - 3 : type == 2 ? dom.index() - 13 : dom.index() - 23;
if (cardArr.indexOf(i) == -1) {
if (cardArr.length < 9) {
dom.addClass(type == 1 ? 'card1_active' : type == 2 ? 'card2_active' : 'card3_active');
cardArr.push(i);
} else {
toastMsg('最多只能选择9张');
}
} else {
dom.removeClass(type == 1 ? 'card1_active' : type == 2 ? 'card2_active' : 'card3_active');
cardArr.splice(cardArr.indexOf(i), 1);
}
$('.tarotBox .cardBoxText').html(`
<p>供選擇 <b>${cardArr.length}</b> 張</p>
<p>翻到钻石卡的概率为<b>${cardArr.length * 10}%</b></p>
`);
console.log(cardArr);
}
}
// 翻牌按钮
$('.tarotBox .cardBoxBut').click(function () {
if (cardArr.length == 0) {
toastMsg('请先选择卡片');
return;
}
if (lock) {
draw();
}
})
// 再翻一次按钮
$('.butAgain').click(function () {
initialStytle();
})
// 跳轉充值頁面
$('.tarotBox .diamond').click(() => {
if (browser.app) {
if (browser.android) {
let channel = pubInfo.deviceInfo.channel;
console.log(pubInfo.deviceInfo);
if (channel == "google") {
window.androidJsObj.openChargePage(6);
} else {
window.androidJsObj.openChargePage(6);
window.location.href = urlPrefix + '/peko/modules/pay/index.html?channelType=4';
}
} else if (browser.ios) {
let channel = pubInfo.deviceInfo.channel;
if (channel == "appstore") {
window.webkit.messageHandlers.openChargePage.postMessage(null);
} else {
window.webkit.messageHandlers.chargePayClickPage.postMessage(6);
window.location.href = urlPrefix + '/peko/modules/pay/index.html?channelType=4';
}
}
} else {
toastMsg('請在app內打開')
}
})