util.js 3.9 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
  defaultTitle: 'navigationBarTitleText',
  pullRefresh: 'enablePullDownRefresh',
  allowsBounceVertical: 'allowsBounceVertical',
  titleBarColor: 'navigationBarBackgroundColor',
  optionMenu: 'optionMenu',
  backgroundColor: 'backgroundColor',
  usingComponents: 'usingComponents',
  navigationBarShadow: 'navigationBarShadow',
  titleImage: 'titleImage',
  transparentTitle: 'transparentTitle',
D
DCloud_LXH 已提交
21 22 23 24 25 26 27
  titlePenetrate: 'titlePenetrate',
  barButtonTheme: {
    key: 'navigationBarTextStyle',
    transform: function (value) {

    }
  }
fxy060608's avatar
fxy060608 已提交
28 29 30
}

const alipayTabBarMap = {
31
  customize: 'customize',
fxy060608's avatar
fxy060608 已提交
32 33 34 35
  textColor: 'color',
  selectedColor: 'selectedColor',
  backgroundColor: 'backgroundColor',
  items: 'list'
fxy060608's avatar
fxy060608 已提交
36 37 38
}

const alipayTabBarItemMap = {
fxy060608's avatar
fxy060608 已提交
39 40 41 42
  pagePath: 'pagePath',
  name: 'text',
  icon: 'iconPath',
  activeIcon: 'selectedIconPath'
fxy060608's avatar
fxy060608 已提交
43 44
}

fxy060608's avatar
fxy060608 已提交
45 46
const _hasOwnProperty = Object.prototype.hasOwnProperty

fxy060608's avatar
fxy060608 已提交
47
function hasOwn (obj, key) {
fxy060608's avatar
fxy060608 已提交
48
  return _hasOwnProperty.call(obj, key)
fxy060608's avatar
fxy060608 已提交
49 50
}

fxy060608's avatar
fxy060608 已提交
51
function trimMPJson (json) {
fxy060608's avatar
fxy060608 已提交
52
  delete json.maxWidth
fxy060608's avatar
fxy060608 已提交
53 54 55
  delete json.topWindow
  delete json.leftWindow
  delete json.rightWindow
fxy060608's avatar
fxy060608 已提交
56 57 58
  if (json.tabBar) {
    delete json.tabBar.matchMedia
  }
fxy060608's avatar
fxy060608 已提交
59 60 61
  return json
}

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

fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76
  let platformStyle = {}

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

77 78
  if (process.env.UNI_PLATFORM === 'app-plus') {
    if (root && Array.isArray(platformStyle.subNVues) && platformStyle.subNVues.length) { // 处理分包逻辑
fxy060608's avatar
fxy060608 已提交
79 80 81 82
      platformStyle.subNVues.forEach(subNVue => {
        subNVue.path = normalizePath(path.join(root, subNVue.path))
      })
    }
83 84 85 86 87

    style.disableSwipeBack === true
      ? platformStyle.popGesture = 'none'
      : delete platformStyle.popGesture
    delete style.disableSwipeBack
fxy060608's avatar
fxy060608 已提交
88 89 90 91 92 93 94 95 96 97 98 99
  }

  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 已提交
100 101 102 103 104 105 106
  if (
    style.navigationBarTextStyle &&
    (
      style.navigationBarTextStyle !== 'black' &&
      style.navigationBarTextStyle.indexOf('@') !== 0
    )
  ) {
fxy060608's avatar
fxy060608 已提交
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
    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
}
139 140 141
const NON_APP_JSON_KEYS = [
  'appid',
  'unipush',
雪洛's avatar
雪洛 已提交
142
  'secureNetwork',
143 144 145 146
  'usingComponents',
  'optimization',
  'scopedSlotsCompiler',
  'usingComponents',
147 148
  'uniStatistics',
  'mergeVirtualHostAttributes'
149
]
fxy060608's avatar
fxy060608 已提交
150 151 152
module.exports = {
  hasOwn,
  parseStyle,
fxy060608's avatar
fxy060608 已提交
153
  parseTabBar,
154 155
  trimMPJson,
  NON_APP_JSON_KEYS
156
}