3
0

utils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. function pickExclude(obj, keys) {
  2. // 某些情况下,type可能会为
  3. if (!['[object Object]', '[object File]'].includes(Object.prototype.toString.call(obj))) {
  4. return {}
  5. }
  6. return Object.keys(obj).reduce((prev, key) => {
  7. if (!keys.includes(key)) {
  8. prev[key] = obj[key]
  9. }
  10. return prev
  11. }, {})
  12. }
  13. function formatImage(res) {
  14. return res.tempFiles.map((item) => ({
  15. ...pickExclude(item, ['path']),
  16. type: 'image',
  17. url: item.path,
  18. thumb: item.path,
  19. size: item.size,
  20. // #ifdef H5
  21. name: item.name,
  22. file: item
  23. // #endif
  24. }))
  25. }
  26. function formatVideo(res) {
  27. return [
  28. {
  29. ...pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg']),
  30. type: 'video',
  31. url: res.tempFilePath,
  32. thumb: res.thumbTempFilePath,
  33. size: res.size,
  34. // #ifdef H5
  35. name: res.name,
  36. file: res
  37. // #endif
  38. }
  39. ]
  40. }
  41. function formatMedia(res) {
  42. return res.tempFiles.map((item) => ({
  43. ...pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath']),
  44. type: res.type,
  45. url: item.tempFilePath,
  46. thumb: res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
  47. size: item.size,
  48. // #ifdef H5
  49. file: item
  50. // #endif
  51. }))
  52. }
  53. function formatFile(res) {
  54. return res.tempFiles.map((item) => ({
  55. ...pickExclude(item, ['path']),
  56. url: item.path,
  57. size:item.size,
  58. // #ifdef H5
  59. name: item.name,
  60. type: item.type,
  61. file: item
  62. // #endif
  63. }))
  64. }
  65. export function chooseFile({
  66. accept,
  67. multiple,
  68. capture,
  69. compressed,
  70. maxDuration,
  71. sizeType,
  72. camera,
  73. maxCount,
  74. extension
  75. }) {
  76. return new Promise((resolve, reject) => {
  77. switch (accept) {
  78. case 'image':
  79. uni.chooseImage({
  80. count: multiple ? Math.min(maxCount, 9) : 1,
  81. sourceType: capture,
  82. sizeType,
  83. success: (res) => resolve(formatImage(res)),
  84. fail: reject
  85. })
  86. break
  87. // #ifdef MP-WEIXIN
  88. // 只有微信小程序才支持chooseMedia接口
  89. case 'media':
  90. wx.chooseMedia({
  91. count: multiple ? Math.min(maxCount, 9) : 1,
  92. sourceType: capture,
  93. maxDuration,
  94. sizeType,
  95. camera,
  96. success: (res) => resolve(formatMedia(res)),
  97. fail: reject
  98. })
  99. break
  100. // #endif
  101. case 'video':
  102. uni.chooseVideo({
  103. sourceType: capture,
  104. compressed,
  105. maxDuration,
  106. camera,
  107. success: (res) => resolve(formatVideo(res)),
  108. fail: reject
  109. })
  110. break
  111. // #ifdef MP-WEIXIN || H5
  112. // 只有微信小程序才支持chooseMessageFile接口
  113. case 'file':
  114. // #ifdef MP-WEIXIN
  115. wx.chooseMessageFile({
  116. count: multiple ? maxCount : 1,
  117. type: accept,
  118. success: (res) => resolve(formatFile(res)),
  119. fail: reject
  120. })
  121. // #endif
  122. // #ifdef H5
  123. // 需要hx2.9.9以上才支持uni.chooseFile
  124. let params = {
  125. count: multiple ? maxCount : 1,
  126. type: accept,
  127. success: (res) => resolve(formatFile(res)),
  128. fail: reject
  129. }
  130. if (extension.length && extension.length > 0) {
  131. params.extension = extension
  132. }
  133. uni.chooseFile(params)
  134. // #endif
  135. break
  136. // #endif
  137. default:
  138. // 此为保底选项,在accept不为上面任意一项的时候选取全部文件
  139. // #ifdef MP-WEIXIN
  140. wx.chooseMessageFile({
  141. count: multiple ? maxCount : 1,
  142. type: 'all',
  143. success: (res) => resolve(formatFile(res)),
  144. fail: reject
  145. })
  146. // #endif
  147. // #ifdef H5
  148. // 需要hx2.9.9以上才支持uni.chooseFile
  149. let paramsFile = {
  150. count: multiple ? maxCount : 1,
  151. type: 'all',
  152. success: (res) => resolve(formatFile(res)),
  153. fail: reject
  154. }
  155. if (extension.length && extension.length > 0) {
  156. paramsFile.extension = extension
  157. }
  158. uni.chooseFile(paramsFile)
  159. // #endif
  160. }
  161. })
  162. }