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
import { ON_BACK_PRESS, ON_SHOW } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19
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 已提交
20 21 22 23 24 25

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

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

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