You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.3 KiB

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