result.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <!-- 支付结果页面 -->
  2. <template>
  3. <s-layout title="支付结果" :bgStyle="{ color: '#FFF' }">
  4. <view class="pay-result-box ss-flex-col ss-row-center ss-col-center">
  5. <view class="pay-waiting ss-m-b-30" v-if="payResult === 'waiting'"> </view>
  6. <image
  7. class="pay-img ss-m-b-30"
  8. v-if="payResult === 'success'"
  9. :src="sheep.$url.static('/static/img/shop/order/order_pay_success.gif')"
  10. ></image>
  11. <image
  12. class="pay-img ss-m-b-30"
  13. v-if="['failed', 'closed'].includes(payResult)"
  14. :src="sheep.$url.static('/static/img/shop/order/order_paty_fail.gif')"
  15. ></image>
  16. <view class="tip-text ss-m-b-30" v-if="payResult == 'success'">{{
  17. state.orderInfo.pay_mode === 'offline' ? '下单成功' : '支付成功'
  18. }}</view>
  19. <view class="tip-text ss-m-b-30" v-if="payResult == 'failed'">支付失败</view>
  20. <view class="tip-text ss-m-b-30" v-if="payResult == 'closed'">该订单已关闭</view>
  21. <view class="tip-text ss-m-b-30" v-if="payResult == 'waiting'">检测支付结果...</view>
  22. <view class="pay-total-num ss-flex" v-if="payResult === 'success'">
  23. <view v-if="Number(state.orderInfo.pay_fee) > 0">¥{{ state.orderInfo.pay_fee }}</view>
  24. <view v-if="state.orderInfo.score_amount && Number(state.orderInfo.pay_fee) > 0">+</view>
  25. <view class="price-text ss-flex ss-col-center" v-if="state.orderInfo.score_amount">
  26. <image
  27. :src="sheep.$url.static('/static/img/shop/goods/score1.svg')"
  28. class="score-img"
  29. ></image>
  30. <view>{{ state.orderInfo.score_amount }}</view>
  31. </view>
  32. </view>
  33. <view class="btn-box ss-flex ss-row-center ss-m-t-50">
  34. <button class="back-btn ss-reset-button" @tap="sheep.$router.go('/pages/index/index')">
  35. 返回首页
  36. </button>
  37. <button
  38. class="check-btn ss-reset-button"
  39. v-if="payResult === 'failed'"
  40. @tap="sheep.$router.redirect('/pages/pay/index', { orderSN: state.orderId })"
  41. >
  42. 重新支付
  43. </button>
  44. <button class="check-btn ss-reset-button" v-if="payResult === 'success'" @tap="onOrder">
  45. 查看订单
  46. </button>
  47. <button
  48. class="check-btn ss-reset-button"
  49. v-if="
  50. payResult === 'success' &&
  51. ['groupon', 'groupon_ladder'].includes(state.orderInfo.activity_type)
  52. "
  53. @tap="sheep.$router.redirect('/pages/activity/groupon/order')"
  54. >
  55. 我的拼团
  56. </button>
  57. </view>
  58. <!-- #ifdef MP -->
  59. <view class="subscribe-box ss-flex ss-m-t-44">
  60. <image
  61. class="subscribe-img"
  62. :src="sheep.$url.static('/static/img/shop/order/cargo.png')"
  63. ></image>
  64. <view class="subscribe-title ss-m-r-48 ss-m-l-16">获取实时发货信息与订单状态</view>
  65. <view class="subscribe-start" @tap="subscribeMessage">立即订阅</view>
  66. </view>
  67. <!-- #endif -->
  68. </view>
  69. </s-layout>
  70. </template>
  71. <script setup>
  72. import { onLoad, onHide, onShow } from '@dcloudio/uni-app';
  73. import { reactive, computed } from 'vue';
  74. import { isEmpty } from 'lodash';
  75. import sheep from '@/sheep';
  76. const state = reactive({
  77. orderId: 0,
  78. orderType: 'goods',
  79. result: 'unpaid', // 支付状态
  80. orderInfo: {}, // 订单详情
  81. counter: 0, // 获取结果次数
  82. });
  83. const payResult = computed(() => {
  84. if (state.result === 'unpaid') {
  85. return 'waiting';
  86. }
  87. if (state.result === 'paid') {
  88. return 'success';
  89. }
  90. if (state.result === 'failed') {
  91. return 'failed';
  92. }
  93. if (state.result === 'closed') {
  94. return 'closed';
  95. }
  96. });
  97. async function getOrderInfo(orderId) {
  98. let checkPayResult;
  99. state.counter++;
  100. if (state.orderType === 'recharge') {
  101. checkPayResult = sheep.$api.trade.order;
  102. } else {
  103. checkPayResult = sheep.$api.order.detail;
  104. }
  105. const { data, error } = await checkPayResult(orderId);
  106. if (error === 0) {
  107. state.orderInfo = data;
  108. if (state.orderInfo.status === 'closed') {
  109. state.result = 'closed';
  110. return;
  111. }
  112. if (state.orderInfo.status !== 'unpaid') {
  113. state.result = 'paid';
  114. // #ifdef MP
  115. subscribeMessage();
  116. // #endif
  117. return;
  118. }
  119. }
  120. if (state.counter < 3 && state.result === 'unpaid') {
  121. setTimeout(() => {
  122. getOrderInfo(orderId);
  123. }, 1500);
  124. }
  125. // 超过三次检测才判断为支付失败
  126. if (state.counter >= 3) {
  127. state.result = 'failed';
  128. }
  129. }
  130. function onOrder() {
  131. if ((state.orderType === 'recharge')) {
  132. sheep.$router.redirect('/pages/pay/recharge-log');
  133. } else {
  134. sheep.$router.redirect('/pages/order/list');
  135. }
  136. }
  137. // #ifdef MP
  138. function subscribeMessage() {
  139. let event = ['order_dispatched'];
  140. if (['groupon', 'groupon_ladder'].includes(state.orderInfo.activity_type)) {
  141. event.push('groupon_finish');
  142. event.push('groupon_fail');
  143. }
  144. sheep.$platform.useProvider('wechat').subscribeMessage(event);
  145. }
  146. // #endif
  147. onLoad(async (options) => {
  148. let id = '';
  149. // 支付订单号
  150. if (options.orderSN) {
  151. id = options.orderSN;
  152. }
  153. if (options.id) {
  154. id = options.id;
  155. }
  156. state.orderId = id;
  157. if (options.orderType === 'recharge') {
  158. state.orderType = 'recharge';
  159. }
  160. // 支付结果传值过来是失败,则直接显示失败界面
  161. if (options.payState === 'fail') {
  162. state.result = 'failed';
  163. } else {
  164. // 轮询三次检测订单支付结果
  165. getOrderInfo(state.orderId);
  166. }
  167. });
  168. onShow(() => {
  169. if(isEmpty(state.orderInfo)) return;
  170. getOrderInfo(state.orderId);
  171. })
  172. onHide(() => {
  173. state.result = 'unpaid';
  174. state.counter = 0;
  175. });
  176. </script>
  177. <style lang="scss" scoped>
  178. @keyframes rotation {
  179. 0% {
  180. transform: rotate(0deg);
  181. }
  182. 100% {
  183. transform: rotate(360deg);
  184. }
  185. }
  186. .score-img {
  187. width: 36rpx;
  188. height: 36rpx;
  189. margin: 0 4rpx;
  190. }
  191. .pay-result-box {
  192. padding: 60rpx 0;
  193. .pay-waiting {
  194. margin-top: 20rpx;
  195. width: 60rpx;
  196. height: 60rpx;
  197. border: 10rpx solid rgb(233, 231, 231);
  198. border-bottom-color: rgb(204, 204, 204);
  199. border-radius: 50%;
  200. display: inline-block;
  201. // -webkit-animation: rotation 1s linear infinite;
  202. animation: rotation 1s linear infinite;
  203. }
  204. .pay-img {
  205. width: 130rpx;
  206. height: 130rpx;
  207. }
  208. .tip-text {
  209. font-size: 30rpx;
  210. font-weight: bold;
  211. color: #333333;
  212. }
  213. .pay-total-num {
  214. font-size: 36rpx;
  215. font-weight: 500;
  216. color: #333333;
  217. font-family: OPPOSANS;
  218. }
  219. .btn-box {
  220. width: 100%;
  221. .back-btn {
  222. width: 190rpx;
  223. height: 70rpx;
  224. font-size: 28rpx;
  225. border: 2rpx solid #dfdfdf;
  226. border-radius: 35rpx;
  227. font-weight: 400;
  228. color: #595959;
  229. }
  230. .check-btn {
  231. width: 190rpx;
  232. height: 70rpx;
  233. font-size: 28rpx;
  234. border: 2rpx solid #dfdfdf;
  235. border-radius: 35rpx;
  236. font-weight: 400;
  237. color: #595959;
  238. margin-left: 32rpx;
  239. }
  240. }
  241. .subscribe-box {
  242. .subscribe-img {
  243. width: 44rpx;
  244. height: 44rpx;
  245. }
  246. .subscribe-title {
  247. font-weight: 500;
  248. font-size: 32rpx;
  249. line-height: 36rpx;
  250. color: #434343;
  251. }
  252. .subscribe-start {
  253. color: var(--ui-BG-Main);
  254. font-weight: 700;
  255. font-size: 32rpx;
  256. line-height: 36rpx;
  257. }
  258. }
  259. }
  260. </style>