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

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