recharge.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view class="page">
  3. <view class="recharge">
  4. <view v-for="(recharge,idx) in state.configList" :key="idx"
  5. @click="handleRechargeClick(recharge,idx)"
  6. class="recharge-item" :class="idx===state.chosenIdx?'active':''">
  7. <view class="recharge-item_amt">{{ (recharge.rechargeAmount/100).toFixed(2) }}</view>
  8. <view class="recharge-item_grant" v-if="recharge.grantsAmount>0">
  9. <uv-icon name="gift" :color="idx===state.chosenIdx?'#C6171E':'#2b2b2b'" size="14px"></uv-icon>
  10. {{ (recharge.grantsAmount/100).toFixed(2) }}元
  11. </view>
  12. </view>
  13. </view>
  14. <view class="recharge-pay">
  15. <uv-button type="primary" iconColor="white" color="#C6171E" text="充值" @click="debounceConfirm"></uv-button>
  16. </view>
  17. </view>
  18. </template>
  19. <script setup lang="ts">
  20. import {reactive, ref} from "vue";
  21. import {onHide, onLoad, onShow} from "@dcloudio/uni-app";
  22. import {debounce} from "@/utils/common";
  23. import {get, post,body} from "@/utils/https";
  24. const initState = () => ({
  25. configList: [],
  26. chosenIdx: -1,
  27. stationId:'000'
  28. })
  29. const state = reactive(initState())
  30. onLoad((options:any)=>{
  31. console.log(options)
  32. if(options.stationId){
  33. state.stationId = options.stationId;
  34. }
  35. })
  36. onShow(() => {
  37. loadRechargeConfig();
  38. })
  39. onHide(() => {
  40. Object.assign(state, initState());
  41. })
  42. const handleRechargeClick = (recharge: any, idx: number) => {
  43. state.chosenIdx = idx;
  44. }
  45. const loadRechargeConfig = () => {
  46. get(`/common/rechargeConfig`).then((res: any) => {
  47. state.configList = res;
  48. })
  49. }
  50. const debounceConfirm = debounce(() => {
  51. confirm();
  52. }, 500)
  53. const confirm = () => {
  54. if (state.chosenIdx < 0) {
  55. return;
  56. }
  57. uni.showLoading({
  58. title: "",
  59. });
  60. let recharge = state.configList[state.chosenIdx]
  61. get("/payment/wxPay", {rechargeConfigId:recharge.id,stationId:state.stationId}).then((res: any) => {
  62. // #ifdef MP-WEIXIN
  63. wx.requestPayment({
  64. timeStamp: `${res.timeStamp}`,
  65. nonceStr: res.nonceStr,
  66. package: res.packageVal,
  67. signType: res.signType,
  68. paySign: res.paySign,
  69. success:function (res: any) {
  70. console.log(res)
  71. uni.showToast({
  72. title:'充值完成',
  73. icon:'success'
  74. })
  75. setTimeout(()=>{
  76. uni.redirectTo({
  77. url:'/pages/user/index'
  78. })
  79. },500)
  80. },
  81. fail: function (e) {
  82. console.error(e)
  83. },
  84. });
  85. // #endif
  86. // #ifndef MP-WEIXIN
  87. uni.showToast({title: '目前仅支持微信支付'})
  88. // #endif
  89. });
  90. }
  91. </script>
  92. <style lang="scss" scoped>
  93. .page {
  94. min-height: 100vh;
  95. width: 100%;
  96. background-color: #fff;
  97. }
  98. .recharge {
  99. display: flex;
  100. flex-wrap: wrap;
  101. justify-content: end;
  102. align-items: start;
  103. align-content: center;
  104. &-item {
  105. display: flex;
  106. flex-direction: column;
  107. width: 200rpx;
  108. height: 100rpx;
  109. background-color: #eeefff;
  110. margin: 20rpx auto;
  111. padding: 10rpx;
  112. border-radius: 10rpx;
  113. &_amt {
  114. font-size: 20px;
  115. font-weight: 600;
  116. }
  117. &_grant {
  118. font-size: 12px;
  119. margin-top: 4rpx;
  120. display: inline-flex;
  121. }
  122. }
  123. }
  124. .active {
  125. border-radius: 10rpx solid $uni-color-primary;
  126. .recharge-item {
  127. &_amt {
  128. color: $uni-color-primary;
  129. }
  130. &_grant {
  131. color: $uni-color-primary;
  132. }
  133. }
  134. }
  135. .recharge-pay {
  136. width: calc(100vw - 40rpx);
  137. position: fixed;
  138. bottom: 10rpx;
  139. left: 20rpx;
  140. }
  141. </style>