index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="pay-page">
  3. <LoadingSkeleton v-if="loading" :rows="2" />
  4. <ErrorState v-else-if="error" message="订单加载失败" @retry="initOrder" />
  5. <EmptyState
  6. v-else-if="!orderNo"
  7. icon="📄"
  8. title="订单创建失败"
  9. desc="未能创建支付订单,可返回列表重新操作"
  10. action-text="返回列表"
  11. @action="backToOrderList"
  12. />
  13. <view v-else class="pay-card">
  14. <text class="pay-title">确认支付</text>
  15. <text class="pay-amount">¥{{ fenToYuan(amount) }}</text>
  16. <text class="pay-order" v-if="orderNo">订单号:{{ orderNo }}</text>
  17. <view v-if="status === 'paid'" class="paid-box">
  18. <text class="paid-icon">✓</text>
  19. <text class="paid-text">支付成功</text>
  20. <button class="primary-btn" @click="goResult('success')">查看结果</button>
  21. </view>
  22. <view v-else>
  23. <button class="pay-btn" :loading="paying" :disabled="paying" @click="handlePay">立即支付</button>
  24. <view class="pay-tip">测试环境将模拟支付成功</view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import { createPayOrder, getPayStatus } from '@/utils/api.js'
  31. import { fenToYuan } from '@/utils/format.js'
  32. import { guardPage } from '@/utils/guard.js'
  33. import LoadingSkeleton from '@/components/loading-skeleton.vue'
  34. import ErrorState from '@/components/error-state.vue'
  35. import EmptyState from '@/components/empty-state.vue'
  36. export default {
  37. components: { LoadingSkeleton, ErrorState, EmptyState },
  38. data() {
  39. return {
  40. enrollmentId: '',
  41. orderNo: '',
  42. amount: 0,
  43. status: '',
  44. prepayParams: null,
  45. loading: false,
  46. error: false,
  47. paying: false,
  48. pollTimer: null
  49. }
  50. },
  51. onLoad(options) {
  52. if (!guardPage()) return
  53. this.enrollmentId = options && options.enrollmentId ? options.enrollmentId : ''
  54. if (!this.enrollmentId) {
  55. uni.showToast({ title: '缺少报名信息', icon: 'none' })
  56. setTimeout(function() {
  57. uni.navigateBack({ delta: 1 })
  58. }, 1500)
  59. return
  60. }
  61. this.initOrder()
  62. },
  63. onUnload() {
  64. if (this.pollTimer) {
  65. clearInterval(this.pollTimer)
  66. this.pollTimer = null
  67. }
  68. },
  69. methods: {
  70. fenToYuan: fenToYuan,
  71. initOrder() {
  72. var self = this
  73. self.loading = true
  74. self.error = false
  75. createPayOrder({ enrollmentId: self.enrollmentId }).then(function(resp) {
  76. var data = resp.data || {}
  77. self.orderNo = data.orderNo || ''
  78. self.amount = data.amount || 0
  79. self.status = data.status || ''
  80. self.prepayParams = data.prepayParams || null
  81. if (self.status === 'paid') {
  82. setTimeout(function() {
  83. self.goResult('success')
  84. }, 800)
  85. }
  86. }).catch(function() {
  87. self.error = true
  88. }).finally(function() {
  89. self.loading = false
  90. })
  91. },
  92. backToOrderList() {
  93. var pages = getCurrentPages()
  94. if (pages.length > 1) {
  95. uni.navigateBack()
  96. } else {
  97. uni.redirectTo({ url: '/pages/enroll/list' })
  98. }
  99. },
  100. handlePay() {
  101. var self = this
  102. if (this.paying) return
  103. // test-mode:后端 create 时已直接置 paid,直接查询确认
  104. var pp = this.prepayParams
  105. if (pp && pp.timeStamp) {
  106. // 生产模式:调起微信支付(预留)
  107. this.paying = true
  108. uni.requestPayment({
  109. provider: 'wxpay',
  110. timeStamp: pp.timeStamp,
  111. nonceStr: pp.nonceStr,
  112. package: pp.package,
  113. signType: pp.signType || 'MD5',
  114. paySign: pp.paySign,
  115. success: function() {
  116. self.startPoll()
  117. },
  118. fail: function() {
  119. self.paying = false
  120. uni.showToast({ title: '支付已取消', icon: 'none' })
  121. }
  122. })
  123. } else {
  124. // 测试模式:模拟支付成功
  125. this.paying = true
  126. uni.showLoading({ title: '模拟支付中…' })
  127. setTimeout(function() {
  128. self.startPoll()
  129. }, 800)
  130. }
  131. },
  132. startPoll() {
  133. var self = this
  134. var max = 6
  135. var count = 0
  136. if (this.pollTimer) clearInterval(this.pollTimer)
  137. this.pollTimer = setInterval(function() {
  138. count++
  139. getPayStatus({ orderNo: self.orderNo }).then(function(resp) {
  140. var data = resp.data || {}
  141. if (data.status === 'paid') {
  142. clearInterval(self.pollTimer)
  143. self.pollTimer = null
  144. self.paying = false
  145. uni.hideLoading()
  146. self.status = 'paid'
  147. self.goResult('success')
  148. } else if (count >= max) {
  149. clearInterval(self.pollTimer)
  150. self.pollTimer = null
  151. self.paying = false
  152. uni.hideLoading()
  153. uni.showToast({ title: '支付处理中,请稍后查看', icon: 'none' })
  154. }
  155. }).catch(function() {
  156. if (count >= max) {
  157. clearInterval(self.pollTimer)
  158. self.pollTimer = null
  159. self.paying = false
  160. uni.hideLoading()
  161. }
  162. })
  163. }, 1500)
  164. },
  165. goResult(status) {
  166. uni.redirectTo({
  167. url: '/pages/pay/result?status=' + status + '&amount=' + this.amount + '&orderNo=' + this.orderNo
  168. })
  169. }
  170. }
  171. }
  172. </script>
  173. <style scoped>
  174. .pay-page { min-height: 100vh; background: #F5FAFE; padding: 32rpx; }
  175. .pay-card { background: #FFF; border-radius: 16rpx; padding: 48rpx 32rpx; display: flex; flex-direction: column; align-items: center; }
  176. .pay-title { font-size: 28rpx; color: #3B5A78; margin-bottom: 24rpx; }
  177. .pay-amount { font-size: 64rpx; font-weight: 700; color: #1E3A5F; margin-bottom: 16rpx; }
  178. .pay-order { font-size: 24rpx; color: #6B7B8C; margin-bottom: 48rpx; }
  179. .paid-box { display: flex; flex-direction: column; align-items: center; width: 100%; }
  180. .paid-icon { width: 96rpx; height: 96rpx; border-radius: 50%; background: #0E8A4F; color: #FFF; font-size: 56rpx; display: flex; align-items: center; justify-content: center; margin-bottom: 24rpx; }
  181. .paid-text { font-size: 32rpx; font-weight: 600; color: #1E3A5F; margin-bottom: 48rpx; }
  182. .pay-btn { width: 100%; height: 88rpx; line-height: 88rpx; background: #2E6FA8; color: #FFF; font-size: 30rpx; font-weight: 600; border-radius: 44rpx; border: none; }
  183. .pay-btn:active { opacity: 0.85; }
  184. .pay-tip { font-size: 24rpx; color: #6B7B8C; margin-top: 16rpx; }
  185. .primary-btn { width: 400rpx; height: 80rpx; line-height: 80rpx; background: #2E6FA8; color: #FFF; font-size: 28rpx; font-weight: 600; border-radius: 40rpx; border: none; }
  186. .primary-btn:active { opacity: 0.85; }
  187. .empty-tip { font-size: 26rpx; color: #6B7B8C; padding: 64rpx 0; text-align: center; }
  188. </style>