recharge.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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?'#19A497':'#2b2b2b'"></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="#19A497" 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. })
  28. const state = reactive(initState())
  29. onShow(() => {
  30. loadRechargeConfig();
  31. })
  32. onHide(() => {
  33. Object.assign(state, initState());
  34. })
  35. const handleRechargeClick = (recharge: any, idx: number) => {
  36. state.chosenIdx = idx;
  37. }
  38. const loadRechargeConfig = () => {
  39. get(`/common/rechargeConfig`).then((res: any) => {
  40. state.configList = res;
  41. })
  42. }
  43. const debounceConfirm = debounce(() => {
  44. confirm();
  45. }, 500)
  46. const confirm = () => {
  47. if (state.chosenIdx < 0) {
  48. return;
  49. }
  50. uni.showLoading({
  51. title: "",
  52. });
  53. let recharge = state.configList[state.chosenIdx]
  54. get("/payment/wxPay", {rechargeConfigId:recharge.id}).then((res: any) => {
  55. // #ifdef MP-WEIXIN
  56. wx.requestPayment({
  57. timeStamp: `${res.timeStamp}`,
  58. nonceStr: res.nonceStr,
  59. package: res.packageVal,
  60. signType: res.signType,
  61. paySign: res.paySign,
  62. success:function (res: any) {
  63. console.log(res)
  64. uni.redirectTo({
  65. url:'/pages-user/user/index'
  66. })
  67. },
  68. fail: function (e) {
  69. console.error(e)
  70. },
  71. });
  72. // #endif
  73. // #ifndef MP-WEIXIN
  74. uni.showToast({title: '目前仅支持微信支付'})
  75. // #endif
  76. });
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. .page {
  81. min-height: 100vh;
  82. width: 100%;
  83. background-color: #fff;
  84. }
  85. .recharge {
  86. display: flex;
  87. flex-wrap: wrap;
  88. justify-content: end;
  89. align-items: start;
  90. align-content: center;
  91. &-item {
  92. display: flex;
  93. flex-direction: column;
  94. width: 200rpx;
  95. height: 100rpx;
  96. background-color: #eeefff;
  97. margin: 20rpx auto;
  98. padding: 10rpx;
  99. border-radius: 10rpx;
  100. &_amt {
  101. font-size: 20px;
  102. font-weight: 600;
  103. }
  104. &_grant {
  105. font-size: 12px;
  106. margin-top: 10rpx;
  107. display: inline-flex;
  108. }
  109. }
  110. }
  111. .active {
  112. border-radius: 10rpx solid $uni-color-primary;
  113. .recharge-item {
  114. &_amt {
  115. color: $uni-color-primary;
  116. }
  117. &_grant {
  118. color: $uni-color-primary;
  119. }
  120. }
  121. }
  122. .recharge-pay {
  123. width: calc(100vw - 40rpx);
  124. position: fixed;
  125. bottom: 10rpx;
  126. left: 20rpx;
  127. }
  128. </style>