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AC;IACL,CAAC;CACF,CAAA;AAlNoB,MAAM;IAD1B,WAAW,EAAE;GACO,MAAM,CAkN1B;eAlNoB,MAAM","file":"upload.js","sourcesContent":["import { isObject, SuperComponent, wxComponent } from '../common/src/index';\nimport props from './props';\nimport { UploadMpConfig, UploadFile } from './type';\nimport config from '../common/config';\n\nconst { prefix } = config;\nconst name = `${prefix}-upload`;\n@wxComponent()\nexport default class Upload extends SuperComponent {\n externalClasses = [`${prefix}-class`];\n\n options = {\n multipleSlots: true,\n };\n\n data = {\n classPrefix: name,\n prefix,\n current: false,\n proofs: [],\n customFiles: [] as UploadFile[], // 内部动态修改的files\n customLimit: 0, // 内部动态修改的limit\n // 以下是声明properties\n config: {} as UploadMpConfig,\n files: [] as UploadFile[],\n max: 0,\n sizeLimit: 0,\n requestMethod: null,\n gridItemStyle: '',\n column: 4,\n };\n\n properties = props;\n\n controlledProps = [\n {\n key: 'files',\n event: 'success',\n },\n ];\n\n observers = {\n files(files: UploadFile) {\n this.handleLimit(files, this.data.max);\n },\n max(max) {\n this.handleLimit(this.data.customFiles, max);\n },\n gridConfig() {\n this.updateGrid();\n },\n };\n\n onProofTap(e: any) {\n const { index } = e.currentTarget.dataset;\n wx.previewImage({\n urls: this.data.customFiles.filter((file) => file.percent !== -1).map((file) => file.url),\n current: this.data.customFiles[index]?.url,\n });\n }\n\n ready() {\n this.handleLimit(this.data.customFiles, this.data.max);\n this.updateGrid();\n }\n\n handleLimit(customFiles: UploadFile[], max: number) {\n while (max !== 0 && customFiles.length - max > 0) {\n customFiles.pop();\n }\n const proofs = [];\n customFiles.forEach((item: UploadFile) => {\n if (item.type !== 'video') {\n proofs.push(item.url);\n }\n });\n this.setData({\n customFiles,\n proofs,\n customLimit: max === 0 ? Number.MAX_SAFE_INTEGER : max - customFiles.length,\n });\n }\n\n uploadFiles(files: UploadFile[]) {\n return new Promise((resolve) => {\n // 开始调用上传函数\n const task = this.data.requestMethod(files);\n if (task instanceof Promise) {\n return task;\n }\n resolve({});\n });\n }\n\n startUpload(files: UploadFile[]) {\n // 如果传入了上传函数,则进度设为0并开始上传,否则跳过上传\n if (typeof this.data.requestMethod === 'function') {\n return this.uploadFiles(files);\n }\n this.handleLimit(this.data.customFiles, this.data.max);\n return Promise.resolve();\n }\n\n /** 选择媒体素材 */\n chooseMedia(mediaType) {\n const { config, sizeLimit, max } = this.data;\n wx.chooseMedia({\n count: max === 0 ? 9 : max, // 在 iOS 里,0 是无效的,会导致抛异常\n mediaType,\n ...config,\n success: (res) => {\n const files = [];\n res.tempFiles.forEach((temp) => {\n const { size, fileType, tempFilePath, width, height, duration, thumbTempFilePath, ...res } = temp;\n if (sizeLimit && size > sizeLimit) {\n wx.showToast({ icon: 'none', title: `${fileType === 'image' ? '图片' : '视频'}大小超过限制` });\n return;\n }\n const name = this.getRandFileName(tempFilePath);\n files.push({\n name,\n type: this.getFileType(mediaType, tempFilePath, fileType),\n url: tempFilePath,\n size: size,\n width: width,\n height: height,\n duration: duration,\n thumb: thumbTempFilePath,\n progress: 0,\n ...res,\n });\n });\n this._trigger('select-change', {\n files: [...this.data.customFiles],\n currentSelectedFiles: [files],\n });\n this._trigger('add', { files });\n this._trigger('success', { files: [...this.data.customFiles, ...files] });\n this.startUpload(files);\n },\n fail: (err) => {\n this.triggerEvent('fail', err);\n },\n complete: (res) => {\n this.triggerEvent('complete', res);\n },\n });\n }\n\n /**\n * 由于小程序暂时在ios上不支持返回上传文件的fileType,这里用文件的后缀来判断\n * @param mediaType\n * @param tempFilePath\n * @returns string\n * @link https://developers.weixin.qq.com/community/develop/doc/00042820b28ee8fb41fc4d0c254c00\n */\n getFileType(mediaType: string[], tempFilePath: string, fileType?: string) {\n if (fileType) return fileType; // 如果有返回fileType就直接用\n if (mediaType.length === 1) {\n // 在单选媒体类型的时候直接使用单选媒体类型\n return mediaType[0];\n }\n // 否则根据文件后缀进行判读\n const videoType = ['avi', 'wmv', 'mkv', 'mp4', 'mov', 'rm', '3gp', 'flv', 'mpg', 'rmvb'];\n const temp = tempFilePath.split('.');\n const postfix = temp[temp.length - 1];\n if (videoType.includes(postfix.toLocaleLowerCase())) {\n return 'video';\n }\n return 'image';\n }\n\n // 选中文件之后,计算一个随机的短文件名\n getRandFileName(filePath) {\n const extIndex = filePath.lastIndexOf('.');\n const extName = extIndex === -1 ? '' : filePath.substr(extIndex);\n return parseInt(`${Date.now()}${Math.floor(Math.random() * 900 + 100)}`, 10).toString(36) + extName;\n }\n\n closePop() {\n this.setData({ showPop: false });\n }\n\n onAddTap() {\n const { mediaType } = this.properties;\n this.chooseMedia(mediaType);\n }\n\n onChooseImage() {\n this.chooseImg();\n // this.setData({ showPop: false });\n }\n\n onChooseVideo() {\n this.chooseVideo();\n // this.setData({ showPop: false });\n }\n\n onDelete(e: any) {\n const { index } = e.currentTarget.dataset;\n this.deleteHandle(index);\n }\n\n deleteHandle(index: number) {\n const { customFiles } = this.data;\n const delFile = customFiles[index];\n this.triggerEvent('remove', { index, file: delFile });\n }\n\n updateGrid() {\n let { gridConfig = {} } = this.properties;\n if (!isObject(gridConfig)) gridConfig = {};\n const { column = 4, width = 160, height = 160 } = gridConfig as any;\n this.setData({\n gridItemStyle: `width:${width}rpx;height:${height}rpx`,\n column: column,\n });\n }\n}\n"]}
|