pay.vue 19 KB

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