|
- import { NavigateType, Router, Route as TaroJsRoute, NavigateOptions } from "tarojs-router-next";
- import { getCurrentInstance } from '@tarojs/taro'
- import { Session } from "./session";
-
- if (!Session.has('prevent-double-click')) {
- Session.set('prevent-double-click', Math.floor(Date.now()/100))
- }
-
- export class Route {
- static redirectTo = (route: TaroJsRoute, options?: NavigateOptions, withoutPreventDoubleClick?: boolean) => {
- if (!withoutPreventDoubleClick && !Route.preventDoubleClick()) return
- if (Route.isTabBar(route.url)) {
- Router.navigate( route, {...options, ...{ type: NavigateType.switchTab }} )
- }
- else {
- Router.navigate( route, {...options, ...{ type: NavigateType.redirectTo }} )
- }
- }
-
- static navigateTo = (route: TaroJsRoute, options?: NavigateOptions, withoutPreventDoubleClick?: boolean) => {
- if (!withoutPreventDoubleClick && !Route.preventDoubleClick()) return
- if (Route.isTabBar(route.url)) {
- Router.navigate( route, {...options, ...{ type: NavigateType.switchTab }} )
- }
- else {
- Router.navigate( route, {...options, ...{ type: NavigateType.navigateTo }} )
- }
- }
-
- static reLaunch = (route: TaroJsRoute, options?: NavigateOptions, withoutPreventDoubleClick?: boolean) => {
- if (!withoutPreventDoubleClick && !Route.preventDoubleClick()) return
- if (Route.isTabBar(route.url)) {
- Router.navigate( route, {...options, ...{ type: NavigateType.switchTab }} )
- }
- else {
- Router.navigate( route, {...options, ...{ type: NavigateType.reLaunch }} )
- }
- }
-
- static preventDoubleClick() {
- const currentTime = Math.floor(Date.now()/100)
- if ((currentTime - Session.get('prevent-double-click')) < 10 ) return false
- Session.set('prevent-double-click', currentTime)
- return true
- }
-
- static isTabBar = (url) => {
- // @ts-ignore
- const tabBarList = getCurrentInstance().app?.config.tabBar.list;
- if (tabBarList == undefined) return false
- for (let item of tabBarList) {
- if (url.match("/"+item.pagePath)) {
- return true;
- }
- }
- return false
- }
- static getRequest = () => {
- return getCurrentInstance().router!.params
- }
- }
|