util.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import methods from "@/common/methodTool";
  2. export function download(url) {
  3. url = methods.splitImgHost(url);
  4. // #ifdef H5
  5. window.location.href = url;
  6. // #endif
  7. // #ifdef MP-WEIXIN
  8. const _s = saveImgToLoca;
  9. uni.getSetting({
  10. success: (res) => {
  11. if (!res.authSetting["scope.writePhotosAlbum"]) {
  12. uni.authorize({
  13. scope: "scope.writePhotosAlbum",
  14. success() {
  15. _s(url);
  16. },
  17. fail() {},
  18. });
  19. } else {
  20. _s(url);
  21. }
  22. },
  23. });
  24. // #endif
  25. }
  26. function saveImgToLoca(url) {
  27. uni.downloadFile({
  28. url: url, //图片地址
  29. success: ({ statusCode, tempFilePath }) => {
  30. if (statusCode === 200) {
  31. const type = tempFilePath.substr(tempFilePath.lastIndexOf(".") + 1);
  32. console.log("🚀 ~ file: util.js:32 ~ saveImgToLoca ~ type:", type);
  33. if (type === "pdf") {
  34. uni.getFileSystemManager().saveFile({
  35. tempFilePath,
  36. success: function (res) {
  37. console.log(
  38. "🚀 ~ file: util.js:37 ~ uni.getFileSystemManager ~ res:",
  39. res
  40. );
  41. uni.showToast({
  42. title: "保存成功",
  43. icon: "none",
  44. });
  45. },
  46. fail: function () {
  47. uni.showToast({
  48. title: "保存失败",
  49. icon: "none",
  50. });
  51. },
  52. });
  53. return;
  54. }
  55. uni.showModal({
  56. title: "提示",
  57. content: "确定保存到相册吗",
  58. success: (res) => {
  59. if (res.confirm) {
  60. uni.saveImageToPhotosAlbum({
  61. filePath: tempFilePath,
  62. success: function () {
  63. uni.showToast({
  64. title: "保存成功",
  65. icon: "none",
  66. });
  67. },
  68. fail: function () {
  69. uni.showToast({
  70. title: "保存失败",
  71. icon: "none",
  72. });
  73. },
  74. });
  75. }
  76. },
  77. });
  78. }
  79. },
  80. });
  81. }
  82. export function preview(url) {
  83. url = methods.splitImgHost(url);
  84. // #ifdef H5
  85. uni.navigateTo({
  86. url: `/pages/webview/sdlink?url=https://preview.xyyxt.net?src=${url}`,
  87. });
  88. // #endif
  89. // #ifdef MP-WEIXIN
  90. uni.downloadFile({
  91. url: url,
  92. success: ({ tempFilePath }) => {
  93. uni.openDocument({
  94. filePath: tempFilePath,
  95. showMenu: true,
  96. success: (res) => {},
  97. fail: (err) => {
  98. uni.showToast({
  99. icon: "none",
  100. title: "文档地址错误",
  101. });
  102. },
  103. });
  104. },
  105. fail: (err) => {
  106. this.downLoading = false;
  107. uni.showModal({
  108. title: "提示",
  109. content: "文档错误," + err.errMsg,
  110. showCancel: false,
  111. });
  112. },
  113. });
  114. // #endif
  115. }