result.vue 8.7 KB

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