当前位置:   article > 正文

taro3 路由工具-万能跳转_taro 跳转外部链接

taro 跳转外部链接
import Taro, {
    navigateTo as _navigateTo,
    redirectTo as _redirectTo,
    switchTab as _switchTab,
    navigateBack as _navigateBack,
    reLaunch as _reLaunch,
    useRouter
} from "@tarojs/taro";
import TokenUtils from "@/utils/token";

function switchTab(url: string, params?: {}, option?: Taro.switchTab.Option) {
    const newUrl = createUrl(url);
    return _switchTab({
        url: newUrl,
        ...option
    });
}

function reLaunch(url: string, params?: {}, option?: Taro.reLaunch.Option) {
    const newUrl = createUrl(url, params);
    return _reLaunch({
        url: newUrl,
        ...option
    });
}

//创建url
function createUrl(url: string, params?: {}): string {
    if (params) {
        const finalUrl = `${url}?` + (Object.entries(params).map(([key, value]) => `${key}=${value ? encodeURIComponent(String(value)) : ''}`).join('&'))
        // console.log('跳转的链接', finalUrl)
        return finalUrl;
    }
    return url;
}

/**
 * @fn navigateTo 需要跳转的应用内非 tabBar 的页面的路径
 * @url 路由url
 * @params 路由参数
 */
function navigateTo(url: string, params?: {}, option?: Taro.navigateTo.Option) {
    const newUrl = createUrl(url, params);
    return _navigateTo({
        url: newUrl,
        ...option
    });
}

/**
 * @fn navigateTo 需要跳转的应用内非 tabBar 的页面的路径
 * @url 路由url
 * @params 路由参数
 */
function navigateBack(delta: number = 1, option?: Taro.navigateBack.Option) {
    return _navigateBack({
        delta,
        ...option
    });
}

function redirectTo(url: string, params?: {}, option?: Taro.redirectTo.Option) {
    const newUrl = createUrl(url, params);
    return _redirectTo({
        url: newUrl,
        ...option
    });
}

const RouterUtils = {
    createUrl,
    navigateTo,
    redirectTo,
    switchTab,
    navigateBack,
    reLaunch,
    useRouter,
    //获取路由参数
    getRouterParams() {
        return Taro.getCurrentInstance().router?.params || {} as any
    },
    getCurrentFullPath: (params?: any) => {
        const router = Taro.getCurrentInstance().router || {} as any;
        return createUrl(router.path, {
            ...router.params,
            ...params
        })
    },
    //去授权页面
    toAuthPage(url?:string,params?:Record<string, any>) {
        if (TokenUtils.getToken()) {
            return this.toPage(url,params)
        }
        return navigateTo('/pages/login/index', {
            redirectUrl: url?createUrl(url, params):Taro.getCurrentInstance().router?.path
        })
    },
    toIndexPage() {
        return switchTab('/pages/index/index')
    },
    toPage(...args) {
        return new Promise((resolve, reject) => {
            // @ts-ignore
            this.navigateTo(...args).catch(() => {
                // @ts-ignore
                this.switchTab(...args).catch(() => {
                    // @ts-ignore
                    this.redirectTo(...args).catch(reject)
                })
            })
        })
    },
    //跳转
    toUrl: (url?: string) => {
        if (!url) {
            return;
        }
        //小程序地址
        if (url && url.startsWith('/pages/')) {
            Taro.navigateTo({url}).catch(() => {
                Taro.switchTab({url}).catch(() => {
                })
            })
        }
        if (process.env.TARO_ENV == 'weapp') {
            //互联网地址
            if (url && url.startsWith('http')) {
                RouterUtils.navigateTo('/pages/webview/index', {
                    url
                })
            }
        } else {
            window.location.href = url
        }
    },
}

export default RouterUtils

  • 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
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/751325
推荐阅读
相关标签
  

闽ICP备14008679号