'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
import {
  TEMP_PATH
} from '../constants'
 
import {
  invoke
} from '../../bridge'
 
import {
  warpPlusErrorCallback
} from '../util'
 
import {
  t
} from 'uni-core/helpers/i18n'
 
/**
 * 获取文件信息
 * @param {string} filePath 文件路径
 * @returns {Promise} 文件信息Promise
 */
function getFileInfo (filePath) {
  return new Promise((resolve, reject) => {
    plus.io.resolveLocalFileSystemURL(filePath, function (entry) {
      entry.getMetadata(resolve, reject, false)
    }, reject)
  })
}
 
export function chooseImage ({
  count,
  sizeType,
  sourceType,
  crop
} = {}, callbackId) {
  const errorCallback = warpPlusErrorCallback(callbackId, 'chooseImage', 'cancel')
 
  function successCallback (paths) {
    const tempFiles = []
    const tempFilePaths = []
    Promise.all(paths.map((path) => getFileInfo(path)))
      .then((filesInfo) => {
        filesInfo.forEach((file, index) => {
          const path = paths[index]
          tempFilePaths.push(path)
          tempFiles.push({ path, size: file.size })
        })
 
        invoke(callbackId, {
          errMsg: 'chooseImage:ok',
          tempFilePaths,
          tempFiles
        })
      })
      .catch(errorCallback)
  }
 
  function openCamera () {
    const camera = plus.camera.getCamera()
    camera.captureImage(path => successCallback([path]),
      errorCallback, {
        filename: TEMP_PATH + '/camera/',
        resolution: 'high',
        crop,
        sizeType
      })
  }
 
  function openAlbum () {
    plus.gallery.pick(({ files }) => successCallback(files), errorCallback, {
      maximum: count,
      multiple: true,
      system: false,
      filename: TEMP_PATH + '/gallery/',
      permissionAlert: true,
      crop,
      sizeType
    })
  }
 
  if (sourceType.length === 1) {
    if (sourceType.includes('album')) {
      openAlbum()
      return
    } else if (sourceType.includes('camera')) {
      openCamera()
      return
    }
  }
  plus.nativeUI.actionSheet({
    cancel: t('uni.chooseImage.cancel'),
    buttons: [{
      title: t('uni.chooseImage.sourceType.camera')
    }, {
      title: t('uni.chooseImage.sourceType.album')
    }]
  }, (e) => {
    switch (e.index) {
      case 1:
        openCamera()
        break
      case 2:
        openAlbum()
        break
      default:
        errorCallback()
        break
    }
  })
}