util.js 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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
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'
}

const alipayTabBarMap = {
  'textColor': 'color',
  'selectedColor': 'selectedColor',
  'backgroundColor': 'backgroundColor',
  'items': 'list'
}

const alipayTabBarItemMap = {
  'pagePath': 'pagePath',
  'name': 'text',
  'icon': 'iconPath',
  'activeIcon': 'selectedIconPath'
}

function hasOwn (obj, key) {
  return hasOwnProperty.call(obj, key)
}

function parseStyle (style = {}, root = '') {
  let platformStyle = {}

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

  if (root && process.env.UNI_PLATFORM === 'app-plus') { // 处理分包逻辑
    if (Array.isArray(platformStyle.subNVues) && platformStyle.subNVues.length) {
      platformStyle.subNVues.forEach(subNVue => {
        subNVue.path = normalizePath(path.join(root, subNVue.path))
      })
    }
  }

  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 = '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
}

module.exports = {
  hasOwn,
  parseStyle,
  parseTabBar
}