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

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

fxy060608's avatar
fxy060608 已提交
10 11 12 13
import {
  closeWebview
} from './util'

fxy060608's avatar
fxy060608 已提交
14 15
let firstBackTime = 0

fxy060608's avatar
fxy060608 已提交
16
function quit () {
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21 22 23 24 25 26 27
  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 已提交
28
function backWebview (webview, callback) {
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38 39 40
  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 已提交
41
    }
fxy060608's avatar
fxy060608 已提交
42 43 44
  })
}

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

  if (delta > 1) {
    // 中间页隐藏
    pages.slice(len - delta, len - 1).reverse().forEach(deltaPage => {
fxy060608's avatar
fxy060608 已提交
53
      closeWebview(deltaPage.$getAppWebview(), 'none')
fxy060608's avatar
fxy060608 已提交
54 55 56
    })
  }

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

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

    setStatusBarStyle()

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

77
  const webview = currentPage.$getAppWebview()
雪洛's avatar
雪洛 已提交
78
  if (!currentPage.__uniapp_webview) {
79
    return backPage(webview)
雪洛's avatar
雪洛 已提交
80
  }
fxy060608's avatar
fxy060608 已提交
81 82
  backWebview(webview, () => {
    backPage(webview)
83
  })
fxy060608's avatar
fxy060608 已提交
84 85
}

fxy060608's avatar
fxy060608 已提交
86
export function navigateBack ({
87
  from = 'navigateBack',
fxy060608's avatar
fxy060608 已提交
88 89 90 91
  delta,
  animationType,
  animationDuration
}) {
92
  const pages = getCurrentPages()
93 94 95 96 97 98 99 100 101 102 103

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

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

fxy060608's avatar
fxy060608 已提交
108 109 110 111
  if (currentPage.$page.meta.isQuit) {
    quit()
  } else if (currentPage.$page.id === 1 && __uniConfig.realEntryPagePath) {
    // condition
fxy060608's avatar
fxy060608 已提交
112 113
    __uniConfig.entryPagePath = __uniConfig.realEntryPagePath
    delete __uniConfig.realEntryPagePath
fxy060608's avatar
fxy060608 已提交
114
    uni.reLaunch({
fxy060608's avatar
fxy060608 已提交
115
      url: '/' + __uniConfig.entryPagePath
fxy060608's avatar
fxy060608 已提交
116 117
    })
  } else {
fxy060608's avatar
fxy060608 已提交
118
    back(delta, animationType, animationDuration)
fxy060608's avatar
fxy060608 已提交
119
  }
120 121 122
  return {
    errMsg: 'navigateBack:ok'
  }
fxy060608's avatar
fxy060608 已提交
123
}