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 = {
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
function trimMPJson (json) {
fxy060608's avatar
fxy060608 已提交
45
  delete json.maxWidth
fxy060608's avatar
fxy060608 已提交
46 47 48
  delete json.topWindow
  delete json.leftWindow
  delete json.rightWindow
fxy060608's avatar
fxy060608 已提交
49 50 51
  if (json.tabBar) {
    delete json.tabBar.matchMedia
  }
fxy060608's avatar
fxy060608 已提交
52 53 54
  return json
}

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

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

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

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

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

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