'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
import { updateElementStyle } from 'uni-shared'
import MIMEType from './MIMEType'
import { interact } from 'uni-mixins'
 
interact.addInteractListener()
 
const ALL = '*'
 
function isWXEnv () {
  const ua = window.navigator.userAgent.toLowerCase()
  if (ua.match(/MicroMessenger/i) && ua.match(/MicroMessenger/i)[0] === 'micromessenger') {
    return true
  } else {
    return false
  }
}
 
export default function ({ count, sourceType, type, extension }) {
  const inputEl = document.createElement('input')
  inputEl.type = 'file'
 
  updateElementStyle(inputEl, {
    position: 'absolute',
    visibility: 'hidden',
    'z-index': -999,
    width: 0,
    height: 0,
    top: 0,
    left: 0
  })
 
  /**
   * 选择文件
   * chooseFile 使用后缀名
   * chooseImage、chooseVideo 使用MIME类型
   */
  inputEl.accept = extension.map(item => {
    if (type !== ALL) {
      const MIMEKey = item.replace('.', '')
      return `${type}/${MIMEType[type][MIMEKey] || MIMEKey}`
    } else {
      // 在微信环境里,'.jpeg,.png' 会提示没有应用可执行此操作
      if (isWXEnv()) {
        return '.'
      }
      return item.indexOf('.') === 0 ? item : `.${item}`
    }
  }).join(',')
 
  if (count > 1) {
    inputEl.multiple = 'multiple'
  }
 
  // 经过测试,仅能限制只通过相机拍摄,不能限制只允许从相册选择。
  if (sourceType.length === 1 && sourceType[0] === 'camera') {
    inputEl.capture = 'camera'
  }
 
  return inputEl
}