choose-image.js 3.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 10 11 12 13
import {
  warpPlusErrorCallback,
  getFileName
} from '../util'

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

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

Q
qiang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44
function compressImage (tempFilePath) {
  const dstPath = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName(tempFilePath)}`
  return new Promise((resolve, reject) => {
    plus.nativeUI.showWaiting()
    plus.zip.compressImage({
      src: tempFilePath,
      dst: dstPath,
      overwrite: true
    }, () => {
      plus.nativeUI.closeWaiting()
      resolve(dstPath)
    }, (error) => {
      plus.nativeUI.closeWaiting()
      reject(error)
fxy060608's avatar
fxy060608 已提交
45
    })
Q
qiang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  })
}

export function chooseImage ({
  count,
  sizeType,
  sourceType
} = {}, callbackId) {
  const errorCallback = warpPlusErrorCallback(callbackId, 'chooseImage', 'cancel')

  function successCallback (paths) {
    const tempFiles = []
    const tempFilePaths = []
    // plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行
    paths.reduce((promise, path) => {
      return promise.then(() => {
        return getFileInfo(path)
      }).then(fileInfo => {
        const size = fileInfo.size
fxy060608's avatar
fxy060608 已提交
65
        // 压缩阈值 0.5 兆
Q
qiang 已提交
66
        const THRESHOLD = 1024 * 1024 * 0.5
fxy060608's avatar
fxy060608 已提交
67
        // 判断是否需要压缩
Q
qiang 已提交
68 69 70 71
        if (sizeType.includes('compressed') && size > THRESHOLD) {
          return compressImage(path).then(dstPath => {
            path = dstPath
            return getFileInfo(path)
fxy060608's avatar
fxy060608 已提交
72 73
          })
        }
Q
qiang 已提交
74 75 76 77 78 79
        return fileInfo
      }).then(({ size }) => {
        tempFilePaths.push(path)
        tempFiles.push({
          path,
          size
fxy060608's avatar
fxy060608 已提交
80 81
        })
      })
Q
qiang 已提交
82
    }, Promise.resolve()).then(() => {
fxy060608's avatar
fxy060608 已提交
83
      invoke(callbackId, {
Q
qiang 已提交
84
        errMsg: 'chooseImage:ok',
fxy060608's avatar
fxy060608 已提交
85 86 87
        tempFilePaths,
        tempFiles
      })
Q
qiang 已提交
88 89 90 91 92 93 94 95 96
    }).catch(errorCallback)
  }

  function openCamera () {
    const camera = plus.camera.getCamera()
    camera.captureImage(path => successCallback([path]),
      errorCallback, {
        filename: TEMP_PATH + '/camera/',
        resolution: 'high'
fxy060608's avatar
fxy060608 已提交
97
      })
Q
qiang 已提交
98 99 100 101 102 103 104
  }

  function openAlbum () {
    plus.gallery.pick(({ files }) => successCallback(files), errorCallback, {
      maximum: count,
      multiple: true,
      system: false,
105 106
      filename: TEMP_PATH + '/gallery/',
      permissionAlert: true
fxy060608's avatar
fxy060608 已提交
107
    })
Q
qiang 已提交
108
  }
fxy060608's avatar
fxy060608 已提交
109 110

  if (sourceType.length === 1) {
Q
qiang 已提交
111 112 113 114 115 116
    if (sourceType.includes('album')) {
      openAlbum()
      return
    } else if (sourceType.includes('camera')) {
      openCamera()
      return
fxy060608's avatar
fxy060608 已提交
117 118
    }
  }
Q
qiang 已提交
119
  plus.nativeUI.actionSheet({
120
    cancel: t('uni.chooseImage.cancel'),
Q
qiang 已提交
121
    buttons: [{
122
      title: t('uni.chooseImage.sourceType.camera')
Q
qiang 已提交
123
    }, {
124
      title: t('uni.chooseImage.sourceType.album')
Q
qiang 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
    }]
  }, (e) => {
    switch (e.index) {
      case 1:
        openCamera()
        break
      case 2:
        openAlbum()
        break
      default:
        errorCallback()
        break
    }
  })
fxy060608's avatar
fxy060608 已提交
139
}