result.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/img/shop/order/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/img/shop/order/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. <!-- 操作区 -->
  25. <view class="btn-box ss-flex ss-row-center ss-m-t-50">
  26. <button class="back-btn ss-reset-button" @tap="sheep.$router.go('/pages/index/index')">
  27. 返回首页
  28. </button>
  29. <button
  30. class="check-btn ss-reset-button"
  31. v-if="payResult === 'failed'"
  32. @tap="
  33. sheep.$router.redirect('/pages/pay/index', { id: state.id, orderType: state.orderType })
  34. "
  35. >
  36. 重新支付
  37. </button>
  38. <button class="check-btn ss-reset-button" v-if="payResult === 'success'" @tap="onOrder">
  39. 查看订单
  40. </button>
  41. <button
  42. class="check-btn ss-reset-button"
  43. v-if="payResult === 'success' && state.tradeOrder.type === 3"
  44. @tap="sheep.$router.redirect('/pages/activity/groupon/order')"
  45. >
  46. 我的拼团
  47. </button>
  48. </view>
  49. <!-- #ifdef MP -->
  50. <view
  51. class="subscribe-box ss-flex ss-m-t-44"
  52. v-if="showSubscribeBtn && state.orderType === 'goods'"
  53. >
  54. <image class="subscribe-img" :src="sheep.$url.static('/static/img/shop/order/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, ref } from 'vue';
  65. import { isEmpty } from 'lodash-es';
  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. import { WxaSubscribeTemplate } from '@/sheep/util/const';
  71. const state = reactive({
  72. id: 0, // 支付单号
  73. orderType: 'goods', // 订单类型
  74. result: 'unpaid', // 支付状态
  75. orderInfo: {}, // 支付订单信息
  76. tradeOrder: {}, // 商品订单信息,只有在 orderType 为 goods 才会请求。目的:【我的拼团】按钮的展示
  77. counter: 0, // 获取结果次数
  78. });
  79. // 支付结果 result => payResult
  80. const payResult = computed(() => {
  81. if (state.result === 'unpaid') {
  82. return 'waiting';
  83. }
  84. if (state.result === 'paid') {
  85. return 'success';
  86. }
  87. if (state.result === 'failed') {
  88. return 'failed';
  89. }
  90. if (state.result === 'closed') {
  91. return 'closed';
  92. }
  93. });
  94. // 获得订单信息
  95. async function getOrderInfo(id) {
  96. state.counter++;
  97. // 1. 加载订单信息
  98. const { data, code } = await PayOrderApi.getOrder(id);
  99. if (code === 0) {
  100. state.orderInfo = data;
  101. if (!state.orderInfo || state.orderInfo.status === 30) {
  102. // 支付关闭
  103. state.result = 'closed';
  104. return;
  105. }
  106. if (state.orderInfo.status !== 0) {
  107. // 非待支付,可能是已支付,可能是已退款
  108. state.result = 'paid';
  109. // #ifdef MP
  110. uni.showModal({
  111. title: '支付结果',
  112. showCancel: false, // 不要取消按钮
  113. content: '支付成功',
  114. success: () => {
  115. // 订阅只能由用户主动触发,只能包一层 showModal 诱导用户点击
  116. autoSubscribeMessage();
  117. },
  118. });
  119. // #endif
  120. // 特殊:获得商品订单信息
  121. if (state.orderType === 'goods') {
  122. const { data, code } = await OrderApi.getOrderDetail(
  123. state.orderInfo.merchantOrderId,
  124. true,
  125. );
  126. if (code === 0) {
  127. state.tradeOrder = data;
  128. }
  129. }
  130. return;
  131. }
  132. }
  133. // 2.1 情况三一:未支付,且轮询次数小于三次,则继续轮询
  134. if (state.counter < 3 && state.result === 'unpaid') {
  135. setTimeout(() => {
  136. getOrderInfo(id);
  137. }, 1500);
  138. }
  139. // 2.2 情况二:超过三次检测才判断为支付失败
  140. if (state.counter >= 3) {
  141. state.result = 'failed';
  142. }
  143. }
  144. function onOrder() {
  145. if (state.orderType === 'recharge') {
  146. sheep.$router.redirect('/pages/pay/recharge-log');
  147. } else {
  148. sheep.$router.redirect('/pages/order/list');
  149. }
  150. }
  151. // #ifdef MP
  152. const showSubscribeBtn = ref(false); // 默认隐藏
  153. const SUBSCRIBE_BTN_STATUS_STORAGE_KEY = 'subscribe_btn_status';
  154. function subscribeMessage() {
  155. if (state.orderType !== 'goods') {
  156. return;
  157. }
  158. const event = [WxaSubscribeTemplate.TRADE_ORDER_DELIVERY];
  159. if (state.tradeOrder.type === 3) {
  160. event.push(WxaSubscribeTemplate.PROMOTION_COMBINATION_SUCCESS);
  161. }
  162. sheep.$platform.useProvider('wechat').subscribeMessage(event, () => {
  163. // 订阅后记录一下订阅状态
  164. uni.removeStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY);
  165. uni.setStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY, '已订阅');
  166. // 隐藏订阅按钮
  167. showSubscribeBtn.value = false;
  168. });
  169. }
  170. async function autoSubscribeMessage() {
  171. // 1. 校验是否手动订阅过
  172. const subscribeBtnStatus = uni.getStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY);
  173. if (!subscribeBtnStatus) {
  174. showSubscribeBtn.value = true;
  175. return;
  176. }
  177. // 2. 订阅消息
  178. subscribeMessage();
  179. }
  180. // #endif
  181. onLoad(async (options) => {
  182. // 支付订单号
  183. if (options.id) {
  184. state.id = options.id;
  185. }
  186. // 订单类型
  187. if (options.orderType) {
  188. state.orderType = options.orderType;
  189. }
  190. // 支付结果传值过来是失败,则直接显示失败界面
  191. if (options.payState === 'fail') {
  192. state.result = 'failed';
  193. } else {
  194. // 轮询三次检测订单支付结果
  195. await getOrderInfo(state.id);
  196. }
  197. });
  198. onShow(() => {
  199. if (isEmpty(state.orderInfo)) {
  200. return;
  201. }
  202. getOrderInfo(state.id);
  203. });
  204. onHide(() => {
  205. state.result = 'unpaid';
  206. state.counter = 0;
  207. });
  208. </script>
  209. <style lang="scss" scoped>
  210. @keyframes rotation {
  211. 0% {
  212. transform: rotate(0deg);
  213. }
  214. 100% {
  215. transform: rotate(360deg);
  216. }
  217. }
  218. .score-img {
  219. width: 36rpx;
  220. height: 36rpx;
  221. margin: 0 4rpx;
  222. }
  223. .pay-result-box {
  224. padding: 60rpx 0;
  225. .pay-waiting {
  226. margin-top: 20rpx;
  227. width: 60rpx;
  228. height: 60rpx;
  229. border: 10rpx solid rgb(233, 231, 231);
  230. border-bottom-color: rgb(204, 204, 204);
  231. border-radius: 50%;
  232. display: inline-block;
  233. // -webkit-animation: rotation 1s linear infinite;
  234. animation: rotation 1s linear infinite;
  235. }
  236. .pay-img {
  237. width: 130rpx;
  238. height: 130rpx;
  239. }
  240. .tip-text {
  241. font-size: 30rpx;
  242. font-weight: bold;
  243. color: #333333;
  244. }
  245. .pay-total-num {
  246. font-size: 36rpx;
  247. font-weight: 500;
  248. color: #333333;
  249. font-family: OPPOSANS;
  250. }
  251. .btn-box {
  252. width: 100%;
  253. .back-btn {
  254. width: 190rpx;
  255. height: 70rpx;
  256. font-size: 28rpx;
  257. border: 2rpx solid #dfdfdf;
  258. border-radius: 35rpx;
  259. font-weight: 400;
  260. color: #595959;
  261. }
  262. .check-btn {
  263. width: 190rpx;
  264. height: 70rpx;
  265. font-size: 28rpx;
  266. border: 2rpx solid #dfdfdf;
  267. border-radius: 35rpx;
  268. font-weight: 400;
  269. color: #595959;
  270. margin-left: 32rpx;
  271. }
  272. }
  273. .subscribe-box {
  274. .subscribe-img {
  275. width: 44rpx;
  276. height: 44rpx;
  277. }
  278. .subscribe-title {
  279. font-weight: 500;
  280. font-size: 32rpx;
  281. line-height: 36rpx;
  282. color: #434343;
  283. }
  284. .subscribe-start {
  285. color: var(--ui-BG-Main);
  286. font-weight: 700;
  287. font-size: 32rpx;
  288. line-height: 36rpx;
  289. }
  290. }
  291. }
  292. </style>