navigate-back.js 2.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
fxy060608's avatar
fxy060608 已提交
2
  ANI_CLOSE,
fxy060608's avatar
fxy060608 已提交
3 4 5
  ANI_DURATION
} from './util'

fxy060608's avatar
fxy060608 已提交
6 7 8 9
import {
  setStatusBarStyle
} from '../../bridge'

fxy060608's avatar
fxy060608 已提交
10 11
let firstBackTime = 0

fxy060608's avatar
fxy060608 已提交
12
function quit () {
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18 19 20 21 22 23
  if (!firstBackTime) {
    firstBackTime = Date.now()
    plus.nativeUI.toast('再按一次退出应用')
    setTimeout(() => {
      firstBackTime = null
    }, 2000)
  } else if (Date.now() - firstBackTime < 2000) {
    plus.runtime.quit()
  }
}

fxy060608's avatar
fxy060608 已提交
24
function backWebview (webview, callback) {
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  if (!webview.__uniapp_webview) {
    return callback()
  }
  const children = webview.children()
  if (!children || !children.length) { // 有子 webview
    return callback()
  }
  const childWebview = children[0]
  childWebview.canBack(({
    canBack
  }) => {
    if (canBack) {
      childWebview.back() // webview 返回
    } else {
      callback()
fxy060608's avatar
fxy060608 已提交
40
    }
fxy060608's avatar
fxy060608 已提交
41 42 43
  })
}

fxy060608's avatar
fxy060608 已提交
44
function back (delta, animationType, animationDuration) {
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50 51 52 53 54 55 56
  const pages = getCurrentPages()
  const len = pages.length
  const currentPage = pages[len - 1]

  if (delta > 1) {
    // 中间页隐藏
    pages.slice(len - delta, len - 1).reverse().forEach(deltaPage => {
      deltaPage.$getAppWebview().close('none')
    })
  }

  backWebview(currentPage, () => {
fxy060608's avatar
fxy060608 已提交
57
    if (animationType) {
fxy060608's avatar
fxy060608 已提交
58
      currentPage.$getAppWebview().close(animationType, animationDuration || ANI_DURATION)
fxy060608's avatar
fxy060608 已提交
59
    } else {
fxy060608's avatar
fxy060608 已提交
60 61 62
      if (currentPage.$page.openType === 'redirect') { // 如果是 redirectTo 跳转的,需要制定 back 动画
        currentPage.$getAppWebview().close(ANI_CLOSE, ANI_DURATION)
      }
fxy060608's avatar
fxy060608 已提交
63
      currentPage.$getAppWebview().close('auto')
fxy060608's avatar
fxy060608 已提交
64
    }
fxy060608's avatar
fxy060608 已提交
65 66

    pages.slice(len - delta, len).forEach(page => page.$remove())
fxy060608's avatar
fxy060608 已提交
67 68 69

    setStatusBarStyle()

fxy060608's avatar
fxy060608 已提交
70 71 72
    UniServiceJSBridge.emit('onAppRoute', {
      type: 'navigateBack'
    })
fxy060608's avatar
fxy060608 已提交
73 74 75
  })
}

fxy060608's avatar
fxy060608 已提交
76
export function navigateBack ({
77
  from = 'navigateBack',
fxy060608's avatar
fxy060608 已提交
78 79 80 81
  delta,
  animationType,
  animationDuration
}) {
82 83 84 85 86 87 88 89 90 91 92 93 94
  const pages = getCurrentPages()

  const currentPage = pages[pages.length - 1]
  if (
    currentPage.$vm &&
    currentPage.$vm.$options.onBackPress &&
    currentPage.$vm.__call_hook &&
    currentPage.$vm.__call_hook('onBackPress', {
      from
    })
  ) {
    return
  }
fxy060608's avatar
fxy060608 已提交
95 96 97

  uni.hideToast() // 后退时,关闭 toast,loading

98
  currentPage.$page.meta.isQuit
fxy060608's avatar
fxy060608 已提交
99 100
    ? quit()
    : back(delta, animationType, animationDuration)
fxy060608's avatar
fxy060608 已提交
101
}