util.js 3.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const path = require('path')

fxy060608's avatar
fxy060608 已提交
3
const {
fxy060608's avatar
fxy060608 已提交
4 5 6 7
  getPlatforms,
  normalizePath
} = require('@dcloudio/uni-cli-shared')

fxy060608's avatar
fxy060608 已提交
8
const PLATFORMS = getPlatforms()
fxy060608's avatar
fxy060608 已提交
9 10

const alipayWindowMap = {
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15 16 17 18 19 20 21
  defaultTitle: 'navigationBarTitleText',
  pullRefresh: 'enablePullDownRefresh',
  allowsBounceVertical: 'allowsBounceVertical',
  titleBarColor: 'navigationBarBackgroundColor',
  optionMenu: 'optionMenu',
  backgroundColor: 'backgroundColor',
  usingComponents: 'usingComponents',
  navigationBarShadow: 'navigationBarShadow',
  titleImage: 'titleImage',
  transparentTitle: 'transparentTitle',
  titlePenetrate: 'titlePenetrate'
fxy060608's avatar
fxy060608 已提交
22 23 24
}

const alipayTabBarMap = {
25
  customize: 'customize',
fxy060608's avatar
fxy060608 已提交
26 27 28 29
  textColor: 'color',
  selectedColor: 'selectedColor',
  backgroundColor: 'backgroundColor',
  items: 'list'
fxy060608's avatar
fxy060608 已提交
30 31 32
}

const alipayTabBarItemMap = {
fxy060608's avatar
fxy060608 已提交
33 34 35 36
  pagePath: 'pagePath',
  name: 'text',
  icon: 'iconPath',
  activeIcon: 'selectedIconPath'
fxy060608's avatar
fxy060608 已提交
37 38
}

fxy060608's avatar
fxy060608 已提交
39 40
const _hasOwnProperty = Object.prototype.hasOwnProperty

fxy060608's avatar
fxy060608 已提交
41
function hasOwn (obj, key) {
fxy060608's avatar
fxy060608 已提交
42
  return _hasOwnProperty.call(obj, key)
fxy060608's avatar
fxy060608 已提交
43 44
}

fxy060608's avatar
fxy060608 已提交
45
function trimMPJson (json) {
fxy060608's avatar
fxy060608 已提交
46
  delete json.maxWidth
fxy060608's avatar
fxy060608 已提交
47 48 49
  delete json.topWindow
  delete json.leftWindow
  delete json.rightWindow
fxy060608's avatar
fxy060608 已提交
50 51 52
  if (json.tabBar) {
    delete json.tabBar.matchMedia
  }
fxy060608's avatar
fxy060608 已提交
53 54 55
  return json
}

fxy060608's avatar
fxy060608 已提交
56 57
function parseStyle (style = {}, root = '') {
  // TODO pages.json 触发了两次,需要排查
fxy060608's avatar
fxy060608 已提交
58
  style = trimMPJson(JSON.parse(JSON.stringify(style)))
fxy060608's avatar
fxy060608 已提交
59

fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65 66 67 68 69 70
  let platformStyle = {}

  Object.keys(style).forEach(name => {
    if (PLATFORMS.includes(name)) {
      if (name === process.env.UNI_PLATFORM) {
        platformStyle = style[name] || {}
      }
      delete style[name]
    }
  })

71 72
  if (process.env.UNI_PLATFORM === 'app-plus') {
    if (root && Array.isArray(platformStyle.subNVues) && platformStyle.subNVues.length) { // 处理分包逻辑
fxy060608's avatar
fxy060608 已提交
73 74 75 76
      platformStyle.subNVues.forEach(subNVue => {
        subNVue.path = normalizePath(path.join(root, subNVue.path))
      })
    }
77 78 79 80 81

    style.disableSwipeBack === true
      ? platformStyle.popGesture = 'none'
      : delete platformStyle.popGesture
    delete style.disableSwipeBack
fxy060608's avatar
fxy060608 已提交
82 83 84 85 86 87 88 89 90 91 92 93
  }

  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)
  }

fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99 100
  if (
    style.navigationBarTextStyle &&
    (
      style.navigationBarTextStyle !== 'black' &&
      style.navigationBarTextStyle.indexOf('@') !== 0
    )
  ) {
fxy060608's avatar
fxy060608 已提交
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
    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
}
133 134 135 136 137 138 139
const NON_APP_JSON_KEYS = [
  'appid',
  'unipush',
  'usingComponents',
  'optimization',
  'scopedSlotsCompiler',
  'usingComponents',
140 141
  'uniStatistics',
  'mergeVirtualHostAttributes'
142
]
fxy060608's avatar
fxy060608 已提交
143 144 145
module.exports = {
  hasOwn,
  parseStyle,
fxy060608's avatar
fxy060608 已提交
146
  parseTabBar,
147 148
  trimMPJson,
  NON_APP_JSON_KEYS
149
}