import Taro from '@tarojs/taro';
|
import { object2query } from '@life-payment/utils';
|
import { goBack } from '@/utils';
|
import { useSwitchTab } from './router';
|
import { TabBarPageRouter } from '@/constants';
|
import { useQueryClient } from '@tanstack/vue-query';
|
|
function getOtherQuery(query: Partial<Record<string, string>>) {
|
if (query) {
|
return Object.keys(query).reduce((acc, cur) => {
|
if (cur !== 'redirect' && cur !== '$taroTimestamp') {
|
acc[cur] = query[cur];
|
}
|
return acc;
|
}, {});
|
}
|
}
|
|
export function useLoginedJump() {
|
const router = Taro.useRouter();
|
const { redirect } = router.params;
|
|
const otherQuery = getOtherQuery(router.params);
|
|
const otherQueryString = computed(() => {
|
const str = object2query(otherQuery);
|
return str.length > 0 ? `${str}` : '';
|
});
|
|
const redirectPath = computed(() => {
|
return redirect
|
? otherQueryString.value
|
? `${redirect}?${otherQueryString.value}`
|
: redirect
|
: '';
|
});
|
|
const redirectParams = computed(() => {
|
return redirect ? `${redirect}&${otherQueryString.value}` : '';
|
});
|
|
const switchTab = useSwitchTab();
|
|
const queryClient = useQueryClient();
|
|
const pages = Taro.getCurrentPages();
|
|
function goBackFromAuthorization() {
|
const authorizationIndex = pages.findIndex((x) =>
|
`/${x.route}`.includes(RouterPath.authorization)
|
);
|
let delta = authorizationIndex >= 0 ? pages.length - authorizationIndex : 1;
|
goBack(delta);
|
}
|
|
function jump() {
|
if (redirect) {
|
const isTabBarPage = Object.values(TabBarPageRouter).includes(redirect as any);
|
if (isTabBarPage) {
|
switchTab({
|
url: redirectPath.value,
|
});
|
} else {
|
// Taro.redirectTo({
|
// url: redirectPath.value,
|
// });
|
goBackFromAuthorization();
|
}
|
} else {
|
goBackFromAuthorization();
|
}
|
queryClient.refetchQueries();
|
}
|
|
return { jump, redirectPath, redirectParams };
|
}
|