switch-tab.js 2.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import {
  ANI_CLOSE,
fxy060608's avatar
fxy060608 已提交
3 4 5 6
  ANI_DURATION
} from '../../constants'

import {
fxy060608's avatar
fxy060608 已提交
7 8 9 10
  showWebview
} from './util'

import {
fxy060608's avatar
fxy060608 已提交
11 12
  setStatusBarStyle,
  invoke
fxy060608's avatar
fxy060608 已提交
13 14
} from '../../bridge'

fxy060608's avatar
init v3  
fxy060608 已提交
15 16 17 18 19 20 21 22
import {
  registerPage
} from '../../framework/page'

import {
  navigate
} from '../../framework/navigator'

fxy060608's avatar
fxy060608 已提交
23
import tabBar from '../../framework/tab-bar'
fxy060608's avatar
fxy060608 已提交
24

fxy060608's avatar
init v3  
fxy060608 已提交
25 26
function _switchTab ({
  path,
fxy060608's avatar
fxy060608 已提交
27
  from
fxy060608's avatar
fxy060608 已提交
28
}, callbackId) {
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  tabBar.switchTab(path.slice(1))

  const pages = getCurrentPages()
  const len = pages.length

  if (len >= 1) { // 前一个页面是非 tabBar 页面
    const currentPage = pages[len - 1]
    if (!currentPage.$page.meta.isTabBar) {
      pages.reverse().forEach(page => {
        if (!page.$page.meta.isTabBar && page !== currentPage) {
          page.$remove()
          page.$getAppWebview().close('none')
        }
      })
      currentPage.$remove()
fxy060608's avatar
fxy060608 已提交
44 45 46 47 48 49 50 51
      // 延迟执行避免iOS应用退出
      setTimeout(() => {
        if (currentPage.$page.openType === 'redirect') {
          currentPage.$getAppWebview().close(ANI_CLOSE, ANI_DURATION)
        } else {
          currentPage.$getAppWebview().close('auto')
        }
      }, 100)
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    } else {
      // 前一个 tabBar 触发 onHide
      currentPage.$vm.__call_hook('onHide')
    }
  }

  let tabBarPage
  // 查找当前 tabBarPage,且设置 visible
  getCurrentPages(true).forEach(page => {
    if (('/' + page.route) === path) {
      page.$page.meta.visible = true
      tabBarPage = page
    } else {
      if (page.$page.meta.isTabBar) {
        page.$page.meta.visible = false
      }
    }
  })

  if (tabBarPage) {
    tabBarPage.$vm.__call_hook('onShow')
    tabBarPage.$getAppWebview().show('none')
  } else {
fxy060608's avatar
fxy060608 已提交
75 76 77 78 79 80 81
    return showWebview(registerPage({
      path,
      query: {},
      openType: 'switchTab'
    }), 'none', 0, () => {
      invoke(callbackId, {
        errMsg: 'switchTab:ok'
fxy060608's avatar
fxy060608 已提交
82
      })
fxy060608's avatar
fxy060608 已提交
83
    }, 70)
fxy060608's avatar
fxy060608 已提交
84 85 86
  }

  setStatusBarStyle()
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91
  return {
    errMsg: 'switchTab:ok'
  }
}

fxy060608's avatar
init v3  
fxy060608 已提交
92 93 94 95 96 97 98 99 100 101 102
export function switchTab ({
  url,
  from
}) {
  const path = url.split('?')[0]
  navigate(path, function () {
    _switchTab({
      path,
      from
    })
  })
fxy060608's avatar
fxy060608 已提交
103
}