result.vue 9.1 KB

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