withdraw.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 class="alter_info">若提现不成功,将在24小时内返回您的余额,可重新提现</view>
  14. </view>
  15. <button class="withdraw_btn" :class="isDisabled ? 'disabled' : ''" @click="_getMoney">确定提现</button>
  16. </view>
  17. </template>
  18. <script setup>
  19. import { ref, watch, onMounted } from 'vue';
  20. import http from '@/utils/request';
  21. import common from '@/utils/common';
  22. const withdrawAmount = ref(0);
  23. const maxWithdrawAmount = ref(0);
  24. const isDisabled = ref(true);
  25. const userInfo = ref({});
  26. const loading = ref(false);
  27. onMounted(() => {
  28. _getUserInfo();
  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. uni.setStorageSync('userInfo', callback.data);
  98. }
  99. });
  100. };
  101. watch(withdrawAmount, (newValue) => {
  102. isDisabled.value = isNaN(newValue) || newValue == 0;
  103. });
  104. </script>
  105. <style lang="less" scoped>
  106. .withdraw {
  107. width: 100vw;
  108. height: 100vh;
  109. padding: 46rpx 26rpx;
  110. display: flex;
  111. flex-direction: column;
  112. align-items: center;
  113. justify-content: space-between;
  114. box-sizing: border-box;
  115. background-color: #ffffff;
  116. .withdraw_content {
  117. border: 1px solid #f3f3f3;
  118. border-radius: 6px;
  119. width: 100%;
  120. padding: 20rpx;
  121. box-sizing: border-box;
  122. .title {
  123. font-weight: bold;
  124. margin-bottom: 36rpx;
  125. }
  126. .content {
  127. position: relative;
  128. border-bottom: 2rpx solid #f3f3f3;
  129. padding: 10rpx 0;
  130. box-sizing: border-box;
  131. input {
  132. width: 100%;
  133. height: 60rpx;
  134. font-size: 36rpx;
  135. border-radius: 6px;
  136. padding-left: 45rpx;
  137. }
  138. view {
  139. position: absolute;
  140. left: 5rpx;
  141. top: 50%;
  142. transform: translateY(-50%);
  143. font-size: 36rpx;
  144. }
  145. }
  146. .bottom {
  147. display: flex;
  148. justify-content: space-between;
  149. align-items: center;
  150. font-size: 28rpx;
  151. margin-top: 20rpx;
  152. }
  153. }
  154. .withdraw_btn {
  155. width: 100%;
  156. height: 70rpx;
  157. background-color: #169bd5;
  158. color: #ffffff;
  159. font-size: 36rpx;
  160. border-radius: 10rpx;
  161. line-height: 70rpx;
  162. box-shadow: 0rpx 5rpx 10rpx #169bd5;
  163. &.disabled {
  164. background-color: #97d2ec;
  165. color: #fff;
  166. box-shadow: none;
  167. }
  168. }
  169. }
  170. .alter_info {
  171. display: block;
  172. color: #f89c33;
  173. font-size: 20rpx;
  174. overflow: hidden;
  175. margin: 20rpx auto;
  176. background: #ffffff;
  177. line-height: 40rpx;
  178. padding: 35rpx 35rpx;
  179. text-align: center;
  180. }
  181. </style>