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 { 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 { 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(); })