洛阳学员端
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.

151 lines
3.9 KiB

10 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. }) {
  69. return new Promise((resolve, reject) => {
  70. switch (accept) {
  71. case 'image':
  72. uni.chooseImage({
  73. count: multiple ? Math.min(maxCount, 9) : 1,
  74. sourceType: capture,
  75. sizeType,
  76. success: (res) => resolve(formatImage(res)),
  77. fail: reject
  78. })
  79. break
  80. // #ifdef MP-WEIXIN
  81. // 只有微信小程序才支持chooseMedia接口
  82. case 'media':
  83. wx.chooseMedia({
  84. count: multiple ? Math.min(maxCount, 9) : 1,
  85. sourceType: capture,
  86. maxDuration,
  87. sizeType,
  88. camera,
  89. success: (res) => resolve(formatMedia(res)),
  90. fail: reject
  91. })
  92. break
  93. // #endif
  94. case 'video':
  95. uni.chooseVideo({
  96. sourceType: capture,
  97. compressed,
  98. maxDuration,
  99. camera,
  100. success: (res) => resolve(formatVideo(res)),
  101. fail: reject
  102. })
  103. break
  104. // #ifdef MP-WEIXIN || H5
  105. // 只有微信小程序才支持chooseMessageFile接口
  106. case 'file':
  107. // #ifdef MP-WEIXIN
  108. wx.chooseMessageFile({
  109. count: multiple ? maxCount : 1,
  110. type: accept,
  111. success: (res) => resolve(formatFile(res)),
  112. fail: reject
  113. })
  114. // #endif
  115. // #ifdef H5
  116. // 需要hx2.9.9以上才支持uni.chooseFile
  117. uni.chooseFile({
  118. count: multiple ? maxCount : 1,
  119. type: accept,
  120. success: (res) => resolve(formatFile(res)),
  121. fail: reject
  122. })
  123. // #endif
  124. break
  125. // #endif
  126. default:
  127. // 此为保底选项,在accept不为上面任意一项的时候选取全部文件
  128. // #ifdef MP-WEIXIN
  129. wx.chooseMessageFile({
  130. count: multiple ? maxCount : 1,
  131. type: 'all',
  132. success: (res) => resolve(formatFile(res)),
  133. fail: reject
  134. })
  135. // #endif
  136. // #ifdef H5
  137. // 需要hx2.9.9以上才支持uni.chooseFile
  138. uni.chooseFile({
  139. count: multiple ? maxCount : 1,
  140. type: 'all',
  141. success: (res) => resolve(formatFile(res)),
  142. fail: reject
  143. })
  144. // #endif
  145. }
  146. })
  147. }