utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isImageFile = isImageFile;
  6. exports.isVideoFile = isVideoFile;
  7. exports.chooseFile = chooseFile;
  8. var _utils = require("../common/utils");
  9. var _validator = require("../common/validator");
  10. var _my = require("../../__antmove/api/index.js")(my);
  11. var wx = _my;
  12. function isImageFile(item) {
  13. if (item.isImage != null) {
  14. return item.isImage;
  15. }
  16. if (item.type) {
  17. return item.type === "image";
  18. }
  19. if (item.url) {
  20. return (0, _validator.isImageUrl)(item.url);
  21. }
  22. return false;
  23. }
  24. function isVideoFile(item) {
  25. if (item.isVideo != null) {
  26. return item.isVideo;
  27. }
  28. if (item.type) {
  29. return item.type === "video";
  30. }
  31. if (item.url) {
  32. return (0, _validator.isVideoUrl)(item.url);
  33. }
  34. return false;
  35. }
  36. function formatImage(res) {
  37. var fileInfoName = "tempFiles";
  38. fileInfoName = "tempFilePaths";
  39. return res[fileInfoName].map(function (item) {
  40. return Object.assign(Object.assign({}, (0, _utils.pickExclude)(item, ["path"])), {
  41. type: "image",
  42. url: item.path || item,
  43. thumb: item.path || item
  44. });
  45. });
  46. }
  47. function formatVideo(res) {
  48. return [Object.assign(Object.assign({}, (0, _utils.pickExclude)(res, ["tempFilePath", "thumbTempFilePath", "errMsg"])), {
  49. type: "video",
  50. url: res.tempFilePath,
  51. thumb: res.thumbTempFilePath
  52. })];
  53. }
  54. function formatMedia(res) {
  55. return res.tempFiles.map(function (item) {
  56. return Object.assign(Object.assign({}, (0, _utils.pickExclude)(item, ["fileType", "thumbTempFilePath", "tempFilePath"])), {
  57. type: res.type,
  58. url: item.tempFilePath,
  59. thumb: res.type === "video" ? item.thumbTempFilePath : item.tempFilePath
  60. });
  61. });
  62. }
  63. function formatFile(res) {
  64. return res.tempFiles.map(function (item) {
  65. return Object.assign(Object.assign({}, (0, _utils.pickExclude)(item, ["path"])), {
  66. url: item.path
  67. });
  68. });
  69. }
  70. function chooseFile(_ref) {
  71. var accept = _ref.accept,
  72. multiple = _ref.multiple,
  73. capture = _ref.capture,
  74. compressed = _ref.compressed,
  75. maxDuration = _ref.maxDuration,
  76. sizeType = _ref.sizeType,
  77. camera = _ref.camera,
  78. maxCount = _ref.maxCount;
  79. return new Promise(function (resolve, reject) {
  80. switch (accept) {
  81. case "image":
  82. wx.chooseImage({
  83. count: multiple ? Math.min(maxCount, 9) : 1,
  84. sourceType: capture,
  85. sizeType: sizeType,
  86. success: function success(res) {
  87. return resolve(formatImage(res));
  88. },
  89. fail: reject
  90. });
  91. break;
  92. case "media":
  93. wx.chooseMedia({
  94. count: multiple ? Math.min(maxCount, 9) : 1,
  95. sourceType: capture,
  96. maxDuration: maxDuration,
  97. sizeType: sizeType,
  98. camera: camera,
  99. success: function success(res) {
  100. return resolve(formatMedia(res));
  101. },
  102. fail: reject
  103. });
  104. break;
  105. case "video":
  106. wx.chooseVideo({
  107. sourceType: capture,
  108. compressed: compressed,
  109. maxDuration: maxDuration,
  110. camera: camera,
  111. success: function success(res) {
  112. return resolve(formatVideo(res));
  113. },
  114. fail: reject
  115. });
  116. break;
  117. default:
  118. wx.chooseMessageFile({
  119. count: multiple ? maxCount : 1,
  120. type: accept,
  121. success: function success(res) {
  122. return resolve(formatFile(res));
  123. },
  124. fail: reject
  125. });
  126. break;
  127. }
  128. });
  129. }