'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
95
96
97
98
99
100
101
102
import {
  callAppHook,
  callPageHook
} from '../plugins/util'
 
import {
  setPullDownRefreshPageId
} from 'uni-platform/service/api/ui/pull-down-refresh'
 
import onWebInvokeAppService from 'uni-platform/service/on-web-invoke-app-service'
 
export default function initOn (on, {
  getApp,
  getCurrentPages
}) {
  function onError (err) {
    callAppHook(getApp(), 'onError', err)
  }
 
  function onPageNotFound (page) {
    callAppHook(getApp(), 'onPageNotFound', page)
  }
 
  function onResize (args, pageId) {
    const page = getCurrentPages().find(page => page.$page.id === pageId)
    page && callPageHook(page, 'onResize', args)
  }
 
  function onPullDownRefresh (args, pageId) {
    const page = getCurrentPages().find(page => page.$page.id === pageId)
    if (page) {
      setPullDownRefreshPageId(pageId)
      callPageHook(page, 'onPullDownRefresh')
    }
  }
 
  function callCurrentPageHook (hook, args) {
    const pages = getCurrentPages()
    if (pages.length) {
      callPageHook(pages[pages.length - 1], hook, args)
    }
  }
 
  function createCallCurrentPageHook (hook) {
    return function (args) {
      callCurrentPageHook(hook, args)
    }
  }
 
  function onAppEnterBackground () {
    callAppHook(getApp(), 'onHide')
    callCurrentPageHook('onHide')
  }
 
  function onAppEnterForeground (enterOptions) {
    callAppHook(getApp(), 'onShow', enterOptions)
    const pages = getCurrentPages()
    if (pages.length === 0) {
      return
    }
    callCurrentPageHook('onShow')
  }
 
  const routeHooks = {
    navigateTo () {
      callCurrentPageHook('onHide')
    },
    navigateBack () {
      callCurrentPageHook('onShow')
    }
  }
 
  function onAppRoute ({
    type
  }) {
    const routeHook = routeHooks[type]
    routeHook && routeHook()
  }
 
  on('onError', onError)
  on('onPageNotFound', onPageNotFound)
 
  if (__PLATFORM__ !== 'h5') { // 后续有时间,h5 平台也要迁移到 onAppRoute
    on('onAppRoute', onAppRoute)
  }
 
  on('onAppEnterBackground', onAppEnterBackground)
  on('onAppEnterForeground', onAppEnterForeground)
 
  on('onResize', onResize)
  on('onPullDownRefresh', onPullDownRefresh)
 
  on('onTabItemTap', createCallCurrentPageHook('onTabItemTap'))
  on('onNavigationBarButtonTap', createCallCurrentPageHook('onNavigationBarButtonTap'))
 
  on('onNavigationBarSearchInputChanged', createCallCurrentPageHook('onNavigationBarSearchInputChanged'))
  on('onNavigationBarSearchInputConfirmed', createCallCurrentPageHook('onNavigationBarSearchInputConfirmed'))
  on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked'))
  on('onNavigationBarSearchInputFocusChanged', createCallCurrentPageHook('onNavigationBarSearchInputFocusChanged'))
 
  on('onWebInvokeAppService', onWebInvokeAppService)
}