路泰科技体检小程序UI设计新版本
1
wwl
5 天以前 a6cdbcfe28fcc40ebb4919f57d60fb20122e8e57
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 * @fileoverview 将微信端的代码转换到各个平台
 */
const through2 = require('through2')
 
// api 前缀
const prefix = {
  weixin: {
    wxml: 'wx:',
    js: 'wx.'
  },
  qq: {
    wxml: 'qq:',
    js: 'qq.'
  },
  baidu: {
    wxml: 's-',
    js: 'swan.'
  },
  alipay: {
    wxml: 'a:',
    js: 'my.'
  },
  toutiao: {
    wxml: 'tt:',
    js: 'tt.'
  }
}
// 文件名后缀
const suffix = {
  weixin: {
    wxml: '.wxml',
    wxss: '.wxss'
  },
  qq: {
    wxml: '.qml',
    wxss: '.qss'
  },
  baidu: {
    wxml: '.swan',
    wxss: '.css'
  },
  alipay: {
    wxml: '.axml',
    wxss: '.acss'
  },
  toutiao: {
    wxml: '.ttml',
    wxss: '.ttss'
  }
}
 
/**
 * @description 取出两个括号之间的内容
 * @param {string} content 总内容
 * @param {number} i 开始位置
 */
function getSection (content, i) {
  let j = i + 1
  let num = 1
  const start = content[i]
  const end = start === '(' ? ')' : '}'
  while (num) {
    if (content[j] === start) {
      num++
    } else if (content[j] === end) {
      num--
    }
    j++
  }
  return content.substring(i, j)
}
 
/**
 * @description 处理不同小程序平台间的差异
 * @param {string} platform 使用平台
 */
module.exports = function (platform) {
  if (platform !== 'uni-app') {
    platform = platform.split('-')[1]
  }
  return through2.obj(function (file, _, callback) {
    if (file.isBuffer()) {
      let content = file.contents.toString()
      if (platform === 'uni-app') {
        if (file.extname === '.js') {
          content = content.replace(/\.properties/g, '')
        }
      } else {
        // wxml 文件处理
        if (file.extname === '.wxml') {
          content = content.replace(/wx:/g, prefix[platform].wxml) // 替换 api 前缀
          file.extname = suffix[platform].wxml // 修改后缀名
          if (platform === 'qq') {
            // wxs 转为 qs
            content = content.replace(/<wxs/g, '<qs').replace(/<\/wxs/g, '</qs')
          } else if (platform === 'baidu') {
            content = content.replace(/s-if=['"]{{(\S+)}}['"]/g, 's-if="$1"') // s-if 和 s-for 后不加 {{}}
              .replace(/s-for=['"]{{(\S+)}}['"]/g, 's-for="$1"')
              .replace(/data="(.*?)"/g, 'data="{$1}"')
          } else if (platform === 'alipay') {
            content = content.replace('block-size', 'handle-size')
              .replace(/longpress/g, 'longTap')
              .replace(/bind([\S])/g, (_, $1) => { // bindevent 转为 onEvent
                return 'on' + $1.toUpperCase()
              }).replace(/catch([\S])/g, (_, $1) => { // catchevent 转为 catchEvent
                return 'catch' + $1.toUpperCase()
              })
          }
        } else if (file.extname === '.js') {
          // js 文件处理
          // 替换 api 前缀
          content = content.replace(/wx\./g, prefix[platform].js)
 
          // 支付宝格式转换
          if (platform === 'alipay') {
            // 将 aa.triggerEvent('bb', cc) 替换为 aa.props.onBb && aa.props.onBb(cc)
            content = content.replace(/([a-zA-Z0-9._]+).triggerEvent\(['"](\S+?)['"],*/g, function (_, $1, $2) {
              const method = `${$1}.props.on${$2[0].toUpperCase()}${$2.slice(1)}`
              return `${method}&&${method}(`
            })
 
            // 转换 showToast
            let i = content.indexOf('.showToast')
            while (i !== -1) {
              i += 10
              const section = getSection(content, i)
              content = content.substr(0, i) + section.replace('title', 'content') + content.substr(i + section.length)
              i = content.indexOf('.showToast', i)
            }
            // 转换 showActionSheet
            i = content.indexOf('.showActionSheet')
            while (i !== -1) {
              i += 16
              const section = getSection(content, i)
              content = content.substr(0, i) + section.replace('itemList', 'items') + content.substr(i + section.length)
              i = content.indexOf('.showActionSheet', i)
            }
            // 转换 setClipboardData
            i = content.indexOf('.setClipboardData')
            while (i !== -1) {
              i += 17
              const section = getSection(content, i)
              content = content.substr(0, i - 4) + section.replace('data', 'text') + content.substr(i + section.length)
              i = content.indexOf('.setClipboardData', i)
            }
            // 组件格式转换
            if (content.includes('Component({')) {
              // 替换生命周期
              content = content.replace('created:', 'didMount:')
                .replace('attached:', 'didMount:')
                .replace('detached:', 'didUnmount:')
              // 将 properties 字段转为 props 格式
              i = content.indexOf('{', content.indexOf('properties:'))
              let props
              let propsStr = '{'
              const objStr = getSection(content, i)
              // 取出整个 properties 字段
              eval('props = ' + objStr) // eslint-disable-line
              for (const item in props) {
                if (!props[item]) continue
                propsStr += item + ':'
                if (props[item].value) {
                  // 设置了默认值
                  if (typeof props[item].value === 'boolean') {
                    propsStr += props[item].value ? '!0' : '!1'
                  } else {
                    propsStr += props[item].value
                  }
                } else {
                  // 没有设置默认值
                  const type = props[item].type || props[item]
                  if (type === String) {
                    propsStr += '""'
                  } else if (type === Boolean) {
                    propsStr += '!1'
                  } else if (type === Number) {
                    propsStr += '0'
                  } else if (type === Object) {
                    propsStr += '{}'
                  } else if (type === Array) {
                    propsStr += '[]'
                  }
                }
                propsStr += ','
              }
              content = content.substr(0, i) + propsStr.substring(0, propsStr.length - 1) + '}' + content.substr(i + objStr.length)
            }
 
            content = content.replace(/properties/g, 'props')
              .replace(/\.setNavigationBarTitle/g, '.setNavigationBar')
          } else {
            content = content.replace(/\.properties/g, '.data')
          }
        } else if (file.extname === '.wxss') {
          // wxss 文件处理
          file.extname = suffix[platform].wxss // 修改后缀名
        }
      }
      file.contents = Buffer.from(content)
    }
    this.push(file)
    callback()
  })
}