result.vue 8.7 KB

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