choose-image.js 2.4 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 {
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
 */
Q
qiang 已提交
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)
  })
}

Q
qiang 已提交
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')

Q
qiang 已提交
38
  function successCallback (paths) {
Q
qiang 已提交
39 40
    const tempFiles = []
    const tempFilePaths = []
D
DCloud_LXH 已提交
41 42 43 44 45 46 47 48 49 50 51 52
    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 已提交
53 54
        })
      })
D
DCloud_LXH 已提交
55
      .catch(errorCallback)
Q
qiang 已提交
56 57
  }

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

Q
qiang 已提交
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
}