route.js 2.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
fxy060608's avatar
fxy060608 已提交
2 3
  hasLifecycleHook
} from 'uni-helpers/index'
fxy060608's avatar
fxy060608 已提交
4

fxy060608's avatar
fxy060608 已提交
5 6 7 8
const {
  invokeCallbackHandler: invoke
} = UniServiceJSBridge

fxy060608's avatar
fxy060608 已提交
9 10 11
function onAppRoute (type, {
  url,
  delta,
fxy060608's avatar
fxy060608 已提交
12 13
  animationType,
  animationDuration,
fxy060608's avatar
fxy060608 已提交
14 15
  from = 'navigateBack',
  detail
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22 23 24 25 26 27
} = {}) {
  const router = getApp().$router
  switch (type) {
    case 'redirectTo':
      router.replace({
        type,
        path: url
      })
      break
    case 'navigateTo':
      router.push({
        type,
fxy060608's avatar
fxy060608 已提交
28 29 30
        path: url,
        animationType,
        animationDuration
fxy060608's avatar
fxy060608 已提交
31 32 33
      })
      break
    case 'navigateBack':
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      {
        let canBack = true
        const pages = getCurrentPages()
        if (pages.length) {
          const page = pages[pages.length - 1]
          if (hasLifecycleHook(page.$options, 'onBackPress') && page.__call_hook('onBackPress', {
            from
          }) === true) {
            canBack = false
          }
        }
        if (canBack) {
          if (delta > 1) {
            router._$delta = delta
          }
          router.go(-delta, {
            animationType,
            animationDuration
          })
        }
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61 62 63 64
      }
      break
    case 'reLaunch':
      router.replace({
        type,
        path: url
      })
      break
    case 'switchTab':
      router.replace({
        type,
fxy060608's avatar
fxy060608 已提交
65 66 67 68
        path: url,
        params: {
          detail
        }
fxy060608's avatar
fxy060608 已提交
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
      })
      break
  }
  return {
    errMsg: type + ':ok'
  }
}

export function redirectTo (args) {
  return onAppRoute('redirectTo', args)
}

export function navigateTo (args) {
  return onAppRoute('navigateTo', args)
}

export function navigateBack (args) {
  return onAppRoute('navigateBack', args)
}

export function reLaunch (args) {
  return onAppRoute('reLaunch', args)
}

export function switchTab (args) {
  return onAppRoute('switchTab', args)
fxy060608's avatar
fxy060608 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

export function preloadPage ({
  url
}, callbackId) {
  const path = url.split('?')[0].replace(/\//g, '-')
  __uniConfig.__webpack_chunk_load__(path.substr(1)).then(() => {
    invoke(callbackId, {
      url,
      errMsg: 'preloadPage:ok'
    })
  }).catch(err => {
    invoke(callbackId, {
      url,
      errMsg: 'preloadPage:fail ' + err
    })
  })
fxy060608's avatar
fxy060608 已提交
112
}