utils.js 3.8 KB

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