¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view class="uni-file-picker"> |
| | | <view v-if="title" class="uni-file-picker__header"> |
| | | <text class="file-title">{{ title }}</text> |
| | | <text class="file-count">{{ filesList.length }}/{{ limitLength }}</text> |
| | | </view> |
| | | <upload-image v-if="fileMediatype === 'image' && showType === 'grid'" :readonly="readonly" |
| | | :image-styles="imageStyles" :files-list="filesList" :limit="limitLength" :disablePreview="disablePreview" |
| | | :delIcon="delIcon" @uploadFiles="uploadFiles" @choose="choose" @delFile="delFile"> |
| | | <slot> |
| | | <view class="is-add"> |
| | | <view class="icon-add"></view> |
| | | <view class="icon-add rotate"></view> |
| | | </view> |
| | | </slot> |
| | | </upload-image> |
| | | <upload-file v-if="fileMediatype !== 'image' || showType !== 'grid'" :readonly="readonly" |
| | | :list-styles="listStyles" :files-list="filesList" :showType="showType" :delIcon="delIcon" |
| | | @uploadFiles="uploadFiles" @choose="choose" @delFile="delFile"> |
| | | <slot><button type="primary" size="mini">éæ©æä»¶</button></slot> |
| | | </upload-file> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import { |
| | | chooseAndUploadFile, |
| | | uploadCloudFiles |
| | | } from './choose-and-upload-file.js' |
| | | import { |
| | | get_file_ext, |
| | | get_extname, |
| | | get_files_and_is_max, |
| | | get_file_info, |
| | | get_file_data |
| | | } from './utils.js' |
| | | import uploadImage from './upload-image.vue' |
| | | import uploadFile from './upload-file.vue' |
| | | let fileInput = null |
| | | /** |
| | | * FilePicker æä»¶éæ©ä¸ä¼ |
| | | * @description æä»¶éæ©ä¸ä¼ ç»ä»¶ï¼å¯ä»¥éæ©å¾çãè§é¢çä»»ææä»¶å¹¶ä¸ä¼ å°å½åç»å®çæå¡ç©ºé´ |
| | | * @tutorial https://ext.dcloud.net.cn/plugin?id=4079 |
| | | * @property {Object|Array} value ç»ä»¶æ°æ®ï¼éå¸¸ç¨æ¥åæ¾ ,ç±»åç±return-type屿§å³å® |
| | | * @property {Boolean} disabled = [true|false] ç»ä»¶ç¦ç¨ |
| | | * @value true ç¦ç¨ |
| | | * @value false åæ¶ç¦ç¨ |
| | | * @property {Boolean} readonly = [true|false] ç»ä»¶åªè¯»ï¼ä¸å¯éæ©ï¼ä¸æ¾ç¤ºè¿åº¦ï¼ä¸æ¾ç¤ºå é¤æé® |
| | | * @value true åªè¯» |
| | | * @value false åæ¶åªè¯» |
| | | * @property {String} return-type = [array|object] éå¶ value æ ¼å¼ï¼å½ä¸º object æ¶ ï¼ç»ä»¶åªè½åéï¼ä¸ä¼è¦ç |
| | | * @value array è§å® value 屿§çç±»å为æ°ç» |
| | | * @value object è§å® value 屿§çç±»å为对象 |
| | | * @property {Boolean} disable-preview = [true|false] ç¦ç¨å¾çé¢è§ï¼ä»
mode:grid æ¶çæ |
| | | * @value true ç¦ç¨å¾çé¢è§ |
| | | * @value false åæ¶ç¦ç¨å¾çé¢è§ |
| | | * @property {Boolean} del-icon = [true|false] æ¯å¦æ¾ç¤ºå é¤æé® |
| | | * @value true æ¾ç¤ºå é¤æé® |
| | | * @value false 䏿¾ç¤ºå é¤æé® |
| | | * @property {Boolean} auto-upload = [true|false] æ¯å¦èªå¨ä¸ä¼ ï¼å¼ä¸ºtrueååªè§¦å@select,å¯èªè¡ä¸ä¼ |
| | | * @value true èªå¨ä¸ä¼ |
| | | * @value false åæ¶èªå¨ä¸ä¼ |
| | | * @property {Number|String} limit æå¤§éæ©ä¸ªæ° ï¼h5 ä¼èªå¨å¿½ç¥å¤éçé¨å |
| | | * @property {String} title ç»ä»¶æ é¢ï¼å³ä¾§æ¾ç¤ºä¸ä¼ è®¡æ° |
| | | * @property {String} mode = [list|grid] éæ©æä»¶åçæä»¶åè¡¨æ ·å¼ |
| | | * @value list å表æ¾ç¤º |
| | | * @value grid å®«æ ¼æ¾ç¤º |
| | | * @property {String} file-mediatype = [image|video|all] éæ©æä»¶ç±»å |
| | | * @value image åªéæ©å¾ç |
| | | * @value video åªéæ©è§é¢ |
| | | * @value all éæ©æææä»¶ |
| | | * @property {Array} file-extname éæ©æä»¶åç¼ï¼æ ¹æ® file-mediatype 屿§èä¸å |
| | | * @property {Object} list-style mode:list æ¶çæ ·å¼ |
| | | * @property {Object} image-styles éæ©æä»¶åç¼ï¼æ ¹æ® file-mediatype 屿§èä¸å |
| | | * @event {Function} select éæ©æä»¶å触å |
| | | * @event {Function} progress æä»¶ä¸ä¼ æ¶è§¦å |
| | | * @event {Function} success ä¸ä¼ æå触å |
| | | * @event {Function} fail ä¸ä¼ 失败触å |
| | | * @event {Function} delete æä»¶ä»åè¡¨ç§»é¤æ¶è§¦å |
| | | */ |
| | | export default { |
| | | name: 'uniFilePicker', |
| | | components: { |
| | | uploadImage, |
| | | uploadFile |
| | | }, |
| | | options: { |
| | | virtualHost: true |
| | | }, |
| | | emits: ['select', 'success', 'fail', 'progress', 'delete', 'update:modelValue', 'input'], |
| | | props: { |
| | | // #ifdef VUE3 |
| | | modelValue: { |
| | | type: [Array, Object], |
| | | default () { |
| | | return [] |
| | | } |
| | | }, |
| | | // #endif |
| | | |
| | | // #ifndef VUE3 |
| | | value: { |
| | | type: [Array, Object], |
| | | default () { |
| | | return [] |
| | | } |
| | | }, |
| | | // #endif |
| | | |
| | | disabled: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | disablePreview: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | delIcon: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | // èªå¨ä¸ä¼ |
| | | autoUpload: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | // æå¤§éæ©ä¸ªæ° ï¼h5åªè½éå¶åéææ¯å¤é |
| | | limit: { |
| | | type: [Number, String], |
| | | default: 9 |
| | | }, |
| | | // åè¡¨æ ·å¼ grid | list | list-card |
| | | mode: { |
| | | type: String, |
| | | default: 'grid' |
| | | }, |
| | | // éæ©æä»¶ç±»å image/video/all |
| | | fileMediatype: { |
| | | type: String, |
| | | default: 'image' |
| | | }, |
| | | // æä»¶ç±»åçé |
| | | fileExtname: { |
| | | type: [Array, String], |
| | | default () { |
| | | return [] |
| | | } |
| | | }, |
| | | title: { |
| | | type: String, |
| | | default: '' |
| | | }, |
| | | listStyles: { |
| | | type: Object, |
| | | default () { |
| | | return { |
| | | // æ¯å¦æ¾ç¤ºè¾¹æ¡ |
| | | border: true, |
| | | // æ¯å¦æ¾ç¤ºåé线 |
| | | dividline: true, |
| | | // çº¿æ¡æ ·å¼ |
| | | borderStyle: {} |
| | | } |
| | | } |
| | | }, |
| | | imageStyles: { |
| | | type: Object, |
| | | default () { |
| | | return { |
| | | width: 'auto', |
| | | height: 'auto' |
| | | } |
| | | } |
| | | }, |
| | | readonly: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | returnType: { |
| | | type: String, |
| | | default: 'array' |
| | | }, |
| | | sizeType: { |
| | | type: Array, |
| | | default () { |
| | | return ['original', 'compressed'] |
| | | } |
| | | } |
| | | }, |
| | | data() { |
| | | return { |
| | | files: [], |
| | | localValue: [] |
| | | } |
| | | }, |
| | | watch: { |
| | | // #ifndef VUE3 |
| | | value: { |
| | | handler(newVal, oldVal) { |
| | | this.setValue(newVal, oldVal) |
| | | }, |
| | | immediate: true |
| | | }, |
| | | // #endif |
| | | // #ifdef VUE3 |
| | | modelValue: { |
| | | handler(newVal, oldVal) { |
| | | this.setValue(newVal, oldVal) |
| | | }, |
| | | immediate: true |
| | | }, |
| | | // #endif |
| | | }, |
| | | computed: { |
| | | filesList() { |
| | | let files = [] |
| | | this.files.forEach(v => { |
| | | files.push(v) |
| | | }) |
| | | return files |
| | | }, |
| | | showType() { |
| | | if (this.fileMediatype === 'image') { |
| | | return this.mode |
| | | } |
| | | return 'list' |
| | | }, |
| | | limitLength() { |
| | | if (this.returnType === 'object') { |
| | | return 1 |
| | | } |
| | | if (!this.limit) { |
| | | return 1 |
| | | } |
| | | if (this.limit >= 9) { |
| | | return 9 |
| | | } |
| | | return this.limit |
| | | } |
| | | }, |
| | | created() { |
| | | // TODO å
¼å®¹ä¸å¼éæå¡ç©ºé´çæ
åµ |
| | | if (!(uniCloud.config && uniCloud.config.provider)) { |
| | | this.noSpace = true |
| | | uniCloud.chooseAndUploadFile = chooseAndUploadFile |
| | | } |
| | | this.form = this.getForm('uniForms') |
| | | this.formItem = this.getForm('uniFormsItem') |
| | | if (this.form && this.formItem) { |
| | | if (this.formItem.name) { |
| | | this.rename = this.formItem.name |
| | | this.form.inputChildrens.push(this) |
| | | } |
| | | } |
| | | }, |
| | | methods: { |
| | | /** |
| | | * å
¬å¼ç¨æ·ä½¿ç¨ï¼æ¸
空æä»¶ |
| | | * @param {Object} index |
| | | */ |
| | | clearFiles(index) { |
| | | if (index !== 0 && !index) { |
| | | this.files = [] |
| | | this.$nextTick(() => { |
| | | this.setEmit() |
| | | }) |
| | | } else { |
| | | this.files.splice(index, 1) |
| | | } |
| | | this.$nextTick(() => { |
| | | this.setEmit() |
| | | }) |
| | | }, |
| | | /** |
| | | * å
¬å¼ç¨æ·ä½¿ç¨ï¼ç»§ç»ä¸ä¼ |
| | | */ |
| | | upload() { |
| | | let files = [] |
| | | this.files.forEach((v, index) => { |
| | | if (v.status === 'ready' || v.status === 'error') { |
| | | files.push(Object.assign({}, v)) |
| | | } |
| | | }) |
| | | return this.uploadFiles(files) |
| | | }, |
| | | async setValue(newVal, oldVal) { |
| | | const newData = async (v) => { |
| | | const reg = /cloud:\/\/([\w.]+\/?)\S*/ |
| | | let url = '' |
| | | if(v.fileID){ |
| | | url = v.fileID |
| | | }else{ |
| | | url = v.url |
| | | } |
| | | if (reg.test(url)) { |
| | | v.fileID = url |
| | | v.url = await this.getTempFileURL(url) |
| | | } |
| | | if(v.url) v.path = v.url |
| | | return v |
| | | } |
| | | if (this.returnType === 'object') { |
| | | if (newVal) { |
| | | await newData(newVal) |
| | | } else { |
| | | newVal = {} |
| | | } |
| | | } else { |
| | | if (!newVal) newVal = [] |
| | | for(let i =0 ;i < newVal.length ;i++){ |
| | | let v = newVal[i] |
| | | await newData(v) |
| | | } |
| | | } |
| | | this.localValue = newVal |
| | | if (this.form && this.formItem &&!this.is_reset) { |
| | | this.is_reset = false |
| | | this.formItem.setValue(this.localValue) |
| | | } |
| | | let filesData = Object.keys(newVal).length > 0 ? newVal : []; |
| | | this.files = [].concat(filesData) |
| | | }, |
| | | |
| | | /** |
| | | * éæ©æä»¶ |
| | | */ |
| | | choose() { |
| | | |
| | | if (this.disabled) return |
| | | if (this.files.length >= Number(this.limitLength) && this.showType !== 'grid' && this.returnType === |
| | | 'array') { |
| | | uni.showToast({ |
| | | title: `æ¨æå¤éæ© ${this.limitLength} 个æä»¶`, |
| | | icon: 'none' |
| | | }) |
| | | return |
| | | } |
| | | this.chooseFiles() |
| | | }, |
| | | |
| | | /** |
| | | * éæ©æä»¶å¹¶ä¸ä¼ |
| | | */ |
| | | chooseFiles() { |
| | | const _extname = get_extname(this.fileExtname) |
| | | // è·ååç¼ |
| | | uniCloud |
| | | .chooseAndUploadFile({ |
| | | type: this.fileMediatype, |
| | | compressed: false, |
| | | sizeType: this.sizeType, |
| | | // TODO å¦æä¸ºç©ºï¼video æé®é¢ |
| | | extension: _extname.length > 0 ? _extname : undefined, |
| | | count: this.limitLength - this.files.length, //é»è®¤9 |
| | | onChooseFile: this.chooseFileCallback, |
| | | onUploadProgress: progressEvent => { |
| | | this.setProgress(progressEvent, progressEvent.index) |
| | | } |
| | | }) |
| | | .then(result => { |
| | | this.setSuccessAndError(result.tempFiles) |
| | | }) |
| | | .catch(err => { |
| | | console.log('éæ©å¤±è´¥', err) |
| | | }) |
| | | }, |
| | | |
| | | /** |
| | | * éæ©æä»¶åè° |
| | | * @param {Object} res |
| | | */ |
| | | async chooseFileCallback(res) { |
| | | const _extname = get_extname(this.fileExtname) |
| | | const is_one = (Number(this.limitLength) === 1 && |
| | | this.disablePreview && |
| | | !this.disabled) || |
| | | this.returnType === 'object' |
| | | // å¦æè¿æä¸ä¸ªæä»¶ ï¼éè¦æ¸
空æ¬å°ç¼åæ°æ® |
| | | if (is_one) { |
| | | this.files = [] |
| | | } |
| | | |
| | | let { |
| | | filePaths, |
| | | files |
| | | } = get_files_and_is_max(res, _extname) |
| | | if (!(_extname && _extname.length > 0)) { |
| | | filePaths = res.tempFilePaths |
| | | files = res.tempFiles |
| | | } |
| | | |
| | | let currentData = [] |
| | | for (let i = 0; i < files.length; i++) { |
| | | if (this.limitLength - this.files.length <= 0) break |
| | | files[i].uuid = Date.now() |
| | | let filedata = await get_file_data(files[i], this.fileMediatype) |
| | | filedata.progress = 0 |
| | | filedata.status = 'ready' |
| | | this.files.push(filedata) |
| | | currentData.push({ |
| | | ...filedata, |
| | | file: files[i] |
| | | }) |
| | | } |
| | | this.$emit('select', { |
| | | tempFiles: currentData, |
| | | tempFilePaths: filePaths |
| | | }) |
| | | res.tempFiles = files |
| | | // 忢èªå¨ä¸ä¼ |
| | | if (!this.autoUpload || this.noSpace) { |
| | | res.tempFiles = [] |
| | | } |
| | | }, |
| | | |
| | | /** |
| | | * æ¹ä¼ |
| | | * @param {Object} e |
| | | */ |
| | | uploadFiles(files) { |
| | | files = [].concat(files) |
| | | return uploadCloudFiles.call(this, files, 5, res => { |
| | | this.setProgress(res, res.index, true) |
| | | }) |
| | | .then(result => { |
| | | this.setSuccessAndError(result) |
| | | return result; |
| | | }) |
| | | .catch(err => { |
| | | console.log(err) |
| | | }) |
| | | }, |
| | | |
| | | /** |
| | | * æåæå¤±è´¥ |
| | | */ |
| | | async setSuccessAndError(res, fn) { |
| | | let successData = [] |
| | | let errorData = [] |
| | | let tempFilePath = [] |
| | | let errorTempFilePath = [] |
| | | for (let i = 0; i < res.length; i++) { |
| | | const item = res[i] |
| | | const index = item.uuid ? this.files.findIndex(p => p.uuid === item.uuid) : item.index |
| | | |
| | | if (index === -1 || !this.files) break |
| | | if (item.errMsg === 'request:fail') { |
| | | this.files[index].url = item.path |
| | | this.files[index].status = 'error' |
| | | this.files[index].errMsg = item.errMsg |
| | | // this.files[index].progress = -1 |
| | | errorData.push(this.files[index]) |
| | | errorTempFilePath.push(this.files[index].url) |
| | | } else { |
| | | this.files[index].errMsg = '' |
| | | this.files[index].fileID = item.url |
| | | const reg = /cloud:\/\/([\w.]+\/?)\S*/ |
| | | if (reg.test(item.url)) { |
| | | this.files[index].url = await this.getTempFileURL(item.url) |
| | | }else{ |
| | | this.files[index].url = item.url |
| | | } |
| | | |
| | | this.files[index].status = 'success' |
| | | this.files[index].progress += 1 |
| | | successData.push(this.files[index]) |
| | | tempFilePath.push(this.files[index].fileID) |
| | | } |
| | | } |
| | | |
| | | if (successData.length > 0) { |
| | | this.setEmit() |
| | | // ç¶ææ¹åè¿å |
| | | this.$emit('success', { |
| | | tempFiles: this.backObject(successData), |
| | | tempFilePaths: tempFilePath |
| | | }) |
| | | } |
| | | |
| | | if (errorData.length > 0) { |
| | | this.$emit('fail', { |
| | | tempFiles: this.backObject(errorData), |
| | | tempFilePaths: errorTempFilePath |
| | | }) |
| | | } |
| | | }, |
| | | |
| | | /** |
| | | * è·åè¿åº¦ |
| | | * @param {Object} progressEvent |
| | | * @param {Object} index |
| | | * @param {Object} type |
| | | */ |
| | | setProgress(progressEvent, index, type) { |
| | | const fileLenth = this.files.length |
| | | const percentNum = (index / fileLenth) * 100 |
| | | const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total) |
| | | let idx = index |
| | | if (!type) { |
| | | idx = this.files.findIndex(p => p.uuid === progressEvent.tempFile.uuid) |
| | | } |
| | | if (idx === -1 || !this.files[idx]) return |
| | | // fix by mehaotian 100 就伿¶å¤±ï¼-1 æ¯ä¸ºäºè®©è¿åº¦æ¡æ¶å¤± |
| | | this.files[idx].progress = percentCompleted - 1 |
| | | // ä¸ä¼ ä¸ |
| | | this.$emit('progress', { |
| | | index: idx, |
| | | progress: parseInt(percentCompleted), |
| | | tempFile: this.files[idx] |
| | | }) |
| | | }, |
| | | |
| | | /** |
| | | * å 餿件 |
| | | * @param {Object} index |
| | | */ |
| | | delFile(index) { |
| | | this.$emit('delete', { |
| | | tempFile: this.files[index], |
| | | tempFilePath: this.files[index].url |
| | | }) |
| | | this.files.splice(index, 1) |
| | | this.$nextTick(() => { |
| | | this.setEmit() |
| | | }) |
| | | }, |
| | | |
| | | /** |
| | | * è·åæä»¶åååç¼ |
| | | * @param {Object} name |
| | | */ |
| | | getFileExt(name) { |
| | | const last_len = name.lastIndexOf('.') |
| | | const len = name.length |
| | | return { |
| | | name: name.substring(0, last_len), |
| | | ext: name.substring(last_len + 1, len) |
| | | } |
| | | }, |
| | | |
| | | /** |
| | | * å¤çè¿åäºä»¶ |
| | | */ |
| | | setEmit() { |
| | | let data = [] |
| | | if (this.returnType === 'object') { |
| | | data = this.backObject(this.files)[0] |
| | | this.localValue = data?data:null |
| | | } else { |
| | | data = this.backObject(this.files) |
| | | if (!this.localValue) { |
| | | this.localValue = [] |
| | | } |
| | | this.localValue = [...data] |
| | | } |
| | | // #ifdef VUE3 |
| | | this.$emit('update:modelValue', this.localValue) |
| | | // #endif |
| | | // #ifndef VUE3 |
| | | this.$emit('input', this.localValue) |
| | | // #endif |
| | | }, |
| | | |
| | | /** |
| | | * å¤çè¿ååæ° |
| | | * @param {Object} files |
| | | */ |
| | | backObject(files) { |
| | | let newFilesData = [] |
| | | files.forEach(v => { |
| | | newFilesData.push({ |
| | | extname: v.extname, |
| | | fileType: v.fileType, |
| | | image: v.image, |
| | | name: v.name, |
| | | path: v.path, |
| | | size: v.size, |
| | | fileID:v.fileID, |
| | | url: v.url |
| | | }) |
| | | }) |
| | | return newFilesData |
| | | }, |
| | | async getTempFileURL(fileList) { |
| | | fileList = { |
| | | fileList: [].concat(fileList) |
| | | } |
| | | const urls = await uniCloud.getTempFileURL(fileList) |
| | | return urls.fileList[0].tempFileURL || '' |
| | | }, |
| | | /** |
| | | * è·åç¶å
ç´ å®ä¾ |
| | | */ |
| | | getForm(name = 'uniForms') { |
| | | let parent = this.$parent; |
| | | let parentName = parent.$options.name; |
| | | while (parentName !== name) { |
| | | parent = parent.$parent; |
| | | if (!parent) return false; |
| | | parentName = parent.$options.name; |
| | | } |
| | | return parent; |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style> |
| | | .uni-file-picker { |
| | | /* #ifndef APP-NVUE */ |
| | | box-sizing: border-box; |
| | | overflow: hidden; |
| | | width: 100%; |
| | | /* #endif */ |
| | | flex: 1; |
| | | } |
| | | |
| | | .uni-file-picker__header { |
| | | padding-top: 5px; |
| | | padding-bottom: 10px; |
| | | /* #ifndef APP-NVUE */ |
| | | display: flex; |
| | | /* #endif */ |
| | | justify-content: space-between; |
| | | } |
| | | |
| | | .file-title { |
| | | font-size: 14px; |
| | | color: #333; |
| | | } |
| | | |
| | | .file-count { |
| | | font-size: 14px; |
| | | color: #999; |
| | | } |
| | | |
| | | .is-add { |
| | | /* #ifndef APP-NVUE */ |
| | | display: flex; |
| | | /* #endif */ |
| | | align-items: center; |
| | | justify-content: center; |
| | | } |
| | | |
| | | .icon-add { |
| | | width: 50px; |
| | | height: 5px; |
| | | background-color: #f1f1f1; |
| | | border-radius: 2px; |
| | | } |
| | | |
| | | .rotate { |
| | | position: absolute; |
| | | transform: rotate(90deg); |
| | | } |
| | | </style> |