uniapp-提现功能(demo)

页面布局

提现页面 有一个输入框 一个提现按钮 一段提现全部的文字

首先用v-model 和data内的数据双向绑定

输入框逻辑分析

输入框的逻辑 为了符合日常输出 所以要对输入框加一些条件限制

因为是提现 所以对输入的字符做筛选,只允许出现小数点和数字 这里用正则实现的

小数点后只能输入两位小数, 超过两位的去除掉 因为提现的最小金额是两位数

.前面如果没有数字 就自动补零(首个字符为.的时候)

只能输入一个小数点

输入的金额要小于等于余额 如果大于,就把余额赋值给提现的金额

点击全部提现,也是把余额赋值给提现金额

javascript 复制代码
<input type="number" step="0.01" min="0" v-model="withdrawMoney" @input="validateInput">
<view class="btn" @click="apply">提现申请</view>
 
// 对输入的金额做处理
validateInput(e) {
  let inputValue = e.detail.value;
  let integerPart = parseInt(inputValue);  // 整数
  let decimalPart = inputValue - parseInt(inputValue);  // 小数
  // 移除非数字和小数点以外的字符  
  inputValue = inputValue.replace(/[^0-9.]/g, ''); 
  // 小数点只能输入两位小数,并去除多余的  
  if (inputValue.includes('.')) { 
    if (inputValue.indexOf('.') === inputValue.length - 1) {
      decimalPart = '.'
    } else if (inputValue.indexOf('.') === inputValue.length - 2) {  
      if (decimalPart == 0) {
        decimalPart = '.0'
      } else {
        decimalPart = parseFloat(decimalPart);
      }
    } else {
      decimalPart = inputValue.substr(inputValue.indexOf('.') + 1, 2);
      decimalPart = parseFloat(decimalPart / 100)
      console.log(decimalPart)
    } 
  }
  // 整数部分补0,只针对第一位数字为0的情况  
  if (inputValue.length === 1 && inputValue === '0') {  
    inputValue = '';  
    console.log(integerPart)
  }  else if (inputValue[0] === '.') {  
    integerPart = ''
    console.log(integerPart, decimalPart, inputValue)
  } else if (inputValue[0] !== '.') {   
    inputValue = inputValue
    console.log(integerPart, decimalPart, inputValue)
    if (integerPart[0] === '0' && integerPart.length >= 1) {  
      integerPart = integerPart.substr(1);  
      console.log(integerPart)
    }  
  }  
  // // 整数部分补0,只针对没有其他整数的情况  
  if (integerPart === '' && decimalPart === '') {  
    integerPart = '0';  
  } else if (integerPart === '' && decimalPart !== '') {  
    integerPart = 0;  
  }
  // 如果输入的值大于余额,则强制转换为余额值  
  if (parseFloat(inputValue) > this.amount) {  
    inputValue = this.amount;  
  } else {  
    console.log(integerPart, decimalPart)
    inputValue = integerPart + decimalPart; // 重新组合整数部分和小数部分,并更新v-model的值  
  }  
 
  this.$nextTick(() => {
    console.log(inputValue)
    this.withdrawMoney = inputValue 
  }); 
},
 
// 全部提现
handleAllWithdraw () {
  this.withdrawMoney = this.amount
},
 
// 提现
async apply() {
  const data = {
    amount: this.withdrawMoney,
    type: "weixin"
  }
  await takeMoney(data) 
  .then(result => {  
   // 成功
    this.amount = this.amount - this.withdrawMoney
    this.withdrawMoney = ''
      uni.showToast({  
      title: '申请提现成功',  
      icon: 'success',  
      duration: 1000  
      });  
  })  
  .catch(error => {  
      // 失败 
    this.withdrawMoney = ''
      uni.showToast({  
      title: '申请提现失败',  
      icon: 'none',  
      duration: 2000  
      });  
  })  
}
相关推荐
2501_916008893 小时前
iOS 性能测试的深度实战方法 构建从底层指标到真实场景回放的多工具测试体系
android·ios·小程序·https·uni-app·iphone·webview
2501_9159090616 小时前
iOS APP 抓包全流程解析,HTTPS 调试、网络协议分析与多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063216 小时前
游戏上架 App Store 的技术流程解析 从构建到审核的全流程指南
游戏·macos·ios·小程序·uni-app·cocoa·iphone
行云流水6261 天前
uniapp pinia实现数据持久化插件
前端·javascript·uni-app
zhangyao9403301 天前
uniapp动态修改 顶部导航栏标题和右侧按钮权限显示隐藏
前端·javascript·uni-app
2501_916007471 天前
iOS 压力测试的工程化体系,构建高强度、多维度、跨工具协同的真实负载测试流程
android·ios·小程序·uni-app·cocoa·压力测试·iphone
2501_916008891 天前
API接口调试全攻略 Fiddler抓包工具、HTTPS配置与代理设置实战指南
前端·ios·小程序·https·fiddler·uni-app·webview
2501_915921431 天前
iOS 开发者工具推荐,构建从调试到性能优化的多维度生产力工具链(2025 深度工程向)
android·ios·性能优化·小程序·uni-app·iphone·webview
00后程序员张1 天前
全面解析网络抓包工具使用:Wireshark和TCPDUMP教程
网络·ios·小程序·uni-app·wireshark·iphone·tcpdump
游戏开发爱好者81 天前
Mac 抓包软件怎么选?从 HTTPS 调试、TCP 数据流分析到多工具协同的完整抓包方案
tcp/ip·macos·ios·小程序·https·uni-app·iphone