order-pay.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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">
  24. <text class="info-label">订单金额</text>
  25. <text class="order-amount">¥{{ formatAmount(totalAmount) }}</text>
  26. </view>
  27. </view>
  28. <!-- Payment Method -->
  29. <view class="section">
  30. <text class="section-title">支付方式</text>
  31. <view class="payment-method-row">
  32. <view class="payment-method-left">
  33. <text class="payment-icon">💳</text>
  34. <text class="payment-name">微信支付</text>
  35. </view>
  36. <text class="payment-checked">✓</text>
  37. </view>
  38. </view>
  39. <!-- Pay Button -->
  40. <view class="pay-section">
  41. <button
  42. :class="['pay-btn', (paying || countdown <= 0) ? 'disabled' : '']"
  43. :disabled="paying || countdown <= 0"
  44. @click="onPay"
  45. >
  46. {{ paying ? '支付中...' : '微信支付 ¥' + formatAmount(totalAmount) }}
  47. </button>
  48. <text class="cancel-link" @click="onCancelPay">暂不支付</text>
  49. </view>
  50. </scroll-view>
  51. </view>
  52. </template>
  53. <script>
  54. import { payActivityOrder, handleActivityOrderSuccess } from '@/utils/api.js'
  55. import fontScaleMixin from '@/mixins/font-scale.js'
  56. export default {
  57. mixins: [fontScaleMixin],
  58. data() {
  59. return {
  60. orderNo: '',
  61. totalAmount: 0,
  62. activityId: null,
  63. countdown: 900,
  64. paying: false,
  65. loading: false,
  66. timer: null
  67. }
  68. },
  69. computed: {
  70. formatCountdown() {
  71. var min = Math.floor(this.countdown / 60)
  72. var sec = this.countdown % 60
  73. return String(min).padStart(2, '0') + ':' + String(sec).padStart(2, '0')
  74. }
  75. },
  76. onLoad(options) {
  77. if (options.orderNo) {
  78. this.orderNo = options.orderNo
  79. }
  80. if (options.totalAmount) {
  81. this.totalAmount = parseFloat(options.totalAmount)
  82. }
  83. if (options.activityId) {
  84. this.activityId = options.activityId
  85. }
  86. this.startCountdown()
  87. },
  88. onUnload() {
  89. if (this.timer) {
  90. clearInterval(this.timer)
  91. this.timer = null
  92. }
  93. },
  94. methods: {
  95. formatAmount: function(val) {
  96. var num = parseFloat(val) || 0
  97. return (num / 100).toFixed(2)
  98. },
  99. startCountdown() {
  100. var that = this
  101. this.timer = setInterval(function() {
  102. if (that.countdown > 0) {
  103. that.countdown = that.countdown - 1
  104. } else {
  105. if (that.timer) {
  106. clearInterval(that.timer)
  107. that.timer = null
  108. }
  109. }
  110. }, 1000)
  111. },
  112. onPay() {
  113. if (this.countdown <= 0) {
  114. uni.showToast({ title: '订单已取消,无法支付', icon: 'none' })
  115. return
  116. }
  117. this.paying = true
  118. var that = this
  119. payActivityOrder(this.orderNo).then(function(res) {
  120. that.paying = false
  121. if (res && res.code === 200 && res.data && res.data.wechatPayParams) {
  122. var params = res.data.wechatPayParams
  123. uni.requestPayment({
  124. timeStamp: params.timeStamp,
  125. nonceStr: params.nonceStr,
  126. package: params.package,
  127. signType: params.signType || 'RSA',
  128. paySign: params.paySign,
  129. success: function() {
  130. handleActivityOrderSuccess(that.orderNo).then(function() {
  131. uni.showToast({ title: '支付成功', icon: 'success' })
  132. setTimeout(function() {
  133. that.backToDetail(true)
  134. }, 1500)
  135. }).catch(function() {
  136. uni.showToast({ title: '支付成功', icon: 'success' })
  137. setTimeout(function() {
  138. that.backToDetail(true)
  139. }, 1500)
  140. })
  141. },
  142. fail: function() {
  143. uni.showToast({ title: '支付失败', icon: 'none' })
  144. }
  145. })
  146. } else {
  147. uni.showToast({ title: (res && res.message) || '获取支付参数失败', icon: 'none' })
  148. }
  149. }).catch(function() {
  150. that.paying = false
  151. uni.showToast({ title: '网络请求失败', icon: 'none' })
  152. })
  153. },
  154. onCancelPay() {
  155. this.backToDetail(false)
  156. },
  157. backToDetail(refresh) {
  158. if (this.activityId) {
  159. uni.redirectTo({
  160. url: '/pages/activity/activity-detail/activity-detail?id=' + this.activityId + (refresh ? '&paid=1' : '')
  161. })
  162. } else {
  163. uni.navigateBack()
  164. }
  165. }
  166. }
  167. }
  168. </script>
  169. <style scoped lang="scss">
  170. .container {
  171. min-height: 100vh;
  172. background: $app-bg;
  173. }
  174. .loading-wrap {
  175. display: flex;
  176. align-items: center;
  177. justify-content: center;
  178. padding: 120rpx 40rpx;
  179. }
  180. .loading-text {
  181. font-size: calc(28rpx * var(--font-scale, 1));
  182. color: $app-text-subtle;
  183. }
  184. .content {
  185. height: 100vh;
  186. box-sizing: border-box;
  187. }
  188. .header-section {
  189. background: $brand-primary;
  190. padding: 48rpx 32rpx;
  191. display: flex;
  192. flex-direction: column;
  193. align-items: center;
  194. }
  195. .header-title {
  196. font-size: calc(36rpx * var(--font-scale, 1));
  197. font-weight: bold;
  198. color: $app-surface;
  199. }
  200. .header-countdown {
  201. margin-top: 12rpx;
  202. font-size: calc(26rpx * var(--font-scale, 1));
  203. color: $facade-brand-tint-2;
  204. }
  205. .header-cancelled {
  206. margin-top: 12rpx;
  207. font-size: calc(26rpx * var(--font-scale, 1));
  208. color: $facade-brand-tint-2;
  209. }
  210. .section {
  211. background: $app-surface;
  212. border-radius: 16rpx;
  213. margin: 24rpx;
  214. padding: 28rpx;
  215. }
  216. .section-title {
  217. display: block;
  218. font-size: calc(28rpx * var(--font-scale, 1));
  219. font-weight: 600;
  220. color: $uni-text-color;
  221. margin-bottom: 20rpx;
  222. }
  223. .info-row {
  224. display: flex;
  225. flex-direction: row;
  226. justify-content: space-between;
  227. align-items: center;
  228. padding: 12rpx 0;
  229. }
  230. .info-label {
  231. font-size: calc(26rpx * var(--font-scale, 1));
  232. color: $uni-text-color-secondary;
  233. }
  234. .info-value {
  235. font-size: calc(26rpx * var(--font-scale, 1));
  236. color: $uni-text-color;
  237. max-width: 400rpx;
  238. word-break: break-all;
  239. text-align: right;
  240. }
  241. .order-amount {
  242. font-size: calc(32rpx * var(--font-scale, 1));
  243. font-weight: bold;
  244. color: $brand-primary;
  245. }
  246. .payment-method-row {
  247. display: flex;
  248. flex-direction: row;
  249. justify-content: space-between;
  250. align-items: center;
  251. padding: 12rpx 0;
  252. }
  253. .payment-method-left {
  254. display: flex;
  255. flex-direction: row;
  256. align-items: center;
  257. }
  258. .payment-icon {
  259. font-size: calc(32rpx * var(--font-scale, 1));
  260. margin-right: 12rpx;
  261. }
  262. .payment-name {
  263. font-size: calc(28rpx * var(--font-scale, 1));
  264. color: $uni-text-color;
  265. }
  266. .payment-checked {
  267. font-size: calc(28rpx * var(--font-scale, 1));
  268. color: $status-success;
  269. }
  270. .pay-section {
  271. margin: 48rpx 24rpx;
  272. display: flex;
  273. flex-direction: column;
  274. align-items: center;
  275. }
  276. .pay-btn {
  277. width: 100%;
  278. height: 88rpx;
  279. border-radius: 24rpx;
  280. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  281. color: $app-surface;
  282. font-size: calc(30rpx * var(--font-scale, 1));
  283. font-weight: 600;
  284. display: flex;
  285. align-items: center;
  286. justify-content: center;
  287. }
  288. .pay-btn.disabled {
  289. background: $uni-border-color;
  290. }
  291. .cancel-link {
  292. margin-top: 24rpx;
  293. font-size: calc(26rpx * var(--font-scale, 1));
  294. color: $app-text-subtle;
  295. }
  296. </style>