withdraw.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="withdraw" catchtouchmove="true">
  3. <view class="withdraw_content">
  4. <view class="title">提现金额</view>
  5. <view class="content">
  6. <view>¥</view>
  7. <input type="digit" class="input" v-model="withdrawAmount" @blur="_valiteMoney" />
  8. </view>
  9. <view class="bottom">
  10. <text>当前可提现余额{{ maxWithdrawAmount }}元</text>
  11. <view style="color: #5baff4" @click="_getAll">全部提现</view>
  12. </view>
  13. </view>
  14. <button class="withdraw_btn" :class="isDisabled ? 'disabled' : ''" @click="_getMoney">确定提现</button>
  15. </view>
  16. </template>
  17. <script setup>
  18. import { ref, watch, onMounted } from "vue";
  19. import http from "@/utils/request";
  20. import common from "@/utils/common";
  21. const withdrawAmount = ref(0);
  22. const maxWithdrawAmount = ref(0);
  23. const isDisabled = ref(true);
  24. const userInfo = ref({});
  25. const loading = ref(false);
  26. onMounted(() => {
  27. _getUserInfo();
  28. });
  29. const _valiteMoney = (e) => {
  30. if (!isNaN(e.target.value)) {
  31. if (maxWithdrawAmount.value < e.target.value) {
  32. withdrawAmount.value = maxWithdrawAmount.value;
  33. } else withdrawAmount.value = e.target.value;
  34. } else {
  35. // 如果不是数字,可以设置为0或者其他默认值
  36. withdrawAmount.value = 0;
  37. }
  38. };
  39. const _getAll = () => {
  40. withdrawAmount.value = maxWithdrawAmount.value;
  41. };
  42. const _getMoney = common.debounce(async () => {
  43. if (isDisabled.value || loading.value) return;
  44. if (withdrawAmount.value < 0.1) {
  45. uni.showToast({ icon: "none", title: "提现金额不能小于0.1元" });
  46. return;
  47. }
  48. loading.value = true;
  49. uni.showLoading({ title: "正在发起提现..." });
  50. try {
  51. const loginRes = await uni.login({ provider: "weixin" });
  52. const response = await http.request("/api/wechat_transfer/transfer", { code: loginRes.code, amount: withdrawAmount.value }, "POST");
  53. if (response.code !== "success") {
  54. throw new Error(response.msg);
  55. }
  56. const systemInfo = uni.getSystemInfoSync();
  57. if (systemInfo.uniPlatform !== "mp-weixin" || !wx.canIUse("requestMerchantTransfer")) {
  58. throw new Error("你的微信版本过低,请更新至最新版本。");
  59. }
  60. await new Promise((resolve, reject) => {
  61. wx.requestMerchantTransfer({
  62. mchId: "1612111355",
  63. appId: wx.getAccountInfoSync().miniProgram.appId,
  64. package: response.data.package_info,
  65. success: resolve,
  66. fail: reject,
  67. });
  68. });
  69. uni.showModal({
  70. title: "温馨提示",
  71. content: "您已成功提现,请注意查看信息",
  72. showCancel: false,
  73. confirmText: "确认",
  74. success: (res) => {
  75. if (res.confirm) {
  76. _getUserInfo();
  77. }
  78. },
  79. });
  80. } catch (error) {
  81. uni.showModal({
  82. title: "温馨提示",
  83. content: error.message || "提现失败,请稍后再试",
  84. showCancel: false,
  85. confirmText: "确认",
  86. });
  87. } finally {
  88. loading.value = false;
  89. uni.hideLoading();
  90. }
  91. }, 300);
  92. const _getUserInfo = () => {
  93. http.request("api/custom/get_info").then((callback) => {
  94. if (callback.code == "success") {
  95. maxWithdrawAmount.value = callback.data.amount;
  96. uni.setStorageSync("userInfo", callback.data);
  97. }
  98. });
  99. };
  100. watch(withdrawAmount, (newValue) => {
  101. isDisabled.value = isNaN(newValue) || newValue == 0;
  102. });
  103. </script>
  104. <style lang="less" scoped>
  105. .withdraw {
  106. width: 100vw;
  107. height: 100vh;
  108. padding: 46rpx 26rpx;
  109. display: flex;
  110. flex-direction: column;
  111. align-items: center;
  112. justify-content: space-between;
  113. box-sizing: border-box;
  114. background-color: #ffffff;
  115. .withdraw_content {
  116. border: 1px solid #f3f3f3;
  117. border-radius: 6px;
  118. width: 100%;
  119. padding: 20rpx;
  120. box-sizing: border-box;
  121. .title {
  122. font-weight: bold;
  123. margin-bottom: 36rpx;
  124. }
  125. .content {
  126. position: relative;
  127. border-bottom: 2rpx solid #f3f3f3;
  128. padding: 10rpx 0;
  129. box-sizing: border-box;
  130. input {
  131. width: 100%;
  132. height: 60rpx;
  133. font-size: 36rpx;
  134. border-radius: 6px;
  135. padding-left: 45rpx;
  136. }
  137. view {
  138. position: absolute;
  139. left: 5rpx;
  140. top: 50%;
  141. transform: translateY(-50%);
  142. font-size: 36rpx;
  143. }
  144. }
  145. .bottom {
  146. display: flex;
  147. justify-content: space-between;
  148. align-items: center;
  149. font-size: 28rpx;
  150. margin-top: 20rpx;
  151. }
  152. }
  153. .withdraw_btn {
  154. width: 100%;
  155. height: 70rpx;
  156. background-color: #169bd5;
  157. color: #ffffff;
  158. font-size: 36rpx;
  159. border-radius: 10rpx;
  160. line-height: 70rpx;
  161. box-shadow: 0rpx 5rpx 10rpx #169bd5;
  162. &.disabled {
  163. background-color: #97d2ec;
  164. color: #fff;
  165. box-shadow: none;
  166. }
  167. }
  168. }
  169. </style>