| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- export class Router {
- constructor() {
- this.interceptor = {
- before: null,
- after: null
- }
- }
- /**
- * 保留当前页面,跳转到应用内的某个页面,使用navigateBack可以返回到原页面。
- * @description navigateTo, redirectTo 只能打开非 tabBar 页面。
- * @param {*} options
- */
- navigateTo(
- options = {
- url: '',
- animationType: 'pop-in',
- animationDuration: 300,
- success: () => {},
- fail: () => {},
- complete: () => {}
- }
- ) {
- if (this.interceptor.before) {
- let newOptions = this.interceptor.before(options)
- if (newOptions) {
- options = newOptions
- } else {
- return
- }
- }
- uni.navigateTo({
- url: options.url,
- animationType: options.animationType,
- animationDuration: options.animationDuration,
- success: options.success,
- fail: options.fail,
- complete: options.complete
- })
- if (this.interceptor.after) {
- let newOptions = this.interceptor.after(options)
- if (newOptions) {
- options = newOptions
- }
- }
- }
- /**
- * 关闭当前页面,跳转到应用内的某个页面。
- * @description navigateTo, redirectTo 只能打开非 tabBar 页面。
- * @param {*} options
- */
- redirectTo(
- options = {
- url: '',
- success: () => {},
- fail: () => {},
- complete: () => {}
- }
- ) {
- if (this.interceptor.before) {
- let newOptions = this.interceptor.before(options)
- if (newOptions) {
- options = newOptions
- } else {
- return
- }
- }
- uni.redirectTo({
- url: options.url,
- success: options.success,
- fail: options.fail,
- complete: options.complete
- })
- if (this.interceptor.after) {
- let newOptions = this.interceptor.after(options)
- if (newOptions) {
- options = newOptions
- }
- }
- }
- /**
- * 关闭所有页面,打开到应用内的某个页面。
- * @description reLaunch 可以打开任意页面。
- * @param {*} options
- */
- reLaunch(
- options = {
- url: '',
- success: () => {},
- fail: () => {},
- complete: () => {}
- }
- ) {
- if (this.interceptor.before) {
- let newOptions = this.interceptor.before(options)
- if (newOptions) {
- options = newOptions
- } else {
- return
- }
- }
- uni.reLaunch({
- url: options.url,
- success: options.success,
- fail: options.fail,
- complete: options.complete
- })
- if (this.interceptor.after) {
- let newOptions = this.interceptor.after(options)
- if (newOptions) {
- options = newOptions
- }
- }
- }
- /**
- * 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
- * @description switchTab 只能打开 tabBar 页面。
- * @param {*} options
- */
- switchTab(
- options = {
- url: '',
- success: () => {},
- fail: () => {},
- complete: () => {}
- }
- ) {
- if (this.interceptor.before) {
- let newOptions = this.interceptor.before(options)
- if (newOptions) {
- options = newOptions
- } else {
- return
- }
- }
- uni.switchTab({
- url: options.url,
- success: options.success,
- fail: options.fail,
- complete: options.complete
- })
- if (this.interceptor.after) {
- let newOptions = this.interceptor.after(options)
- if (newOptions) {
- options = newOptions
- }
- }
- }
- /**
- * 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
- * @param {*} options
- */
- navigateBack(
- options = {
- delta: 1,
- animationType: 'pop-out',
- animationDuration: 300
- }
- ) {
- if (this.interceptor.before) {
- let newOptions = this.interceptor.before(options)
- if (newOptions) {
- options = newOptions
- } else {
- return
- }
- }
- uni.navigateBack({
- url: options.url,
- animationType: options.animationType,
- animationDuration: options.animationDuration,
- complete: options.complete
- })
- if (this.interceptor.after) {
- let newOptions = this.interceptor.after(options)
- if (newOptions) {
- options = newOptions
- }
- }
- }
- }
|