zhengyiming
1 天以前 1327b99efda99ada27a956981648950f580314b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Taro from '@tarojs/taro';
import { object2query } from '@life-payment/utils';
import { goBack } from '@/utils';
import { useSwitchTab } from './router';
import { TabBarPageRouterForCheck } 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(TabBarPageRouterForCheck).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 };
}