utils.js 3.2 KB

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