utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }))
  21. }
  22. function formatVideo(res) {
  23. return [
  24. {
  25. ...pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg']),
  26. type: 'video',
  27. url: res.tempFilePath,
  28. thumb: res.thumbTempFilePath,
  29. size: res.size
  30. }
  31. ]
  32. }
  33. function formatMedia(res) {
  34. return res.tempFiles.map((item) => ({
  35. ...pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath']),
  36. type: res.type,
  37. url: item.tempFilePath,
  38. thumb: res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
  39. size: item.size
  40. }))
  41. }
  42. function formatFile(res) {
  43. return res.tempFiles.map((item) => ({ ...pickExclude(item, ['path']), url: item.path, size:item.size }))
  44. }
  45. export function chooseFile({
  46. accept,
  47. multiple,
  48. capture,
  49. compressed,
  50. maxDuration,
  51. sizeType,
  52. camera,
  53. maxCount
  54. }) {
  55. return new Promise((resolve, reject) => {
  56. switch (accept) {
  57. case 'image':
  58. uni.chooseImage({
  59. count: multiple ? Math.min(maxCount, 9) : 1,
  60. sourceType: capture,
  61. sizeType,
  62. success: (res) => resolve(formatImage(res)),
  63. fail: reject
  64. })
  65. break
  66. // #ifdef MP-WEIXIN
  67. // 只有微信小程序才支持chooseMedia接口
  68. case 'media':
  69. wx.chooseMedia({
  70. count: multiple ? Math.min(maxCount, 9) : 1,
  71. sourceType: capture,
  72. maxDuration,
  73. sizeType,
  74. camera,
  75. success: (res) => resolve(formatMedia(res)),
  76. fail: reject
  77. })
  78. break
  79. // #endif
  80. case 'video':
  81. uni.chooseVideo({
  82. sourceType: capture,
  83. compressed,
  84. maxDuration,
  85. camera,
  86. success: (res) => resolve(formatVideo(res)),
  87. fail: reject
  88. })
  89. break
  90. // #ifdef MP-WEIXIN || H5
  91. // 只有微信小程序才支持chooseMessageFile接口
  92. case 'file':
  93. // #ifdef MP-WEIXIN
  94. wx.chooseMessageFile({
  95. count: multiple ? maxCount : 1,
  96. type: accept,
  97. success: (res) => resolve(formatFile(res)),
  98. fail: reject
  99. })
  100. // #endif
  101. // #ifdef H5
  102. // 需要hx2.9.9以上才支持uni.chooseFile
  103. uni.chooseFile({
  104. count: multiple ? maxCount : 1,
  105. type: accept,
  106. success: (res) => resolve(formatFile(res)),
  107. fail: reject
  108. })
  109. // #endif
  110. break
  111. // #endif
  112. }
  113. })
  114. }