choose-image.js 2.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import {
  TEMP_PATH
} from '../constants'

import {
  invoke
} from '../../bridge'

Q
qiang 已提交
9
import {
D
DCloud_LXH 已提交
10
  warpPlusErrorCallback
Q
qiang 已提交
11 12
} from '../util'

13 14 15 16
import {
  t
} from 'uni-core/helpers/i18n'

fxy060608's avatar
fxy060608 已提交
17 18 19 20 21
/**
 * 获取文件信息
 * @param {string} filePath 文件路径
 * @returns {Promise} 文件信息Promise
 */
D
DCloud_LXH 已提交
22
function getFileInfo(filePath) {
fxy060608's avatar
fxy060608 已提交
23 24
  return new Promise((resolve, reject) => {
    plus.io.resolveLocalFileSystemURL(filePath, function (entry) {
Q
qiang 已提交
25
      entry.getMetadata(resolve, reject, false)
fxy060608's avatar
fxy060608 已提交
26 27 28 29
    }, reject)
  })
}

D
DCloud_LXH 已提交
30
export function chooseImage({
Q
qiang 已提交
31 32
  count,
  sizeType,
Q
qiang 已提交
33 34
  sourceType,
  crop
Q
qiang 已提交
35 36 37
} = {}, callbackId) {
  const errorCallback = warpPlusErrorCallback(callbackId, 'chooseImage', 'cancel')

D
DCloud_LXH 已提交
38
  function successCallback(paths) {
Q
qiang 已提交
39 40 41
    const tempFiles = []
    const tempFilePaths = []
    // plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行
D
DCloud_LXH 已提交
42 43 44 45 46 47 48 49 50 51 52 53
    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
fxy060608's avatar
fxy060608 已提交
54 55
        })
      })
D
DCloud_LXH 已提交
56
      .catch(errorCallback)
Q
qiang 已提交
57 58
  }

D
DCloud_LXH 已提交
59
  function openCamera() {
Q
qiang 已提交
60 61 62
    const camera = plus.camera.getCamera()
    camera.captureImage(path => successCallback([path]),
      errorCallback, {
D
DCloud_LXH 已提交
63 64 65 66
      filename: TEMP_PATH + '/camera/',
      resolution: 'high',
      crop
    })
Q
qiang 已提交
67 68
  }

D
DCloud_LXH 已提交
69
  function openAlbum() {
Q
qiang 已提交
70 71 72 73
    plus.gallery.pick(({ files }) => successCallback(files), errorCallback, {
      maximum: count,
      multiple: true,
      system: false,
74
      filename: TEMP_PATH + '/gallery/',
Q
qiang 已提交
75
      permissionAlert: true,
D
DCloud_LXH 已提交
76 77
      crop,
      sizeType
fxy060608's avatar
fxy060608 已提交
78
    })
Q
qiang 已提交
79
  }
fxy060608's avatar
fxy060608 已提交
80 81

  if (sourceType.length === 1) {
Q
qiang 已提交
82 83 84 85 86 87
    if (sourceType.includes('album')) {
      openAlbum()
      return
    } else if (sourceType.includes('camera')) {
      openCamera()
      return
fxy060608's avatar
fxy060608 已提交
88 89
    }
  }
Q
qiang 已提交
90
  plus.nativeUI.actionSheet({
91
    cancel: t('uni.chooseImage.cancel'),
Q
qiang 已提交
92
    buttons: [{
93
      title: t('uni.chooseImage.sourceType.camera')
Q
qiang 已提交
94
    }, {
95
      title: t('uni.chooseImage.sourceType.album')
Q
qiang 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    }]
  }, (e) => {
    switch (e.index) {
      case 1:
        openCamera()
        break
      case 2:
        openAlbum()
        break
      default:
        errorCallback()
        break
    }
  })
fxy060608's avatar
fxy060608 已提交
110
}