choose-and-upload-file.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use strict';
  2. import FileApi from '@/sheep/api/infra/file';
  3. const ERR_MSG_OK = 'chooseAndUploadFile:ok';
  4. const ERR_MSG_FAIL = 'chooseAndUploadFile:fail';
  5. function chooseImage(opts) {
  6. const {
  7. count,
  8. sizeType = ['original', 'compressed'],
  9. sourceType = ['album', 'camera'],
  10. extension,
  11. } = opts;
  12. return new Promise((resolve, reject) => {
  13. uni.chooseImage({
  14. count,
  15. sizeType,
  16. sourceType,
  17. extension,
  18. success(res) {
  19. resolve(normalizeChooseAndUploadFileRes(res, 'image'));
  20. },
  21. fail(res) {
  22. reject({
  23. errMsg: res.errMsg.replace('chooseImage:fail', ERR_MSG_FAIL),
  24. });
  25. },
  26. });
  27. });
  28. }
  29. function chooseVideo(opts) {
  30. const { camera, compressed, maxDuration, sourceType = ['album', 'camera'], extension } = opts;
  31. return new Promise((resolve, reject) => {
  32. uni.chooseVideo({
  33. camera,
  34. compressed,
  35. maxDuration,
  36. sourceType,
  37. extension,
  38. success(res) {
  39. const { tempFilePath, duration, size, height, width } = res;
  40. resolve(
  41. normalizeChooseAndUploadFileRes(
  42. {
  43. errMsg: 'chooseVideo:ok',
  44. tempFilePaths: [tempFilePath],
  45. tempFiles: [
  46. {
  47. name: (res.tempFile && res.tempFile.name) || '',
  48. path: tempFilePath,
  49. size,
  50. type: (res.tempFile && res.tempFile.type) || '',
  51. width,
  52. height,
  53. duration,
  54. fileType: 'video',
  55. cloudPath: '',
  56. },
  57. ],
  58. },
  59. 'video',
  60. ),
  61. );
  62. },
  63. fail(res) {
  64. reject({
  65. errMsg: res.errMsg.replace('chooseVideo:fail', ERR_MSG_FAIL),
  66. });
  67. },
  68. });
  69. });
  70. }
  71. function chooseAll(opts) {
  72. const { count, extension } = opts;
  73. return new Promise((resolve, reject) => {
  74. let chooseFile = uni.chooseFile;
  75. if (typeof wx !== 'undefined' && typeof wx.chooseMessageFile === 'function') {
  76. chooseFile = wx.chooseMessageFile;
  77. }
  78. if (typeof chooseFile !== 'function') {
  79. return reject({
  80. errMsg: ERR_MSG_FAIL + ' 请指定 type 类型,该平台仅支持选择 image 或 video。',
  81. });
  82. }
  83. chooseFile({
  84. type: 'all',
  85. count,
  86. extension,
  87. success(res) {
  88. resolve(normalizeChooseAndUploadFileRes(res));
  89. },
  90. fail(res) {
  91. reject({
  92. errMsg: res.errMsg.replace('chooseFile:fail', ERR_MSG_FAIL),
  93. });
  94. },
  95. });
  96. });
  97. }
  98. function normalizeChooseAndUploadFileRes(res, fileType) {
  99. res.tempFiles.forEach((item, index) => {
  100. if (!item.name) {
  101. item.name = item.path.substring(item.path.lastIndexOf('/') + 1);
  102. }
  103. if (fileType) {
  104. item.fileType = fileType;
  105. }
  106. item.cloudPath = Date.now() + '_' + index + item.name.substring(item.name.lastIndexOf('.'));
  107. });
  108. if (!res.tempFilePaths) {
  109. res.tempFilePaths = res.tempFiles.map((file) => file.path);
  110. }
  111. return res;
  112. }
  113. function uploadCloudFiles(files, max = 5, onUploadProgress) {
  114. files = JSON.parse(JSON.stringify(files));
  115. const len = files.length;
  116. let count = 0;
  117. let self = this;
  118. return new Promise((resolve) => {
  119. while (count < max) {
  120. next();
  121. }
  122. function next() {
  123. let cur = count++;
  124. if (cur >= len) {
  125. !files.find((item) => !item.url && !item.errMsg) && resolve(files);
  126. return;
  127. }
  128. const fileItem = files[cur];
  129. const index = self.files.findIndex((v) => v.uuid === fileItem.uuid);
  130. fileItem.url = '';
  131. delete fileItem.errMsg;
  132. uniCloud
  133. .uploadFile({
  134. filePath: fileItem.path,
  135. cloudPath: fileItem.cloudPath,
  136. fileType: fileItem.fileType,
  137. onUploadProgress: (res) => {
  138. res.index = index;
  139. onUploadProgress && onUploadProgress(res);
  140. },
  141. })
  142. .then((res) => {
  143. fileItem.url = res.fileID;
  144. fileItem.index = index;
  145. if (cur < len) {
  146. next();
  147. }
  148. })
  149. .catch((res) => {
  150. fileItem.errMsg = res.errMsg || res.message;
  151. fileItem.index = index;
  152. if (cur < len) {
  153. next();
  154. }
  155. });
  156. }
  157. });
  158. }
  159. function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
  160. return choosePromise
  161. .then((res) => {
  162. if (onChooseFile) {
  163. const customChooseRes = onChooseFile(res);
  164. if (typeof customChooseRes !== 'undefined') {
  165. return Promise.resolve(customChooseRes).then((chooseRes) =>
  166. typeof chooseRes === 'undefined' ? res : chooseRes,
  167. );
  168. }
  169. }
  170. return res;
  171. })
  172. .then((res) => {
  173. if (res === false) {
  174. return {
  175. errMsg: ERR_MSG_OK,
  176. tempFilePaths: [],
  177. tempFiles: [],
  178. };
  179. }
  180. return res;
  181. })
  182. .then(async (files) => {
  183. for (let file of files.tempFiles) {
  184. const { data } = await FileApi.uploadFile(file.path);
  185. file.url = data;
  186. }
  187. return files;
  188. });
  189. }
  190. function chooseAndUploadFile(
  191. opts = {
  192. type: 'all',
  193. },
  194. ) {
  195. if (opts.type === 'image') {
  196. return uploadFiles(chooseImage(opts), opts);
  197. } else if (opts.type === 'video') {
  198. return uploadFiles(chooseVideo(opts), opts);
  199. }
  200. return uploadFiles(chooseAll(opts), opts);
  201. }
  202. export { chooseAndUploadFile, uploadCloudFiles };