'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
  ANI_CLOSE,
  ANI_DURATION
} from '../../constants'
 
import {
  WEBVIEW_ID_PREFIX
} from '../../../constants'
 
import {
  setStatusBarStyle
} from '../../bridge'
 
import {
  closeWebview
} from './util'
 
import {
  t
} from 'uni-core/helpers/i18n'
 
let firstBackTime = 0
 
function quit () {
  if (!firstBackTime) {
    firstBackTime = Date.now()
    plus.nativeUI.toast(t('uni.app.quit'))
    setTimeout(() => {
      firstBackTime = null
    }, 2000)
  } else if (Date.now() - firstBackTime < 2000) {
    plus.runtime.quit()
  }
}
 
function backWebview (webview, callback) {
  const children = webview.children()
  if (!children || !children.length) { // 有子 webview
    return callback()
  }
 
  // 如果页面有subNvues,切使用了webview组件,则返回时子webview会取错,因此需要做id匹配
  const childWebview = children.find(webview => webview.id.indexOf(WEBVIEW_ID_PREFIX) === 0) || children[0]
 
  childWebview.canBack(({
    canBack
  }) => {
    if (canBack) {
      childWebview.back() // webview 返回
    } else {
      callback()
    }
  })
}
 
function back (delta, animationType, animationDuration) {
  const pages = getCurrentPages()
  const len = pages.length
  const currentPage = pages[len - 1]
 
  if (delta > 1) {
    // 中间页隐藏
    pages.slice(len - delta, len - 1).reverse().forEach(deltaPage => {
      closeWebview(deltaPage.$getAppWebview(), 'none')
    })
  }
 
  const backPage = function (webview) {
    if (animationType) {
      closeWebview(webview, animationType, animationDuration || ANI_DURATION)
    } else {
      if (currentPage.$page.openType === 'redirect') { // 如果是 redirectTo 跳转的,需要制定 back 动画
        closeWebview(webview, ANI_CLOSE, ANI_DURATION)
      } else {
        closeWebview(webview, 'auto')
      }
    }
 
    pages.slice(len - delta, len).forEach(page => page.$remove())
 
    setStatusBarStyle()
 
    UniServiceJSBridge.emit('onAppRoute', {
      type: 'navigateBack'
    })
  }
 
  const webview = currentPage.$getAppWebview()
  if (!currentPage.__uniapp_webview) {
    return backPage(webview)
  }
  backWebview(webview, () => {
    backPage(webview)
  })
}
 
export function navigateBack ({
  from = 'navigateBack',
  delta,
  animationType,
  animationDuration
}) {
  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
  }
 
  // 后退时,关闭 toast,loading
  uni.hideToast()
  uni.hideLoading()
 
  if (currentPage.$page.meta.isQuit) {
    quit()
  } else if (currentPage.$page.id === 1 && __uniConfig.realEntryPagePath) {
    // condition
    __uniConfig.entryPagePath = __uniConfig.realEntryPagePath
    delete __uniConfig.realEntryPagePath
    uni.reLaunch({
      url: '/' + __uniConfig.entryPagePath
    })
  } else {
    back(delta, animationType, animationDuration)
  }
  return {
    errMsg: 'navigateBack:ok'
  }
}