navigateBack.ts 3.2 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 { addLeadingSlash, 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
    } else if (
      // 处于直达页面
      page.$page.route === __uniConfig.entryPagePath &&
      __uniConfig.realEntryPagePath
    ) {
fxy060608's avatar
fxy060608 已提交
44 45 46 47
      // condition
      __uniConfig.entryPagePath = __uniConfig.realEntryPagePath
      delete __uniConfig.realEntryPagePath
      uni.reLaunch({
fxy060608's avatar
fxy060608 已提交
48
        url: addLeadingSlash(__uniConfig.entryPagePath),
fxy060608's avatar
fxy060608 已提交
49 50 51 52
      })
    } else {
      const { delta, animationType, animationDuration } = args
      back(delta!, animationType, animationDuration)
fxy060608's avatar
fxy060608 已提交
53 54
    }
    return resolve()
fxy060608's avatar
fxy060608 已提交
55 56 57
  },
  NavigateBackProtocol,
  NavigateBackOptions
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
)
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 已提交
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

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

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