2022-09-17 19:12:43 +08:00

67 lines
2.0 KiB
TypeScript

import { NAVIGATE_TYPE } from '@/enums/routerEnum';
import { warn } from 'vue';
import { deepMerge } from '@/utils';
import { routerBeforeEach } from '@/utils/router/interceptor';
import { cloneDeep } from 'lodash-es';
export type NavigateOptions = Partial<Omit<UniApp.NavigateToOptions, 'url'>> & { delta?: number };
export class Navigates {
private type: string;
private readonly options: NavigateOptions;
constructor(type?: string, options?: NavigateOptions) {
this.type = type || NAVIGATE_TYPE.NAVIGATE_TO;
this.options = options || {};
}
navigate(url: string, options?: NavigateOptions) {
const navigateOptions = deepMerge(cloneDeep(this.options), options);
const _options = deepMerge({ url }, navigateOptions);
switch (this.type) {
case NAVIGATE_TYPE.NAVIGATE_TO:
uni.navigateTo(_options);
break;
case NAVIGATE_TYPE.REDIRECT_TO:
uni.redirectTo(_options);
break;
case NAVIGATE_TYPE.RE_LAUNCH:
uni.reLaunch(_options);
break;
case NAVIGATE_TYPE.SWITCH_TAB:
uni.switchTab(_options);
break;
case NAVIGATE_TYPE.NAVIGATE_BACK:
uni.navigateBack(navigateOptions);
break;
default:
warn('navigate Error');
break;
}
}
push(url: string, options?: NavigateOptions) {
this.type = NAVIGATE_TYPE.NAVIGATE_TO;
this.navigate(url, options);
}
replace(url: string, options?: NavigateOptions) {
this.type = NAVIGATE_TYPE.REDIRECT_TO;
this.navigate(url, options);
}
replaceAll(url: string, options?: NavigateOptions) {
this.type = NAVIGATE_TYPE.REDIRECT_TO;
this.navigate(url, options);
}
pushTab(url: string, options?: NavigateOptions) {
// 微信小程序端uni.switchTab拦截无效处理
/* #ifdef MP-WEIXIN */
if (!routerBeforeEach(url)) {
return;
}
/* #endif */
this.type = NAVIGATE_TYPE.SWITCH_TAB;
this.navigate(url, options);
}
back(options?: NavigateOptions) {
this.type = NAVIGATE_TYPE.NAVIGATE_BACK;
this.navigate('', options);
}
}