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

393 lines
11 KiB

3 months ago
3 months 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.walletData.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. // setTimeout(() => {
  161. // uni.hideLoading()
  162. // uni.showToast({
  163. // title: '提现申请已提交',
  164. // icon: 'success'
  165. // })
  166. // // 模拟余额变更
  167. // this.walletData.balance -= parseFloat(this.withdrawAmount)
  168. // // 清空表单
  169. // this.withdrawAmount = ''
  170. // this.realName = ''
  171. // this.isFormValid = true
  172. // }, 1500)
  173. },
  174. recharge() {
  175. uni.showModal({
  176. title: '确认充值',
  177. content: '充值金额为' + this.rechargeAmount + '元',
  178. confirmColor: '#019245',
  179. success: (res) => {
  180. // 这里编写函数逻辑
  181. if (res.confirm) {
  182. uni.showLoading({
  183. title: '充值中...'
  184. })
  185. this.$api('cashIn', { amount: this.rechargeAmount } , res => {
  186. uni.hideLoading()
  187. if (res.code === 200) {
  188. uni.requestPaymentWxPay(res)
  189. .then(() => {
  190. this.userInfo.balance += parseFloat(this.rechargeAmount)
  191. this.rechargeAmount = ''
  192. this.isRecharge = false
  193. })
  194. .catch(() => {
  195. uni.showToast({
  196. title: '充值失败',
  197. icon: 'error'
  198. })
  199. })
  200. }
  201. })
  202. // 模拟重置时间
  203. // setTimeout(() => {
  204. // // 模拟余额变更
  205. // this.walletData.balance += parseFloat(this.rechargeAmount)
  206. // // 重置表单
  207. // this.rechargeAmount = ''
  208. // this.isRecharge = false
  209. // }, 1500)
  210. }
  211. },
  212. fail: (err) => {
  213. console.log(err);
  214. }
  215. })
  216. }
  217. }
  218. }
  219. </script>
  220. <style lang="scss" scoped>
  221. .wallet-page {
  222. }
  223. .balance-card {
  224. width: 96%;
  225. height: 280rpx;
  226. background-size: cover;
  227. background-position: center;
  228. padding: 30rpx;
  229. box-sizing: border-box;
  230. position: relative;
  231. margin: 20rpx auto;
  232. border-radius: 20rpx;
  233. .balance-info {
  234. position: relative;
  235. z-index: 2;
  236. color: #fff;
  237. }
  238. .balance-title {
  239. font-size: 28rpx;
  240. margin-bottom: 10rpx;
  241. }
  242. .balance-amount {
  243. font-size: 56rpx;
  244. font-weight: bold;
  245. margin-bottom: 20rpx;
  246. }
  247. .balance-actions {
  248. display: flex;
  249. justify-content: space-between;
  250. // justify-content: center;
  251. align-items: center;
  252. .action-btn {
  253. padding: 10rpx 24rpx;
  254. font-size: 24rpx;
  255. border-radius: 30rpx;
  256. display: flex;
  257. align-items: center;
  258. }
  259. .recharge-btn {
  260. background-color: #fff;
  261. color: $uni-color;
  262. border: none;
  263. min-width: 120rpx;
  264. height: 60rpx;
  265. justify-content: center;
  266. font-size: 24rpx;
  267. font-weight: normal;
  268. gap: 4rpx;
  269. // line-height: 1;
  270. padding: 0 20rpx;
  271. }
  272. .detail-btn {
  273. .arrow {
  274. margin-left: 10rpx;
  275. }
  276. }
  277. }
  278. }
  279. .withdraw-section {
  280. padding: 30rpx;
  281. // background-color: #fff;
  282. .section-title {
  283. font-size: 32rpx;
  284. color: #333;
  285. margin-bottom: 30rpx;
  286. font-weight: bold;
  287. }
  288. .input-item {
  289. display: flex;
  290. align-items: center;
  291. padding: 24rpx 20rpx;
  292. margin-bottom: 20rpx;
  293. background-color: #e7e7e7;
  294. border-radius: 20rpx;
  295. .currency-symbol {
  296. color: #FF0000;
  297. margin-right: 20rpx;
  298. }
  299. .amount-input,
  300. .name-input {
  301. flex: 1;
  302. font-size: 28rpx;
  303. height: 60rpx;
  304. }
  305. .name-input {
  306. padding-left: 40rpx;
  307. }
  308. }
  309. .withdraw-notes {
  310. margin-top: 40rpx;
  311. .notes-title {
  312. font-size: 28rpx;
  313. color: #333;
  314. margin-bottom: 20rpx;
  315. }
  316. .notes-list {
  317. .note-item {
  318. font-size: 26rpx;
  319. color: #666;
  320. line-height: 1.6;
  321. margin-bottom: 10rpx;
  322. }
  323. }
  324. }
  325. }
  326. .submit-btn-wrapper {
  327. padding: 40rpx 30rpx;
  328. .submit-btn {
  329. width: 100%;
  330. height: 88rpx;
  331. background-color: $uni-color;
  332. color: #fff;
  333. font-size: 32rpx;
  334. border-radius: 44rpx;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. border: none;
  339. &:disabled {
  340. background-color: #ccc;
  341. color: rgba(255, 255, 255, 0.6);
  342. }
  343. }
  344. }
  345. </style>