util.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. export 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. if (type === "pdf") {
  33. uni.getFileSystemManager().saveFile({
  34. tempFilePath,
  35. success: function (res) {
  36. uni.showToast({
  37. title: "保存成功",
  38. icon: "none",
  39. });
  40. },
  41. fail: function () {
  42. uni.showToast({
  43. title: "保存失败",
  44. icon: "none",
  45. });
  46. },
  47. });
  48. return;
  49. }
  50. uni.showModal({
  51. title: "提示",
  52. content: "确定保存到相册吗",
  53. success: (res) => {
  54. if (res.confirm) {
  55. uni.saveImageToPhotosAlbum({
  56. filePath: tempFilePath,
  57. success: function () {
  58. uni.showToast({
  59. title: "保存成功",
  60. icon: "none",
  61. });
  62. },
  63. fail: function () {
  64. uni.showToast({
  65. title: "保存失败",
  66. icon: "none",
  67. });
  68. },
  69. });
  70. }
  71. },
  72. });
  73. }
  74. },
  75. });
  76. }
  77. export function preview(url) {
  78. url = methods.splitImgHost(url);
  79. // #ifdef H5
  80. uni.navigateTo({
  81. url: `/pages5/webview/sdlink?url=https://preview.xyyxt.net?src=${url}`,
  82. });
  83. // #endif
  84. // #ifdef MP-WEIXIN
  85. uni.downloadFile({
  86. url: url,
  87. success: ({ tempFilePath }) => {
  88. uni.openDocument({
  89. filePath: tempFilePath,
  90. showMenu: true,
  91. success: (res) => {},
  92. fail: (err) => {
  93. uni.showToast({
  94. icon: "none",
  95. title: "文档地址错误",
  96. });
  97. },
  98. });
  99. },
  100. fail: (err) => {
  101. this.downLoading = false;
  102. uni.showModal({
  103. title: "提示",
  104. content: "文档错误," + err.errMsg,
  105. showCancel: false,
  106. });
  107. },
  108. });
  109. // #endif
  110. }
  111. export function throttle(fn, delay) {
  112. let timeout = null;
  113. return function () {
  114. let args = arguments;
  115. if (!timeout) {
  116. timeout = setTimeout(() => {
  117. timeout = null;
  118. fn.apply(this, args);
  119. }, delay);
  120. }
  121. };
  122. }