wallet-recharge.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view class="pl-30 pr-30">
  3. <view class="pay">
  4. <view class="title pt-30 pb-30">充值金额</view>
  5. <view class="flex-wrap">
  6. <view
  7. :class="[
  8. 'option',
  9. 'flex-center',
  10. `option-${index === payOption && !payValue ? 'active' : ''}`,
  11. ]"
  12. v-for="(item, index) in payOptions"
  13. :key="index"
  14. @click="changeOption(index)"
  15. >
  16. <view
  17. class="tag fs-24 color-fff fw-500"
  18. v-if="payOptionsDiscount[index]"
  19. >{{ payOptionsDiscount[index] }}</view
  20. >
  21. {{ item }}
  22. </view>
  23. </view>
  24. <view v-if="payOptionsDiscountDay[payOption]" class="flex-align-center">
  25. <view class="fs-30 color-666"
  26. >折扣优惠有效期{{ payOptionsDiscountDay[payOption] }}天</view
  27. >
  28. <view
  29. class="ml-12 color-primary fs-30"
  30. @click="to(`/pages-common/activity/activity?id=${activityId}`)"
  31. >
  32. <text>详细规则</text>
  33. <text class="fs-24 ml-6">>></text>
  34. </view>
  35. </view>
  36. <view class="title pt-60 pb-30">自定义金额</view>
  37. <style-input
  38. :value="payValue > 0 ? payValue : ''"
  39. title="金额"
  40. type="digit"
  41. @input="input"
  42. />
  43. </view>
  44. </view>
  45. <style-bottom-view>
  46. <view class="pl-60 pr-60 pb-20">
  47. <style-button size="small" type="primary" @click="confirm"
  48. >充值</style-button
  49. >
  50. </view>
  51. </style-bottom-view>
  52. </template>
  53. <script setup lang="ts">
  54. import { ref } from "vue";
  55. import { fetchProfile, insertMoney } from "../../api/user";
  56. import { onLoad } from "@dcloudio/uni-app";
  57. import { to, back } from "@/utils/navigate";
  58. const balance = ref(0);
  59. const payOption = ref(3);
  60. const payOptions = ref([30, 50, 100, 200, 500, 1000]);
  61. const payOptionsDiscount = ref(["", "", "", "", "", ""]);
  62. const payOptionsDiscountDay = ref([0, 0, 0, 0, 0, 0]);
  63. const payValue = ref(0);
  64. const activityId = ref();
  65. const needBack = ref(false);
  66. const input = (e: any) => {
  67. payValue.value = e.value;
  68. };
  69. const changeOption = (index: number) => {
  70. payValue.value = 0;
  71. payOption.value = index;
  72. };
  73. const confirm = () => {
  74. if (payValue.value && !/^[0-9]*(\.\d{1,2})?$/.test(`${payValue.value}`)) {
  75. uni.showModal({
  76. title: "温馨提示",
  77. content: "请输入正确的金额",
  78. showCancel: false,
  79. confirmColor: "#347DFF",
  80. });
  81. return;
  82. }
  83. const params = payValue.value
  84. ? Number(payValue.value)
  85. : payOptions.value[payOption.value];
  86. if (params > 10000 || params <= 0) {
  87. uni.showModal({
  88. title: "温馨提示",
  89. content: "每次最大充值金额10000,请修改金额",
  90. showCancel: false,
  91. confirmColor: "#347DFF",
  92. });
  93. return;
  94. }
  95. uni.showLoading({
  96. title: "加载中",
  97. });
  98. insertMoney(params)
  99. .then(() => {
  100. return fetchProfile();
  101. })
  102. .then((res) => {
  103. payValue.value = 0;
  104. balance.value = res.balance;
  105. uni.hideLoading();
  106. uni.showToast({
  107. title: "已支付",
  108. icon: "success",
  109. });
  110. setTimeout(() => {
  111. if (needBack.value) {
  112. back();
  113. } else {
  114. to("/pages-user/wallet/wallet");
  115. }
  116. }, 2000)
  117. })
  118. .catch((err) => {
  119. if (/cancel/.test(err.errMsg)) {
  120. return;
  121. }
  122. uni.showModal({
  123. content: `${err.errMsg},请重试`,
  124. });
  125. });
  126. };
  127. onLoad((options: any) => {
  128. if (options.value) {
  129. payOption.value = payOptions.value.findIndex(
  130. (item) => item === Number((Number(options.value) / 100).toFixed(2))
  131. );
  132. needBack.value = !!options.back;
  133. }
  134. fetchProfile().then((res) => {
  135. // console.log(res);
  136. balance.value = res.balance;
  137. if (res && res.activityList && res.activityList.length) {
  138. res.activityList[0].rechargeRightsList.forEach((item: any) => {
  139. const val = Number((Number(item.amountMin) / 100).toFixed(2));
  140. const fi = payOptions.value.findIndex((o) => o === val);
  141. if (fi >= 0) {
  142. payOptionsDiscount.value[fi] = item.rightsDesc;
  143. payOptionsDiscountDay.value[fi] = item.validity;
  144. }
  145. });
  146. activityId.value = res.activityList[0].id;
  147. }
  148. });
  149. });
  150. </script>
  151. <style lang="scss">
  152. .pay {
  153. .title {
  154. font-weight: 500;
  155. font-size: 32rpx;
  156. color: #000;
  157. }
  158. .option {
  159. position: relative;
  160. width: 214rpx;
  161. height: 140rpx;
  162. background: var(--color-sec);
  163. border-radius: 10rpx;
  164. margin-left: 20rpx;
  165. margin-bottom: 20rpx;
  166. font-size: 36rpx;
  167. color: #000;
  168. font-weight: 500;
  169. overflow: hidden;
  170. &:nth-child(3n + 1) {
  171. margin-left: 0;
  172. }
  173. .tag {
  174. position: absolute;
  175. top: 0px;
  176. right: 0px;
  177. border-radius: 30rpx 0px 0px 30rpx;
  178. box-sizing: content-box;
  179. padding: 2rpx 10rpx;
  180. background: linear-gradient(90deg, #f366ff 0%, #5e98ff 100%);
  181. white-space: nowrap;
  182. }
  183. }
  184. .option-active {
  185. background-color: var(--color-primary);
  186. color: #fff;
  187. }
  188. }
  189. </style>