preview.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { ContactChooseTypes } from "@/pages_im/constant";
  2. // import { getPurePath } from "@/pages_im/util/common";
  3. import IMSDK, { IMMethods } from "openim-uniapp-polyfill";
  4. const isWebResource = (url) => {
  5. const webResourceRegex = /^(http|https):\/\//i;
  6. return webResourceRegex.test(url);
  7. };
  8. export const forwardImg = async (localImagePath) => {
  9. const message = await IMSDK.asyncApi(
  10. IMMethods.CreateImageMessageFromFullPath,
  11. IMSDK.uuid(),
  12. getPurePath(localImagePath)
  13. );
  14. uni.navigateTo({
  15. url: `/pages/common/contactChoose/index?type=${
  16. ContactChooseTypes.SendImage
  17. }&forwardMessage=${encodeURIComponent(JSON.stringify(message))}`,
  18. });
  19. };
  20. export const downloadToAlbum = (url) => {
  21. uni.downloadFile({
  22. url,
  23. success(res) {
  24. let url = res.tempFilePath;
  25. uni.saveImageToPhotosAlbum({
  26. filePath: url,
  27. success() {
  28. uni.showToast({
  29. title: "已保存到系统相册",
  30. icon: "none",
  31. });
  32. },
  33. fail() {
  34. uni.showToast({
  35. title: "保存失败",
  36. icon: "none",
  37. });
  38. },
  39. });
  40. },
  41. });
  42. };
  43. export const myPreview = (current, urls) => {
  44. uni.previewImage({
  45. current,
  46. urls,
  47. indicator: "none",
  48. longPressActions: {
  49. itemList: ["转发图片", "保存图片"],
  50. success({ tapIndex, index }) {
  51. if (tapIndex === 0) {
  52. uni.closePreviewImage();
  53. if (isWebResource(urls[index])) {
  54. uni.getImageInfo({
  55. src: urls[index],
  56. success: (res) => {
  57. forwardImg(res.path);
  58. },
  59. });
  60. } else {
  61. forwardImg(urls[index]);
  62. }
  63. } else {
  64. downloadToAlbum(urls[index]);
  65. }
  66. },
  67. },
  68. });
  69. };
  70. export const getPurePath = (path) => {
  71. const prefix = "file://";
  72. const relativeRrefix = "_doc/";
  73. if (path.includes(prefix)) {
  74. path = path.replace(prefix, "");
  75. }
  76. if (path.includes(relativeRrefix)) {
  77. path = plus.io.convertLocalFileSystemURL(path);
  78. }
  79. return path;
  80. };