result.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. <!-- 正常填写物流商品不参与抽奖 -->
  61. <SlotMachine
  62. v-if="showPrizeDraw"
  63. class="prizeDraw"
  64. height="120"
  65. :lotteryId="lotteryId"
  66. @end="lotteryEnd"
  67. ></SlotMachine>
  68. <!-- 抽奖结果 -->
  69. <prizePage v-if="showLotteryResult" :prizeData="prizeData"></prizePage>
  70. </view>
  71. </s-layout>
  72. </template>
  73. <script setup>
  74. import { onLoad, onHide, onShow } from '@dcloudio/uni-app';
  75. import { reactive, computed, ref } from 'vue';
  76. import { isEmpty } from 'lodash-es';
  77. import sheep from '@/sheep';
  78. import PayOrderApi from '@/sheep/api/pay/order';
  79. import { fen2yuan } from '@/sheep/hooks/useGoods';
  80. import OrderApi from '@/sheep/api/trade/order';
  81. import { WxaSubscribeTemplate } from '@/sheep/util/const';
  82. import SlotMachine from '@/pages/lucky/SlotMachine'
  83. import PrizeApi from '@/sheep/api/prizeDraw'
  84. import prizePage from './prizePage'
  85. const state = reactive({
  86. id: 0, // 支付单号
  87. orderType: 'goods', // 订单类型
  88. result: 'unpaid', // 支付状态
  89. orderInfo: {}, // 支付订单信息
  90. tradeOrder: {}, // 商品订单信息,只有在 orderType 为 goods 才会请求。目的:【我的拼团】按钮的展示
  91. counter: 0, // 获取结果次数
  92. });
  93. // 支付结果 result => payResult
  94. const payResult = computed(() => {
  95. if (state.result === 'unpaid') {
  96. return 'waiting';
  97. }
  98. if (state.result === 'paid') {
  99. return 'success';
  100. }
  101. if (state.result === 'failed') {
  102. return 'failed';
  103. }
  104. if (state.result === 'closed') {
  105. return 'closed';
  106. }
  107. });
  108. // 获得订单信息
  109. async function getOrderInfo(id) {
  110. state.counter++;
  111. // 1. 加载订单信息
  112. const { data, code } = await PayOrderApi.getOrder(id);
  113. // console.log('getOrder:', data)
  114. if (code === 0) {
  115. state.orderInfo = data;
  116. if (!state.orderInfo || state.orderInfo.status === 30) {
  117. // 支付关闭
  118. state.result = 'closed';
  119. return;
  120. }
  121. if (state.orderInfo.status !== 0) {
  122. getRecord(state.orderInfo.merchantOrderId)
  123. // 非待支付,可能是已支付,可能是已退款
  124. state.result = 'paid';
  125. // #ifdef MP
  126. uni.showModal({
  127. title: '支付结果',
  128. showCancel: false, // 不要取消按钮
  129. content: '支付成功',
  130. success: () => {
  131. // 订阅只能由用户主动触发,只能包一层 showModal 诱导用户点击
  132. autoSubscribeMessage();
  133. },
  134. });
  135. // #endif
  136. // 特殊:获得商品订单信息
  137. if (state.orderType === 'goods') {
  138. const { data, code } = await OrderApi.getOrderDetail(
  139. state.orderInfo.merchantOrderId,
  140. true,
  141. );
  142. if (code === 0) {
  143. state.tradeOrder = data;
  144. }
  145. }
  146. return;
  147. }
  148. }
  149. // 2.1 情况三一:未支付,且轮询次数小于三次,则继续轮询
  150. if (state.counter < 3 && state.result === 'unpaid') {
  151. setTimeout(() => {
  152. getOrderInfo(id);
  153. }, 1500);
  154. }
  155. // 2.2 情况二:超过三次检测才判断为支付失败
  156. if (state.counter >= 3) {
  157. state.result = 'failed';
  158. }
  159. }
  160. function onOrder() {
  161. if (state.orderType === 'recharge') {
  162. sheep.$router.redirect('/pages/pay/recharge-log');
  163. } else {
  164. sheep.$router.redirect('/pages/order/list');
  165. }
  166. }
  167. // #ifdef MP
  168. const showSubscribeBtn = ref(false); // 默认隐藏
  169. const SUBSCRIBE_BTN_STATUS_STORAGE_KEY = 'subscribe_btn_status';
  170. function subscribeMessage() {
  171. if (state.orderType !== 'goods') {
  172. return;
  173. }
  174. const event = [WxaSubscribeTemplate.TRADE_ORDER_DELIVERY];
  175. if (state.tradeOrder.type === 3) {
  176. event.push(WxaSubscribeTemplate.PROMOTION_COMBINATION_SUCCESS);
  177. }
  178. sheep.$platform.useProvider('wechat').subscribeMessage(event, () => {
  179. // 订阅后记录一下订阅状态
  180. uni.removeStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY);
  181. uni.setStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY, '已订阅');
  182. // 隐藏订阅按钮
  183. showSubscribeBtn.value = false;
  184. });
  185. }
  186. async function autoSubscribeMessage() {
  187. // 1. 校验是否手动订阅过
  188. const subscribeBtnStatus = uni.getStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY);
  189. if (!subscribeBtnStatus) {
  190. showSubscribeBtn.value = true;
  191. return;
  192. }
  193. // 2. 订阅消息
  194. subscribeMessage();
  195. }
  196. // #endif
  197. const showLotteryResult = ref(false)
  198. const lotteryEnd = () => {
  199. const bool = Boolean(prizeData.value?.length)
  200. uni.showModal({
  201. title: bool ? '恭喜中奖' : '感谢参与',
  202. showCancel: false, // 不要取消按钮
  203. content: bool ? '恭喜您抽到一张房券,请尽快领取!' : '本次未中奖,欢迎继续关注后续活动。'
  204. })
  205. showPrizeDraw.value = false
  206. if (!bool) return
  207. showLotteryResult.value = true
  208. }
  209. const lotteryId = ref('')
  210. const showPrizeDraw = ref(false)
  211. const getLottery = async () => {
  212. if (!salesLotterySpuId) return
  213. const res = await PrizeApi.getPrizeByGoodsId(salesLotterySpuId)
  214. lotteryId.value = res?.data?.id
  215. showPrizeDraw.value = Boolean(payResult.value === 'success' && lotteryId.value)
  216. }
  217. let salesLotterySpuId = '' // 正常填写物流商品不参与抽奖
  218. onLoad(async (options) => {
  219. // 支付订单号
  220. if (options.id) {
  221. state.id = options.id;
  222. }
  223. // 订单类型
  224. if (options.orderType) {
  225. state.orderType = options.orderType;
  226. }
  227. // 虚拟商品参与抽奖
  228. salesLotterySpuId = options?.salesLotterySpuId || false
  229. getLottery()
  230. // 支付结果传值过来是失败,则直接显示失败界面
  231. if (options.payState === 'fail') {
  232. state.result = 'failed';
  233. } else {
  234. // 轮询三次检测订单支付结果
  235. await getOrderInfo(state.id);
  236. }
  237. });
  238. // 获取中奖记录
  239. const prizeData = ref({})
  240. const getRecord = async (orderId) => {
  241. if (!orderId) return
  242. const res = await PrizeApi.getLuckLotteryRecordByOrderId(orderId)
  243. // console.log('prizeData:', res)
  244. prizeData.value = res?.data || []
  245. // 测试
  246. // showPrizeDraw.value = false
  247. // showLotteryResult.value = true
  248. }
  249. onShow(() => {
  250. if (isEmpty(state.orderInfo)) {
  251. return;
  252. }
  253. getOrderInfo(state.id);
  254. });
  255. onHide(() => {
  256. state.result = 'unpaid';
  257. state.counter = 0;
  258. });
  259. </script>
  260. <style lang="scss" scoped>
  261. @keyframes rotation {
  262. 0% {
  263. transform: rotate(0deg);
  264. }
  265. 100% {
  266. transform: rotate(360deg);
  267. }
  268. }
  269. .score-img {
  270. width: 36rpx;
  271. height: 36rpx;
  272. margin: 0 4rpx;
  273. }
  274. .pay-result-box {
  275. padding: 60rpx 0;
  276. background-color: #fff;
  277. .pay-waiting {
  278. margin-top: 20rpx;
  279. width: 60rpx;
  280. height: 60rpx;
  281. border: 10rpx solid rgb(233, 231, 231);
  282. border-bottom-color: rgb(204, 204, 204);
  283. border-radius: 50%;
  284. display: inline-block;
  285. // -webkit-animation: rotation 1s linear infinite;
  286. animation: rotation 1s linear infinite;
  287. }
  288. .pay-img {
  289. width: 130rpx;
  290. height: 130rpx;
  291. }
  292. .tip-text {
  293. font-size: 30rpx;
  294. font-weight: bold;
  295. color: #333333;
  296. }
  297. .pay-total-num {
  298. font-size: 36rpx;
  299. font-weight: 500;
  300. color: #333333;
  301. font-family: OPPOSANS;
  302. }
  303. .btn-box {
  304. width: 100%;
  305. .back-btn {
  306. width: 190rpx;
  307. height: 70rpx;
  308. font-size: 28rpx;
  309. border: 2rpx solid #dfdfdf;
  310. border-radius: 35rpx;
  311. font-weight: 400;
  312. color: #595959;
  313. }
  314. .check-btn {
  315. width: 190rpx;
  316. height: 70rpx;
  317. font-size: 28rpx;
  318. border: 2rpx solid #dfdfdf;
  319. border-radius: 35rpx;
  320. font-weight: 400;
  321. color: #595959;
  322. margin-left: 32rpx;
  323. }
  324. }
  325. .subscribe-box {
  326. .subscribe-img {
  327. width: 44rpx;
  328. height: 44rpx;
  329. }
  330. .subscribe-title {
  331. font-weight: 500;
  332. font-size: 32rpx;
  333. line-height: 36rpx;
  334. color: #434343;
  335. }
  336. .subscribe-start {
  337. color: var(--ui-BG-Main);
  338. font-weight: 700;
  339. font-size: 32rpx;
  340. line-height: 36rpx;
  341. }
  342. }
  343. }
  344. .prizeDraw {
  345. margin-top: 50rpx;
  346. border-top: 1px solid #b6b5c5;
  347. padding: 50rpx 0;
  348. }
  349. </style>