route.js 3.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import getRealRoute from '../../get-real-route'
fxy060608's avatar
fxy060608 已提交
2

3
function encodeQueryString (url) {
fxy060608's avatar
fxy060608 已提交
4 5 6 7
  if (typeof url !== 'string') {
    return url
  }
  const index = url.indexOf('?')
8

fxy060608's avatar
fxy060608 已提交
9 10 11
  if (index === -1) {
    return url
  }
12

fxy060608's avatar
fxy060608 已提交
13
  const query = url.substr(index + 1).trim().replace(/^(\?|#|&)/, '')
14

fxy060608's avatar
fxy060608 已提交
15 16
  if (!query) {
    return url
17
  }
fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

  url = url.substr(0, index)

  const params = []

  query.split('&').forEach(param => {
    const parts = param.replace(/\+/g, ' ').split('=')
    const key = parts.shift()
    const val = parts.length > 0
      ? parts.join('=')
      : ''

    params.push(key + '=' + encodeURIComponent(val))
  })

  return params.length ? url + '?' + params.join('&') : url
34 35
}

fxy060608's avatar
fxy060608 已提交
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
function createValidator (type) {
  return function validator (url, params) {
    // 格式化为绝对路径路由
    url = getRealRoute(url)

    const pagePath = url.split('?')[0]
    // 匹配路由是否存在
    const routeOptions = __uniRoutes.find(({
      path,
      alias
    }) => path === pagePath || alias === pagePath)

    if (!routeOptions) {
      return 'page `' + url + '` is not found'
    }

    // 检测不同类型跳转
    if (type === 'navigateTo' || type === 'redirectTo') {
      if (routeOptions.meta.isTabBar) {
        return `can not ${type} a tabbar page`
      }
    } else if (type === 'switchTab') {
      if (!routeOptions.meta.isTabBar) {
        return 'can not switch to no-tabBar page'
      }
    }

fxy060608's avatar
fxy060608 已提交
63 64
    // switchTab不允许传递参数,reLaunch到一个tabBar页面是可以的
    if (type === 'switchTab' && routeOptions.meta.isTabBar) {
fxy060608's avatar
fxy060608 已提交
65 66 67 68 69 70 71
      url = pagePath
    }

    // 首页自动格式化为`/`
    if (routeOptions.meta.isEntry) {
      url = url.replace(routeOptions.alias, '/')
    }
72 73 74

    // 参数格式化
    params.url = encodeQueryString(url)
fxy060608's avatar
fxy060608 已提交
75 76 77
  }
}

fxy060608's avatar
fxy060608 已提交
78 79
function createProtocol (type, extras = {}) {
  return Object.assign({
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84
    url: {
      type: String,
      required: true,
      validator: createValidator(type)
    }
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  }, extras)
}

function createAnimationProtocol (animationTypes) {
  return {
    animationType: {
      type: String,
      validator (type) {
        if (type && animationTypes.indexOf(type) === -1) {
          return '`' + type + '` is not supported for `animationType` (supported values are: `' + animationTypes.join(
            '`|`') + '`)'
        }
      }
    },
    animationDuration: {
      type: Number
    }
fxy060608's avatar
fxy060608 已提交
102 103
  }
}
fxy060608's avatar
fxy060608 已提交
104

fxy060608's avatar
fxy060608 已提交
105 106 107 108
export const redirectTo = createProtocol('redirectTo')

export const reLaunch = createProtocol('reLaunch')

fxy060608's avatar
fxy060608 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
export const navigateTo = createProtocol('navigateTo', createAnimationProtocol(
  [
    'slide-in-right',
    'slide-in-left',
    'slide-in-top',
    'slide-in-bottom',
    'fade-in',
    'zoom-out',
    'zoom-fade-out',
    'pop-in',
    'none'
  ]
))
fxy060608's avatar
fxy060608 已提交
122 123 124

export const switchTab = createProtocol('switchTab')

fxy060608's avatar
fxy060608 已提交
125
export const navigateBack = Object.assign({
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131 132
  delta: {
    type: Number,
    validator (delta, params) {
      delta = parseInt(delta) || 1
      params.delta = Math.min(getCurrentPages().length - 1, delta)
    }
  }
fxy060608's avatar
fxy060608 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
}, createAnimationProtocol(
  [
    'slide-out-right',
    'slide-out-left',
    'slide-out-top',
    'slide-out-bottom',
    'fade-out',
    'zoom-in',
    'zoom-fade-in',
    'pop-out',
    'none'
  ]
))