pay.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <template>
  2. <view class="pay-page">
  3. <scroll-view class="page" scroll-y>
  4. <!-- 商品明细卡 -->
  5. <view class="order-card">
  6. <view class="order-header">
  7. <text class="order-title">{{ levelName }} · {{ periodLabel }}</text>
  8. <text class="order-sub">{{ levelCode }}</text>
  9. </view>
  10. <view class="price-row" v-if="showOriginal">
  11. <text class="price-label">原价</text>
  12. <text class="price-original">¥{{ originalPriceText }}</text>
  13. </view>
  14. <view class="price-row">
  15. <text class="price-label">现价</text>
  16. <text class="price-now">¥{{ priceText }}</text>
  17. </view>
  18. <view class="coupon-row" @click="openCouponPicker">
  19. <text class="coupon-row-label">优惠券</text>
  20. <text v-if="selectedCoupon" class="coupon-row-discount">-¥{{ couponDiscountText }} {{ selectedCoupon.name }}</text>
  21. <text v-else class="coupon-row-placeholder">选择优惠券</text>
  22. <text class="coupon-row-arrow">›</text>
  23. </view>
  24. <view class="divider"></view>
  25. <view class="total-row">
  26. <text class="total-label">实付</text>
  27. <text class="total-amount">¥{{ finalPriceText }}</text>
  28. </view>
  29. </view>
  30. <!-- 拆单提示卡 -->
  31. <view class="split-card" v-if="totalPeriods > 1">
  32. <text class="split-title">💡 本订单将分 {{ totalPeriods }} 期支付</text>
  33. <text class="split-desc">每期 ¥{{ splitAmountText }},全部支付完成后自动开通会员</text>
  34. </view>
  35. <!-- 支付进度(拆单逐期时显示) -->
  36. <view class="progress-card" v-if="paying && totalPeriods > 1">
  37. <text class="progress-text">第 {{ currentPeriod }} / {{ totalPeriods }} 期</text>
  38. </view>
  39. </scroll-view>
  40. <!-- 底部固定支付栏 -->
  41. <view class="pay-bar">
  42. <button class="btn-pay" :loading="paying" :disabled="paying" @click="handlePay">
  43. {{ paying ? '支付中…' : '立即支付 ¥' + finalPriceText }}
  44. </button>
  45. </view>
  46. <!-- Coupon Picker Bottom Sheet -->
  47. <view class="modal-mask" v-if="showCouponPicker" @click="showCouponPicker = false">
  48. <view class="coupon-picker" @click.stop>
  49. <view class="picker-header">
  50. <text class="picker-title">选择优惠券</text>
  51. <text class="picker-close" @click="showCouponPicker = false">关闭</text>
  52. </view>
  53. <scroll-view scroll-y class="picker-list">
  54. <view
  55. v-for="coupon in availableCoupons"
  56. :key="coupon.id"
  57. class="picker-item"
  58. :class="selectedCoupon && selectedCoupon.id === coupon.id ? 'picker-item-active' : ''"
  59. @click="selectCoupon(coupon)"
  60. >
  61. <view class="picker-item-left">
  62. <text class="picker-item-value">{{ formatPrice(coupon.value) }}</text>
  63. </view>
  64. <view class="picker-item-right">
  65. <text class="picker-item-name">{{ coupon.name }}</text>
  66. <text class="picker-item-condition">{{ getCouponCondition(coupon.minSpend) }}</text>
  67. </view>
  68. <view class="picker-item-check" v-if="selectedCoupon && selectedCoupon.id === coupon.id">
  69. <text>✓</text>
  70. </view>
  71. </view>
  72. <view class="picker-empty" v-if="availableCoupons.length === 0">
  73. <text>暂无可用优惠券</text>
  74. </view>
  75. </scroll-view>
  76. <view class="picker-footer" v-if="selectedCoupon">
  77. <button class="picker-btn-remove" @click="removeCoupon">不使用优惠券</button>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import { getMembershipPlans, getCouponList, createOrder, requestVirtualPayment, nextOrder } from '../../utils/api.js'
  85. export default {
  86. data() {
  87. return {
  88. levelCode: 'FAMILY',
  89. period: 'yearly',
  90. levelName: '家庭会员',
  91. periodLabel: '年度',
  92. levels: [],
  93. coupons: [],
  94. selectedCoupon: null,
  95. showCouponPicker: false,
  96. originalPrice: null,
  97. price: 0,
  98. totalPeriods: 1,
  99. currentPeriod: 1,
  100. splitAmount: 0,
  101. orderNo: null,
  102. paying: false
  103. }
  104. },
  105. computed: {
  106. showOriginal: function() {
  107. return this.originalPrice != null && this.originalPrice > this.price
  108. },
  109. priceText: function() {
  110. return (this.price / 100).toFixed(2)
  111. },
  112. originalPriceText: function() {
  113. if (this.originalPrice == null) return '0.00'
  114. return (this.originalPrice / 100).toFixed(2)
  115. },
  116. couponDiscount: function() {
  117. if (!this.selectedCoupon) return 0
  118. if (this.selectedCoupon.type === 'DISCOUNT') {
  119. var rate = this.selectedCoupon.discountRate || 100
  120. return Math.round(this.price * (100 - rate) / 100)
  121. }
  122. return this.selectedCoupon.value || 0
  123. },
  124. couponDiscountText: function() {
  125. return (this.couponDiscount / 100).toFixed(2)
  126. },
  127. finalPrice: function() {
  128. return Math.max(0, this.price - this.couponDiscount)
  129. },
  130. finalPriceText: function() {
  131. return (this.finalPrice / 100).toFixed(2)
  132. },
  133. splitAmountText: function() {
  134. return (this.splitAmount / 100).toFixed(2)
  135. },
  136. availableCoupons: function() {
  137. var self = this
  138. return this.coupons.filter(function(c) {
  139. if (c.status && c.status !== 'AVAILABLE') return false
  140. if (c.applicableTo && c.applicableTo !== 'ALL' && c.applicableTo !== 'MEMBERSHIP') return false
  141. return true
  142. })
  143. }
  144. },
  145. onLoad: function(options) {
  146. this.levelCode = options.levelCode || 'FAMILY'
  147. this.period = options.period || 'yearly'
  148. this.periodLabel = this.getPeriodLabel(this.period)
  149. this.loadPlans()
  150. this.loadCoupons()
  151. },
  152. methods: {
  153. getPeriodLabel: function(period) {
  154. var map = { monthly: '月度', quarterly: '季度', yearly: '年度' }
  155. return map[period] || '年度'
  156. },
  157. loadPlans: function() {
  158. var self = this
  159. getMembershipPlans().then(function(res) {
  160. var levels = res.data || []
  161. for (var i = 0; i < levels.length; i++) {
  162. if (levels[i].levelCode === self.levelCode) {
  163. self.levelName = levels[i].levelName || self.levelName
  164. if (self.period === 'monthly') {
  165. self.price = levels[i].monthly || 0
  166. self.originalPrice = levels[i].originalMonthly || null
  167. } else if (self.period === 'quarterly') {
  168. self.price = levels[i].quarterly || 0
  169. self.originalPrice = levels[i].originalQuarterly || null
  170. } else {
  171. self.price = levels[i].yearly || 0
  172. self.originalPrice = levels[i].originalYearly || null
  173. }
  174. break
  175. }
  176. }
  177. }).catch(function(e) {
  178. uni.showToast({ title: '加载价格失败', icon: 'none' })
  179. console.error('加载价格失败', e)
  180. })
  181. },
  182. loadCoupons: function() {
  183. var self = this
  184. getCouponList().then(function(res) {
  185. self.coupons = res.data || []
  186. }).catch(function() {})
  187. },
  188. openCouponPicker: function() {
  189. if (this.availableCoupons.length === 0 && !this.selectedCoupon) {
  190. uni.showToast({ title: '暂无可用优惠券', icon: 'none' })
  191. return
  192. }
  193. this.showCouponPicker = true
  194. },
  195. selectCoupon: function(coupon) {
  196. this.selectedCoupon = coupon
  197. this.showCouponPicker = false
  198. },
  199. removeCoupon: function() {
  200. this.selectedCoupon = null
  201. this.showCouponPicker = false
  202. },
  203. getCouponCondition: function(minSpend) {
  204. if (!minSpend || minSpend <= 0) return '无门槛'
  205. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  206. },
  207. formatPrice: function(value) {
  208. if (value == null) return '0.00'
  209. return (value / 100).toFixed(2)
  210. },
  211. handlePay: function() {
  212. var self = this
  213. if (this.paying) return
  214. this.paying = true
  215. var couponId = this.selectedCoupon ? this.selectedCoupon.id : null
  216. createOrder(this.levelCode, 'pay', this.period, couponId).then(function(res) {
  217. var data = res.data || {}
  218. if (data.orderInfo && data.sign) {
  219. self.orderNo = data.orderNo || (data.orderInfo && data.orderInfo.outTradeNo)
  220. self.totalPeriods = data.totalPeriods || 1
  221. self.currentPeriod = data.currentPeriod || 1
  222. self.splitAmount = data.orderInfo && data.orderInfo.goodsPrice ? data.orderInfo.goodsPrice : (self.finalPrice / self.totalPeriods)
  223. self.payInstallments(data)
  224. } else {
  225. // testMode 或试用:模拟支付成功
  226. self.orderNo = data.orderNo
  227. uni.showToast({ title: '支付成功,开通中…', icon: 'none' })
  228. setTimeout(function() {
  229. self.paying = false
  230. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=success' })
  231. }, 800)
  232. }
  233. }).catch(function(e) {
  234. self.paying = false
  235. if (e.code === 46001) {
  236. uni.showToast({ title: '该商品暂未开放购买', icon: 'none' })
  237. } else {
  238. uni.showToast({ title: e.message || '下单失败', icon: 'none' })
  239. }
  240. })
  241. },
  242. payInstallments: function(dto) {
  243. var self = this
  244. var current = dto.currentPeriod || 1
  245. var total = dto.totalPeriods || 1
  246. requestVirtualPayment(dto, {
  247. success: function() {
  248. if (current < total) {
  249. uni.showToast({ title: '第' + current + '期支付成功', icon: 'none' })
  250. nextOrder({ orderNo: dto.orderNo }).then(function(res) {
  251. var nd = res.data
  252. if (nd && nd.done) {
  253. self.finishPay()
  254. } else if (nd && nd.orderInfo && nd.sign) {
  255. self.currentPeriod = nd.currentPeriod || 1
  256. self.payInstallments(nd)
  257. } else {
  258. self.finishPay()
  259. }
  260. }).catch(function() {
  261. self.finishPay()
  262. })
  263. } else {
  264. self.finishPay()
  265. }
  266. },
  267. fail: function(err) {
  268. self.paying = false
  269. var errCode = err && err.errCode
  270. var msg = ''
  271. if (errCode === -15007 || errCode === -15005) msg = '登录态已过期,请重新登录后支付'
  272. else if (errCode === -15010) msg = '商品未发布,请联系客服'
  273. else if (errCode === -15013) msg = '商品价格异常'
  274. else if (errCode === -2) msg = '支付已取消'
  275. else if (err && err.errMsg) msg = err.errMsg
  276. var status = (errCode === -2 || errCode === -15007 || errCode === -15005) ? 'fail' : 'fail'
  277. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=' + status })
  278. }
  279. })
  280. },
  281. finishPay: function() {
  282. var self = this
  283. uni.showToast({ title: '支付成功,开通中…', icon: 'none' })
  284. setTimeout(function() {
  285. self.paying = false
  286. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=success' })
  287. }, 800)
  288. }
  289. }
  290. }
  291. </script>
  292. <style scoped>
  293. .pay-page {
  294. min-height: 100vh;
  295. background: #FFF7ED;
  296. padding-bottom: 160rpx;
  297. box-sizing: border-box;
  298. }
  299. .page {
  300. padding: 30rpx;
  301. }
  302. /* ===== 商品明细卡 ===== */
  303. .order-card {
  304. background: #FFFFFF;
  305. border-radius: 20rpx;
  306. padding: 40rpx;
  307. box-shadow: 0 4rpx 24rpx rgba(249, 115, 22, 0.08);
  308. }
  309. .order-header {
  310. margin-bottom: 24rpx;
  311. }
  312. .order-title {
  313. font-size: 34rpx;
  314. font-weight: bold;
  315. color: #1E293B;
  316. display: block;
  317. }
  318. .order-sub {
  319. font-size: 24rpx;
  320. color: #94A3B8;
  321. margin-top: 6rpx;
  322. display: block;
  323. }
  324. .price-row {
  325. display: flex;
  326. justify-content: space-between;
  327. align-items: center;
  328. padding: 16rpx 0;
  329. }
  330. .price-label {
  331. font-size: 26rpx;
  332. color: #64748B;
  333. }
  334. .price-original {
  335. font-size: 28rpx;
  336. color: #94A3B8;
  337. text-decoration: line-through;
  338. }
  339. .price-now {
  340. font-size: 36rpx;
  341. font-weight: bold;
  342. color: #F97316;
  343. }
  344. .coupon-row {
  345. display: flex;
  346. align-items: center;
  347. padding: 24rpx 0;
  348. border-top: 1rpx solid #f5f5f5;
  349. margin-top: 16rpx;
  350. }
  351. .coupon-row:active {
  352. opacity: 0.7;
  353. }
  354. .coupon-row-label {
  355. font-size: 26rpx;
  356. color: #666;
  357. margin-right: 16rpx;
  358. }
  359. .coupon-row-discount {
  360. flex: 1;
  361. font-size: 26rpx;
  362. color: #F97316;
  363. font-weight: 600;
  364. text-align: right;
  365. }
  366. .coupon-row-placeholder {
  367. flex: 1;
  368. font-size: 26rpx;
  369. color: #ccc;
  370. text-align: right;
  371. }
  372. .coupon-row-arrow {
  373. font-size: 32rpx;
  374. color: #ccc;
  375. margin-left: 8rpx;
  376. }
  377. .divider {
  378. height: 1rpx;
  379. background: #F1F5F9;
  380. margin: 16rpx 0;
  381. }
  382. .total-row {
  383. display: flex;
  384. justify-content: space-between;
  385. align-items: center;
  386. }
  387. .total-label {
  388. font-size: 28rpx;
  389. font-weight: 600;
  390. color: #1E293B;
  391. }
  392. .total-amount {
  393. font-size: 44rpx;
  394. font-weight: bold;
  395. color: #F97316;
  396. }
  397. /* ===== 拆单提示 ===== */
  398. .split-card {
  399. background: #FEF3C7;
  400. border-radius: 20rpx;
  401. padding: 24rpx;
  402. margin-top: 30rpx;
  403. }
  404. .split-title {
  405. font-size: 28rpx;
  406. font-weight: 600;
  407. color: #92400E;
  408. display: block;
  409. margin-bottom: 8rpx;
  410. }
  411. .split-desc {
  412. font-size: 24rpx;
  413. color: #B45309;
  414. display: block;
  415. }
  416. /* ===== 支付进度 ===== */
  417. .progress-card {
  418. background: #FFFFFF;
  419. border-radius: 20rpx;
  420. padding: 20rpx;
  421. margin-top: 20rpx;
  422. text-align: center;
  423. }
  424. .progress-text {
  425. font-size: 26rpx;
  426. color: #F97316;
  427. font-weight: 600;
  428. }
  429. /* ===== 底部支付栏 ===== */
  430. .pay-bar {
  431. position: fixed;
  432. bottom: 0;
  433. left: 0;
  434. right: 0;
  435. background: #FFFFFF;
  436. padding: 20rpx 30rpx calc(20rpx + env(safe-area-inset-bottom));
  437. box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
  438. }
  439. .btn-pay {
  440. width: 100%;
  441. height: 96rpx;
  442. line-height: 96rpx;
  443. background: linear-gradient(135deg, #F97316 0%, #EA580C 100%);
  444. color: #fff;
  445. font-size: 34rpx;
  446. font-weight: 600;
  447. border-radius: 48rpx;
  448. border: none;
  449. }
  450. .btn-pay::after {
  451. border: none;
  452. }
  453. .btn-pay[disabled] {
  454. opacity: 0.7;
  455. color: #fff;
  456. }
  457. /* ===== Coupon Picker(复用 upgrade 样式) ===== */
  458. .modal-mask {
  459. position: fixed;
  460. top: 0;
  461. left: 0;
  462. right: 0;
  463. bottom: 0;
  464. background: rgba(0,0,0,0.5);
  465. z-index: 999;
  466. display: flex;
  467. align-items: flex-end;
  468. }
  469. .coupon-picker {
  470. background: #fff;
  471. border-radius: 24rpx 24rpx 0 0;
  472. width: 100%;
  473. max-height: 70vh;
  474. display: flex;
  475. flex-direction: column;
  476. }
  477. .picker-header {
  478. display: flex;
  479. justify-content: space-between;
  480. align-items: center;
  481. padding: 30rpx;
  482. border-bottom: 1rpx solid #eee;
  483. }
  484. .picker-title {
  485. font-size: 30rpx;
  486. font-weight: 600;
  487. color: #333;
  488. }
  489. .picker-close {
  490. font-size: 26rpx;
  491. color: #999;
  492. }
  493. .picker-list {
  494. max-height: 50vh;
  495. padding: 0 20rpx;
  496. }
  497. .picker-item {
  498. display: flex;
  499. align-items: center;
  500. padding: 24rpx 16rpx;
  501. border-bottom: 1rpx solid #f5f5f5;
  502. }
  503. .picker-item:active {
  504. background: #fafafa;
  505. }
  506. .picker-item-active {
  507. background: #FFF7ED;
  508. }
  509. .picker-item-left {
  510. width: 120rpx;
  511. text-align: center;
  512. flex-shrink: 0;
  513. }
  514. .picker-item-value {
  515. font-size: 32rpx;
  516. font-weight: bold;
  517. color: #F97316;
  518. }
  519. .picker-item-right {
  520. flex: 1;
  521. margin: 0 16rpx;
  522. }
  523. .picker-item-name {
  524. font-size: 26rpx;
  525. color: #333;
  526. display: block;
  527. margin-bottom: 4rpx;
  528. }
  529. .picker-item-condition {
  530. font-size: 22rpx;
  531. color: #999;
  532. }
  533. .picker-item-check {
  534. width: 40rpx;
  535. height: 40rpx;
  536. background: #F97316;
  537. border-radius: 50%;
  538. display: flex;
  539. align-items: center;
  540. justify-content: center;
  541. flex-shrink: 0;
  542. color: #fff;
  543. font-size: 24rpx;
  544. font-weight: bold;
  545. }
  546. .picker-empty {
  547. text-align: center;
  548. padding: 60rpx 0;
  549. font-size: 26rpx;
  550. color: #999;
  551. }
  552. .picker-footer {
  553. padding: 20rpx;
  554. border-top: 1rpx solid #eee;
  555. }
  556. .picker-btn-remove {
  557. background: #f5f5f5;
  558. color: #666;
  559. font-size: 26rpx;
  560. border-radius: 12rpx;
  561. border: none;
  562. }
  563. .picker-btn-remove::after {
  564. border: none;
  565. }
  566. </style>