withdraw.vue 4.8 KB

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