From 1d76df3ead736e3711d3049e286c62b1cd3cef3d Mon Sep 17 00:00:00 2001 From: H1d3rOne Date: Sun, 8 Mar 2026 22:04:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=91=E5=B8=81=E7=94=A8?= =?UTF-8?q?=E5=AE=8C=E5=90=8E=E9=87=8D=E7=BD=AE=E4=B8=BA10=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题原因: - coinUtils.js中使用 if (!coins) 判断金币是否存在 - 当金币为0时,0被当作falsy值,导致重新设置为10 - 用户金币用完后会自动重置,无法真正耗尽 解决方案: - 修改判断条件为 if (coins === null || coins === undefined || coins === '') - 只有当金币未初始化时才设置为初始值10 - 0金币不会被误判为未初始化状态 - 首次初始化时保存到storage 优化内容: - 金币不足时提示更友好,引导用户获取金币 - 修复代码缩进格式 - 确保金币消耗逻辑正确 --- xuan-deduct-uniapp/src/utils/coinUtils.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/xuan-deduct-uniapp/src/utils/coinUtils.js b/xuan-deduct-uniapp/src/utils/coinUtils.js index 426ec9b..9fd9a36 100644 --- a/xuan-deduct-uniapp/src/utils/coinUtils.js +++ b/xuan-deduct-uniapp/src/utils/coinUtils.js @@ -7,8 +7,12 @@ const checkCoinsAndConsume = () => { } let coins = uni.getStorageSync('userCoins') - if (!coins) coins = 10 - else coins = parseInt(coins) + if (coins === null || coins === undefined || coins === '') { + coins = 10 + uni.setStorageSync('userCoins', coins) + } else { + coins = parseInt(coins) + } if (coins < 1) { uni.showModal({ @@ -18,10 +22,10 @@ const checkCoinsAndConsume = () => { cancelText: '取消', success: (res) => { if (res.confirm) { - uni.navigateTo({ url: '/pages/mine/coins' }) + uni.navigateTo({ url: '/pages/mine/coins' }) + } } - } - }) + }) return { canUse: false, needCoins: true } } @@ -35,8 +39,11 @@ const consumeCoin = async () => { } let coins = uni.getStorageSync('userCoins') - if (!coins) coins = 10 - else coins = parseInt(coins) + if (coins === null || coins === undefined || coins === '') { + coins = 10 + } else { + coins = parseInt(coins) + } coins -= 1 uni.setStorageSync('userCoins', coins)