路泰科技体检小程序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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// 等待初始化完毕
document.addEventListener('UniAppJSBridgeReady', () => {
  document.body.onclick = () =>
    uni.postMessage({
      data: {
        action: 'onClick'
      }
    })
  uni.postMessage({
    data: {
      action: 'onJSBridgeReady'
    }
  })
})
 
let options
let medias = []
 
/**
 * @description 获取标签的所有属性
 * @param {Element} ele
 */
function getAttrs (ele) {
  const attrs = Object.create(null)
  for (let i = ele.attributes.length; i--;) {
    attrs[ele.attributes[i].name] = ele.attributes[i].value
  }
  return attrs
}
 
/**
 * @description 图片加载出错
 */
function onImgError () {
  if (options[1]) {
    this.src = options[1]
    this.onerror = null
  }
  // 取消监听点击
  this.onclick = null
  this.ontouchstart = null
  uni.postMessage({
    data: {
      action: 'onError',
      source: 'img',
      attrs: getAttrs(this)
    }
  })
}
 
/**
 * @description 检查是否所有图片加载完毕
 */
function checkReady () {
  window.unloadimgs -= 1
  if (window.unloadimgs === 0) {
    // 所有图片加载完毕
    uni.postMessage({
      data: {
        action: 'onReady'
      }
    })
  }
}
 
/**
 * @description 创建 dom 结构
 * @param {object[]} nodes 节点数组
 * @param {Element} parent 父节点
 * @param {string} namespace 命名空间
 */
function createDom (nodes, parent, namespace) {
  for (let i = 0; i < nodes.length; i++) {
    const node = nodes[i]
    let ele
    if (!node.type || node.type === 'node') {
      let name = node.name
      // svg 需要设置 namespace
      if (name === 'svg') {
        namespace = 'http://www.w3.org/2000/svg'
      }
      if (name === 'html' || name === 'body') {
        name = 'div'
      }
      // 创建标签
      if (!namespace) {
        ele = document.createElement(name)
      } else {
        ele = document.createElementNS(namespace, name)
      }
      // 设置属性
      for (const item in node.attrs) {
        ele.setAttribute(item, node.attrs[item])
      }
      // 递归创建子节点
      if (node.children) {
        createDom(node.children, ele, namespace)
      }
 
      // 处理图片
      if (name === 'img') {
        window.unloadimgs += 1
        ele.onload = checkReady
        ele.onerror = checkReady
        if (!ele.src && ele.getAttribute('data-src')) {
          ele.src = ele.getAttribute('data-src')
        }
        if (!node.attrs.ignore) {
          // 监听图片点击事件
          ele.onclick = function (e) {
            e.stopPropagation()
            uni.postMessage({
              data: {
                action: 'onImgTap',
                attrs: getAttrs(this)
              }
            })
          }
        }
        if (options[2]) {
          const image = new Image()
          image.src = ele.src
          ele.src = options[2]
          image.onload = function () {
            ele.src = this.src
          }
          image.onerror = function () {
            ele.onerror()
          }
        }
        ele.onerror = onImgError
      } else if (name === 'a') {
        // 处理链接
        ele.addEventListener('click', function (e) {
          e.stopPropagation()
          e.preventDefault() // 阻止默认跳转
          const href = this.getAttribute('href')
          let offset
          if (href && href[0] === '#') {
            offset = (document.getElementById(href.substr(1)) || {}).offsetTop
          }
          uni.postMessage({
            data: {
              action: 'onLinkTap',
              attrs: getAttrs(this),
              offset
            }
          })
        }, true)
      } else if (name === 'video' || name === 'audio') {
        // 处理音视频
        medias.push(ele)
        if (!node.attrs.autoplay && !node.attrs.controls) {
          ele.setAttribute('controls', 'true')
        }
        ele.onplay = function () {
          uni.postMessage({
            data: {
              action: 'onPlay'
            }
          })
          if (options[3]) {
            for (let i = 0; i < medias.length; i++) {
              if (medias[i] !== this) {
                medias[i].pause()
              }
            }
          }
        }
        ele.onerror = function () {
          uni.postMessage({
            data: {
              action: 'onError',
              source: name,
              attrs: getAttrs(this)
            }
          })
        }
      } else if (name === 'table' && options[4] && !ele.style.cssText.includes('inline')) {
        // 处理表格
        const div = document.createElement('div')
        div.style.overflow = 'auto'
        div.appendChild(ele)
        ele = div
      } else if (name === 'svg') {
        namespace = undefined
      }
    } else {
      ele = document.createTextNode(node.text.replace(/&amp;/g, '&'))
    }
    parent.appendChild(ele)
  }
}
 
// 设置 html 内容
window.setContent = function (nodes, opts, append) {
  const ele = document.getElementById('content')
 
  // 容器样式
  if (opts[0]) {
    document.body.style.cssText = opts[0]
  }
 
  // 长按复制
  if (!opts[5]) {
    ele.style.userSelect = 'none'
  }
 
  if (!append) {
    ele.innerHTML = '' // 不追加则先清空
    medias = []
  }
 
  options = opts
  window.unloadimgs = 0
  const fragment = document.createDocumentFragment()
  createDom(nodes, fragment)
  ele.appendChild(fragment)
 
  // 触发事件
  let height = ele.scrollHeight
  uni.postMessage({
    data: {
      action: 'onLoad',
      height
    }
  })
  if (!window.unloadimgs) {
    uni.postMessage({
      data: {
        action: 'onReady',
        height
      }
    })
  }
 
  clearInterval(window.timer)
  window.timer = setInterval(() => {
    if (ele.scrollHeight !== height) {
      height = ele.scrollHeight
      uni.postMessage({
        data: {
          action: 'onHeightChange',
          height: height
        }
      })
    }
  }, 350)
}
 
// 回收计时器
window.onunload = function () {
  clearInterval(window.timer)
}