{"version":3,"file":"choose-and-upload-file.js","sources":["../../../../../../src/sheep/components/s-uploader/choose-and-upload-file.js"],"sourcesContent":["'use strict'\r\nimport FileApi from '@/sheep/api/infra/file'\r\n\r\nconst ERR_MSG_OK = 'chooseAndUploadFile:ok'\r\nconst ERR_MSG_FAIL = 'chooseAndUploadFile:fail'\r\n\r\nfunction chooseImage(opts) {\r\n const {\r\n count,\r\n sizeType = ['original', 'compressed'],\r\n sourceType = ['album', 'camera'],\r\n extension,\r\n } = opts\r\n return new Promise((resolve, reject) => {\r\n uni.chooseImage({\r\n count,\r\n sizeType,\r\n sourceType,\r\n extension,\r\n success(res) {\r\n resolve(normalizeChooseAndUploadFileRes(res, 'image'))\r\n },\r\n fail(res) {\r\n reject({\r\n errMsg: res.errMsg.replace('chooseImage:fail', ERR_MSG_FAIL),\r\n })\r\n },\r\n })\r\n })\r\n}\r\n\r\nfunction chooseVideo(opts) {\r\n const { camera, compressed, maxDuration, sourceType = ['album', 'camera'], extension } = opts\r\n return new Promise((resolve, reject) => {\r\n uni.chooseVideo({\r\n camera,\r\n compressed,\r\n maxDuration,\r\n sourceType,\r\n extension,\r\n success(res) {\r\n const { tempFilePath, duration, size, height, width } = res\r\n resolve(\r\n normalizeChooseAndUploadFileRes(\r\n {\r\n errMsg: 'chooseVideo:ok',\r\n tempFilePaths: [tempFilePath],\r\n tempFiles: [\r\n {\r\n name: (res.tempFile && res.tempFile.name) || '',\r\n path: tempFilePath,\r\n size,\r\n type: (res.tempFile && res.tempFile.type) || '',\r\n width,\r\n height,\r\n duration,\r\n fileType: 'video',\r\n cloudPath: '',\r\n },\r\n ],\r\n },\r\n 'video',\r\n ),\r\n )\r\n },\r\n fail(res) {\r\n reject({\r\n errMsg: res.errMsg.replace('chooseVideo:fail', ERR_MSG_FAIL),\r\n })\r\n },\r\n })\r\n })\r\n}\r\n\r\nfunction chooseAll(opts) {\r\n const { count, extension } = opts\r\n return new Promise((resolve, reject) => {\r\n let chooseFile = uni.chooseFile\r\n if (typeof wx !== 'undefined' && typeof wx.chooseMessageFile === 'function') {\r\n chooseFile = wx.chooseMessageFile\r\n }\r\n if (typeof chooseFile !== 'function') {\r\n return reject({\r\n errMsg: ERR_MSG_FAIL + ' 请指定 type 类型,该平台仅支持选择 image 或 video。',\r\n })\r\n }\r\n chooseFile({\r\n type: 'all',\r\n count,\r\n extension,\r\n success(res) {\r\n resolve(normalizeChooseAndUploadFileRes(res))\r\n },\r\n fail(res) {\r\n reject({\r\n errMsg: res.errMsg.replace('chooseFile:fail', ERR_MSG_FAIL),\r\n })\r\n },\r\n })\r\n })\r\n}\r\n\r\nfunction normalizeChooseAndUploadFileRes(res, fileType) {\r\n res.tempFiles.forEach((item, index) => {\r\n if (!item.name) {\r\n item.name = item.path.substring(item.path.lastIndexOf('/') + 1)\r\n }\r\n if (fileType) {\r\n item.fileType = fileType\r\n }\r\n item.cloudPath = Date.now() + '_' + index + item.name.substring(item.name.lastIndexOf('.'))\r\n })\r\n if (!res.tempFilePaths) {\r\n res.tempFilePaths = res.tempFiles.map((file) => file.path)\r\n }\r\n return res\r\n}\r\n\r\nfunction uploadCloudFiles(files, max = 5, onUploadProgress) {\r\n files = JSON.parse(JSON.stringify(files))\r\n const len = files.length\r\n let count = 0\r\n const self = this\r\n return new Promise((resolve) => {\r\n while (count < max) {\r\n next()\r\n }\r\n\r\n function next() {\r\n const cur = count++\r\n if (cur >= len) {\r\n !files.find((item) => !item.url && !item.errMsg) && resolve(files)\r\n return\r\n }\r\n const fileItem = files[cur]\r\n const index = self.files.findIndex((v) => v.uuid === fileItem.uuid)\r\n fileItem.url = ''\r\n delete fileItem.errMsg\r\n\r\n uniCloud\r\n .uploadFile({\r\n filePath: fileItem.path,\r\n cloudPath: fileItem.cloudPath,\r\n fileType: fileItem.fileType,\r\n onUploadProgress: (res) => {\r\n res.index = index\r\n onUploadProgress && onUploadProgress(res)\r\n },\r\n })\r\n .then((res) => {\r\n fileItem.url = res.fileID\r\n fileItem.index = index\r\n if (cur < len) {\r\n next()\r\n }\r\n })\r\n .catch((res) => {\r\n fileItem.errMsg = res.errMsg || res.message\r\n fileItem.index = index\r\n if (cur < len) {\r\n next()\r\n }\r\n })\r\n }\r\n })\r\n}\r\n\r\nfunction uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {\r\n return choosePromise\r\n .then((res) => {\r\n if (onChooseFile) {\r\n const customChooseRes = onChooseFile(res)\r\n if (typeof customChooseRes !== 'undefined') {\r\n return Promise.resolve(customChooseRes).then((chooseRes) =>\r\n typeof chooseRes === 'undefined' ? res : chooseRes,\r\n )\r\n }\r\n }\r\n return res\r\n })\r\n .then((res) => {\r\n if (res === false) {\r\n return {\r\n errMsg: ERR_MSG_OK,\r\n tempFilePaths: [],\r\n tempFiles: [],\r\n }\r\n }\r\n return res\r\n })\r\n .then(async (files) => {\r\n for (const file of files.tempFiles) {\r\n const { data } = await FileApi.uploadFile(file.path)\r\n file.url = data\r\n }\r\n return files\r\n })\r\n}\r\n\r\nfunction chooseAndUploadFile(\r\n opts = {\r\n type: 'all',\r\n },\r\n) {\r\n if (opts.type === 'image') {\r\n return uploadFiles(chooseImage(opts), opts)\r\n } else if (opts.type === 'video') {\r\n return uploadFiles(chooseVideo(opts), opts)\r\n }\r\n return uploadFiles(chooseAll(opts), opts)\r\n}\r\n\r\nexport { chooseAndUploadFile, uploadCloudFiles }\r\n"],"names":["uni","wx","uniCloud","FileApi"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA,MAAM,aAAa;AACnB,MAAM,eAAe;AAErB,SAAS,YAAY,MAAM;AACzB,QAAM;AAAA,IACJ;AAAA,IACA,WAAW,CAAC,YAAY,YAAY;AAAA,IACpC,aAAa,CAAC,SAAS,QAAQ;AAAA,IAC/B;AAAA,EACJ,IAAM;AACJ,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtCA,kBAAAA,MAAI,YAAY;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,QAAQ,KAAK;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