result.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. const state = reactive({
  76. orderId: 0,
  77. orderType: 'goods',
  78. result: 'unpaid', // 支付状态
  79. orderInfo: {}, // 订单详情
  80. counter: 0, // 获取结果次数
  81. });
  82. const payResult = computed(() => {
  83. if (state.result === 'unpaid') {
  84. return 'waiting';
  85. }
  86. if (state.result === 'paid') {
  87. return 'success';
  88. }
  89. if (state.result === 'failed') {
  90. return 'failed';
  91. }
  92. if (state.result === 'closed') {
  93. return 'closed';
  94. }
  95. });
  96. async function getOrderInfo(orderId) {
  97. let checkPayResult;
  98. state.counter++;
  99. if (state.orderType === 'recharge') {
  100. checkPayResult = sheep.$api.trade.order;
  101. } else {
  102. checkPayResult = sheep.$api.order.detail;
  103. }
  104. const { data, error } = await checkPayResult(orderId);
  105. if (error === 0) {
  106. state.orderInfo = data;
  107. if (state.orderInfo.status === 'closed') {
  108. state.result = 'closed';
  109. return;
  110. }
  111. if (state.orderInfo.status !== 'unpaid') {
  112. state.result = 'paid';
  113. // #ifdef MP
  114. subscribeMessage();
  115. // #endif
  116. return;
  117. }
  118. }
  119. if (state.counter < 3 && state.result === 'unpaid') {
  120. setTimeout(() => {
  121. getOrderInfo(orderId);
  122. }, 1500);
  123. }
  124. // 超过三次检测才判断为支付失败
  125. if (state.counter >= 3) {
  126. state.result = 'failed';
  127. }
  128. }
  129. function onOrder() {
  130. if ((state.orderType === 'recharge')) {
  131. sheep.$router.redirect('/pages/pay/recharge-log');
  132. } else {
  133. sheep.$router.redirect('/pages/order/list');
  134. }
  135. }
  136. // #ifdef MP
  137. function subscribeMessage() {
  138. let event = ['order_dispatched'];
  139. if (['groupon', 'groupon_ladder'].includes(state.orderInfo.activity_type)) {
  140. event.push('groupon_finish');
  141. event.push('groupon_fail');
  142. }
  143. sheep.$platform.useProvider('wechat').subscribeMessage(event);
  144. }
  145. // #endif
  146. onLoad(async (options) => {
  147. let id = '';
  148. // 支付订单号
  149. if (options.orderSN) {
  150. id = options.orderSN;
  151. }
  152. if (options.id) {
  153. id = options.id;
  154. }
  155. state.orderId = id;
  156. if (options.orderType === 'recharge') {
  157. state.orderType = 'recharge';
  158. }
  159. // 支付结果传值过来是失败,则直接显示失败界面
  160. if (options.payState === 'fail') {
  161. state.result = 'failed';
  162. } else {
  163. // 轮询三次检测订单支付结果
  164. getOrderInfo(state.orderId);
  165. }
  166. });
  167. onShow(() => {
  168. if(isEmpty(state.orderInfo)) return;
  169. getOrderInfo(state.orderId);
  170. })
  171. onHide(() => {
  172. state.result = 'unpaid';
  173. state.counter = 0;
  174. });
  175. </script>
  176. <style lang="scss" scoped>
  177. @keyframes rotation {
  178. 0% {
  179. transform: rotate(0deg);
  180. }
  181. 100% {
  182. transform: rotate(360deg);
  183. }
  184. }
  185. .score-img {
  186. width: 36rpx;
  187. height: 36rpx;
  188. margin: 0 4rpx;
  189. }
  190. .pay-result-box {
  191. padding: 60rpx 0;
  192. .pay-waiting {
  193. margin-top: 20rpx;
  194. width: 60rpx;
  195. height: 60rpx;
  196. border: 10rpx solid rgb(233, 231, 231);
  197. border-bottom-color: rgb(204, 204, 204);
  198. border-radius: 50%;
  199. display: inline-block;
  200. // -webkit-animation: rotation 1s linear infinite;
  201. animation: rotation 1s linear infinite;
  202. }
  203. .pay-img {
  204. width: 130rpx;
  205. height: 130rpx;
  206. }
  207. .tip-text {
  208. font-size: 30rpx;
  209. font-weight: bold;
  210. color: #333333;
  211. }
  212. .pay-total-num {
  213. font-size: 36rpx;
  214. font-weight: 500;
  215. color: #333333;
  216. font-family: OPPOSANS;
  217. }
  218. .btn-box {
  219. width: 100%;
  220. .back-btn {
  221. width: 190rpx;
  222. height: 70rpx;
  223. font-size: 28rpx;
  224. border: 2rpx solid #dfdfdf;
  225. border-radius: 35rpx;
  226. font-weight: 400;
  227. color: #595959;
  228. }
  229. .check-btn {
  230. width: 190rpx;
  231. height: 70rpx;
  232. font-size: 28rpx;
  233. border: 2rpx solid #dfdfdf;
  234. border-radius: 35rpx;
  235. font-weight: 400;
  236. color: #595959;
  237. margin-left: 32rpx;
  238. }
  239. }
  240. .subscribe-box {
  241. .subscribe-img {
  242. width: 44rpx;
  243. height: 44rpx;
  244. }
  245. .subscribe-title {
  246. font-weight: 500;
  247. font-size: 32rpx;
  248. line-height: 36rpx;
  249. color: #434343;
  250. }
  251. .subscribe-start {
  252. color: var(--ui-BG-Main);
  253. font-weight: 700;
  254. font-size: 32rpx;
  255. line-height: 36rpx;
  256. }
  257. }
  258. }
  259. </style>