Files
peko-h5/view/peko/modules/h5Income/js/exchangeGold.js
2023-07-12 17:00:50 +08:00

123 lines
3.4 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 rate;//轉換比例
var minDiamonds;//最少兌換鉆石
var maxDiamonds;//最多兌換鉆石
$(function () {
setTimeout(function () {
getInfoFromClient()
if (browser.app) {
$('.back').hide();
}
setTimeout(function () {
getConfig();
}, 100)
})
})
// 鉆石金幣接口
function getConfig () {
showLoading()
networkRequest({
type: 'GET',
url: urlPrefix + '/goldExchangeDiamond/getConfig',
success (res) {
if (res.code === 200) {
rate = res.data.rate;
maxDiamonds = res.data.maxDiamonds;
minDiamonds = res.data.minDiamonds;
$('.box .golds').text('我的金幣:' + res.data.golds);
$('.box .diamonds').text('我的鉆石:' + res.data.diamonds);
} else if (res.code ? res.code == 401 : JSON.parse(res).code == 401) {
window.location.href = './login.html'
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 確認兌換接口
function exchange () {
showLoading()
networkRequest({
type: 'POST',
url: urlPrefix + '/goldExchangeDiamond/exchange',
data: { currency: 1, diamondNum: Number($('.box .num2').val()), goldNum: Number($('.box .num').val()) },
success (res) {
if (res.code === 200) {
$('.pub').hide();
getConfig();
toastMsg('兌換成功')
} else if (res.code ? res.code == 401 : JSON.parse(res).code == 401) {
window.location.href = './login.html'
} else {
toastMsg(res.message)
}
hideLoading(layerIndex)
},
error (err) {
hideLoading(layerIndex)
toastMsg('網絡錯誤,請退出重進')
}
})
}
// 返回按鈕
$('.back').click(function () {
window.history.go(-1)
})
// 監聽輸入
$(".box .num").on("keyup", function () {
var val = $(this).val();
$('.box .num2').val(val * rate);
});
// 打開二次彈窗
$('.but').click(function () {
var num = Number($('.box .num2').val());
if (num < minDiamonds) {
toastMsg(`至少需要兌換${minDiamonds}鉆石`)
} else if (num > maxDiamonds) {
toastMsg(`兌換鉆石不能超過${maxDiamonds}鉆石`)
} else {
$('.pub .pub_in p .gold').text($('.box .num').val());
$('.pub .pub_in p .diamond').text($('.box .num2').val());
$('.pub').show();
}
})
// 確認兌換
$('.pub .pub_in .ok').click(function () {
exchange();
})
// 取消兌換
$('.pub .pub_in .close').click(function () {
$('.pub').hide();
})