敢为人鲜小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
14 KiB

1 month ago
  1. <template>
  2. <view class="wallet-page">
  3. <!-- 导航栏 -->
  4. <navbar title="钱包" leftClick @leftClick="$utils.navigateBack" bgColor="#019245" color="#fff" />
  5. <!-- 总余额展示区 -->
  6. <view class="balance-card" :style="{ backgroundImage: `url(${configList.config_money_image})` }">
  7. <view class="balance-info">
  8. <view class="balance-title">总余额</view>
  9. <view class="balance-amount">{{ userInfo.balance ? userInfo.balance.toFixed(2) : '0.00' }}</view>
  10. <view class="balance-actions">
  11. <view class="action-btn recharge-btn" v-if="!isRecharge" @tap="navigateToRecharge">
  12. <text>去充值</text>
  13. <text class="arrow">></text>
  14. </view>
  15. <view class="action-btn recharge-btn" v-if="isRecharge" @tap="isRecharge = false">
  16. <text>提现</text>
  17. <text class="arrow">></text>
  18. </view>
  19. <view class="action-btn" v-else />
  20. <view class="action-btn detail-btn" @tap="navigateToDetail">
  21. <text>资产明细</text>
  22. <text class="arrow">></text>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <!-- 提现表单 -->
  28. <view class="withdraw-section">
  29. <view class="section-title">{{ isRecharge ? '我要充值' : '我要提现' }}</view>
  30. <!-- 提现金额输入框 -->
  31. <view class="input-item">
  32. <text class="currency-symbol">¥</text>
  33. <input v-if="!isRecharge" class="amount-input" type="digit" v-model="withdrawAmount"
  34. placeholder="请输入提现金额" @blur="validateAmount" />
  35. <input v-else class="amount-input" type="digit" v-model="rechargeAmount" placeholder="请输入充值金额"
  36. @blur="validateAmount" />
  37. </view>
  38. <!-- 真实姓名输入框 -->
  39. <view class="input-item" v-if="!isRecharge">
  40. <input class="name-input" type="nickname" v-model="realName" placeholder="请输入真实姓名"
  41. @blur="validateName" />
  42. </view>
  43. <!-- 提现说明 -->
  44. <view class="withdraw-notes" v-if="!isRecharge">
  45. <view class="notes-title">提现说明</view>
  46. <view class="notes-list">
  47. <view class="note-item" v-for="(rule, index) in walletData.withdrawRules" :key="index">
  48. <text>{{ index + 1 }}{{ rule }}</text>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 提现按钮 -->
  54. <view class="submit-btn-wrapper">
  55. <button class="submit-btn" @tap="submitWithdraw" :disabled="!isFormValid">
  56. {{ isRecharge ? '立即充值' : '立即提现' }}
  57. </button>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import navbar from '@/components/base/navbar.vue'
  63. // import { walletData } from '@/static/js/mockWallet.js'
  64. export default {
  65. components: {
  66. navbar
  67. },
  68. data() {
  69. return {
  70. walletData: null,
  71. withdrawAmount: '',
  72. rechargeAmount: '',
  73. realName: '',
  74. amountError: '',
  75. nameError: '',
  76. isFormValid: true,
  77. isRecharge: false
  78. }
  79. },
  80. onLoad() {
  81. // this.walletData = walletData
  82. },
  83. methods: {
  84. // 导航到充值页面
  85. navigateToRecharge() {
  86. this.isRecharge = true
  87. },
  88. // 导航到资产明细页面
  89. navigateToDetail() {
  90. this.$utils.navigateTo('/pages_order/mine/assets')
  91. },
  92. // 验证提现金额
  93. validateAmount() {
  94. if (!this.withdrawAmount) {
  95. this.amountError = '请输入提现金额'
  96. return false
  97. }
  98. const amount = parseFloat(this.withdrawAmount)
  99. if (isNaN(amount) || amount <= 0) {
  100. this.amountError = '请输入有效的提现金额'
  101. return false
  102. }
  103. if (amount > this.userInfo.balance) {
  104. this.amountError = '提现金额不能大于余额'
  105. return false
  106. }
  107. if (amount > 200) {
  108. this.amountError = '单笔提现不能超过200元'
  109. return false
  110. }
  111. this.amountError = ''
  112. return true
  113. },
  114. // 验证真实姓名
  115. validateName() {
  116. if (!this.realName) {
  117. this.nameError = '请输入真实姓名'
  118. return false
  119. }
  120. if (this.realName.length < 2) {
  121. this.nameError = '请输入有效的姓名'
  122. return false
  123. }
  124. this.nameError = ''
  125. return true
  126. },
  127. // 提交提现申请
  128. submitWithdraw() {
  129. if (this.isRecharge) {
  130. return this.recharge()
  131. }
  132. // 再次验证表单
  133. if (!this.validateAmount() || !this.validateName()) {
  134. // 显示具体错误
  135. if (this.amountError) {
  136. uni.showToast({
  137. title: this.amountError,
  138. icon: 'error'
  139. })
  140. return
  141. }
  142. if (this.nameError) {
  143. uni.showToast({
  144. title: this.nameError,
  145. icon: 'error'
  146. })
  147. return
  148. }
  149. return
  150. }
  151. // 如果在isFormVaild为false的情况下进入函数 则为多次点击 直接返回
  152. if (this.isFormValid) {
  153. this.isFormValid = false
  154. }else return
  155. // 显示提交中状态
  156. uni.showLoading({
  157. title: '提交中...'
  158. })
  159. // 提现
  160. this.$api('cashout', { transferAmount: this.withdrawAmount, userName: this.realName }, res => {
  161. uni.hideLoading()
  162. if (res.code === 200) {
  163. // 处理待收款用户确认的情况
  164. if (res.result && res.result.state === 'WAIT_USER_CONFIRM' && res.result.packageInfo && res.result.outBillNo) {
  165. // 拉起微信收款确认页面
  166. if (!wx.canIUse('requestMerchantTransfer')) {
  167. wx.showModal({
  168. content: '你的微信版本过低,请更新至最新版本。',
  169. showCancel: false,
  170. });
  171. return
  172. }
  173. // 判断是否在开发者工具中
  174. const systemInfo = uni.getSystemInfoSync();
  175. const isDevTools = systemInfo.platform === 'devtools';
  176. if (isDevTools) {
  177. // 在开发者工具中,显示提示信息
  178. console.log('开发者工具中无法调试此API,请在真机上测试');
  179. uni.showModal({
  180. title: '提示',
  181. content: '当前在开发者工具中,无法调试支付相关API,请在真机上测试。',
  182. showCancel: false,
  183. success: () => {
  184. // 模拟成功,方便开发调试
  185. this.$store.commit('getUserInfo')
  186. this.withdrawAmount = ''
  187. this.realName = ''
  188. this.isFormValid = true
  189. }
  190. });
  191. } else {
  192. // 在真机环境中,调用API
  193. wx.requestMerchantTransfer({
  194. mchId: res.result.outBillNo,
  195. appId: wx.getAccountInfoSync().miniProgram.appId,
  196. package: res.result.packageInfo,
  197. success: (res) => {
  198. uni.showToast({
  199. title: '提现申请已提交',
  200. icon: 'success'
  201. })
  202. this.$store.commit('getUserInfo')
  203. this.withdrawAmount = ''
  204. this.realName = ''
  205. },
  206. fail: (res) => {
  207. console.log('fail:', res);
  208. uni.showToast({
  209. title: '提现失败,请稍后再试',
  210. icon: 'none'
  211. })
  212. this.isFormValid = true
  213. },
  214. complete: (res) => {
  215. console.log('requestMerchantTransfer完成:', res);
  216. }
  217. });
  218. }
  219. } else {
  220. uni.showToast({
  221. title: '提现成功',
  222. icon: 'success'
  223. })
  224. this.$store.commit('getUserInfo')
  225. this.withdrawAmount = ''
  226. this.realName = ''
  227. }
  228. }else {
  229. uni.showToast({
  230. title: 'res.message',
  231. icon: 'error'
  232. })
  233. }
  234. this.isFormValid = true
  235. })
  236. },
  237. recharge() {
  238. uni.showModal({
  239. title: '确认充值',
  240. content: '充值金额为' + this.rechargeAmount + '元',
  241. confirmColor: '#019245',
  242. success: (res) => {
  243. // 这里编写函数逻辑
  244. if (res.confirm) {
  245. uni.showLoading({
  246. title: '充值中...'
  247. })
  248. this.$api('cashIn', { amount: this.rechargeAmount } , res => {
  249. uni.hideLoading()
  250. if (res.code === 200) {
  251. uni.requestPaymentWxPay(res)
  252. .then(() => {
  253. this.$store.commit('getUserInfo')
  254. this.rechargeAmount = ''
  255. this.isRecharge = false
  256. })
  257. .catch(() => {
  258. uni.showToast({
  259. title: '充值失败',
  260. icon: 'error'
  261. })
  262. })
  263. }
  264. })
  265. }
  266. },
  267. fail: (err) => {
  268. console.log(err);
  269. }
  270. })
  271. }
  272. }
  273. }
  274. </script>
  275. <style lang="scss" scoped>
  276. .wallet-page {
  277. }
  278. .balance-card {
  279. width: 96%;
  280. height: 280rpx;
  281. background-size: cover;
  282. background-position: center;
  283. padding: 30rpx;
  284. box-sizing: border-box;
  285. position: relative;
  286. margin: 20rpx auto;
  287. border-radius: 20rpx;
  288. .balance-info {
  289. position: relative;
  290. z-index: 2;
  291. color: #fff;
  292. }
  293. .balance-title {
  294. font-size: 28rpx;
  295. margin-bottom: 10rpx;
  296. }
  297. .balance-amount {
  298. font-size: 56rpx;
  299. font-weight: bold;
  300. margin-bottom: 20rpx;
  301. }
  302. .balance-actions {
  303. display: flex;
  304. justify-content: space-between;
  305. // justify-content: center;
  306. align-items: center;
  307. .action-btn {
  308. padding: 10rpx 24rpx;
  309. font-size: 24rpx;
  310. border-radius: 30rpx;
  311. display: flex;
  312. align-items: center;
  313. }
  314. .recharge-btn {
  315. background-color: #fff;
  316. color: $uni-color;
  317. border: none;
  318. min-width: 120rpx;
  319. height: 60rpx;
  320. justify-content: center;
  321. font-size: 24rpx;
  322. font-weight: normal;
  323. gap: 4rpx;
  324. // line-height: 1;
  325. padding: 0 20rpx;
  326. }
  327. .detail-btn {
  328. .arrow {
  329. margin-left: 10rpx;
  330. }
  331. }
  332. }
  333. }
  334. .withdraw-section {
  335. padding: 30rpx;
  336. // background-color: #fff;
  337. .section-title {
  338. font-size: 32rpx;
  339. color: #333;
  340. margin-bottom: 30rpx;
  341. font-weight: bold;
  342. }
  343. .input-item {
  344. display: flex;
  345. align-items: center;
  346. padding: 24rpx 20rpx;
  347. margin-bottom: 20rpx;
  348. background-color: #e7e7e7;
  349. border-radius: 20rpx;
  350. .currency-symbol {
  351. color: #FF0000;
  352. margin-right: 20rpx;
  353. }
  354. .amount-input,
  355. .name-input {
  356. flex: 1;
  357. font-size: 28rpx;
  358. height: 60rpx;
  359. }
  360. .name-input {
  361. padding-left: 40rpx;
  362. }
  363. }
  364. .withdraw-notes {
  365. margin-top: 40rpx;
  366. .notes-title {
  367. font-size: 28rpx;
  368. color: #333;
  369. margin-bottom: 20rpx;
  370. }
  371. .notes-list {
  372. .note-item {
  373. font-size: 26rpx;
  374. color: #666;
  375. line-height: 1.6;
  376. margin-bottom: 10rpx;
  377. }
  378. }
  379. }
  380. }
  381. .submit-btn-wrapper {
  382. padding: 40rpx 30rpx;
  383. .submit-btn {
  384. width: 100%;
  385. height: 88rpx;
  386. background-color: $uni-color;
  387. color: #fff;
  388. font-size: 32rpx;
  389. border-radius: 44rpx;
  390. display: flex;
  391. align-items: center;
  392. justify-content: center;
  393. border: none;
  394. &:disabled {
  395. background-color: #ccc;
  396. color: rgba(255, 255, 255, 0.6);
  397. }
  398. }
  399. }
  400. </style>