cloudImageHandler.js 964 B

1234567891011121314151617181920212223242526272829303132
  1. export async function getCloudImageTempUrl(images) {
  2. const handledIndexToOriginIndex = new Map();
  3. const shouldBeHandledImages = [];
  4. images.forEach((x, index) => {
  5. if (!x.startsWith('cloud')) return;
  6. const handleIndex = shouldBeHandledImages.length;
  7. shouldBeHandledImages.push(x);
  8. handledIndexToOriginIndex.set(handleIndex, index);
  9. });
  10. const handledImages = (
  11. await wx.cloud.getTempFileURL({
  12. fileList: shouldBeHandledImages,
  13. })
  14. ).fileList.map((x) => x.tempFileURL);
  15. const ret = [...images];
  16. handledImages.forEach((image, handleIndex) => (ret[handledIndexToOriginIndex.get(handleIndex)] = image));
  17. return ret;
  18. }
  19. export async function getSingleCloudImageTempUrl(image) {
  20. // 添加类型检查,确保image是字符串类型
  21. if (typeof image !== 'string' || !image.startsWith('cloud')) return image;
  22. return (
  23. await wx.cloud.getTempFileURL({
  24. fileList: [image],
  25. })
  26. ).fileList[0].tempFileURL;
  27. }