pay.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. var openid = uni.getStorageSync('openid') || ''
  217. createOrder(this.levelCode, 'pay', this.period, couponId, openid).then(function(res) {
  218. var data = res.data || {}
  219. self.orderNo = data.orderNo
  220. var payType = data.payType || 'virtual'
  221. if (payType === 'virtual' && data.orderInfo && data.sign) {
  222. // 虚拟支付(现有逻辑不变)
  223. self.totalPeriods = data.totalPeriods || 1
  224. self.currentPeriod = data.currentPeriod || 1
  225. self.splitAmount = data.orderInfo.goodsPrice || (self.finalPrice / (self.totalPeriods || 1))
  226. self.payInstallments(data)
  227. } else if (payType === 'wechat' && data.wechatPayParams) {
  228. // 普通微信支付(新增分支)
  229. self.payWechat(data.wechatPayParams)
  230. } else {
  231. // testMode/trial:模拟支付成功
  232. uni.showToast({ title: '支付成功,开通中…', icon: 'none' })
  233. setTimeout(function() {
  234. self.paying = false
  235. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=success' })
  236. }, 800)
  237. }
  238. }).catch(function(e) {
  239. self.paying = false
  240. if (e.code === 46001) {
  241. uni.showToast({ title: '该商品暂未开放购买', icon: 'none' })
  242. } else {
  243. uni.showToast({ title: e.message || '下单失败', icon: 'none' })
  244. }
  245. })
  246. },
  247. payWechat: function(pp) {
  248. var self = this
  249. if (!pp || !pp.package) {
  250. self.paying = false
  251. uni.showToast({ title: '支付参数异常', icon: 'none' })
  252. return
  253. }
  254. uni.requestPayment({
  255. provider: 'wxpay',
  256. timeStamp: pp.timeStamp,
  257. nonceStr: pp.nonceStr,
  258. package: pp.package,
  259. signType: pp.signType || 'RSA',
  260. paySign: pp.paySign || pp.sign,
  261. success: function() {
  262. uni.showToast({ title: '支付成功,开通中…', icon: 'none' })
  263. setTimeout(function() {
  264. self.paying = false
  265. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=success' })
  266. }, 800)
  267. },
  268. fail: function(err) {
  269. self.paying = false
  270. var errCode = err && err.errCode
  271. var msg = '支付取消'
  272. if (errCode === -15007 || errCode === -15005) msg = '登录态已过期,请重新登录'
  273. else if (err && err.errMsg) msg = err.errMsg
  274. uni.showToast({ title: msg, icon: 'none' })
  275. setTimeout(function() {
  276. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=fail' })
  277. }, 800)
  278. }
  279. })
  280. },
  281. payInstallments: function(dto) {
  282. var self = this
  283. var current = dto.currentPeriod || 1
  284. var total = dto.totalPeriods || 1
  285. requestVirtualPayment(dto, {
  286. success: function() {
  287. if (current < total) {
  288. uni.showToast({ title: '第' + current + '期支付成功', icon: 'none' })
  289. nextOrder({ orderNo: dto.orderNo }).then(function(res) {
  290. var nd = res.data
  291. if (nd && nd.done) {
  292. self.finishPay()
  293. } else if (nd && nd.orderInfo && nd.sign) {
  294. self.currentPeriod = nd.currentPeriod || 1
  295. self.payInstallments(nd)
  296. } else {
  297. self.finishPay()
  298. }
  299. }).catch(function() {
  300. self.finishPay()
  301. })
  302. } else {
  303. self.finishPay()
  304. }
  305. },
  306. fail: function(err) {
  307. self.paying = false
  308. var errCode = err && err.errCode
  309. var msg = ''
  310. if (errCode === -15007 || errCode === -15005) msg = '登录态已过期,请重新登录后支付'
  311. else if (errCode === -15010) msg = '商品未发布,请联系客服'
  312. else if (errCode === -15013) msg = '商品价格异常'
  313. else if (errCode === -2) msg = '支付已取消'
  314. else if (err && err.errMsg) msg = err.errMsg
  315. var status = (errCode === -2 || errCode === -15007 || errCode === -15005) ? 'fail' : 'fail'
  316. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=' + status })
  317. }
  318. })
  319. },
  320. finishPay: function() {
  321. var self = this
  322. uni.showToast({ title: '支付成功,开通中…', icon: 'none' })
  323. setTimeout(function() {
  324. self.paying = false
  325. uni.redirectTo({ url: '/pages/membership/result?orderNo=' + (self.orderNo || '') + '&status=success' })
  326. }, 800)
  327. }
  328. }
  329. }
  330. </script>
  331. <style scoped>
  332. .pay-page {
  333. min-height: 100vh;
  334. background: #FFF7ED;
  335. padding-bottom: 160rpx;
  336. box-sizing: border-box;
  337. }
  338. .page {
  339. padding: 30rpx;
  340. }
  341. /* ===== 商品明细卡 ===== */
  342. .order-card {
  343. background: #FFFFFF;
  344. border-radius: 20rpx;
  345. padding: 40rpx;
  346. box-shadow: 0 4rpx 24rpx rgba(249, 115, 22, 0.08);
  347. }
  348. .order-header {
  349. margin-bottom: 24rpx;
  350. }
  351. .order-title {
  352. font-size: 34rpx;
  353. font-weight: bold;
  354. color: #1E293B;
  355. display: block;
  356. }
  357. .order-sub {
  358. font-size: 24rpx;
  359. color: #94A3B8;
  360. margin-top: 6rpx;
  361. display: block;
  362. }
  363. .price-row {
  364. display: flex;
  365. justify-content: space-between;
  366. align-items: center;
  367. padding: 16rpx 0;
  368. }
  369. .price-label {
  370. font-size: 26rpx;
  371. color: #64748B;
  372. }
  373. .price-original {
  374. font-size: 28rpx;
  375. color: #94A3B8;
  376. text-decoration: line-through;
  377. }
  378. .price-now {
  379. font-size: 36rpx;
  380. font-weight: bold;
  381. color: #F97316;
  382. }
  383. .coupon-row {
  384. display: flex;
  385. align-items: center;
  386. padding: 24rpx 0;
  387. border-top: 1rpx solid #f5f5f5;
  388. margin-top: 16rpx;
  389. }
  390. .coupon-row:active {
  391. opacity: 0.7;
  392. }
  393. .coupon-row-label {
  394. font-size: 26rpx;
  395. color: #666;
  396. margin-right: 16rpx;
  397. }
  398. .coupon-row-discount {
  399. flex: 1;
  400. font-size: 26rpx;
  401. color: #F97316;
  402. font-weight: 600;
  403. text-align: right;
  404. }
  405. .coupon-row-placeholder {
  406. flex: 1;
  407. font-size: 26rpx;
  408. color: #ccc;
  409. text-align: right;
  410. }
  411. .coupon-row-arrow {
  412. font-size: 32rpx;
  413. color: #ccc;
  414. margin-left: 8rpx;
  415. }
  416. .divider {
  417. height: 1rpx;
  418. background: #F1F5F9;
  419. margin: 16rpx 0;
  420. }
  421. .total-row {
  422. display: flex;
  423. justify-content: space-between;
  424. align-items: center;
  425. }
  426. .total-label {
  427. font-size: 28rpx;
  428. font-weight: 600;
  429. color: #1E293B;
  430. }
  431. .total-amount {
  432. font-size: 44rpx;
  433. font-weight: bold;
  434. color: #F97316;
  435. }
  436. /* ===== 拆单提示 ===== */
  437. .split-card {
  438. background: #FEF3C7;
  439. border-radius: 20rpx;
  440. padding: 24rpx;
  441. margin-top: 30rpx;
  442. }
  443. .split-title {
  444. font-size: 28rpx;
  445. font-weight: 600;
  446. color: #92400E;
  447. display: block;
  448. margin-bottom: 8rpx;
  449. }
  450. .split-desc {
  451. font-size: 24rpx;
  452. color: #B45309;
  453. display: block;
  454. }
  455. /* ===== 支付进度 ===== */
  456. .progress-card {
  457. background: #FFFFFF;
  458. border-radius: 20rpx;
  459. padding: 20rpx;
  460. margin-top: 20rpx;
  461. text-align: center;
  462. }
  463. .progress-text {
  464. font-size: 26rpx;
  465. color: #F97316;
  466. font-weight: 600;
  467. }
  468. /* ===== 底部支付栏 ===== */
  469. .pay-bar {
  470. position: fixed;
  471. bottom: 0;
  472. left: 0;
  473. right: 0;
  474. background: #FFFFFF;
  475. padding: 20rpx 30rpx calc(20rpx + env(safe-area-inset-bottom));
  476. box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
  477. }
  478. .btn-pay {
  479. width: 100%;
  480. height: 96rpx;
  481. line-height: 96rpx;
  482. background: linear-gradient(135deg, #F97316 0%, #EA580C 100%);
  483. color: #fff;
  484. font-size: 34rpx;
  485. font-weight: 600;
  486. border-radius: 48rpx;
  487. border: none;
  488. }
  489. .btn-pay::after {
  490. border: none;
  491. }
  492. .btn-pay[disabled] {
  493. opacity: 0.7;
  494. color: #fff;
  495. }
  496. /* ===== Coupon Picker(复用 upgrade 样式) ===== */
  497. .modal-mask {
  498. position: fixed;
  499. top: 0;
  500. left: 0;
  501. right: 0;
  502. bottom: 0;
  503. background: rgba(0,0,0,0.5);
  504. z-index: 999;
  505. display: flex;
  506. align-items: flex-end;
  507. }
  508. .coupon-picker {
  509. background: #fff;
  510. border-radius: 24rpx 24rpx 0 0;
  511. width: 100%;
  512. max-height: 70vh;
  513. display: flex;
  514. flex-direction: column;
  515. }
  516. .picker-header {
  517. display: flex;
  518. justify-content: space-between;
  519. align-items: center;
  520. padding: 30rpx;
  521. border-bottom: 1rpx solid #eee;
  522. }
  523. .picker-title {
  524. font-size: 30rpx;
  525. font-weight: 600;
  526. color: #333;
  527. }
  528. .picker-close {
  529. font-size: 26rpx;
  530. color: #999;
  531. }
  532. .picker-list {
  533. max-height: 50vh;
  534. padding: 0 20rpx;
  535. }
  536. .picker-item {
  537. display: flex;
  538. align-items: center;
  539. padding: 24rpx 16rpx;
  540. border-bottom: 1rpx solid #f5f5f5;
  541. }
  542. .picker-item:active {
  543. background: #fafafa;
  544. }
  545. .picker-item-active {
  546. background: #FFF7ED;
  547. }
  548. .picker-item-left {
  549. width: 120rpx;
  550. text-align: center;
  551. flex-shrink: 0;
  552. }
  553. .picker-item-value {
  554. font-size: 32rpx;
  555. font-weight: bold;
  556. color: #F97316;
  557. }
  558. .picker-item-right {
  559. flex: 1;
  560. margin: 0 16rpx;
  561. }
  562. .picker-item-name {
  563. font-size: 26rpx;
  564. color: #333;
  565. display: block;
  566. margin-bottom: 4rpx;
  567. }
  568. .picker-item-condition {
  569. font-size: 22rpx;
  570. color: #999;
  571. }
  572. .picker-item-check {
  573. width: 40rpx;
  574. height: 40rpx;
  575. background: #F97316;
  576. border-radius: 50%;
  577. display: flex;
  578. align-items: center;
  579. justify-content: center;
  580. flex-shrink: 0;
  581. color: #fff;
  582. font-size: 24rpx;
  583. font-weight: bold;
  584. }
  585. .picker-empty {
  586. text-align: center;
  587. padding: 60rpx 0;
  588. font-size: 26rpx;
  589. color: #999;
  590. }
  591. .picker-footer {
  592. padding: 20rpx;
  593. border-top: 1rpx solid #eee;
  594. }
  595. .picker-btn-remove {
  596. background: #f5f5f5;
  597. color: #666;
  598. font-size: 26rpx;
  599. border-radius: 12rpx;
  600. border: none;
  601. }
  602. .picker-btn-remove::after {
  603. border: none;
  604. }
  605. </style>