recharge.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <uv-navbar title="充值" bgColor="#C6171E" leftIconColor="#FFFFFF" :titleStyle="{ color: '#FFFFFF' }" :autoBack="true" :placeholder="true"></uv-navbar>
  3. <view class="container">
  4. <view class="title">选择充值金额</view>
  5. <view class="amount-list">
  6. <view
  7. v-for="(item, index) in state.configList"
  8. :key="index"
  9. :class="['amount-item', { active: state.chosenIdx === index }]"
  10. @click="handleRechargeClick(item, index)"
  11. >
  12. <view class="amount-text">{{ (item.rechargeAmount / 100).toFixed(0) }}元</view>
  13. <view v-if="item.grantsAmount > 0" class="gift-tag">
  14. <uv-icon name="gift" size="12"></uv-icon>
  15. {{ (item.grantsAmount / 100).toFixed(0) }}元
  16. </view>
  17. </view>
  18. </view>
  19. <view class="selected-info" v-if="state.chosenIdx > -1">
  20. <view class="selected-row">
  21. <text class="selected-label">充值金额</text>
  22. <text class="selected-amount">¥{{ (state.configList[state.chosenIdx].rechargeAmount / 100).toFixed(2) }}</text>
  23. </view>
  24. <view class="selected-row gift-row" v-if="state.rechargeItem.grantsAmount > 0">
  25. <text class="selected-label">赠送金额</text>
  26. <view class="gift-value">
  27. <uv-icon name="gift" size="16"></uv-icon>
  28. <text class="gift-amount">+¥{{ (state.rechargeItem.grantsAmount / 100).toFixed(2) }}</text>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="pay-area">
  33. <uv-button
  34. type="primary"
  35. shape="circle"
  36. iconColor="#FFFFFF"
  37. color="#C6171E"
  38. text="立即充值"
  39. :loading="paying"
  40. :disabled="state.chosenIdx < 0 || paying"
  41. @click="debounceConfirm"
  42. ></uv-button>
  43. </view>
  44. </view>
  45. </template>
  46. <script setup lang="ts">
  47. import { computed, reactive, ref } from "vue";
  48. import { onHide, onLoad, onShow } from "@dcloudio/uni-app";
  49. import { debounce } from "@/utils/common";
  50. import { get } from "@/utils/https";
  51. const initState = () => ({
  52. configList: [] as any[],
  53. chosenIdx: -1,
  54. stationId: "000",
  55. rechargeItem: { grantsAmount: 0 } as any,
  56. });
  57. const state = reactive(initState());
  58. const paying = ref(false);
  59. onLoad((options: any) => {
  60. if (options.stationId) {
  61. state.stationId = options.stationId;
  62. }
  63. });
  64. onShow(() => {
  65. loadRechargeConfig();
  66. });
  67. onHide(() => {
  68. state.chosenIdx = -1;
  69. state.configList = [];
  70. state.rechargeItem = { grantsAmount: 0 };
  71. });
  72. const handleRechargeClick = (recharge: any, idx: number) => {
  73. state.chosenIdx = idx;
  74. state.rechargeItem = state.configList[idx];
  75. };
  76. const loadRechargeConfig = () => {
  77. // 优先使用归属站,无归属站时用当前消费站,都没有则不传(走默认分组)
  78. const homeStationId = getApp<any>().globalData.user?.stationId;
  79. const stationId = homeStationId || state.stationId || undefined;
  80. const params: any = {};
  81. if (stationId) {
  82. params.stationId = stationId;
  83. }
  84. get("/common/rechargeConfig", params)
  85. .then((res: any) => {
  86. state.configList = res;
  87. })
  88. .catch(() => {
  89. uni.showToast({ title: "加载失败,请下拉重试", icon: "none" });
  90. });
  91. };
  92. const debounceConfirm = debounce(() => {
  93. confirm();
  94. }, 500);
  95. const confirm = () => {
  96. if (state.chosenIdx < 0) return;
  97. paying.value = true;
  98. uni.showLoading({ title: "支付中..." });
  99. const recharge = state.configList[state.chosenIdx];
  100. get("/payment/wxPay", {
  101. rechargeConfigId: recharge.id,
  102. stationId: state.stationId,
  103. })
  104. .then((res: any) => {
  105. // #ifdef MP-WEIXIN
  106. wx.requestPayment({
  107. timeStamp: `${res.timeStamp}`,
  108. nonceStr: res.nonceStr,
  109. package: res.packageVal,
  110. signType: res.signType,
  111. paySign: res.paySign,
  112. success: () => {
  113. uni.showToast({ title: "充值完成", icon: "success" });
  114. getApp<any>().globalData.refresh = true;
  115. setTimeout(() => {
  116. uni.switchTab({ url: "/pages/user/index" });
  117. }, 500);
  118. },
  119. fail: () => {
  120. uni.hideLoading();
  121. uni.showToast({ title: "支付取消", icon: "none" });
  122. },
  123. });
  124. // #endif
  125. // #ifndef MP-WEIXIN
  126. uni.hideLoading();
  127. uni.showToast({ title: "目前仅支持微信支付", icon: "none" });
  128. // #endif
  129. })
  130. .catch(() => {
  131. uni.hideLoading();
  132. uni.showToast({ title: "网络异常,请重试", icon: "none" });
  133. })
  134. .finally(() => {
  135. paying.value = false;
  136. });
  137. };
  138. </script>
  139. <style lang="scss" scoped>
  140. .container {
  141. padding: 40rpx 30rpx;
  142. display: flex;
  143. flex-direction: column;
  144. min-height: 100vh;
  145. background-color: $uni-bg-color-page;
  146. box-sizing: border-box;
  147. }
  148. .title {
  149. font-size: 28rpx;
  150. text-align: center;
  151. margin-bottom: 48rpx;
  152. font-weight: $uni-font-weight-semibold;
  153. color: $uni-text-color;
  154. }
  155. // 金额选择网格
  156. .amount-list {
  157. display: grid;
  158. grid-template-columns: repeat(3, 1fr);
  159. gap: 20rpx;
  160. margin-bottom: 48rpx;
  161. width: 100%;
  162. box-sizing: border-box;
  163. }
  164. .amount-item {
  165. background-color: $uni-bg-color-card;
  166. padding: 24rpx 12rpx;
  167. text-align: center;
  168. border: 2rpx solid $uni-border-color;
  169. border-radius: 16rpx;
  170. color: $uni-text-color;
  171. min-height: 128rpx;
  172. display: flex;
  173. flex-direction: column;
  174. align-items: center;
  175. justify-content: center;
  176. gap: 8rpx;
  177. box-sizing: border-box;
  178. transition: transform 0.15s ease, background-color 0.15s ease;
  179. &:active {
  180. transform: scale(0.96);
  181. }
  182. &.active {
  183. background-color: $uni-color-primary;
  184. color: $uni-text-color-inverse;
  185. border-color: $uni-color-primary;
  186. transform: scale(1.03);
  187. }
  188. }
  189. .amount-text {
  190. font-size: 32rpx;
  191. font-weight: $uni-font-weight-bold;
  192. line-height: 1;
  193. }
  194. // 赠金标签 — 金额下方的小字标签,与卡片内联而非悬浮角标
  195. .gift-tag {
  196. display: inline-flex;
  197. align-items: center;
  198. gap: 4rpx;
  199. font-size: 24rpx;
  200. font-weight: $uni-font-weight-medium;
  201. color: $uni-color-primary;
  202. line-height: 1;
  203. }
  204. .amount-item.active .gift-tag {
  205. color: rgba(255, 255, 255, 0.85);
  206. }
  207. // 已选信息
  208. .selected-info {
  209. background-color: $uni-bg-color-card;
  210. border-radius: 16rpx;
  211. padding: 32rpx;
  212. margin-bottom: 48rpx;
  213. .selected-row {
  214. display: flex;
  215. justify-content: space-between;
  216. align-items: center;
  217. padding: 8rpx 0;
  218. .selected-label {
  219. font-size: 26rpx;
  220. color: $uni-text-color-secondary;
  221. }
  222. .selected-amount {
  223. font-size: 36rpx;
  224. font-weight: $uni-font-weight-bold;
  225. color: $uni-text-color;
  226. }
  227. }
  228. .gift-row {
  229. margin-top: 16rpx;
  230. padding-top: 20rpx;
  231. border-top: 1rpx solid $uni-border-color-light;
  232. .gift-value {
  233. display: flex;
  234. align-items: center;
  235. gap: 8rpx;
  236. color: $uni-color-primary;
  237. .gift-amount {
  238. font-size: 28rpx;
  239. font-weight: $uni-font-weight-semibold;
  240. }
  241. }
  242. }
  243. }
  244. // 支付按钮区域
  245. .pay-area {
  246. width: calc(100vw - 60rpx);
  247. position: fixed;
  248. bottom: 40rpx;
  249. bottom: calc(40rpx + constant(safe-area-inset-bottom));
  250. bottom: calc(40rpx + env(safe-area-inset-bottom));
  251. left: 30rpx;
  252. }
  253. </style>