Files
peko-h5/view/peko/modules/act-treasureSnatching/js/index.js
2023-03-21 15:39:33 +08:00

221 lines
6.6 KiB
JavaScript

// 封装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'
})
}
let urlPrefix = getUrlPrefix();
let browser = checkVersion();
var diamondsAll = 0;
var packIds;
let env = EnvCheck();
if (env == 'test') {
new VConsole();
}
// 初始化函数
$(function () {
getInfoFromClient()
setTimeout(function () {
// 返回事件
$('.back').click(() => {
if (browser.app) {
if (browser.android) {
window.androidJsObj.closeWebView();
} else {
window.webkit.messageHandlers.closeWebView.postMessage(null);
}
} else {
toastMsg('请在APP内打开')
}
})
getList();
getUserInfo();
}, 100)
$('body,html').css('padding-top', `${(document.documentElement.clientWidth / 750 * 120) / 75}rem`);
})
// 用户信息接口
function getUserInfo () {
// showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/act/seize-treasure/user/info',
success (res) {
if (res.code === 200) {
$('.my .num2').text(unitProcessing(res.data.diamonds, 100000, 1, 'w'));
$('.my .num1').text(res.data.drawTicketNum);
$('.my .name').text(res.data.nick);
$('.my .tx').attr('src', res.data.avatar);
hideLoading(layerIndex)
} else {
toastMsg(res.message)
hideLoading(layerIndex)
}
},
error (err) {
hideLoading(layerIndex)
toastMsg('网络错误,请退出重进')
}
})
}
// 礼物列表接口
function getList () {
$('ul li').remove();
showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/act/seize-treasure/pack/list',
success (res) {
if (res.code === 200) {
var str = '';
res.data.forEach(res => {
str += `
<li>
<div class="title">赠送夺宝券${res.ticketNum}张</div>
<img src="${res.imgUrl}" alt="" class="giftImg">
<div class="name">${res.name}(1天)</div>
<div class="zs">
<img src="./images/zs.png" alt="" class="icon">
<b>${res.sellingPrice}</b>
</div>
<div class="but" packId='${res.id}' day=1 diamonds=${res.sellingPrice} name='${res.name}'>购买</div>
</li>
`
});
$('ul').append(str);
// 购买头饰按钮
$('ul li .but').bind('on click', function () {
var diamonds = Number($(this).attr('diamonds'));
var day = Number($(this).attr('day'));
var name = $(this).attr('name');
packIds = $(this).attr('packId');
$('.buy .buy_in .day span').text(day);
$('.buy .buy_in .zs span').text(diamonds);
$('.buy .buy_in .title').text(`购买${name}`);
$(".buy .buy_in .num .box input").val(1);
diamondsAll = diamonds;
$('.buy').show();
})
hideLoading(layerIndex)
} else {
toastMsg(res.message)
hideLoading(layerIndex)
}
},
error (err) {
hideLoading(layerIndex)
toastMsg('网络错误,请退出重进')
}
})
}
setInterval(function () {
getUserInfo();
}, 1000)
// 购买接口
function buy (packId, packNum) {
showLoading()
networkRequest({
type: 'POST',
url: urlPrefix + '/activity/pack/buy',
data: { packId, packNum, uid: pubInfo.uid },
success (res) {
if (res.code === 200) {
diamondsAll = 0;
$('.buy').hide();
getUserInfo();
hideLoading(layerIndex)
toastMsg('购买成功')
} else {
diamondsAll = 0;
$('.buy').hide();
toastMsg(res.message)
hideLoading(layerIndex)
}
},
error (err) {
diamondsAll = 0;
$('.buy').hide();
hideLoading(layerIndex)
toastMsg('网络错误,请退出重进')
}
})
}
// 充值按钮
$('.my .but').click(function () {
if (browser.app) {
if (browser.android) {
window.androidJsObj.openHalfRecharge();
} else {
window.webkit.messageHandlers.openHalfRecharge.postMessage(null);
}
} else {
toastMsg('请在app内打开')
}
})
// 取消购买
$('.buy .buy_in .close').click(function () {
diamondsAll = 0;
$('.buy').hide();
})
// 确认购买按钮
$('.buy .buy_in .ok').click(function () {
var num = Number($('.buy .buy_in .num .box input').val());
buy(packIds, num);
})
// 减少天数
$('.buy .buy_in .num .box .subtraction').click(function () {
var val = Number($(".buy .buy_in .num .box input").val());
val--;
if (val < 1) {
toastMsg('购买不能小于1');
val = 1;
}
$('.buy .buy_in .day span').text(val);
$('.buy .buy_in .zs span').text(val * diamondsAll);
$(".buy .buy_in .num .box input").val(val);
})
// 增加天数
$('.buy .buy_in .num .box .add').click(function () {
var val = Number($(".buy .buy_in .num .box input").val());
val++;
if (val > 200) {
toastMsg('购买最大200');
val = 200;
}
$('.buy .buy_in .day span').text(val);
$('.buy .buy_in .zs span').text(val * diamondsAll);
$(".buy .buy_in .num .box input").val(val);
})
// 监听input
$("input[name='content']").bind('input propertychange', function () {
var val = Number($(this).val());
if (val > 200) {
$(this).val(200);
val = 200;
toastMsg('购买最大200');
} else if (val < 1) {
$(this).val(1);
val = 1;
toastMsg('购买不能小于1');
} else {
$(this).val(val);
val = val;
}
$('.buy .buy_in .day span').text(val);
$('.buy .buy_in .zs span').text(val * diamondsAll);
});