123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- 'use strict';
- const ERR_MSG_OK = 'chooseAndUploadFile:ok';
- const ERR_MSG_FAIL = 'chooseAndUploadFile:fail';
- function chooseImage(opts) {
- const {
- count,
- sizeType = ['original', 'compressed'],
- sourceType,
- extension
- } = opts
- return new Promise((resolve, reject) => {
- // 微信由于旧接口不再维护,针对微信小程序平台改用chooseMedia接口
- // #ifdef MP-WEIXIN
- uni.chooseMedia({
- count,
- sizeType,
- sourceType,
- mediaType: ['image'],
- extension,
- success(res) {
- res.tempFiles.forEach(item => {
- item.path = item.tempFilePath;
- })
- resolve(normalizeChooseAndUploadFileRes(res, 'image'));
- },
- fail(res) {
- reject({
- errMsg: res.errMsg.replace('chooseImage:fail', ERR_MSG_FAIL),
- });
- },
- })
- // #endif
- // #ifndef MP-WEIXIN
- uni.chooseImage({
- count,
- sizeType,
- sourceType,
- extension,
- success(res) {
- resolve(normalizeChooseAndUploadFileRes(res, 'image'));
- },
- fail(res) {
- reject({
- errMsg: res.errMsg.replace('chooseImage:fail', ERR_MSG_FAIL),
- });
- },
- });
- // #endif
- });
- }
- function chooseVideo(opts) {
- const {
- count,
- camera,
- compressed,
- maxDuration,
- sourceType,
- extension
- } = opts;
- return new Promise((resolve, reject) => {
- // 微信由于旧接口不再维护,针对微信小程序平台改用chooseMedia接口
- // #ifdef MP-WEIXIN
- uni.chooseMedia({
- count,
- compressed,
- maxDuration,
- sourceType,
- extension,
- mediaType: ['video'],
- success(res) {
- const {
- tempFiles,
- } = res;
- resolve(normalizeChooseAndUploadFileRes({
- errMsg: 'chooseVideo:ok',
- tempFiles: tempFiles.map(item => {
- return {
- name: item.name || '',
- path: item.tempFilePath,
- thumbTempFilePath: item.thumbTempFilePath,
- size:item.size,
- type: (res.tempFile && res.tempFile.type) || '',
- width:item.width,
- height:item.height,
- duration:item.duration,
- fileType: 'video',
- cloudPath: '',
- }
- }),
- }, 'video'));
- },
- fail(res) {
- reject({
- errMsg: res.errMsg.replace('chooseVideo:fail', ERR_MSG_FAIL),
- });
- },
- })
- // #endif
- // #ifndef MP-WEIXIN
- uni.chooseVideo({
- camera,
- compressed,
- maxDuration,
- sourceType,
- extension,
- success(res) {
- const {
- tempFilePath,
- duration,
- size,
- height,
- width
- } = res;
- resolve(normalizeChooseAndUploadFileRes({
- errMsg: 'chooseVideo:ok',
- tempFilePaths: [tempFilePath],
- tempFiles: [{
- name: (res.tempFile && res.tempFile.name) || '',
- path: tempFilePath,
- size,
- type: (res.tempFile && res.tempFile.type) || '',
- width,
- height,
- duration,
- fileType: 'video',
- cloudPath: '',
- }, ],
- }, 'video'));
- },
- fail(res) {
- reject({
- errMsg: res.errMsg.replace('chooseVideo:fail', ERR_MSG_FAIL),
- });
- },
- });
- // #endif
- });
- }
- function chooseAll(opts) {
- const {
- count,
- extension
- } = opts;
- return new Promise((resolve, reject) => {
- let chooseFile = uni.chooseFile;
- if (typeof wx !== 'undefined' &&
- typeof wx.chooseMessageFile === 'function') {
- chooseFile = wx.chooseMessageFile;
- }
- if (typeof chooseFile !== 'function') {
- return reject({
- errMsg: ERR_MSG_FAIL + ' 请指定 type 类型,该平台仅支持选择 image 或 video。',
- });
- }
- chooseFile({
- type: 'all',
- count,
- extension,
- success(res) {
- resolve(normalizeChooseAndUploadFileRes(res));
- },
- fail(res) {
- reject({
- errMsg: res.errMsg.replace('chooseFile:fail', ERR_MSG_FAIL),
- });
- },
- });
- });
- }
- function normalizeChooseAndUploadFileRes(res, fileType) {
- res.tempFiles.forEach((item, index) => {
- if (!item.name) {
- item.name = item.path.substring(item.path.lastIndexOf('/') + 1);
- }
- if (fileType) {
- item.fileType = fileType;
- }
- item.cloudPath =
- Date.now() + '_' + index + item.name.substring(item.name.lastIndexOf('.'));
- });
- if (!res.tempFilePaths) {
- res.tempFilePaths = res.tempFiles.map((file) => file.path);
- }
- return res;
- }
- function uploadCloudFiles(files, max = 5, onUploadProgress) {
- files = JSON.parse(JSON.stringify(files))
- const len = files.length
- let count = 0
- let self = this
- return new Promise(resolve => {
- while (count < max) {
- next()
- }
- function next() {
- let cur = count++
- if (cur >= len) {
- !files.find(item => !item.url && !item.errMsg) && resolve(files)
- return
- }
- const fileItem = files[cur]
- const index = self.files.findIndex(v => v.uuid === fileItem.uuid)
- fileItem.url = ''
- delete fileItem.errMsg
- uniCloud
- .uploadFile({
- filePath: fileItem.path,
- cloudPath: fileItem.cloudPath,
- fileType: fileItem.fileType,
- onUploadProgress: res => {
- res.index = index
- onUploadProgress && onUploadProgress(res)
- }
- })
- .then(res => {
- fileItem.url = res.fileID
- fileItem.index = index
- if (cur < len) {
- next()
- }
- })
- .catch(res => {
- fileItem.errMsg = res.errMsg || res.message
- fileItem.index = index
- if (cur < len) {
- next()
- }
- })
- }
- })
- }
- function uploadFiles(choosePromise, {
- onChooseFile,
- onUploadProgress
- }) {
- return choosePromise
- .then((res) => {
- if (onChooseFile) {
- const customChooseRes = onChooseFile(res);
- if (typeof customChooseRes !== 'undefined') {
- return Promise.resolve(customChooseRes).then((chooseRes) => typeof chooseRes === 'undefined' ?
- res : chooseRes);
- }
- }
- return res;
- })
- .then((res) => {
- if (res === false) {
- return {
- errMsg: ERR_MSG_OK,
- tempFilePaths: [],
- tempFiles: [],
- };
- }
- return res
- })
- }
- function chooseAndUploadFile(opts = {
- type: 'all'
- }) {
- if (opts.type === 'image') {
- return uploadFiles(chooseImage(opts), opts);
- } else if (opts.type === 'video') {
- return uploadFiles(chooseVideo(opts), opts);
- }
- return uploadFiles(chooseAll(opts), opts);
- }
- export {
- chooseAndUploadFile,
- uploadCloudFiles
- };
|