navigateBack.ts 3.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { ComponentPublicInstance } from 'vue'
fxy060608's avatar
fxy060608 已提交
2 3 4 5
import {
  API_NAVIGATE_BACK,
  API_TYPE_NAVIGATE_BACK,
  defineAsyncApi,
fxy060608's avatar
fxy060608 已提交
6 7
  NavigateBackOptions,
  NavigateBackProtocol,
fxy060608's avatar
fxy060608 已提交
8
} from '@dcloudio/uni-api'
fxy060608's avatar
fxy060608 已提交
9 10 11 12 13
import {
  getCurrentPage,
  initI18nAppMsgsOnce,
  invokeHook,
} from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
14
import { useI18n } from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
15 16
import { ON_BACK_PRESS, ON_SHOW } from '@dcloudio/uni-shared'

fxy060608's avatar
fxy060608 已提交
17 18 19 20
import { ANI_CLOSE, ANI_DURATION } from '../../constants'
import { removePage } from '../../framework/page/getCurrentPages'
import { setStatusBarStyle } from '../../statusBar'
import { backWebview, closeWebview } from './webview'
fxy060608's avatar
fxy060608 已提交
21
import { isDirectPage, reLaunchEntryPage } from './direct'
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27

export const navigateBack = defineAsyncApi<API_TYPE_NAVIGATE_BACK>(
  API_NAVIGATE_BACK,
  (args, { resolve, reject }) => {
    const page = getCurrentPage()
    if (!page) {
fxy060608's avatar
fxy060608 已提交
28 29 30
      return reject(`getCurrentPages is empty`)
    }
    if (
fxy060608's avatar
fxy060608 已提交
31
      invokeHook(page as ComponentPublicInstance, ON_BACK_PRESS, {
32
        from: (args as any).from || 'navigateBack',
fxy060608's avatar
fxy060608 已提交
33 34 35
      })
    ) {
      return resolve()
fxy060608's avatar
fxy060608 已提交
36
    }
fxy060608's avatar
fxy060608 已提交
37 38
    uni.hideToast()
    uni.hideLoading()
fxy060608's avatar
fxy060608 已提交
39 40
    if (page.$page.meta.isQuit) {
      quit()
fxy060608's avatar
fxy060608 已提交
41 42
    } else if (isDirectPage(page)) {
      reLaunchEntryPage()
fxy060608's avatar
fxy060608 已提交
43 44 45
    } else {
      const { delta, animationType, animationDuration } = args
      back(delta!, animationType, animationDuration)
fxy060608's avatar
fxy060608 已提交
46 47
    }
    return resolve()
fxy060608's avatar
fxy060608 已提交
48 49 50
  },
  NavigateBackProtocol,
  NavigateBackOptions
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
)
let firstBackTime = 0

function quit() {
  initI18nAppMsgsOnce()
  if (!firstBackTime) {
    firstBackTime = Date.now()
    plus.nativeUI.toast(useI18n().t('uni.app.quit'))
    setTimeout(() => {
      firstBackTime = 0
    }, 2000)
  } else if (Date.now() - firstBackTime < 2000) {
    plus.runtime.quit()
  }
}
fxy060608's avatar
fxy060608 已提交
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

function back(
  delta: number,
  animationType?: string,
  animationDuration?: number
) {
  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(
          plus.webview.getWebviewById(deltaPage.$page.id + ''),
          'none',
          0
        )
      })
  }

  const backPage = function (webview: PlusWebviewWebviewObject) {
    if (animationType) {
      closeWebview(webview, animationType, animationDuration || ANI_DURATION)
    } else {
      if (currentPage.$page.openType === 'redirectTo') {
fxy060608's avatar
fxy060608 已提交
95
        // 如果是 redirectTo 跳转的,需要指定 back 动画
fxy060608's avatar
fxy060608 已提交
96 97 98 99 100 101 102 103 104
        closeWebview(webview, ANI_CLOSE, ANI_DURATION)
      } else {
        closeWebview(webview, 'auto')
      }
    }
    pages
      .slice(len - delta, len)
      .forEach((page) => removePage(page as ComponentPublicInstance))
    setStatusBarStyle()
fxy060608's avatar
fxy060608 已提交
105
    // 前一个页面触发 onShow
fxy060608's avatar
fxy060608 已提交
106
    invokeHook(ON_SHOW)
fxy060608's avatar
fxy060608 已提交
107 108 109 110 111 112 113 114 115 116
  }

  const webview = plus.webview.getWebviewById(currentPage.$page.id + '')
  if (!(currentPage as any).__uniapp_webview) {
    return backPage(webview)
  }
  backWebview(webview, () => {
    backPage(webview)
  })
}