{"version":3,"sources":["../src/upload/upload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,cAAc,EAAe,MAAM,qBAAqB,CAAC;AAE5E,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAMpD,MAAM,CAAC,OAAO,OAAO,MAAO,SAAQ,cAAc;IAChD,eAAe,WAAuB;IAEtC,OAAO;;MAEL;IAEF,IAAI;;;;;;;;;;;;;;MAeF;IAEF,UAAU,iCAAS;IAEnB,eAAe;;;QAKb;IAEF,SAAS;qBACM,UAAU;;;MASvB;IAEF,UAAU,CAAC,CAAC,EAAE,GAAG;IAQjB,KAAK;IAKL,WAAW,CAAC,WAAW,EAAE,UAAU,EAAE,EAAE,GAAG,EAAE,MAAM;IAiBlD,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE;IAW/B,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE;IAS/B,aAAa;IACb,WAAW,CAAC,SAAS,KAAA;IA6CrB;;;;;;OAMG;IACH,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;IAiBxE,eAAe,CAAC,QAAQ,KAAA;IAMxB,QAAQ;IAIR,QAAQ;IAKR,aAAa;IAKb,aAAa;IAKb,QAAQ,CAAC,CAAC,EAAE,GAAG;IAKf,YAAY,CAAC,KAAK,EAAE,MAAM;IAM1B,UAAU;CASX","file":"upload.d.ts","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"]}