uniapp-提现功能(demo)

文章详细描述了在页面布局中,如何使用Vue的v-model实现提现功能,包括输入框的金额验证逻辑,如限制小数位数、补零、金额不超过余额等,并展示了全部提现和调用后端接口的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面布局

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

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

输入框逻辑分析

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

  1. 因为是提现 所以对输入的字符做筛选,只允许出现小数点和数字 这里用正则实现的
  2. 小数点后只能输入两位小数, 超过两位的去除掉 因为提现的最小金额是两位数
  3. .前面如果没有数字 就自动补零(首个字符为.的时候)
  4. 只能输入一个小数点
  5. 输入的金额要小于等于余额 如果大于,就把余额赋值给提现的金额

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

参考代码

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

然后调后端给的接口 携带需要的数据 发对应的请求就可以了

uniapp中实现提现功能通常需要与后端服务器进行交互,并使用支付平台的API来完成。以下是一个基本的实现步骤: 1. **前端页面设计**: - 创建一个提现页面,用户可以输入提现金额和选择提现方式(如支付宝、微信等)。 - 添加表单验证,确保用户输入的金额合法。 2. **前端与后端交互**: - 使用uniapp的网络请求API(如`uni.request`)将用户输入的提现金额和方式发送到后端服务器。 3. **后端处理**: - 后端接收到请求后,验证用户信息和提现金额。 - 调用支付平台的API(如支付宝的转账API或微信的商户转账API)进行转账操作。 - 处理支付平台的回调,确保转账成功。 4. **前端接收结果**: - 后端处理完成后,返回结果给前端,前端根据结果提示用户提现成功或失败。 以下是一个简单的示例代码: ```javascript // 前端页面 <template> <view> <form @submit="handleWithdraw"> <input v-model="amount" placeholder="提现金额" type="number" /> <button type="submit">提现</button> </form> </view> </template> <script> export default { data() { return { amount: '' }; }, methods: { handleWithdraw(e) { e.preventDefault(); // 表单验证 if (this.amount <= 0) { uni.showToast({ title: '金额必须大于0', icon: 'none' }); return; } // 发送请求到后端 uni.request({ url: 'https://your-backend-api.com/withdraw', method: 'POST', data: { amount: this.amount, // 其他必要的数据,如用户ID等 }, success: (res) => { if (res.data.success) { uni.showToast({ title: '提现成功', icon: 'success' }); } else { uni.showToast({ title: '提现失败', icon: 'none' }); } }, fail: () => { uni.showToast({ title: '请求失败', icon: 'none' }); } }); } } }; </script> ``` ```javascript // 后端处理示例(以Node.js为例) const express = require('express'); const app = express(); const bodyParser = require('body-parser'); const axios = require('axios'); app.use(bodyParser.json()); app.post('/withdraw', async (req, res) => { const { amount, userId } = req.body; // 验证用户信息和金额 if (!userId || amount <= 0) { return res.json({ success: false, message: '无效的请求' }); } // 调用支付平台的API进行转账 try { const response = await axios.post('https://payment-platform-api.com/transfer', { amount, userId, // 其他必要的数据 }); if (response.data.success) { return res.json({ success: true, message: '提现成功' }); } else { return res.json({ success: false, message: '提现失败' }); } } catch (error) { return res.json({ success: false, message: '提现失败' }); } }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值