Liuyi
2024-12-20 d470e67ac1997882502b75cbfdaf359626cfaaa8
pages/wxScanWater/index.vue
@@ -1,6 +1,6 @@
<script setup>
   import{ ref ,onMounted} from 'vue'
   import { wxPayApi,buyWaterApi,getPriceBySnApi,getVipInfoApi } from '../../api/index.js'
   import { wxPayApi,buyWaterApi,getPriceBySnApi,getVipInfoApi,wxLoginApi } from '../../api/index.js'
   import { onLoad } from "@dcloudio/uni-app"
   //选中金额
   const choosedMoney = ref(1)
@@ -37,15 +37,16 @@
      cardNumber:'',
      cardBalance:'',
   })
   //获取用户会员卡信息,判断是否有余额
   async function setPayMethod(){
      await getVipInfoApi().then((res) =>{
         console.log('获取会员卡返回响应-微信扫码取水页',res)
         console.log('会员卡信息响应-微信扫码取水',res)
         if(res.code == 200 && res.data.id){
            cardInfo.value.cardNumber = res.data.cardNumber   
            cardInfo.value.cardBalance = res.data.balance   
         }else{
            console.log('未绑定会员卡')
            payList.value.splice(1,1)
            console.log('未绑定会员卡','支付方式',payList.value)
         }
      })      
   }
@@ -60,7 +61,14 @@
      payMethod.value = payList.value[index1].value
      console.log('index',index1,payMethod.value)
   }
      let payState = ref(true)
      let timerId
   async function submit(){
      if(payState.value){
         payState.value = false
         timerId = setTimeout(() =>{
            payState.value = true
         },20000)
      if(payMethod.value == 1){
         //余额支付,比较金额大小
         if(choosedMoney.value > Number(cardInfo.value.cardBalance)){
@@ -156,12 +164,19 @@
            }
         })
      }
      }else{
         uni.showToast({
            title:'支付间隔20秒,请稍后!',
            icon:'none'
         })
      }
   }
   const facilityCode = ref()
   const facilityCode = ref('')
   onLoad((query) =>{
      let url = decodeURIComponent(query.q)
      // let url = 'https://www.huiwuyuntong.com/miniapp/paywater?facilitycode=1060003979'
      facilityCode.value = url.split('=')[1]
      console.log('微信扫码取水','url',url,'facilityCode',facilityCode.value)
      console.log('微信扫码取水','url',url,'设备SN',facilityCode.value)
   })
   //设置水价
   async function getWaterPrice(){
@@ -171,7 +186,7 @@
            item.volume = ((index + 1) * res.data.yield).toFixed(1)
         })
         choosedMoney.value = res.data.price
         console.log('水价设置回调',res,'传参',facilityCode.value)
         console.log('水价设置响应',res,'传参设备SN',facilityCode.value,'水价显示列表',choosedMoney.value)
      })
   }
   function navTo(){
@@ -179,10 +194,54 @@
         url:'/pages/index/index'
      })
   }
   onMounted(async() =>{
      await getWaterPrice()
      await setPayMethod()
   })
   //用户通过微信外部扫描二维码进入页面,默认微信登录授权
   async function wxLogin() {
       if (!uni.getStorageSync('openId') || !uni.getStorageSync('token')) {
           try {
               const res1 = await new Promise((resolve, reject) => {
                   wx.login({
                       success: (result) => resolve(result),
                       fail: (error) => reject(error)
                   });
               });
               uni.setStorageSync('code', res1.code);
               const res2 = await wxLoginApi({ code: res1.code });
               console.log('微信登录响应', res2);
               if (res2.code == 200) {
                   uni.setStorageSync('openId', res2.data.userId);
                   uni.setStorageSync('token', res2.data.token);
               } else {
                   uni.showToast({
                       title: '登录失败',
                       icon: 'none'
                   });
                   throw new Error('登录失败'); // 抛出错误,以便在调用 wxLogin 的地方可以捕获
               }
           } catch (error) {
               uni.showToast({
                   title: '微信登录失败,请退出重试!',
                   duration: 2000,
                   icon: 'fail'
               });
               throw error; // 抛出错误,以便在调用 wxLogin 的地方可以捕获
           }
       }
   }
   onMounted(async () => {
       try {
           await wxLogin();
           await getWaterPrice();
           await setPayMethod();
       } catch (error) {
           console.error('在登录或初始化过程中发生错误:', error);
           // 可以在这里处理错误,比如显示错误消息给用户
       }
   });
   
</script>
<template>
@@ -227,7 +286,7 @@
               </view>
            </view>
         </view>
         <view class="submit-btn" @click="submit()">确认支付</view>
         <view :style="{opacity:payState ? 1 : 0.7}" class="submit-btn" @click="submit()">确认支付</view>
      </view>
   </view>
</template>