util.js 3.3 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 = {
fxy060608's avatar
fxy060608 已提交
25 26 27 28
  textColor: 'color',
  selectedColor: 'selectedColor',
  backgroundColor: 'backgroundColor',
  items: 'list'
fxy060608's avatar
fxy060608 已提交
29 30 31
}

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

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

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

fxy060608's avatar
fxy060608 已提交
44 45 46 47 48 49 50
function trimMPJson (json) {
  delete json.topWindow
  delete json.leftWindow
  delete json.rightWindow
  return json
}

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

fxy060608's avatar
fxy060608 已提交
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
  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)
  }

fxy060608's avatar
fxy060608 已提交
84 85 86 87 88 89 90
  if (
    style.navigationBarTextStyle &&
    (
      style.navigationBarTextStyle !== 'black' &&
      style.navigationBarTextStyle.indexOf('@') !== 0
    )
  ) {
fxy060608's avatar
fxy060608 已提交
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
    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,
fxy060608's avatar
fxy060608 已提交
127 128
  parseTabBar,
  trimMPJson
fxy060608's avatar
fxy060608 已提交
129
}