敢为人鲜小程序前端代码仓库
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.

385 lines
11 KiB

1 month ago
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(/static/image/红烧肉.webp)' }">
  7. <view class="balance-info">
  8. <view class="balance-title">总余额</view>
  9. <view class="balance-amount">{{ userInfo.balance.toFixed(2) }}</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" v-else />
  16. <view class="action-btn detail-btn" @tap="navigateToDetail">
  17. <text>资产明细</text>
  18. <text class="arrow">></text>
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. <!-- 提现表单 -->
  24. <view class="withdraw-section">
  25. <view class="section-title">{{ isRecharge ? '我要充值' : '我要提现' }}</view>
  26. <!-- 提现金额输入框 -->
  27. <view class="input-item">
  28. <text class="currency-symbol">¥</text>
  29. <input v-if="!isRecharge" class="amount-input" type="digit" v-model="withdrawAmount" placeholder="请输入提现金额"
  30. @blur="validateAmount" />
  31. <input v-else class="amount-input" type="digit" v-model="rechargeAmount" placeholder="请输入充值金额"
  32. @blur="validateAmount" />
  33. </view>
  34. <!-- 真实姓名输入框 -->
  35. <view class="input-item" v-if="!isRecharge">
  36. <input class="name-input" type="nickname" v-model="realName" placeholder="请输入真实姓名"
  37. @blur="validateName" />
  38. </view>
  39. <!-- 提现说明 -->
  40. <view class="withdraw-notes" v-if="!isRecharge">
  41. <view class="notes-title">提现说明</view>
  42. <view class="notes-list">
  43. <view class="note-item" v-for="(rule, index) in walletData.withdrawRules" :key="index">
  44. <text>{{ index + 1 }}{{ rule }}</text>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. <!-- 提现按钮 -->
  50. <view class="submit-btn-wrapper">
  51. <button class="submit-btn" @tap="submitWithdraw" :disabled="!isFormValid">
  52. {{ isRecharge ? '立即充值' : '立即提现' }}
  53. </button>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. import navbar from '@/components/base/navbar.vue'
  59. import { walletData } from '@/static/js/mockWallet.js'
  60. export default {
  61. components: {
  62. navbar
  63. },
  64. data() {
  65. return {
  66. walletData: null,
  67. withdrawAmount: '',
  68. rechargeAmount: '',
  69. realName: '',
  70. amountError: '',
  71. nameError: '',
  72. isFormValid: true,
  73. isRecharge: false
  74. }
  75. },
  76. onLoad() {
  77. this.walletData = walletData
  78. },
  79. methods: {
  80. // 导航到充值页面
  81. navigateToRecharge() {
  82. this.isRecharge = true
  83. },
  84. // 导航到资产明细页面
  85. navigateToDetail() {
  86. this.$utils.navigateTo('/pages_order/mine/assets')
  87. },
  88. // 验证提现金额
  89. validateAmount() {
  90. if (!this.withdrawAmount) {
  91. this.amountError = '请输入提现金额'
  92. return false
  93. }
  94. const amount = parseFloat(this.withdrawAmount)
  95. if (isNaN(amount) || amount <= 0) {
  96. this.amountError = '请输入有效的提现金额'
  97. return false
  98. }
  99. if (amount > this.walletData.balance) {
  100. this.amountError = '提现金额不能大于余额'
  101. return false
  102. }
  103. if (amount > 200) {
  104. this.amountError = '单笔提现不能超过200元'
  105. return false
  106. }
  107. this.amountError = ''
  108. return true
  109. },
  110. // 验证真实姓名
  111. validateName() {
  112. if (!this.realName) {
  113. this.nameError = '请输入真实姓名'
  114. return false
  115. }
  116. if (this.realName.length < 2) {
  117. this.nameError = '请输入有效的姓名'
  118. return false
  119. }
  120. this.nameError = ''
  121. return true
  122. },
  123. // 提交提现申请
  124. submitWithdraw() {
  125. if (this.isRecharge) {
  126. return this.recharge()
  127. }
  128. // 再次验证表单
  129. if (!this.validateAmount() || !this.validateName()) {
  130. console.log(2);
  131. // 显示具体错误
  132. if (this.amountError) {
  133. console.log(3);
  134. uni.showToast({
  135. title: this.amountError,
  136. icon: 'error'
  137. })
  138. return
  139. }
  140. if (this.nameError) {
  141. uni.showToast({
  142. title: this.nameError,
  143. icon: 'error'
  144. })
  145. return
  146. }
  147. return
  148. }
  149. // 如果在isFormVaild为false的情况下进入函数 则为多次点击 直接返回
  150. if (this.isFormValid) {
  151. this.isFormValid = false
  152. }else return
  153. // 显示提交中状态
  154. uni.showLoading({
  155. title: '提交中...'
  156. })
  157. // 模拟提交过程
  158. // setTimeout(() => {
  159. // uni.hideLoading()
  160. // uni.showToast({
  161. // title: '提现申请已提交',
  162. // icon: 'success'
  163. // })
  164. // // 模拟余额变更
  165. // this.walletData.balance -= parseFloat(this.withdrawAmount)
  166. // // 清空表单
  167. // this.withdrawAmount = ''
  168. // this.realName = ''
  169. // this.isFormValid = true
  170. // }, 1500)
  171. },
  172. recharge() {
  173. uni.showModal({
  174. title: '确认充值',
  175. content: '充值金额为' + this.rechargeAmount + '元',
  176. confirmColor: '#019245',
  177. success: (res) => {
  178. // 这里编写函数逻辑
  179. if (res.confirm) {
  180. uni.showLoading({
  181. title: '充值中...'
  182. })
  183. this.$api('cashIn', { amount: this.rechargeAmount } , res => {
  184. uni.hideLoading()
  185. if (res.code === 200) {
  186. uni.requestPaymentWxPay(res)
  187. .catch(() => {
  188. this.userInfo.balance += parseFloat(this.rechargeAmount)
  189. this.rechargeAmount = ''
  190. this.isRecharge = false
  191. })
  192. }
  193. })
  194. // 模拟重置时间
  195. // setTimeout(() => {
  196. // // 模拟余额变更
  197. // this.walletData.balance += parseFloat(this.rechargeAmount)
  198. // // 重置表单
  199. // this.rechargeAmount = ''
  200. // this.isRecharge = false
  201. // }, 1500)
  202. }
  203. },
  204. fail: (err) => {
  205. console.log(err);
  206. }
  207. })
  208. }
  209. }
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. .wallet-page {
  214. }
  215. .balance-card {
  216. width: 96%;
  217. height: 280rpx;
  218. background-size: cover;
  219. background-position: center;
  220. padding: 30rpx;
  221. box-sizing: border-box;
  222. position: relative;
  223. margin: 20rpx auto;
  224. border-radius: 20rpx;
  225. .balance-info {
  226. position: relative;
  227. z-index: 2;
  228. color: #fff;
  229. }
  230. .balance-title {
  231. font-size: 28rpx;
  232. margin-bottom: 10rpx;
  233. }
  234. .balance-amount {
  235. font-size: 56rpx;
  236. font-weight: bold;
  237. margin-bottom: 20rpx;
  238. }
  239. .balance-actions {
  240. display: flex;
  241. justify-content: space-between;
  242. // justify-content: center;
  243. align-items: center;
  244. .action-btn {
  245. padding: 10rpx 24rpx;
  246. font-size: 24rpx;
  247. border-radius: 30rpx;
  248. display: flex;
  249. align-items: center;
  250. }
  251. .recharge-btn {
  252. background-color: #fff;
  253. color: $uni-color;
  254. border: none;
  255. min-width: 120rpx;
  256. height: 60rpx;
  257. justify-content: center;
  258. font-size: 24rpx;
  259. font-weight: normal;
  260. gap: 4rpx;
  261. // line-height: 1;
  262. padding: 0 20rpx;
  263. }
  264. .detail-btn {
  265. .arrow {
  266. margin-left: 10rpx;
  267. }
  268. }
  269. }
  270. }
  271. .withdraw-section {
  272. padding: 30rpx;
  273. // background-color: #fff;
  274. .section-title {
  275. font-size: 32rpx;
  276. color: #333;
  277. margin-bottom: 30rpx;
  278. font-weight: bold;
  279. }
  280. .input-item {
  281. display: flex;
  282. align-items: center;
  283. padding: 24rpx 20rpx;
  284. margin-bottom: 20rpx;
  285. background-color: #e7e7e7;
  286. border-radius: 20rpx;
  287. .currency-symbol {
  288. color: #FF0000;
  289. margin-right: 20rpx;
  290. }
  291. .amount-input,
  292. .name-input {
  293. flex: 1;
  294. font-size: 28rpx;
  295. height: 60rpx;
  296. }
  297. .name-input {
  298. padding-left: 40rpx;
  299. }
  300. }
  301. .withdraw-notes {
  302. margin-top: 40rpx;
  303. .notes-title {
  304. font-size: 28rpx;
  305. color: #333;
  306. margin-bottom: 20rpx;
  307. }
  308. .notes-list {
  309. .note-item {
  310. font-size: 26rpx;
  311. color: #666;
  312. line-height: 1.6;
  313. margin-bottom: 10rpx;
  314. }
  315. }
  316. }
  317. }
  318. .submit-btn-wrapper {
  319. padding: 40rpx 30rpx;
  320. .submit-btn {
  321. width: 100%;
  322. height: 88rpx;
  323. background-color: $uni-color;
  324. color: #fff;
  325. font-size: 32rpx;
  326. border-radius: 44rpx;
  327. display: flex;
  328. align-items: center;
  329. justify-content: center;
  330. border: none;
  331. &:disabled {
  332. background-color: #ccc;
  333. color: rgba(255, 255, 255, 0.6);
  334. }
  335. }
  336. }
  337. </style>