choose-and-upload-file.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = ['album', 'camera'], 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 = ['album', 'camera'], 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. function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
  108. return choosePromise
  109. .then((res) => {
  110. if (onChooseFile) {
  111. const customChooseRes = onChooseFile(res);
  112. if (typeof customChooseRes !== 'undefined') {
  113. return Promise.resolve(customChooseRes).then((chooseRes) => typeof chooseRes === 'undefined' ? res : chooseRes);
  114. }
  115. }
  116. return res;
  117. })
  118. .then((res) => {
  119. if (res === false) {
  120. return {
  121. errMsg: ERR_MSG_OK,
  122. tempFilePaths: [],
  123. tempFiles: [],
  124. };
  125. }
  126. return res
  127. // return uploadCloudFiles(res, 5, onUploadProgress);
  128. })
  129. }
  130. function chooseAndUploadFile(opts = { type: 'all' }) {
  131. if (opts.type === 'image') {
  132. return uploadFiles(chooseImage(opts), opts);
  133. }
  134. else if (opts.type === 'video') {
  135. return uploadFiles(chooseVideo(opts), opts);
  136. }
  137. return uploadFiles(chooseAll(opts), opts);
  138. }
  139. export {chooseAndUploadFile};