payment.vue 16 KB

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