Files
peko-h5/view/peko/activity/explore/js/index.js

357 lines
12 KiB
JavaScript
Raw Normal View History

2022-09-08 18:22:18 +08:00
let urlPrefix = getUrlPrefix()
let browser = checkVersion()
let env = EnvCheck();
if (env == 'test') {
new VConsole();
}
// 封裝layer消息提醒框
2022-09-08 18:22:18 +08:00
let layerIndex
const showLoading = (content = '加載中...') => {
2022-09-08 18:22:18 +08:00
layer.open({
type: 2,
shadeClose: false,
content,
2022-12-08 17:13:55 +08:00
success (e) {
2022-09-08 18:22:18 +08:00
layerIndex = $(e).attr('index')
}
})
}
const hideLoading = (index) => {
layer.close(index)
}
const toastMsg = (content = '操作完成', time = 2) => {
layer.open({
content,
time,
skin: 'msg'
})
}
// 獲取用戶信息
let isGetUserInfoInterFace //是否獲取到個人信息接口
2022-09-08 18:22:18 +08:00
const getUserInfo = (param) => {
showLoading('獲取個人信息中...')
2022-09-08 18:22:18 +08:00
networkRequest({
type: 'GET',
url: urlPrefix + '/activities/draw/queryTicketNum',
data: {
uid: pubInfo.uid
},
2022-12-08 17:13:55 +08:00
timeout: 8000, //超时时间设置,单位毫秒
complete: function (XMLHttpRequest, status) {//请求完成后最终执行参数
if (status == 'timeout') {//超时,status还有success,error等值的情况
//这里我使用的是layer中的弹出框当然也可以用其他的
hideLoading(layerIndex)
toastMsg('请求连接超时')
}
},
success (res) {
if (res.code === 200) {
2022-09-08 18:22:18 +08:00
isGetUserInfoInterFace = true
isShow()
$('.mine').find('.avatar img').attr('src', res.data.avatar)
2022-12-08 17:13:55 +08:00
if (!res.data.nick) {
2022-09-08 18:22:18 +08:00
res.data.nick = '未知'
}
if (res.data.nick.length > 10) {
res.data.nick = res.data.nick.substring(0, 10) + '...'
}
$('.mine').find('.nick').html(res.data.nick)
$('.mine').find('.diamond-num span').html(res.data.userPurse.diamonds)
2022-12-08 17:13:55 +08:00
if (param) {
$('.mine').find('.meteor-num span')[0].innerHTML = parseInt($('.mine').find('.meteor-num span')[0].innerHTML) + param
} else {
2022-09-08 18:22:18 +08:00
$('.mine').find('.meteor-num span').html(res.data.num)
}
2022-12-08 17:13:55 +08:00
getListPack()
} else {
2022-09-08 18:22:18 +08:00
toastMsg(res.message)
}
hideLoading(layerIndex)
},
2022-12-08 17:13:55 +08:00
error (err) {
toastMsg('網絡錯誤')
2022-09-08 18:22:18 +08:00
hideLoading(layerIndex)
}
})
}
// 獲取禮包信息
let isGetListPackInterFace //是否獲取到禮包接口
2022-09-08 18:22:18 +08:00
let listPack = []
const getListPack = () => {
showLoading('獲取禮包信息中...')
2022-09-08 18:22:18 +08:00
networkRequest({
type: 'POST',
url: urlPrefix + '/activities/draw/getPackList',
data: {
2022-12-08 17:13:55 +08:00
uid: pubInfo.uid
},
timeout: 8000, //超时时间设置,单位毫秒
complete: function (XMLHttpRequest, status) {//请求完成后最终执行参数
if (status == 'timeout') {//超时,status还有success,error等值的情况
//这里我使用的是layer中的弹出框当然也可以用其他的
hideLoading(layerIndex)
toastMsg('请求连接超时')
}
2022-09-08 18:22:18 +08:00
},
2022-12-08 17:13:55 +08:00
success (res) {
2022-09-08 18:22:18 +08:00
if (res.code === 200) {
isGetListPackInterFace = true
isShow()
listPack = res.data
renderList()
2022-12-08 17:13:55 +08:00
} else {
2022-09-08 18:22:18 +08:00
toastMsg(res.message)
}
hideLoading(layerIndex)
},
2022-12-08 17:13:55 +08:00
error () {
toastMsg('網絡錯誤')
2022-09-08 18:22:18 +08:00
hideLoading(layerIndex)
}
})
}
// 渲染禮包
2022-09-08 18:22:18 +08:00
let arrTips = [
2022-10-22 16:20:49 +08:00
'獲得信物1個',
'獲得信物10個',
'獲得信物100個',
'獲得信物1000個',
2022-09-08 18:22:18 +08:00
]
const renderList = () => {
listPack.map((item, index) => {
let $li = $('ul.buy-area li').eq(index)
$li.find('.headwear-name').html(item.name)
$li.find('.headwear-price').html(item.sellingPrice + '鉆石/天')
2022-09-08 18:22:18 +08:00
$li.find('.headwear-award').html(arrTips[index])
$('.content span').eq(index).html(item.name)
let currentBuyBtn = $('.buy-btn').eq(index)
currentBuyBtn.data('name', item.name)
currentBuyBtn.data('day', 1)
currentBuyBtn.data('gold', item.sellingPrice)
currentBuyBtn.data('giftId', item.id)
currentBuyBtn.data('ticketNum', item.ticketNum)
})
}
//只有兩個接口都請求到數據,才渲染默認頁面
2022-09-08 18:22:18 +08:00
const isShow = () => {
2022-12-08 17:13:55 +08:00
if (isGetListPackInterFace && isGetUserInfoInterFace) {
2022-09-08 18:22:18 +08:00
hideLoading(layerIndex)
$('.wrap').show()
2022-12-08 17:13:55 +08:00
} else {
showLoading('加載中...')
2022-09-08 18:22:18 +08:00
}
}
2022-12-08 17:13:55 +08:00
$(function () {
2022-09-08 18:22:18 +08:00
getInfoFromClient()
2022-12-08 17:13:55 +08:00
setTimeout(function () {
2022-09-08 18:22:18 +08:00
getUserInfo()
2022-12-08 11:51:52 +08:00
}, 500)
2022-09-08 18:22:18 +08:00
// 點擊玩法介紹按鈕
2022-09-08 18:22:18 +08:00
$('.rule-btn').on('click', function () {
$('.shade').fadeIn(300)
$('.rule-mask').fadeIn(300)
$('body').css('overflow', 'hidden')
})
// 關閉規則彈窗
2022-12-08 17:13:55 +08:00
$('.rule-mask .cancel').on('click', function () {
2022-09-08 18:22:18 +08:00
$('.shade').fadeOut(300)
$('.rule-mask').fadeOut(300)
$('body').css('overflow', 'auto')
})
2022-12-08 17:13:55 +08:00
$('.shade').on('click', function () {
2022-09-08 18:22:18 +08:00
$('.shade').hide()
$('.rule-mask').hide()
$('.shade-mask-success').hide()
$('.shade-mask-no-money').hide()
$('body').css('overflow', 'auto')
})
2022-12-08 17:13:55 +08:00
$('.rule-mask').on('click', function (e) {
2022-09-08 18:22:18 +08:00
e.stopPropagation()
})
// 監聽購買按鈕點擊事件
2022-12-08 17:13:55 +08:00
$('ul.buy-area').on('click', '.buy-btn', function () {
2022-09-08 18:22:18 +08:00
console.log($(this).data());
if (!$(this).data('name') || !$(this).data('day') || !$(this).data('gold') || !$(this).data('giftId')) {
toastMsg('禮包信息不完整')
2022-09-08 18:22:18 +08:00
return
}
giftObj = {}
giftObj['name'] = $(this).data('name')
giftObj['day'] = $(this).data('day')
giftObj['gold'] = $(this).data('gold')
giftObj['giftId'] = $(this).data('giftId')
giftObj['num'] = 1
giftObj['ticketNum'] = $(this).data('ticketNum')
2022-12-08 17:13:55 +08:00
2022-09-08 18:22:18 +08:00
$('.shade-mask-buy').find('.title span').html(giftObj['name'])
$('.shade-mask-buy').find('.buy-day span').html(giftObj['day'])
$('.shade-mask-buy').find('.buy-price span').html(giftObj['gold'])
$('.shade-mask-buy').find('.inputNum').val(giftObj['num'])
$('.shade-mask-buy').fadeIn(50)
$('body').css('overflow', 'hidden')
})
// 關閉購買彈窗
2022-12-08 17:13:55 +08:00
$('.buy-confirm-btn .cancel').on('click', function () {
2022-09-08 18:22:18 +08:00
$('.shade-mask-buy').fadeOut(50)
$('body').css('overflow', 'auto')
})
2022-12-08 17:13:55 +08:00
$('.shade-mask-buy').on('click', function () {
2022-09-08 18:22:18 +08:00
$('.shade-mask-buy').fadeOut(50)
$('body').css('overflow', 'auto')
})
2022-12-08 17:13:55 +08:00
$('.shade-content-buy').on('click', function (e) {
2022-09-08 18:22:18 +08:00
e.stopPropagation()
})
// 增加購買數量
2022-12-08 17:13:55 +08:00
$('.increase').on('click', function () {
if (giftObj['num'] < 999) {
2022-09-08 18:22:18 +08:00
giftObj['num']++;
giftObj['day']++;
let allPrice = giftObj['num'] * giftObj['gold'];
$('.inputNum').val(giftObj['num'])
$('.buy-day span').html(giftObj['day'])
$('.buy-price span').html(allPrice)
2022-12-08 17:13:55 +08:00
} else {
toastMsg('單次購買數量最多為999')
2022-09-08 18:22:18 +08:00
}
})
// 減少購買數量
2022-12-08 17:13:55 +08:00
$('.decrease').on('click', function () {
if ($('.inputNum').val() > 0) {
2022-09-08 18:22:18 +08:00
giftObj['num']--;
giftObj['day']--;
let allPrice = giftObj['num'] * giftObj['gold'];
$('.inputNum').val(giftObj['num'])
$('.buy-day span').html(giftObj['day'])
$('.buy-price span').html(allPrice)
2022-12-08 17:13:55 +08:00
} else {
toastMsg('最少購買數量為1')
2022-09-08 18:22:18 +08:00
}
})
// 手動輸入購買數量
2022-12-08 17:13:55 +08:00
$('.inputNum').on('input', function () {
2022-09-08 18:22:18 +08:00
let exp = /^[0-9]+$/
if (!exp.test($(this).val())) {
toastMsg('請輸入數字')
2022-09-08 18:22:18 +08:00
return
}
2022-12-08 17:13:55 +08:00
if ($(this).val() > 999) {
2022-09-08 18:22:18 +08:00
giftObj['num'] = 999
2022-12-08 17:13:55 +08:00
} else {
2022-09-08 18:22:18 +08:00
giftObj['num'] = $(this).val()
}
giftObj['day'] = giftObj['num'];
let allPrice = giftObj['num'] * giftObj['gold'];
$('.inputNum').val(giftObj['num'])
$('.buy-day span').html(giftObj['day'])
$('.buy-price span').html(allPrice)
})
// 確認購買
2022-09-08 18:22:18 +08:00
let lock = false
2022-12-08 17:13:55 +08:00
$('.confirm').on('click', function () {
2022-09-08 18:22:18 +08:00
let exp = /^[0-9]+$/
2022-12-08 17:13:55 +08:00
if (!exp.test($('.inputNum').val())) {
toastMsg('請輸入數字')
2022-09-08 18:22:18 +08:00
$('.inputNum').val(1)
return
}
2022-12-08 17:13:55 +08:00
if ($('.inputNum').val() == 0) {
return toastMsg('最少購買數量為1')
2022-09-08 18:22:18 +08:00
}
2022-12-08 17:13:55 +08:00
if (!lock) {
2022-09-08 18:22:18 +08:00
lock = true
networkRequest({
type: 'POST',
url: urlPrefix + '/activity/pack/buy',
data: {
uid: pubInfo.uid,
packId: giftObj.giftId,
packNum: giftObj.num,
ticket: pubInfo.ticket
},
2022-12-08 17:13:55 +08:00
success (res) {
if (res.code === 200) {
2022-09-08 18:22:18 +08:00
getUserInfo(res.data)
$('.shade-mask-buy').hide()
$('.shade').fadeIn(300)
2022-12-08 17:13:55 +08:00
$('.shade-mask-success .tip').html(`獲贈信物x` + giftObj.ticketNum * giftObj.num)
2022-09-08 18:22:18 +08:00
$('.shade-mask-success').fadeIn()
2022-12-08 17:13:55 +08:00
} else if (res.code === 2103) {
2022-09-08 18:22:18 +08:00
$('.shade-mask-buy').hide()
$('.shade').fadeIn(300)
$('.shade-mask-no-money').show()
$('.shade-mask-no-money').fadeIn()
2022-12-08 17:13:55 +08:00
} else {
2022-09-08 18:22:18 +08:00
toastMsg(res.message)
}
lock = false
},
2022-12-08 17:13:55 +08:00
error (err) {
toastMsg('網絡錯誤')
2022-09-08 18:22:18 +08:00
}
})
}
2022-12-08 17:13:55 +08:00
2022-09-08 18:22:18 +08:00
})
// 關閉購買成功彈窗
2022-12-08 17:13:55 +08:00
$('.in-btn').on('click', function () {
2022-09-08 18:22:18 +08:00
$('.shade-mask-success').fadeOut()
$('.shade').fadeOut()
$('body').css('overflow', 'auto')
})
// 跳轉充值
2022-12-08 17:13:55 +08:00
$('.recharge').on('click', function () {
2022-09-08 18:22:18 +08:00
$('.shade-mask-no-money').fadeOut(300)
$('.shade').fadeOut(300)
$('body').css('overflow', 'auto')
2022-12-08 17:13:55 +08:00
if (browser.app) {
if (browser.android) {
2023-03-08 18:25:49 +08:00
// tools.nativeUtils.jumpAppointPage('RECHARGE_PAGE')
window.androidJsObj.openChargePage(8);
2022-12-08 17:13:55 +08:00
} else if (browser.ios) {
2023-03-08 18:25:49 +08:00
window.webkit.messageHandlers.chargePayClickPage.postMessage(8)
2022-09-08 18:22:18 +08:00
window.webkit.messageHandlers.openChargePage.postMessage(null)
}
2022-12-08 17:13:55 +08:00
} else {
toastMsg('請在app內打開')
2022-09-08 18:22:18 +08:00
}
})
2022-12-08 17:13:55 +08:00
// //從充值頁面返回活動頁面 重新請求用戶信息接口
// var hiddenProperty = 'hidden' in document ? 'hidden' :
// 'webkitHidden' in document ? 'webkitHidden' :
// 'mozHidden' in document ? 'mozHidden' : null;
// var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
// var onVisibilityChange = function () {
// if(!document[hiddenProperty]){
// $('.shade-mask-no-money').hide()
// getUserInfo()
// }
// }
// document.addEventListener(visibilityChangeEvent, onVisibilityChange);
//返回页面 重新请求接口
2022-09-08 18:22:18 +08:00
var hiddenProperty = 'hidden' in document ? 'hidden' :
2022-12-08 17:13:55 +08:00
'webkitHidden' in document ? 'webkitHidden' :
'mozHidden' in document ? 'mozHidden' : null;
2022-09-08 18:22:18 +08:00
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var onVisibilityChange = function () {
2022-12-08 17:13:55 +08:00
if (!document[hiddenProperty]) {
location.reload()
}
2022-09-08 18:22:18 +08:00
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);
})