choose-and-upload-file.js 5.9 KB

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