mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const path = require('path')
 
const {
  getPlatforms,
  normalizePath
} = require('@dcloudio/uni-cli-shared')
 
const PLATFORMS = getPlatforms()
 
const alipayWindowMap = {
  defaultTitle: 'navigationBarTitleText',
  pullRefresh: 'enablePullDownRefresh',
  allowsBounceVertical: 'allowsBounceVertical',
  titleBarColor: 'navigationBarBackgroundColor',
  optionMenu: 'optionMenu',
  backgroundColor: 'backgroundColor',
  usingComponents: 'usingComponents',
  navigationBarShadow: 'navigationBarShadow',
  titleImage: 'titleImage',
  transparentTitle: 'transparentTitle',
  titlePenetrate: 'titlePenetrate',
  barButtonTheme: {
    key: 'navigationBarTextStyle',
    transform: function (value) {
 
    }
  }
}
 
const alipayTabBarMap = {
  customize: 'customize',
  textColor: 'color',
  selectedColor: 'selectedColor',
  backgroundColor: 'backgroundColor',
  items: 'list'
}
 
const alipayTabBarItemMap = {
  pagePath: 'pagePath',
  name: 'text',
  icon: 'iconPath',
  activeIcon: 'selectedIconPath'
}
 
const _hasOwnProperty = Object.prototype.hasOwnProperty
 
function hasOwn (obj, key) {
  return _hasOwnProperty.call(obj, key)
}
 
function trimMPJson (json) {
  delete json.maxWidth
  delete json.topWindow
  delete json.leftWindow
  delete json.rightWindow
  if (json.tabBar) {
    delete json.tabBar.matchMedia
  }
  return json
}
 
function parseStyle (style = {}, root = '') {
  // TODO pages.json 触发了两次,需要排查
  style = trimMPJson(JSON.parse(JSON.stringify(style)))
 
  let platformStyle = {}
 
  Object.keys(style).forEach(name => {
    if (name === 'app') {
      name = 'app-plus'
    } else if (name === 'web') {
      name = 'h5'
    }
    if (PLATFORMS.includes(name)) {
      if (name === process.env.UNI_PLATFORM) {
        if (name === 'app-plus') {
          platformStyle = style.app || style[name] || {}
        } else if (name === 'h5') {
          platformStyle = style.web || style[name] || {}
        } else {
          platformStyle = style[name] || {}
        }
      }
      delete style[name]
    }
  })
 
  delete style.app
  delete style.web
 
  if (process.env.UNI_PLATFORM === 'app-plus') {
    if (root && Array.isArray(platformStyle.subNVues) && platformStyle.subNVues.length) { // 处理分包逻辑
      platformStyle.subNVues.forEach(subNVue => {
        subNVue.path = normalizePath(path.join(root, subNVue.path))
      })
    }
 
    style.disableSwipeBack === true
      ? platformStyle.popGesture = 'none'
      : delete platformStyle.popGesture
    delete style.disableSwipeBack
  }
 
  if (process.env.UNI_PLATFORM === 'mp-alipay') {
    const windowOptions = {}
    Object.keys(alipayWindowMap).forEach(key => {
      if (hasOwn(style, alipayWindowMap[key])) {
        windowOptions[key] = style[alipayWindowMap[key]]
      }
    })
    return Object.assign(windowOptions, platformStyle)
  }
 
  if (
    style.navigationBarTextStyle &&
    (
      style.navigationBarTextStyle !== 'black' &&
      style.navigationBarTextStyle.indexOf('@') !== 0
    )
  ) {
    style.navigationBarTextStyle = 'white'
  }
 
  return Object.assign({
    usingComponents: {}
  }, style, platformStyle)
}
 
function parseTabBar (style = {}) {
  if (process.env.UNI_PLATFORM === 'mp-alipay') {
    const tabBarOptions = {}
    Object.keys(alipayTabBarMap).forEach(key => {
      if (hasOwn(style, alipayTabBarMap[key])) {
        tabBarOptions[key] = style[alipayTabBarMap[key]]
      }
    })
    if (Array.isArray(tabBarOptions.items) && tabBarOptions.items.length) {
      tabBarOptions.items = tabBarOptions.items.map(itemOptions => {
        const tabBarItemOptions = {}
        Object.keys(alipayTabBarItemMap).forEach(key => {
          if (hasOwn(itemOptions, alipayTabBarItemMap[key])) {
            tabBarItemOptions[key] = itemOptions[alipayTabBarItemMap[key]]
          }
        })
        return tabBarItemOptions
      })
    }
    return tabBarOptions
  }
 
  return style
}
const NON_APP_JSON_KEYS = [
  'appid',
  'unipush',
  'secureNetwork',
  'usingComponents',
  'optimization',
  'scopedSlotsCompiler',
  'slotMultipleInstance',
  'uniStatistics',
  'mergeVirtualHostAttributes',
  'styleIsolation'
]
module.exports = {
  hasOwn,
  parseStyle,
  parseTabBar,
  trimMPJson,
  NON_APP_JSON_KEYS
}