小说小程序前端代码仓库(小程序)
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.

529 lines
11 KiB

  1. <template>
  2. <!-- 豆豆充值页面 -->
  3. <view class="recharge-page">
  4. <!-- 顶部导航栏 -->
  5. <navbar title="豆豆充值" leftClick @leftClick="$utils.navigateBack" />
  6. <!-- 当前余额卡片 -->
  7. <view class="card balance-card">
  8. <view class="card-title">当前余额</view>
  9. <view class="current-balance">
  10. <text class="balance-amount">{{ userInfo.integerPrice || 0 }}</text>
  11. <text class="balance-unit">豆豆</text>
  12. </view>
  13. </view>
  14. <!-- 充值套餐选择 -->
  15. <view class="card package-card">
  16. <view class="card-title">选择充值套餐</view>
  17. <view class="package-grid">
  18. <view
  19. v-for="(item, index) in rechargePackages"
  20. :key="index"
  21. :class="['package-item', { selected: selectedPackage === index }]"
  22. @click="selectPackage(index)"
  23. >
  24. <view class="package-beans">{{ item.num }}豆豆</view>
  25. <view class="package-price">¥{{ item.money }}</view>
  26. <view v-if="item.giveNum" class="package-bonus">{{ item.giveNum }}豆豆</view>
  27. </view>
  28. </view>
  29. </view>
  30. <!-- 自定义充值 -->
  31. <!-- <view class="card custom-card">
  32. <view class="card-title">自定义充值</view>
  33. <view class="form-row">
  34. <view class="form-label required">
  35. <text class="star">*</text> 充值金额
  36. </view>
  37. <input
  38. class="form-input"
  39. placeholder="请输入充值金额"
  40. v-model="customAmount"
  41. type="number"
  42. :placeholder-style="'color:#bbb;'"
  43. :style="customAmount ? 'color:#222;' : ''"
  44. @input="onCustomAmountChange"
  45. />
  46. </view>
  47. <view class="divider"></view>
  48. <view class="form-row">
  49. <view class="form-label">可获得豆豆</view>
  50. <view class="form-value">{{ customBeans }}豆豆</view>
  51. </view>
  52. </view> -->
  53. <!-- 订单信息卡片 -->
  54. <view class="card order-card" v-if="totalPrice > 0">
  55. <view class="order-title">订单信息</view>
  56. <view class="order-item">
  57. <text class="order-label">充值金额</text>
  58. <text class="order-value">¥{{ totalPrice.toFixed(2) }}</text>
  59. </view>
  60. <view class="order-item">
  61. <text class="order-label">获得豆豆</text>
  62. <text class="order-value">{{ totalBeans }}豆豆</text>
  63. </view>
  64. <view class="order-divider"></view>
  65. <view class="order-item total-row">
  66. <view class="order-total-label">合计</view>
  67. <view class="order-total">
  68. <text class="order-total-highlight">¥{{ totalPrice.toFixed(2) }}</text>
  69. </view>
  70. </view>
  71. </view>
  72. <!-- 支付方式 -->
  73. <!-- <view class="card payment-card">
  74. <view class="card-title">支付方式</view>
  75. <view class="payment-methods">
  76. <view
  77. v-for="(method, index) in paymentMethods"
  78. :key="index"
  79. :class="['payment-item', { selected: selectedPayment === index }]"
  80. @click="selectPayment(index)"
  81. >
  82. <view class="payment-icon">
  83. <uv-icon :name="method.icon" size="40rpx" color="#223a7a" />
  84. </view>
  85. <text class="payment-name">{{ method.name }}</text>
  86. <view class="payment-radio">
  87. <view v-if="selectedPayment === index" class="radio-checked"></view>
  88. </view>
  89. </view>
  90. </view>
  91. </view> -->
  92. <!-- 提示信息 -->
  93. <view class="tip-text">
  94. 请仔细核查并确认相关信息因用户个人疏忽导致的充值错误需由用户自行承担一旦完成充值概不退换
  95. </view>
  96. <!-- 底部充值按钮 -->
  97. <view class="footer-bar">
  98. <button class="recharge-btn" @click="handleRecharge" :disabled="totalPrice <= 0">
  99. 立即充值 ¥{{ totalPrice.toFixed(2) }}
  100. </button>
  101. </view>
  102. </view>
  103. </template>
  104. <script>
  105. export default {
  106. data() {
  107. return {
  108. selectedPackage: null,
  109. customAmount: '',
  110. selectedPayment: 0,
  111. rechargePackages: [
  112. // { beans: 100, price: 10, bonus: 0 },
  113. // { beans: 300, price: 30, bonus: 20 },
  114. // { beans: 680, price: 68, bonus: 50 },
  115. // { beans: 1280, price: 128, bonus: 120 },
  116. // { beans: 3280, price: 328, bonus: 320 },
  117. // { beans: 6480, price: 648, bonus: 680 }
  118. ],
  119. paymentMethods: [
  120. { name: '微信支付', icon: 'weixin-fill' },
  121. ]
  122. }
  123. },
  124. computed: {
  125. customBeans() {
  126. const amount = parseFloat(this.customAmount)
  127. return isNaN(amount) ? 0 : Math.floor(amount * 10)
  128. },
  129. totalPrice() {
  130. if (this.selectedPackage !== null) {
  131. return this.rechargePackages[this.selectedPackage].money
  132. } else if (this.customAmount) {
  133. return parseFloat(this.customAmount) || 0
  134. }
  135. return 0
  136. },
  137. totalBeans() {
  138. if (this.selectedPackage !== null) {
  139. const pkg = this.rechargePackages[this.selectedPackage]
  140. return pkg.num + (pkg.giveNum || 0)
  141. } else if (this.customAmount) {
  142. return this.customBeans
  143. }
  144. return 0
  145. }
  146. },
  147. onLoad(query) {
  148. // 可以通过参数预设充值金额
  149. if (query.amount) {
  150. this.customAmount = query.amount
  151. }
  152. },
  153. onShow() {
  154. this.$store.commit('getUserInfo')
  155. this.getPayPackageList()
  156. },
  157. methods: {
  158. getPayPackageList(){
  159. this.$api('getPayPackageList')
  160. .then(res => {
  161. if(res.code == 200){
  162. this.rechargePackages = res.result
  163. }
  164. })
  165. },
  166. // 选择充值套餐
  167. selectPackage(index) {
  168. this.selectedPackage = index
  169. this.customAmount = ''
  170. },
  171. // 选择支付方式
  172. selectPayment(index) {
  173. this.selectedPayment = index
  174. },
  175. // 自定义金额输入变化
  176. onCustomAmountChange() {
  177. if (this.customAmount) {
  178. this.selectedPackage = null
  179. }
  180. },
  181. // 处理充值
  182. async handleRecharge() {
  183. if (this.totalPrice <= 0) {
  184. uni.showToast({
  185. title: '请选择充值套餐或输入充值金额',
  186. icon: 'none'
  187. })
  188. return
  189. }
  190. if (this.selectedPayment === null) {
  191. uni.showToast({
  192. title: '请选择支付方式',
  193. icon: 'none'
  194. })
  195. return
  196. }
  197. try {
  198. // 这里应该调用充值接口
  199. const result = await this.$fetch('createPayPackageOrder', {
  200. // amount: this.totalPrice,
  201. // beans: this.totalBeans,
  202. // paymentMethod: this.paymentMethods[this.selectedPayment].name
  203. packageId : this.rechargePackages[this.selectedPackage].id
  204. })
  205. await uni.requestPaymentWxPay({result})
  206. uni.showToast({
  207. title: `充值成功,获得${this.totalBeans}豆豆`,
  208. icon: 'success'
  209. })
  210. // 更新用户信息
  211. this.$store.commit('getUserInfo')
  212. setTimeout(() => {
  213. uni.navigateBack()
  214. }, 1500)
  215. } catch (error) {
  216. uni.showToast({
  217. title: '充值失败,请重试',
  218. icon: 'none'
  219. })
  220. }
  221. }
  222. }
  223. }
  224. </script>
  225. <style scoped lang="scss">
  226. .recharge-page {
  227. min-height: 100vh;
  228. background: #f8f8f8;
  229. padding-bottom: 120rpx;
  230. }
  231. .card {
  232. background: #fff;
  233. border-radius: 20rpx;
  234. margin: 24rpx 16rpx 0 16rpx;
  235. padding: 24rpx;
  236. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
  237. }
  238. .card-title {
  239. font-size: 32rpx;
  240. font-weight: bold;
  241. color: #222;
  242. margin-bottom: 24rpx;
  243. }
  244. // 当前余额卡片
  245. .balance-card {
  246. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  247. color: #fff;
  248. text-align: center;
  249. .card-title {
  250. color: #fff;
  251. opacity: 0.9;
  252. }
  253. .current-balance {
  254. display: flex;
  255. align-items: baseline;
  256. justify-content: center;
  257. gap: 8rpx;
  258. margin-top: 16rpx;
  259. .balance-amount {
  260. font-size: 56rpx;
  261. font-weight: bold;
  262. }
  263. .balance-unit {
  264. font-size: 28rpx;
  265. opacity: 0.8;
  266. }
  267. }
  268. }
  269. // 充值套餐
  270. .package-grid {
  271. display: flex;
  272. flex-wrap: wrap;
  273. gap: 16rpx;
  274. }
  275. .package-item {
  276. width: calc(50% - 8rpx);
  277. background: #f8f9ff;
  278. border: 2rpx solid #f0f0f0;
  279. border-radius: 16rpx;
  280. padding: 24rpx 16rpx;
  281. text-align: center;
  282. transition: all 0.2s;
  283. position: relative;
  284. box-sizing: border-box;
  285. &.selected {
  286. border-color: #223a7a;
  287. background: rgba(34, 58, 122, 0.05);
  288. }
  289. .package-beans {
  290. font-size: 32rpx;
  291. font-weight: bold;
  292. color: #222;
  293. margin-bottom: 8rpx;
  294. }
  295. .package-price {
  296. font-size: 28rpx;
  297. color: #223a7a;
  298. font-weight: 500;
  299. margin-bottom: 4rpx;
  300. }
  301. .package-bonus {
  302. font-size: 20rpx;
  303. color: #e94f7a;
  304. background: rgba(233, 79, 122, 0.1);
  305. padding: 2rpx 8rpx;
  306. border-radius: 8rpx;
  307. display: inline-block;
  308. }
  309. }
  310. // 表单样式
  311. .form-row {
  312. display: flex;
  313. flex-direction: column;
  314. align-items: flex-start;
  315. margin-bottom: 24rpx;
  316. }
  317. .form-label {
  318. color: #888;
  319. font-size: 28rpx;
  320. margin-bottom: 12rpx;
  321. font-weight: 400;
  322. &.required {
  323. color: #222;
  324. font-weight: 500;
  325. display: flex;
  326. align-items: center;
  327. }
  328. }
  329. .star {
  330. color: #e94f7a;
  331. margin-right: 4rpx;
  332. font-size: 28rpx;
  333. }
  334. .form-value {
  335. color: #222;
  336. font-size: 28rpx;
  337. font-weight: 600;
  338. }
  339. .form-input {
  340. width: 100%;
  341. border: 1rpx solid #e5e5e5;
  342. border-radius: 12rpx;
  343. font-size: 28rpx;
  344. padding: 16rpx;
  345. background: #fafafa;
  346. color: #222;
  347. box-sizing: border-box;
  348. }
  349. .divider {
  350. height: 1rpx;
  351. background: #f2f2f2;
  352. margin: 16rpx 0;
  353. width: 100%;
  354. }
  355. // 订单信息
  356. .order-card {
  357. .order-item {
  358. display: flex;
  359. justify-content: space-between;
  360. align-items: center;
  361. margin-bottom: 16rpx;
  362. .order-label {
  363. font-size: 28rpx;
  364. color: #666;
  365. }
  366. .order-value {
  367. font-size: 28rpx;
  368. color: #222;
  369. font-weight: 500;
  370. }
  371. &.total-row {
  372. margin-bottom: 0;
  373. margin-top: 16rpx;
  374. .order-total-label {
  375. font-size: 32rpx;
  376. color: #222;
  377. font-weight: 600;
  378. }
  379. .order-total-highlight {
  380. font-size: 32rpx;
  381. color: #223a7a;
  382. font-weight: bold;
  383. }
  384. }
  385. }
  386. .order-divider {
  387. height: 1rpx;
  388. background: #f2f2f2;
  389. margin: 16rpx 0;
  390. }
  391. }
  392. // 支付方式
  393. .payment-methods {
  394. display: flex;
  395. flex-direction: column;
  396. gap: 16rpx;
  397. }
  398. .payment-item {
  399. display: flex;
  400. align-items: center;
  401. padding: 20rpx;
  402. border: 2rpx solid #f0f0f0;
  403. border-radius: 12rpx;
  404. transition: all 0.2s;
  405. &.selected {
  406. border-color: #223a7a;
  407. background: rgba(34, 58, 122, 0.05);
  408. }
  409. .payment-icon {
  410. width: 48rpx;
  411. height: 48rpx;
  412. margin-right: 16rpx;
  413. image {
  414. width: 100%;
  415. height: 100%;
  416. }
  417. }
  418. .payment-name {
  419. flex: 1;
  420. font-size: 28rpx;
  421. color: #222;
  422. }
  423. .payment-radio {
  424. width: 32rpx;
  425. height: 32rpx;
  426. border: 2rpx solid #ddd;
  427. border-radius: 50%;
  428. position: relative;
  429. .radio-checked {
  430. width: 16rpx;
  431. height: 16rpx;
  432. background: #223a7a;
  433. border-radius: 50%;
  434. position: absolute;
  435. top: 50%;
  436. left: 50%;
  437. transform: translate(-50%, -50%);
  438. }
  439. }
  440. }
  441. .tip-text {
  442. color: #bbb;
  443. font-size: 22rpx;
  444. margin: 32rpx 32rpx 0 32rpx;
  445. line-height: 1.6;
  446. }
  447. .footer-bar {
  448. // position: fixed;
  449. // left: 0;
  450. // right: 0;
  451. // bottom: 90rpx;
  452. background: #fff;
  453. padding: 24rpx 32rpx 32rpx 32rpx;
  454. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  455. z-index: 10;
  456. }
  457. .recharge-btn {
  458. width: 100%;
  459. background: #223a7a;
  460. color: #fff;
  461. font-size: 32rpx;
  462. border-radius: 32rpx;
  463. height: 88rpx;
  464. line-height: 88rpx;
  465. border: none;
  466. font-weight: 500;
  467. &[disabled] {
  468. background: #ccc;
  469. color: #999;
  470. }
  471. }
  472. </style>