page.js 1.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import {
  initWebview,
  createWebview
fxy060608's avatar
fxy060608 已提交
4 5
} from './webview/index'

fxy060608's avatar
fxy060608 已提交
6
const pages = []
fxy060608's avatar
fxy060608 已提交
7

fxy060608's avatar
fxy060608 已提交
8 9 10 11
export function getCurrentPages (returnAll) {
  return returnAll ? pages.slice(0) : pages.filter(page => {
    return !page.$page.meta.isTabBar || page.$page.meta.visible
  })
fxy060608's avatar
fxy060608 已提交
12
}
fxy060608's avatar
fxy060608 已提交
13

fxy060608's avatar
fxy060608 已提交
14 15 16
/**
 * 首页需要主动registerPage,二级页面路由跳转时registerPage
 */
fxy060608's avatar
fxy060608 已提交
17 18
export function registerPage ({
  path,
fxy060608's avatar
fxy060608 已提交
19
  query,
fxy060608's avatar
fxy060608 已提交
20
  openType,
fxy060608's avatar
fxy060608 已提交
21
  webview
fxy060608's avatar
fxy060608 已提交
22 23
}) {
  const routeOptions = JSON.parse(JSON.stringify(__uniRoutes.find(route => route.path === path)))
fxy060608's avatar
fxy060608 已提交
24

fxy060608's avatar
fxy060608 已提交
25 26 27 28 29
  if (openType === 'reLaunch' || pages.length === 0) {
    // pages.length===0 表示首页触发 redirectTo
    routeOptions.meta.isQuit = true
  }

fxy060608's avatar
fxy060608 已提交
30
  if (!webview) {
fxy060608's avatar
fxy060608 已提交
31
    webview = createWebview(path, routeOptions)
fxy060608's avatar
fxy060608 已提交
32 33
  }

fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41 42
  if (routeOptions.meta.isTabBar) {
    routeOptions.meta.visible = true
  }

  if (routeOptions.meta.isTabBar && webview.id !== '1') {
    const launchWebview = plus.webview.getLaunchWebview()
    launchWebview && launchWebview.append(webview)
  }

fxy060608's avatar
fxy060608 已提交
43 44 45
  if (process.env.NODE_ENV !== 'production') {
    console.log(`[uni-app] registerPage`, path, webview.id)
  }
fxy060608's avatar
fxy060608 已提交
46

fxy060608's avatar
fxy060608 已提交
47 48 49
  initWebview(webview, webview.id === '1' && routeOptions)

  const route = path.slice(1)
fxy060608's avatar
fxy060608 已提交
50

fxy060608's avatar
fxy060608 已提交
51
  webview.__uniapp_route = route
fxy060608's avatar
fxy060608 已提交
52

fxy060608's avatar
fxy060608 已提交
53
  pages.push({
fxy060608's avatar
fxy060608 已提交
54
    route,
fxy060608's avatar
fxy060608 已提交
55
    options: Object.assign({}, query || {}),
fxy060608's avatar
fxy060608 已提交
56 57 58
    $getAppWebview () {
      return webview
    },
fxy060608's avatar
fxy060608 已提交
59 60 61 62
    $page: {
      id: parseInt(webview.id),
      meta: routeOptions.meta,
      path,
fxy060608's avatar
fxy060608 已提交
63 64 65 66 67 68 69 70 71 72 73
      route,
      openType
    },
    $remove () {
      const index = pages.findIndex(page => page === this)
      if (index !== -1) {
        pages.splice(index, 1)
        if (process.env.NODE_ENV !== 'production') {
          console.log(`[uni-app] removePage`, path, webview.id)
        }
      }
fxy060608's avatar
fxy060608 已提交
74
    }
fxy060608's avatar
fxy060608 已提交
75
  })
fxy060608's avatar
fxy060608 已提交
76 77

  return webview
fxy060608's avatar
fxy060608 已提交
78
}