result.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. <view v-if="showLotteryResult">
  70. <view class="line">
  71. <image class="ss-m-t-30" :src="sheep.$url.static('/static/images/line.png', 'local')" />
  72. </view>
  73. <uni-card class="lotteryResult ss-m-t-30">
  74. <view>
  75. <p v-for="(k, i) in prizeData" :key="i" class="color-primary ss-m-t-20">{{ k.prize.prompt }}</p>
  76. </view>
  77. <view class="tip ss-m-t-10">凭此房券在规定有效期内可享受免费住宿一晚。</view>
  78. <view class="tip ss-m-t-2">请提供收货地址,以便安排房券派送。</view>
  79. <!-- 收货地址 -->
  80. <view class="addressBox">
  81. <AddressSelection v-model="addressState"></AddressSelection>
  82. </view>
  83. <!-- button -->
  84. <view class="footer-box ss-flex-col ss-row-between">
  85. <view class="ss-m-b-20 ss-flex ss-row-center">
  86. <!-- <button class="ss-reset-button tool-btn" @tap.stop="null">放弃</button> -->
  87. <button class="ss-reset-button save-btn" @tap.stop="handleReceive">立即领取</button>
  88. </view>
  89. </view>
  90. <view class="color-999 ss-m-15 text-center">(10天内未领取的,则视为主动放弃当前奖品)</view>
  91. </uni-card>
  92. </view>
  93. </view>
  94. </s-layout>
  95. </template>
  96. <script setup>
  97. import { onLoad, onHide, onShow } from '@dcloudio/uni-app';
  98. import { reactive, computed, ref } from 'vue';
  99. import { isEmpty } from 'lodash-es';
  100. import sheep from '@/sheep';
  101. import PayOrderApi from '@/sheep/api/pay/order';
  102. import { fen2yuan } from '@/sheep/hooks/useGoods';
  103. import OrderApi from '@/sheep/api/trade/order';
  104. import { WxaSubscribeTemplate } from '@/sheep/util/const';
  105. import SlotMachine from '@/pages/lucky/SlotMachine'
  106. import PrizeApi from '@/sheep/api/prizeDraw'
  107. import AddressApi from '@/sheep/api/member/address'
  108. import AddressSelection from './addressSelection.vue';
  109. // import prizePage from './prizePage'
  110. const state = reactive({
  111. id: 0, // 支付单号
  112. orderType: 'goods', // 订单类型
  113. result: 'unpaid', // 支付状态
  114. orderInfo: {}, // 支付订单信息
  115. tradeOrder: {}, // 商品订单信息,只有在 orderType 为 goods 才会请求。目的:【我的拼团】按钮的展示
  116. counter: 0, // 获取结果次数
  117. });
  118. // 支付结果 result => payResult
  119. const payResult = computed(() => {
  120. if (state.result === 'unpaid') {
  121. return 'waiting';
  122. }
  123. if (state.result === 'paid') {
  124. return 'success';
  125. }
  126. if (state.result === 'failed') {
  127. return 'failed';
  128. }
  129. if (state.result === 'closed') {
  130. return 'closed';
  131. }
  132. });
  133. // 获得订单信息
  134. async function getOrderInfo(id) {
  135. state.counter++;
  136. // 1. 加载订单信息
  137. const { data, code } = await PayOrderApi.getOrder(id);
  138. if (code === 0) {
  139. state.orderInfo = data;
  140. if (!state.orderInfo || state.orderInfo.status === 30) {
  141. // 支付关闭
  142. state.result = 'closed';
  143. return;
  144. }
  145. if (state.orderInfo.status !== 0) {
  146. getRecord(state.orderInfo.merchantOrderId)
  147. // 非待支付,可能是已支付,可能是已退款
  148. state.result = 'paid';
  149. // #ifdef MP
  150. // uni.showModal({
  151. // title: '支付结果',
  152. // showCancel: false, // 不要取消按钮
  153. // content: '支付成功',
  154. // success: () => {
  155. // // 订阅只能由用户主动触发,只能包一层 showModal 诱导用户点击
  156. // autoSubscribeMessage();
  157. // },
  158. // });
  159. // #endif
  160. // 特殊:获得商品订单信息
  161. if (state.orderType === 'goods') {
  162. const { data, code } = await OrderApi.getOrderDetail(
  163. state.orderInfo.merchantOrderId,
  164. true,
  165. );
  166. if (code === 0) {
  167. state.tradeOrder = data;
  168. }
  169. }
  170. return;
  171. }
  172. }
  173. // 2.1 情况三一:未支付,且轮询次数小于三次,则继续轮询
  174. if (state.counter < 3 && state.result === 'unpaid') {
  175. setTimeout(() => {
  176. getOrderInfo(id);
  177. }, 1500);
  178. }
  179. // 2.2 情况二:超过三次检测才判断为支付失败
  180. if (state.counter >= 3) {
  181. state.result = 'failed';
  182. }
  183. }
  184. function onOrder() {
  185. if (state.orderType === 'recharge') {
  186. sheep.$router.redirect('/pages/pay/recharge-log');
  187. } else {
  188. sheep.$router.redirect('/pages/order/list');
  189. }
  190. }
  191. // #ifdef MP
  192. const showSubscribeBtn = ref(false); // 默认隐藏
  193. const SUBSCRIBE_BTN_STATUS_STORAGE_KEY = 'subscribe_btn_status';
  194. function subscribeMessage() {
  195. if (state.orderType !== 'goods') {
  196. return;
  197. }
  198. const event = [WxaSubscribeTemplate.TRADE_ORDER_DELIVERY];
  199. if (state.tradeOrder.type === 3) {
  200. event.push(WxaSubscribeTemplate.PROMOTION_COMBINATION_SUCCESS);
  201. }
  202. sheep.$platform.useProvider('wechat').subscribeMessage(event, () => {
  203. // 订阅后记录一下订阅状态
  204. uni.removeStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY);
  205. uni.setStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY, '已订阅');
  206. // 隐藏订阅按钮
  207. showSubscribeBtn.value = false;
  208. });
  209. }
  210. async function autoSubscribeMessage() {
  211. // 1. 校验是否手动订阅过
  212. const subscribeBtnStatus = uni.getStorageSync(SUBSCRIBE_BTN_STATUS_STORAGE_KEY);
  213. if (!subscribeBtnStatus) {
  214. showSubscribeBtn.value = true;
  215. return;
  216. }
  217. // 2. 订阅消息
  218. subscribeMessage();
  219. }
  220. // #endif
  221. const showLotteryResult = ref(false)
  222. const lotteryEnd = () => {
  223. const bool = Boolean(prizeData.value?.length)
  224. uni.showModal({
  225. title: bool ? '恭喜中奖' : '感谢参与',
  226. showCancel: false, // 不要取消按钮
  227. content: bool ? '恭喜您抽到一张房券,请尽快领取!' : '本次未中奖,欢迎继续关注后续活动。'
  228. })
  229. showPrizeDraw.value = false
  230. if (!bool) return
  231. showLotteryResult.value = true
  232. }
  233. const lotteryId = ref('')
  234. const showPrizeDraw = ref(false)
  235. const getLottery = async () => {
  236. if (!salesLotterySpuId) return
  237. const res = await PrizeApi.getPrizeByGoodsId(salesLotterySpuId)
  238. lotteryId.value = res?.data?.id
  239. showPrizeDraw.value = Boolean(payResult.value === 'success' && lotteryId.value)
  240. }
  241. let salesLotterySpuId = '' // 正常填写物流商品不参与抽奖
  242. onLoad(async (options) => {
  243. // 支付订单号
  244. if (options.id) {
  245. state.id = options.id;
  246. }
  247. // 订单类型
  248. if (options.orderType) {
  249. state.orderType = options.orderType;
  250. }
  251. // 虚拟商品参与抽奖
  252. salesLotterySpuId = options?.salesLotterySpuId || false
  253. getLottery()
  254. // 支付结果传值过来是失败,则直接显示失败界面
  255. if (options.payState === 'fail') {
  256. state.result = 'failed';
  257. } else {
  258. // 轮询三次检测订单支付结果
  259. await getOrderInfo(state.id);
  260. }
  261. });
  262. // 获取中奖记录
  263. const prizeData = ref({})
  264. const getRecord = async (orderId) => {
  265. if (!orderId) return
  266. const res = await PrizeApi.getLuckLotteryRecordByOrderId(orderId)
  267. prizeData.value = res?.data || []
  268. }
  269. const addressState = ref({
  270. addressInfo: {}, // 选择的收货地址
  271. deliveryType: 1, // 收货方式:1-快递配送,2-门店自提
  272. isPickUp: false, // 门店自提是否开启
  273. pickUpInfo: {}, // 选择的自提门店信息
  274. receiverName: '', // 门店自提收件人名称
  275. receiverMobile: '', // 门店自提收件人手机
  276. });
  277. // 获取收货地址
  278. const getAddressList = async () => {
  279. const list = (await AddressApi.getAddressList()).data;
  280. if (list?.length) {
  281. const item = list.find(e => e.defaultStatus)
  282. if (item) addressState.value.addressInfo = item
  283. else addressState.value.addressInfo = list[0]
  284. }
  285. }
  286. getAddressList()
  287. // 领取
  288. const handleReceive = async () => {
  289. if (!addressState.value?.addressInfo || !Object.keys(addressState.value.addressInfo).length) {
  290. uni.showToast({
  291. title: '请选择收货地址',
  292. icon: 'none',
  293. duration: 2000
  294. })
  295. return
  296. }
  297. const { code } = await PrizeApi.luckyLotteryRecordReceive({ id: prizeData.value[0].record?.id, receiveInfo: JSON.stringify(addressState.value.addressInfo) })
  298. if (code === 0) {
  299. setTimeout(() => {
  300. uni.showToast({
  301. title: '领取成功',
  302. icon: 'none',
  303. duration: 2000
  304. })
  305. }, 500)
  306. // sheep.$router.back();
  307. }
  308. sheep.$router.redirect('/pages/user/prize/index');
  309. }
  310. onShow(() => {
  311. if (isEmpty(state.orderInfo)) {
  312. return;
  313. }
  314. getOrderInfo(state.id);
  315. });
  316. onHide(() => {
  317. state.result = 'unpaid';
  318. state.counter = 0;
  319. });
  320. </script>
  321. <style lang="scss" scoped>
  322. @keyframes rotation {
  323. 0% {
  324. transform: rotate(0deg);
  325. }
  326. 100% {
  327. transform: rotate(360deg);
  328. }
  329. }
  330. .score-img {
  331. width: 36rpx;
  332. height: 36rpx;
  333. margin: 0 4rpx;
  334. }
  335. .pay-result-box {
  336. padding: 60rpx 0;
  337. background-color: #fff;
  338. .pay-waiting {
  339. margin-top: 20rpx;
  340. width: 60rpx;
  341. height: 60rpx;
  342. border: 10rpx solid rgb(233, 231, 231);
  343. border-bottom-color: rgb(204, 204, 204);
  344. border-radius: 50%;
  345. display: inline-block;
  346. // -webkit-animation: rotation 1s linear infinite;
  347. animation: rotation 1s linear infinite;
  348. }
  349. .pay-img {
  350. width: 130rpx;
  351. height: 130rpx;
  352. }
  353. .tip-text {
  354. font-size: 30rpx;
  355. font-weight: bold;
  356. color: #333333;
  357. }
  358. .pay-total-num {
  359. font-size: 36rpx;
  360. font-weight: 500;
  361. color: #333333;
  362. font-family: OPPOSANS;
  363. }
  364. .btn-box {
  365. width: 100%;
  366. .back-btn {
  367. width: 190rpx;
  368. height: 70rpx;
  369. font-size: 28rpx;
  370. border: 2rpx solid #dfdfdf;
  371. border-radius: 35rpx;
  372. font-weight: 400;
  373. color: #595959;
  374. }
  375. .check-btn {
  376. width: 190rpx;
  377. height: 70rpx;
  378. font-size: 28rpx;
  379. border: 2rpx solid #dfdfdf;
  380. border-radius: 35rpx;
  381. font-weight: 400;
  382. color: #595959;
  383. margin-left: 32rpx;
  384. }
  385. }
  386. .subscribe-box {
  387. .subscribe-img {
  388. width: 44rpx;
  389. height: 44rpx;
  390. }
  391. .subscribe-title {
  392. font-weight: 500;
  393. font-size: 32rpx;
  394. line-height: 36rpx;
  395. color: #434343;
  396. }
  397. .subscribe-start {
  398. color: var(--ui-BG-Main);
  399. font-weight: 700;
  400. font-size: 32rpx;
  401. line-height: 36rpx;
  402. }
  403. }
  404. }
  405. .prizeDraw {
  406. margin-top: 50rpx;
  407. border-top: 1px solid #b6b5c5;
  408. padding: 50rpx 0;
  409. }
  410. .lotteryResult {
  411. width: 100vw;
  412. .color-primary {
  413. color: #00B760;
  414. font-size: 40rpx;
  415. }
  416. .color-999 {
  417. color: #999;
  418. font-size: 26rpx;
  419. }
  420. .tip {
  421. font-size: 28rpx;
  422. font-weight: bold;
  423. }
  424. .addressBox {
  425. width: 100%;
  426. margin: 20px 0;
  427. }
  428. .refresh-btn {
  429. width: 200rpx;
  430. height: 80rpx;
  431. border-radius: 40rpx;
  432. }
  433. .footer-box {
  434. margin-top: 30rpx;
  435. .tool-btn {
  436. width: calc(48vw - 40rpx);
  437. height: 80rpx;
  438. border-radius: 40rpx;
  439. background: #f6f6f6;
  440. color: #666;
  441. font-size: 26rpx;
  442. margin: 0 20rpx;
  443. }
  444. .save-btn {
  445. width: calc(48vw - 40rpx);
  446. height: 80rpx;
  447. border-radius: 40rpx;
  448. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  449. color: $white;
  450. margin: 0 20rpx;
  451. }
  452. }
  453. }
  454. .line {
  455. width: 100%;
  456. height: 3rpx;
  457. margin: 0 auto;
  458. image {
  459. width: 100%;
  460. height: 100%;
  461. display: block;
  462. }
  463. }
  464. </style>