feat(支付): 新增支付成功后跳转逻辑与状态提示
提取支付成功跳转逻辑为独立工具函数,整合至支付确认页面;添加跳转防重复机制与对应状态文案,处理未找到我的菜单、链接为空及请求失败等异常场景。
Showing
2 changed files
with
81 additions
and
1 deletions
| ... | @@ -34,10 +34,12 @@ | ... | @@ -34,10 +34,12 @@ |
| 34 | import { computed, ref, watch } from 'vue' | 34 | import { computed, ref, watch } from 'vue' |
| 35 | import { useLoad } from '@tarojs/taro' | 35 | import { useLoad } from '@tarojs/taro' |
| 36 | import { useWechatMiniPay } from '@/composables/useWechatMiniPay' | 36 | import { useWechatMiniPay } from '@/composables/useWechatMiniPay' |
| 37 | +import { redirectAfterPaySuccess } from '@/utils/paySuccessRedirect' | ||
| 37 | 38 | ||
| 38 | const order_id = ref('') | 39 | const order_id = ref('') |
| 39 | const amount = ref('') | 40 | const amount = ref('') |
| 40 | const status_text = ref('') | 41 | const status_text = ref('') |
| 42 | +const navigating_after_pay_success = ref(false) | ||
| 41 | 43 | ||
| 42 | const { | 44 | const { |
| 43 | pay_loading, | 45 | pay_loading, |
| ... | @@ -58,17 +60,50 @@ watch(last_result_text, (value) => { | ... | @@ -58,17 +60,50 @@ watch(last_result_text, (value) => { |
| 58 | } | 60 | } |
| 59 | }) | 61 | }) |
| 60 | 62 | ||
| 63 | +const goUserWebviewAfterPaySuccess = async () => { | ||
| 64 | + if (navigating_after_pay_success.value) return | ||
| 65 | + | ||
| 66 | + navigating_after_pay_success.value = true | ||
| 67 | + | ||
| 68 | + try { | ||
| 69 | + status_text.value = '支付成功,正在跳转...' | ||
| 70 | + | ||
| 71 | + const redirect_result = await redirectAfterPaySuccess() | ||
| 72 | + if (redirect_result?.reason === 'missing-user-menu') { | ||
| 73 | + status_text.value = '支付成功,未找到“我的”菜单,正在返回首页...' | ||
| 74 | + return | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + if (redirect_result?.reason === 'empty-user-link') { | ||
| 78 | + status_text.value = '支付成功,但“我的”菜单地址为空,正在返回首页...' | ||
| 79 | + return | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + if (redirect_result?.target === 'home') { | ||
| 83 | + status_text.value = '支付成功,但菜单获取失败,正在返回首页...' | ||
| 84 | + } | ||
| 85 | + } catch (error) { | ||
| 86 | + status_text.value = '支付成功,但菜单获取失败,正在返回首页...' | ||
| 87 | + } finally { | ||
| 88 | + navigating_after_pay_success.value = false | ||
| 89 | + } | ||
| 90 | +} | ||
| 91 | + | ||
| 61 | const handlePay = async () => { | 92 | const handlePay = async () => { |
| 62 | if (!order_id.value || pay_loading.value) return | 93 | if (!order_id.value || pay_loading.value) return |
| 63 | 94 | ||
| 64 | status_text.value = '' | 95 | status_text.value = '' |
| 65 | 96 | ||
| 66 | - await pay_by_order_id(order_id.value, { | 97 | + const pay_res = await pay_by_order_id(order_id.value, { |
| 67 | auto_auth: true, | 98 | auto_auth: true, |
| 68 | on_status: (text) => { | 99 | on_status: (text) => { |
| 69 | status_text.value = text | 100 | status_text.value = text |
| 70 | }, | 101 | }, |
| 71 | }) | 102 | }) |
| 103 | + | ||
| 104 | + if (pay_res?.status === 'success') { | ||
| 105 | + await goUserWebviewAfterPaySuccess() | ||
| 106 | + } | ||
| 72 | } | 107 | } |
| 73 | 108 | ||
| 74 | useLoad((options) => { | 109 | useLoad((options) => { | ... | ... |
src/utils/paySuccessRedirect.js
0 → 100644
| 1 | +import Taro from '@tarojs/taro' | ||
| 2 | +import { getTabbarConfigAPI } from '@/api/tabbar' | ||
| 3 | +import { buildWebviewPreviewUrl } from '@/utils/webview' | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * @description 支付成功后跳转到“我的”菜单对应的 WebView;没有 user 菜单则回首页 | ||
| 7 | + * @returns {Promise<{ target: 'user-webview' | 'home', reason: 'success' | 'missing-user-menu' | 'empty-user-link' | 'request-failed' }>} | ||
| 8 | + */ | ||
| 9 | +export const redirectAfterPaySuccess = async () => { | ||
| 10 | + try { | ||
| 11 | + const response = await getTabbarConfigAPI() | ||
| 12 | + const user_menu = response?.data?.user | ||
| 13 | + | ||
| 14 | + if (!user_menu) { | ||
| 15 | + await Taro.reLaunch({ | ||
| 16 | + url: '/pages/index/index', | ||
| 17 | + }) | ||
| 18 | + return { target: 'home', reason: 'missing-user-menu' } | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + const user_link = String(user_menu?.link || '').trim() | ||
| 22 | + if (!user_link) { | ||
| 23 | + await Taro.reLaunch({ | ||
| 24 | + url: '/pages/index/index', | ||
| 25 | + }) | ||
| 26 | + return { target: 'home', reason: 'empty-user-link' } | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + const user_title = String(user_menu?.title || '我的').trim() || '我的' | ||
| 30 | + | ||
| 31 | + await Taro.redirectTo({ | ||
| 32 | + url: buildWebviewPreviewUrl(user_link, user_title), | ||
| 33 | + }) | ||
| 34 | + | ||
| 35 | + return { target: 'user-webview', reason: 'success' } | ||
| 36 | + } catch (error) { | ||
| 37 | + console.error('支付成功后跳转目标获取失败:', error) | ||
| 38 | + | ||
| 39 | + await Taro.reLaunch({ | ||
| 40 | + url: '/pages/index/index', | ||
| 41 | + }) | ||
| 42 | + | ||
| 43 | + return { target: 'home', reason: 'request-failed' } | ||
| 44 | + } | ||
| 45 | +} |
-
Please register or login to post a comment