choose-and-upload-file.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. 'use strict';
  2. const ERR_MSG_OK = 'chooseAndUploadFile:ok';
  3. const ERR_MSG_FAIL = 'chooseAndUploadFile:fail';
  4. function chooseImage(opts) {
  5. const {
  6. count,
  7. sizeType = ['original', 'compressed'],
  8. sourceType,
  9. extension
  10. } = opts
  11. return new Promise((resolve, reject) => {
  12. // 微信由于旧接口不再维护,针对微信小程序平台改用chooseMedia接口
  13. // #ifdef MP-WEIXIN
  14. uni.chooseMedia({
  15. count,
  16. sizeType,
  17. sourceType,
  18. mediaType: ['image'],
  19. extension,
  20. success(res) {
  21. res.tempFiles.forEach(item => {
  22. item.path = item.tempFilePath;
  23. })
  24. resolve(normalizeChooseAndUploadFileRes(res, 'image'));
  25. },
  26. fail(res) {
  27. reject({
  28. errMsg: res.errMsg.replace('chooseImage:fail', ERR_MSG_FAIL),
  29. });
  30. },
  31. })
  32. // #endif
  33. // #ifndef MP-WEIXIN
  34. uni.chooseImage({
  35. count,
  36. sizeType,
  37. sourceType,
  38. extension,
  39. success(res) {
  40. resolve(normalizeChooseAndUploadFileRes(res, 'image'));
  41. },
  42. fail(res) {
  43. reject({
  44. errMsg: res.errMsg.replace('chooseImage:fail', ERR_MSG_FAIL),
  45. });
  46. },
  47. });
  48. // #endif
  49. });
  50. }
  51. function chooseVideo(opts) {
  52. const {
  53. count,
  54. camera,
  55. compressed,
  56. maxDuration,
  57. sourceType,
  58. extension
  59. } = opts;
  60. return new Promise((resolve, reject) => {
  61. // 微信由于旧接口不再维护,针对微信小程序平台改用chooseMedia接口
  62. // #ifdef MP-WEIXIN
  63. uni.chooseMedia({
  64. count,
  65. compressed,
  66. maxDuration,
  67. sourceType,
  68. extension,
  69. mediaType: ['video'],
  70. success(res) {
  71. const {
  72. tempFiles,
  73. } = res;
  74. resolve(normalizeChooseAndUploadFileRes({
  75. errMsg: 'chooseVideo:ok',
  76. tempFiles: tempFiles.map(item => {
  77. return {
  78. name: item.name || '',
  79. path: item.tempFilePath,
  80. thumbTempFilePath: item.thumbTempFilePath,
  81. size:item.size,
  82. type: (res.tempFile && res.tempFile.type) || '',
  83. width:item.width,
  84. height:item.height,
  85. duration:item.duration,
  86. fileType: 'video',
  87. cloudPath: '',
  88. }
  89. }),
  90. }, 'video'));
  91. },
  92. fail(res) {
  93. reject({
  94. errMsg: res.errMsg.replace('chooseVideo:fail', ERR_MSG_FAIL),
  95. });
  96. },
  97. })
  98. // #endif
  99. // #ifndef MP-WEIXIN
  100. uni.chooseVideo({
  101. camera,
  102. compressed,
  103. maxDuration,
  104. sourceType,
  105. extension,
  106. success(res) {
  107. const {
  108. tempFilePath,
  109. duration,
  110. size,
  111. height,
  112. width
  113. } = res;
  114. resolve(normalizeChooseAndUploadFileRes({
  115. errMsg: 'chooseVideo:ok',
  116. tempFilePaths: [tempFilePath],
  117. tempFiles: [{
  118. name: (res.tempFile && res.tempFile.name) || '',
  119. path: tempFilePath,
  120. size,
  121. type: (res.tempFile && res.tempFile.type) || '',
  122. width,
  123. height,
  124. duration,
  125. fileType: 'video',
  126. cloudPath: '',
  127. }, ],
  128. }, 'video'));
  129. },
  130. fail(res) {
  131. reject({
  132. errMsg: res.errMsg.replace('chooseVideo:fail', ERR_MSG_FAIL),
  133. });
  134. },
  135. });
  136. // #endif
  137. });
  138. }
  139. function chooseAll(opts) {
  140. const {
  141. count,
  142. extension
  143. } = opts;
  144. return new Promise((resolve, reject) => {
  145. let chooseFile = uni.chooseFile;
  146. if (typeof wx !== 'undefined' &&
  147. typeof wx.chooseMessageFile === 'function') {
  148. chooseFile = wx.chooseMessageFile;
  149. }
  150. if (typeof chooseFile !== 'function') {
  151. return reject({
  152. errMsg: ERR_MSG_FAIL + ' 请指定 type 类型,该平台仅支持选择 image 或 video。',
  153. });
  154. }
  155. chooseFile({
  156. type: 'all',
  157. count,
  158. extension,
  159. success(res) {
  160. resolve(normalizeChooseAndUploadFileRes(res));
  161. },
  162. fail(res) {
  163. reject({
  164. errMsg: res.errMsg.replace('chooseFile:fail', ERR_MSG_FAIL),
  165. });
  166. },
  167. });
  168. });
  169. }
  170. function normalizeChooseAndUploadFileRes(res, fileType) {
  171. res.tempFiles.forEach((item, index) => {
  172. if (!item.name) {
  173. item.name = item.path.substring(item.path.lastIndexOf('/') + 1);
  174. }
  175. if (fileType) {
  176. item.fileType = fileType;
  177. }
  178. item.cloudPath =
  179. Date.now() + '_' + index + item.name.substring(item.name.lastIndexOf('.'));
  180. });
  181. if (!res.tempFilePaths) {
  182. res.tempFilePaths = res.tempFiles.map((file) => file.path);
  183. }
  184. return res;
  185. }
  186. function uploadCloudFiles(files, max = 5, onUploadProgress) {
  187. files = JSON.parse(JSON.stringify(files))
  188. const len = files.length
  189. let count = 0
  190. let self = this
  191. return new Promise(resolve => {
  192. while (count < max) {
  193. next()
  194. }
  195. function next() {
  196. let cur = count++
  197. if (cur >= len) {
  198. !files.find(item => !item.url && !item.errMsg) && resolve(files)
  199. return
  200. }
  201. const fileItem = files[cur]
  202. const index = self.files.findIndex(v => v.uuid === fileItem.uuid)
  203. fileItem.url = ''
  204. delete fileItem.errMsg
  205. uniCloud
  206. .uploadFile({
  207. filePath: fileItem.path,
  208. cloudPath: fileItem.cloudPath,
  209. fileType: fileItem.fileType,
  210. onUploadProgress: res => {
  211. res.index = index
  212. onUploadProgress && onUploadProgress(res)
  213. }
  214. })
  215. .then(res => {
  216. fileItem.url = res.fileID
  217. fileItem.index = index
  218. if (cur < len) {
  219. next()
  220. }
  221. })
  222. .catch(res => {
  223. fileItem.errMsg = res.errMsg || res.message
  224. fileItem.index = index
  225. if (cur < len) {
  226. next()
  227. }
  228. })
  229. }
  230. })
  231. }
  232. function uploadFiles(choosePromise, {
  233. onChooseFile,
  234. onUploadProgress
  235. }) {
  236. return choosePromise
  237. .then((res) => {
  238. if (onChooseFile) {
  239. const customChooseRes = onChooseFile(res);
  240. if (typeof customChooseRes !== 'undefined') {
  241. return Promise.resolve(customChooseRes).then((chooseRes) => typeof chooseRes === 'undefined' ?
  242. res : chooseRes);
  243. }
  244. }
  245. return res;
  246. })
  247. .then((res) => {
  248. if (res === false) {
  249. return {
  250. errMsg: ERR_MSG_OK,
  251. tempFilePaths: [],
  252. tempFiles: [],
  253. };
  254. }
  255. return res
  256. })
  257. }
  258. function chooseAndUploadFile(opts = {
  259. type: 'all'
  260. }) {
  261. if (opts.type === 'image') {
  262. return uploadFiles(chooseImage(opts), opts);
  263. } else if (opts.type === 'video') {
  264. return uploadFiles(chooseVideo(opts), opts);
  265. }
  266. return uploadFiles(chooseAll(opts), opts);
  267. }
  268. export {
  269. chooseAndUploadFile,
  270. uploadCloudFiles
  271. };