utils.js 4.8 KB

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