payment.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  3. <view v-if="loading" class="loading-wrap">
  4. <text class="loading-text">加载中...</text>
  5. </view>
  6. <scroll-view v-else scroll-y class="content">
  7. <!-- Header Section -->
  8. <view class="header-section">
  9. <text class="header-title">订单待支付</text>
  10. <text class="header-countdown" v-if="countdown > 0">
  11. 支付剩余时间: {{ formatCountdown }}
  12. </text>
  13. <text class="header-cancelled" v-else>
  14. 订单已取消
  15. </text>
  16. </view>
  17. <!-- Order Info Card -->
  18. <view class="section">
  19. <view class="info-row">
  20. <text class="info-label">订单编号</text>
  21. <text class="info-value">{{ orderNo }}</text>
  22. </view>
  23. <view class="info-row" v-if="pointsUsed > 0">
  24. <text class="info-label">积分抵扣</text>
  25. <text class="info-value points-value">-{{ formatPrice(pointsUsed) }} ({{ pointsUsed }}积分)</text>
  26. </view>
  27. <view class="info-row">
  28. <text class="info-label">订单金额</text>
  29. <text class="order-amount">{{ formatPriceWithSymbol(amount) }}</text>
  30. </view>
  31. </view>
  32. <!-- Payment Method -->
  33. <view class="section">
  34. <text class="section-title">支付方式</text>
  35. <view class="payment-method-row">
  36. <view class="payment-method-left">
  37. <text class="payment-icon">💳</text>
  38. <text class="payment-name">微信支付</text>
  39. </view>
  40. <text class="payment-checked">✓</text>
  41. </view>
  42. </view>
  43. <!-- Pay Button -->
  44. <view class="pay-section">
  45. <button
  46. :class="['pay-btn', (paying || countdown <= 0) ? 'disabled' : '']"
  47. :disabled="paying || countdown <= 0"
  48. @click="onPay"
  49. >
  50. {{ paying ? '支付中...' : '微信支付 ' + formatPriceWithSymbol(amount) }}
  51. </button>
  52. <text class="cancel-link" @click="onCancelOrder">取消订单</text>
  53. </view>
  54. <!-- Post-payment Upgrade Card -->
  55. <view v-if="showUpgradeCard" class="upgrade-card">
  56. <view class="upgrade-card-header">
  57. <text class="upgrade-card-icon">🎉</text>
  58. <text class="upgrade-card-title">支付成功!开通会员更划算</text>
  59. </view>
  60. <view class="upgrade-card-body">
  61. <view class="upgrade-benefit-row">
  62. <text class="upgrade-benefit-icon">✓</text>
  63. <text class="upgrade-benefit-text">全场商品享受会员专享价</text>
  64. </view>
  65. <view class="upgrade-benefit-row">
  66. <text class="upgrade-benefit-icon">✓</text>
  67. <text class="upgrade-benefit-text">不限次一对一成长咨询服务</text>
  68. </view>
  69. <view class="upgrade-benefit-row">
  70. <text class="upgrade-benefit-icon">✓</text>
  71. <text class="upgrade-benefit-text">专属家庭成长计划与测评报告</text>
  72. </view>
  73. <view class="upgrade-price-row">
  74. <text class="upgrade-price-label">仅需</text>
  75. <text class="upgrade-price-value">{{ formatPriceWithSymbol(familyYearlyPrice * 100) }}</text>
  76. <text class="upgrade-price-unit">/年</text>
  77. <text class="upgrade-price-original" v-if="familyYearlyPrice > 0">{{ formatPriceWithSymbol((familyYearlyPrice * 1.5 * 100).toFixed(0)) }}</text>
  78. </view>
  79. </view>
  80. <view class="upgrade-card-action">
  81. <button class="upgrade-btn" @click="goUpgrade">立即开通</button>
  82. <text class="upgrade-skip" @click="showUpgradeCard = false">暂不开通</text>
  83. </view>
  84. </view>
  85. </scroll-view>
  86. </view>
  87. </template>
  88. <script>
  89. import config from '@/config.js'
  90. import { getMyMembership } from '@/utils/api.js'
  91. import fontScaleMixin from '@/mixins/font-scale.js'
  92. export default {
  93. mixins: [fontScaleMixin],
  94. data() {
  95. return {
  96. orderNo: '',
  97. amount: 0,
  98. countdown: 900,
  99. paying: false,
  100. loading: false,
  101. timer: null,
  102. pointsUsed: 0,
  103. isMember: false,
  104. showUpgradeCard: false,
  105. familyYearlyPrice: '365',
  106. taskId: null
  107. }
  108. },
  109. computed: {
  110. formatCountdown() {
  111. var min = Math.floor(this.countdown / 60)
  112. var sec = this.countdown % 60
  113. return String(min).padStart(2, '0') + ':' + String(sec).padStart(2, '0')
  114. }
  115. },
  116. onLoad(options) {
  117. this.checkMemberStatus()
  118. this.checkMembershipLevels()
  119. if (options.orderNo) {
  120. this.orderNo = options.orderNo
  121. }
  122. if (options.amount) {
  123. this.amount = parseFloat(options.amount)
  124. }
  125. if (options.pointsUsed) {
  126. this.pointsUsed = parseInt(options.pointsUsed)
  127. }
  128. // 购买任务打卡(系统商品:支付成功即完成任务;外部平台商品走独立拍照上传页)
  129. this.taskId = options && options.taskId ? parseInt(options.taskId) : null
  130. this.startCountdown()
  131. },
  132. onUnload() {
  133. if (this.timer) {
  134. clearInterval(this.timer)
  135. this.timer = null
  136. }
  137. },
  138. methods: {
  139. checkMemberStatus() {
  140. var that = this
  141. getMyMembership().then(function(res) {
  142. if (res.data && res.data.memberLevel) {
  143. that.isMember = res.data.memberLevel !== 'FREE'
  144. }
  145. }).catch(function() {})
  146. },
  147. checkMembershipLevels() {
  148. var that = this
  149. uni.request({
  150. url: config.api('/api/membership/levels'),
  151. method: 'POST',
  152. header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  153. success: function(res) {
  154. if (res.data && res.data.code === 200 && res.data.data) {
  155. var levels = res.data.data
  156. for (var i = 0; i < levels.length; i++) {
  157. if (levels[i].levelCode === 'FAMILY' && levels[i].priceYearly > 0) {
  158. that.familyYearlyPrice = (levels[i].priceYearly / 100).toFixed(0)
  159. break
  160. }
  161. }
  162. }
  163. }
  164. })
  165. },
  166. handlePaymentSuccess() {
  167. var that = this
  168. // 购买任务打卡:系统商品支付成功即完成任务
  169. //(外部平台商品走 /pages/tasks/task-upload-purchase 拍照上传,不经此页)
  170. if (that.taskId) {
  171. var memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || ''
  172. if (memberId) {
  173. uni.request({
  174. url: config.api('/api/tasks/' + that.taskId + '/complete'),
  175. method: 'POST',
  176. data: { memberId: memberId, photoUrl: '', content: '购买商品打卡完成', contentType: 'text' },
  177. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  178. success: function(cr) {
  179. if (cr.data && cr.data.code === 200) {
  180. uni.showToast({ title: '任务已完成', icon: 'success' })
  181. }
  182. },
  183. fail: function() {}
  184. })
  185. }
  186. }
  187. if (!this.isMember) {
  188. setTimeout(function() {
  189. that.showUpgradeCard = true
  190. }, 2000)
  191. }
  192. },
  193. goUpgrade() {
  194. uni.navigateTo({ url: '/pages/membership/upgrade' })
  195. },
  196. startCountdown() {
  197. var that = this
  198. this.timer = setInterval(function() {
  199. if (that.countdown > 0) {
  200. that.countdown = that.countdown - 1
  201. } else {
  202. if (that.timer) {
  203. clearInterval(that.timer)
  204. that.timer = null
  205. }
  206. }
  207. }, 1000)
  208. },
  209. onPay() {
  210. if (this.countdown <= 0) {
  211. uni.showToast({ title: '订单已取消,无法支付', icon: 'none' })
  212. return
  213. }
  214. this.paying = true
  215. var that = this
  216. uni.request({
  217. url: config.api('/api/product/order/pay'),
  218. method: 'POST',
  219. data: { orderNo: this.orderNo },
  220. header: {
  221. 'Content-Type': 'application/json',
  222. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  223. },
  224. success: function(res) {
  225. that.paying = false
  226. if (res.data && res.data.code === 200) {
  227. var data = res.data.data
  228. if (data.noMoneyPayment) {
  229. // Points fully covered the order, no WeChat needed
  230. uni.request({
  231. url: config.api('/api/product/order/handlePaymentSuccess'),
  232. method: 'POST',
  233. data: { orderNo: that.orderNo },
  234. header: {
  235. 'Content-Type': 'application/json',
  236. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  237. },
  238. success: function(payRes) {
  239. if (payRes.data && payRes.data.code === 200) {
  240. uni.showToast({ title: '支付成功', icon: 'success' })
  241. that.handlePaymentSuccess()
  242. setTimeout(function() {
  243. uni.redirectTo({
  244. url: '/pages/shop/order-list/order-list?tab=paid'
  245. })
  246. }, 1500)
  247. } else {
  248. uni.showToast({ title: payRes.data.message || '处理失败', icon: 'none' })
  249. }
  250. },
  251. fail: function() {
  252. uni.showToast({ title: '网络请求失败', icon: 'none' })
  253. }
  254. })
  255. } else if (data.wechatPayParams) {
  256. var params = data.wechatPayParams
  257. uni.requestPayment({
  258. timeStamp: params.timeStamp,
  259. nonceStr: params.nonceStr,
  260. package: params.package,
  261. signType: params.signType || 'RSA',
  262. paySign: params.paySign,
  263. success: function() {
  264. // Notify backend to update order status
  265. uni.request({
  266. url: config.api('/api/product/order/handlePaymentSuccess'),
  267. method: 'POST',
  268. data: { orderNo: that.orderNo },
  269. header: {
  270. 'Content-Type': 'application/json',
  271. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  272. }
  273. })
  274. uni.showToast({ title: '支付成功', icon: 'success' })
  275. that.handlePaymentSuccess()
  276. setTimeout(function() {
  277. uni.redirectTo({
  278. url: '/pages/shop/order-list/order-list?tab=paid'
  279. })
  280. }, 1500)
  281. },
  282. fail: function() {
  283. uni.showToast({ title: '支付失败', icon: 'none' })
  284. }
  285. })
  286. } else {
  287. uni.showToast({ title: '获取支付参数失败', icon: 'none' })
  288. }
  289. } else {
  290. uni.showToast({ title: res.data.message || '支付失败', icon: 'none' })
  291. }
  292. },
  293. fail: function() {
  294. that.paying = false
  295. uni.showToast({ title: '网络请求失败', icon: 'none' })
  296. }
  297. })
  298. },
  299. onCancelOrder() {
  300. var that = this
  301. uni.showModal({
  302. title: '确认取消',
  303. content: '确定取消该订单吗?',
  304. success: function(confirm) {
  305. if (confirm.confirm) {
  306. uni.request({
  307. url: config.api('/api/product/order/cancel'),
  308. method: 'POST',
  309. data: { orderNo: that.orderNo },
  310. header: {
  311. 'Content-Type': 'application/json',
  312. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  313. },
  314. success: function(res) {
  315. if (res.data && res.data.code === 200) {
  316. uni.showToast({ title: '订单已取消', icon: 'success' })
  317. setTimeout(function() {
  318. uni.navigateBack()
  319. }, 1500)
  320. } else {
  321. uni.showToast({ title: res.data.message || '取消失败', icon: 'none' })
  322. }
  323. }
  324. })
  325. }
  326. }
  327. })
  328. },
  329. formatPrice(price) {
  330. if (price === null || price === undefined) return '0.00'
  331. return (Number(price) / 100).toFixed(2)
  332. }
  333. }
  334. }
  335. </script>
  336. <style lang="scss">
  337. .container {
  338. min-height: 100vh;
  339. background: $facade-fill-2;
  340. }
  341. .loading-wrap {
  342. display: flex;
  343. justify-content: center;
  344. padding-top: 200rpx;
  345. }
  346. .loading-text {
  347. font-size: calc(28rpx * var(--font-scale, 1));
  348. color: $facade-text-muted;
  349. }
  350. .content {
  351. height: calc(100vh - 120rpx);
  352. }
  353. .header-section {
  354. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  355. padding: 50rpx $uni-space-page;
  356. display: flex;
  357. flex-direction: column;
  358. align-items: center;
  359. }
  360. .header-title {
  361. font-size: calc(36rpx * var(--font-scale, 1));
  362. font-weight: bold;
  363. color: $app-surface;
  364. margin-bottom: 12rpx;
  365. }
  366. .header-countdown {
  367. font-size: calc(26rpx * var(--font-scale, 1));
  368. color: rgba(255,255,255,0.9);
  369. }
  370. .header-cancelled {
  371. font-size: calc(26rpx * var(--font-scale, 1));
  372. color: rgba(255,255,255,0.7);
  373. }
  374. .section {
  375. background: $app-surface;
  376. margin: 20rpx;
  377. border-radius: 16rpx;
  378. padding: 24rpx;
  379. }
  380. .section-title {
  381. font-size: calc(28rpx * var(--font-scale, 1));
  382. font-weight: bold;
  383. color: $facade-text-strong;
  384. margin-bottom: 20rpx;
  385. display: block;
  386. }
  387. .info-row {
  388. display: flex;
  389. justify-content: space-between;
  390. align-items: center;
  391. padding: 16rpx 0;
  392. border-bottom: 1rpx solid $facade-fill-2;
  393. }
  394. .info-row:last-child {
  395. border-bottom: none;
  396. }
  397. .info-label {
  398. font-size: calc(26rpx * var(--font-scale, 1));
  399. color: $facade-text-muted;
  400. }
  401. .info-value {
  402. font-size: calc(26rpx * var(--font-scale, 1));
  403. color: $facade-text-strong;
  404. }
  405. .order-amount {
  406. font-size: calc(40rpx * var(--font-scale, 1));
  407. color: $brand-primary;
  408. font-weight: bold;
  409. }
  410. .payment-method-row {
  411. display: flex;
  412. align-items: center;
  413. justify-content: space-between;
  414. padding: 16rpx 0;
  415. }
  416. .payment-method-left {
  417. display: flex;
  418. align-items: center;
  419. }
  420. .payment-icon {
  421. font-size: calc(40rpx * var(--font-scale, 1));
  422. margin-right: 16rpx;
  423. }
  424. .payment-name {
  425. font-size: calc(28rpx * var(--font-scale, 1));
  426. color: $facade-text-strong;
  427. }
  428. .payment-checked {
  429. font-size: calc(32rpx * var(--font-scale, 1));
  430. color: $status-success-3;
  431. font-weight: bold;
  432. }
  433. .pay-section {
  434. display: flex;
  435. flex-direction: column;
  436. align-items: center;
  437. padding: 40rpx $uni-space-page;
  438. }
  439. .pay-btn {
  440. width: 100%;
  441. height: 88rpx;
  442. line-height: 88rpx;
  443. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  444. color: $app-surface;
  445. font-size: calc(32rpx * var(--font-scale, 1));
  446. font-weight: bold;
  447. border-radius: 24rpx;
  448. border: none;
  449. text-align: center;
  450. }
  451. .pay-btn::after {
  452. border: none;
  453. }
  454. .pay-btn.disabled {
  455. background: $facade-border-soft;
  456. color: $facade-text-muted;
  457. }
  458. .cancel-link {
  459. margin-top: 24rpx;
  460. font-size: calc(26rpx * var(--font-scale, 1));
  461. color: $facade-text-muted;
  462. text-decoration: underline;
  463. padding: 10rpx;
  464. }
  465. .points-value {
  466. color: $brand-primary;
  467. }
  468. /* Upgrade Card */
  469. .upgrade-card {
  470. margin: $uni-space-lg 20rpx;
  471. background: linear-gradient(135deg, $uni-primary-pale, $facade-brand-tint-2);
  472. border-radius: 24rpx;
  473. padding: $uni-space-lg;
  474. border: 2rpx solid $brand-primary-light-2;
  475. animation: fadeInUp 0.5s ease;
  476. }
  477. .upgrade-card-header {
  478. display: flex;
  479. align-items: center;
  480. margin-bottom: 20rpx;
  481. }
  482. .upgrade-card-icon {
  483. font-size: calc(40rpx * var(--font-scale, 1));
  484. margin-right: 12rpx;
  485. }
  486. .upgrade-card-title {
  487. font-size: calc(30rpx * var(--font-scale, 1));
  488. font-weight: bold;
  489. color: $facade-text-strong;
  490. }
  491. .upgrade-card-body {
  492. margin-bottom: 24rpx;
  493. }
  494. .upgrade-benefit-row {
  495. display: flex;
  496. align-items: center;
  497. padding: 8rpx 0;
  498. }
  499. .upgrade-benefit-icon {
  500. font-size: calc(24rpx * var(--font-scale, 1));
  501. color: $status-success-2;
  502. margin-right: 12rpx;
  503. font-weight: bold;
  504. }
  505. .upgrade-benefit-text {
  506. font-size: calc(26rpx * var(--font-scale, 1));
  507. color: $facade-text-body;
  508. }
  509. .upgrade-price-row {
  510. display: flex;
  511. align-items: baseline;
  512. margin-top: 16rpx;
  513. padding-top: 16rpx;
  514. border-top: 1rpx solid $brand-primary-light-2;
  515. }
  516. .upgrade-price-label {
  517. font-size: calc(26rpx * var(--font-scale, 1));
  518. color: $facade-text-muted;
  519. margin-right: 8rpx;
  520. }
  521. .upgrade-price-value {
  522. font-size: calc(48rpx * var(--font-scale, 1));
  523. font-weight: bold;
  524. color: $brand-primary;
  525. }
  526. .upgrade-price-unit {
  527. font-size: calc(24rpx * var(--font-scale, 1));
  528. color: $facade-text-muted;
  529. margin-left: 6rpx;
  530. }
  531. .upgrade-price-original {
  532. font-size: calc(24rpx * var(--font-scale, 1));
  533. color: $facade-text-ghost;
  534. text-decoration: line-through;
  535. margin-left: 12rpx;
  536. }
  537. .upgrade-card-action {
  538. display: flex;
  539. flex-direction: column;
  540. align-items: center;
  541. }
  542. .upgrade-btn {
  543. width: 100%;
  544. height: 80rpx;
  545. line-height: 80rpx;
  546. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  547. color: $app-surface;
  548. font-size: calc(30rpx * var(--font-scale, 1));
  549. font-weight: bold;
  550. border-radius: 24rpx;
  551. border: none;
  552. text-align: center;
  553. }
  554. .upgrade-btn::after {
  555. border: none;
  556. }
  557. .upgrade-skip {
  558. margin-top: 16rpx;
  559. font-size: calc(24rpx * var(--font-scale, 1));
  560. color: $facade-text-muted;
  561. padding: 8rpx;
  562. }
  563. @keyframes fadeInUp {
  564. from {
  565. opacity: 0;
  566. transform: translateY(30rpx);
  567. }
  568. to {
  569. opacity: 1;
  570. transform: translateY(0);
  571. }
  572. }
  573. </style>