result.vue 7.7 KB

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