util.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import methods from "@/common/methodTool";
  2. export function download(url) {
  3. // #ifdef H5
  4. window.location.href = url;
  5. // #endif
  6. // #ifdef MP-WEIXIN
  7. const _s = saveImgToLoca;
  8. uni.getSetting({
  9. success: (res) => {
  10. if (!res.authSetting["scope.writePhotosAlbum"]) {
  11. uni.authorize({
  12. scope: "scope.writePhotosAlbum",
  13. success() {
  14. _s(url);
  15. },
  16. fail() {},
  17. });
  18. } else {
  19. _s(url);
  20. }
  21. },
  22. });
  23. // #endif
  24. }
  25. function saveImgToLoca(url) {
  26. uni.downloadFile({
  27. url: url, //图片地址
  28. success: ({ statusCode, tempFilePath }) => {
  29. if (statusCode === 200) {
  30. const type = tempFilePath.substr(tempFilePath.lastIndexOf(".") + 1);
  31. console.log("🚀 ~ file: util.js:32 ~ saveImgToLoca ~ type:", type);
  32. if (type === "pdf") {
  33. uni.getFileSystemManager().saveFile({
  34. tempFilePath,
  35. success: function (res) {
  36. console.log("🚀 ~ file: util.js:37 ~ uni.getFileSystemManager ~ res:", res)
  37. uni.showToast({
  38. title: "保存成功",
  39. icon: "none",
  40. });
  41. },
  42. fail: function () {
  43. uni.showToast({
  44. title: "保存失败",
  45. icon: "none",
  46. });
  47. },
  48. });
  49. return;
  50. }
  51. uni.showModal({
  52. title: "提示",
  53. content: "确定保存到相册吗",
  54. success: (res) => {
  55. if (res.confirm) {
  56. uni.saveImageToPhotosAlbum({
  57. filePath: tempFilePath,
  58. success: function () {
  59. uni.showToast({
  60. title: "保存成功",
  61. icon: "none",
  62. });
  63. },
  64. fail: function () {
  65. uni.showToast({
  66. title: "保存失败",
  67. icon: "none",
  68. });
  69. },
  70. });
  71. }
  72. },
  73. });
  74. }
  75. },
  76. });
  77. }