resultYuan.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 class="pay-img ss-m-b-30" v-if="payResult === 'success'"
  8. :src="sheep.$url.static('/static/images/order_pay_success.gif')" />
  9. <image class="pay-img ss-m-b-30" v-if="['failed', 'closed'].includes(payResult)"
  10. :src="sheep.$url.static('/static/images/order_paty_fail.gif')" />
  11. <view class="tip-text ss-m-b-30" v-if="payResult === 'success'">支付成功</view>
  12. <view class="tip-text ss-m-b-30" v-if="payResult === 'failed'">支付失败</view>
  13. <view class="tip-text ss-m-b-30" v-if="payResult === 'closed'">该订单已关闭</view>
  14. <view class="tip-text ss-m-b-30" v-if="payResult === 'waiting'">检测支付结果...</view>
  15. <view class="pay-total-num ss-flex" v-if="payResult === 'success'">
  16. <view>¥{{ fen2yuan(state.orderInfo.price) }}</view>
  17. </view>
  18. <!-- 操作区 -->
  19. <view class="btn-box ss-flex ss-row-center ss-m-t-50">
  20. <button class="back-btn ss-reset-button" @tap="sheep.$router.go('/pages/index/index')">
  21. 返回首页
  22. </button>
  23. <button class="check-btn ss-reset-button" v-if="payResult === 'failed'" @tap="
  24. sheep.$router.redirect('/pages/pay/index', { id: state.id, orderType: state.orderType })
  25. ">
  26. 重新支付
  27. </button>
  28. <button class="check-btn ss-reset-button" v-if="payResult === 'success'" @tap="onOrder">
  29. 查看订单
  30. </button>
  31. <!-- TODO 非繁人:拼团接入 -->
  32. <button class="check-btn ss-reset-button" v-if="payResult === 'success' && state.tradeOrder.type === 3"
  33. @tap="sheep.$router.redirect('/pages/activity/groupon/order')">
  34. 我的拼团
  35. </button>
  36. </view>
  37. <!-- TODO 非繁人:订阅 -->
  38. <!-- #ifdef MP -->
  39. <view class="subscribe-box ss-flex ss-m-t-44">
  40. <image class="subscribe-img" :src="sheep.$url.static('/static/images/cargo.png')" />
  41. <view class="subscribe-title ss-m-r-48 ss-m-l-16">获取实时发货信息与订单状态</view>
  42. <view class="subscribe-start" @tap="subscribeMessage">立即订阅</view>
  43. </view>
  44. <!-- #endif -->
  45. </view>
  46. </s-layout>
  47. </template>
  48. <script setup>
  49. import {
  50. onLoad,
  51. onHide,
  52. onShow
  53. } from '@dcloudio/uni-app';
  54. import {
  55. reactive,
  56. computed,
  57. watchEffect
  58. } from 'vue';
  59. import {
  60. isEmpty
  61. } from 'lodash';
  62. import sheep from '@/sheep';
  63. import PayOrderApi from '@/sheep/api/pay/order';
  64. import {
  65. fen2yuan
  66. } from '../../sheep/hooks/useGoods';
  67. import OrderApi from '@/sheep/api/trade/order';
  68. import {
  69. showWalletModal,
  70. colseWalletModal
  71. } from '@/sheep/hooks/useModal';
  72. const state = reactive({
  73. id: 0, // 支付单号
  74. orderType: 'goods', // 订单类型
  75. result: 'unpaid', // 支付状态
  76. orderInfo: {}, // 支付订单信息
  77. tradeOrder: {}, // 商品订单信息,只有在 orderType 为 goods 才会请求。目的:【我的拼团】按钮的展示
  78. counter: 0, // 获取结果次数
  79. });
  80. // 支付结果 result => payResult
  81. const payResult = computed(() => {
  82. if (state.result === 'unpaid') {
  83. return 'waiting';
  84. }
  85. if (state.result === 'paid') {
  86. return 'success';
  87. }
  88. if (state.result === 'failed') {
  89. return 'failed';
  90. }
  91. if (state.result === 'closed') {
  92. return 'closed';
  93. }
  94. });
  95. watchEffect(()=>{
  96. if(payResult.value == 'success'){
  97. showWalletModal({points: state.orderInfo.jf,socialStatus: state.orderInfo.sj})
  98. }
  99. })
  100. // 获得订单信息
  101. async function getOrderInfo(id) {
  102. state.counter++;
  103. // 1. 加载订单信息
  104. // 测试
  105. // await PayOrderApi.getByStatus3(id);
  106. const {
  107. data,
  108. code
  109. } = await PayOrderApi.getByStatus2(id);
  110. if (code === 0) {
  111. state.orderInfo = data;
  112. if (!state.orderInfo || state.orderInfo.status === 30) {
  113. // 支付关闭
  114. state.result = 'closed';
  115. return;
  116. }
  117. if (state.orderInfo.status !== 0) {
  118. // 非待支付,可能是已支付,可能是已退款
  119. state.result = 'paid';
  120. // #ifdef MP
  121. subscribeMessage();
  122. // #endif
  123. // 特殊:获得商品订单信息
  124. // if (state.orderType === 'goods') {
  125. // const {
  126. // data,
  127. // code
  128. // } = await PayOrderApi.getByStatus2(state.orderInfo.merchantOrderId);
  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. // TODO 非繁人:待测试
  149. if (state.orderType === 'recharge') {
  150. sheep.$router.redirect('/pages/pay/recharge-log');
  151. } else {
  152. sheep.$router.redirect('/pages/order/list',{type:2});
  153. }
  154. }
  155. // TODO 非繁人:待测试
  156. // #ifdef MP
  157. function subscribeMessage() {
  158. let event = ['order_dispatched'];
  159. if (state.tradeOrder.type === 3) {
  160. event.push('groupon_finish');
  161. event.push('groupon_fail');
  162. }
  163. sheep.$platform.useProvider('wechat').subscribeMessage(event);
  164. }
  165. // #endif
  166. onLoad(async (options) => {
  167. // console.log(options.payRes)
  168. // 支付订单号
  169. if (options.id) {
  170. state.id = options.id;
  171. }
  172. // 订单类型
  173. if (options.orderType) {
  174. state.orderType = options.orderType;
  175. }
  176. // 传payRes过来,是0元购,不需要在当前页面请求了
  177. if (options.payRes) {
  178. state.payState = 'success'
  179. state.orderInfo = JSON.parse(options.payRes)
  180. state.result = 'paid';
  181. } else {
  182. // console.log('getOrderInfo')
  183. // 轮询三次检测订单支付结果
  184. await getOrderInfo(state.id);
  185. }
  186. });
  187. onShow(() => {
  188. if (isEmpty(state.orderInfo) || state.payState === 'success') {
  189. return;
  190. }
  191. getOrderInfo(state.id);
  192. });
  193. onHide(() => {
  194. state.result = 'unpaid';
  195. state.counter = 0;
  196. });
  197. </script>
  198. <style lang="scss" scoped>
  199. @keyframes rotation {
  200. 0% {
  201. transform: rotate(0deg);
  202. }
  203. 100% {
  204. transform: rotate(360deg);
  205. }
  206. }
  207. .score-img {
  208. width: 36rpx;
  209. height: 36rpx;
  210. margin: 0 4rpx;
  211. }
  212. .pay-result-box {
  213. padding: 60rpx 0;
  214. .pay-waiting {
  215. margin-top: 20rpx;
  216. width: 60rpx;
  217. height: 60rpx;
  218. border: 10rpx solid rgb(233, 231, 231);
  219. border-bottom-color: rgb(204, 204, 204);
  220. border-radius: 50%;
  221. display: inline-block;
  222. // -webkit-animation: rotation 1s linear infinite;
  223. animation: rotation 1s linear infinite;
  224. }
  225. .pay-img {
  226. width: 130rpx;
  227. height: 130rpx;
  228. }
  229. .tip-text {
  230. font-size: 30rpx;
  231. font-weight: bold;
  232. color: #333333;
  233. }
  234. .pay-total-num {
  235. font-size: 36rpx;
  236. font-weight: 500;
  237. color: #333333;
  238. font-family: OPPOSANS;
  239. }
  240. .btn-box {
  241. width: 100%;
  242. .back-btn {
  243. width: 190rpx;
  244. height: 70rpx;
  245. font-size: 28rpx;
  246. border: 2rpx solid #dfdfdf;
  247. border-radius: 35rpx;
  248. font-weight: 400;
  249. color: #595959;
  250. }
  251. .check-btn {
  252. width: 190rpx;
  253. height: 70rpx;
  254. font-size: 28rpx;
  255. border: 2rpx solid #dfdfdf;
  256. border-radius: 35rpx;
  257. font-weight: 400;
  258. color: #595959;
  259. margin-left: 32rpx;
  260. }
  261. }
  262. .subscribe-box {
  263. .subscribe-img {
  264. width: 44rpx;
  265. height: 44rpx;
  266. }
  267. .subscribe-title {
  268. font-weight: 500;
  269. font-size: 32rpx;
  270. line-height: 36rpx;
  271. color: #434343;
  272. }
  273. .subscribe-start {
  274. color: var(--ui-BG-Main);
  275. font-weight: 700;
  276. font-size: 32rpx;
  277. line-height: 36rpx;
  278. }
  279. }
  280. }
  281. </style>