navigateBack.ts 3.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import {
  API_NAVIGATE_BACK,
  API_TYPE_NAVIGATE_BACK,
  defineAsyncApi,
fxy060608's avatar
fxy060608 已提交
5 6
  NavigateBackOptions,
  NavigateBackProtocol,
fxy060608's avatar
fxy060608 已提交
7
} from '@dcloudio/uni-api'
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12
import {
  getCurrentPage,
  initI18nAppMsgsOnce,
  invokeHook,
} from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
13
import { useI18n } from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18
import { ComponentPublicInstance } from 'vue'
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 已提交
19 20 21 22 23 24

export const navigateBack = defineAsyncApi<API_TYPE_NAVIGATE_BACK>(
  API_NAVIGATE_BACK,
  (args, { resolve, reject }) => {
    const page = getCurrentPage()
    if (!page) {
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32
      return reject(`getCurrentPages is empty`)
    }
    if (
      invokeHook(page as ComponentPublicInstance, 'onBackPress', {
        from: (args as any).from,
      })
    ) {
      return resolve()
fxy060608's avatar
fxy060608 已提交
33
    }
fxy060608's avatar
fxy060608 已提交
34 35
    uni.hideToast()
    uni.hideLoading()
fxy060608's avatar
fxy060608 已提交
36 37
    if (page.$page.meta.isQuit) {
      quit()
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43 44 45 46 47
    } else if (page.$page.id === 1 && __uniConfig.realEntryPagePath) {
      // condition
      __uniConfig.entryPagePath = __uniConfig.realEntryPagePath
      delete __uniConfig.realEntryPagePath
      uni.reLaunch({
        url: '/' + __uniConfig.entryPagePath,
      })
    } else {
      const { delta, animationType, animationDuration } = args
      back(delta!, animationType, animationDuration)
fxy060608's avatar
fxy060608 已提交
48 49
    }
    return resolve()
fxy060608's avatar
fxy060608 已提交
50 51 52
  },
  NavigateBackProtocol,
  NavigateBackOptions
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
)
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 已提交
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

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 已提交
97
        // 如果是 redirectTo 跳转的,需要指定 back 动画
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104 105 106
        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 已提交
107 108
    // 前一个页面触发 onShow
    invokeHook('onShow')
fxy060608's avatar
fxy060608 已提交
109 110 111 112 113 114 115 116 117 118
  }

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