123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import methods from "@/common/methodTool";
- export function download(url) {
- url = methods.splitImgHost(url);
- // #ifdef H5
- window.location.href = url;
- // #endif
- // #ifdef MP-WEIXIN
- const _s = saveImgToLoca;
- uni.getSetting({
- success: (res) => {
- if (!res.authSetting["scope.writePhotosAlbum"]) {
- uni.authorize({
- scope: "scope.writePhotosAlbum",
- success() {
- _s(url);
- },
- fail() {},
- });
- } else {
- _s(url);
- }
- },
- });
- // #endif
- }
- function saveImgToLoca(url) {
- uni.downloadFile({
- url: url, //图片地址
- success: ({ statusCode, tempFilePath }) => {
- if (statusCode === 200) {
- const type = tempFilePath.substr(tempFilePath.lastIndexOf(".") + 1);
- console.log("🚀 ~ file: util.js:32 ~ saveImgToLoca ~ type:", type);
- if (type === "pdf") {
- uni.getFileSystemManager().saveFile({
- tempFilePath,
- success: function (res) {
- console.log(
- "🚀 ~ file: util.js:37 ~ uni.getFileSystemManager ~ res:",
- res
- );
- uni.showToast({
- title: "保存成功",
- icon: "none",
- });
- },
- fail: function () {
- uni.showToast({
- title: "保存失败",
- icon: "none",
- });
- },
- });
- return;
- }
- uni.showModal({
- title: "提示",
- content: "确定保存到相册吗",
- success: (res) => {
- if (res.confirm) {
- uni.saveImageToPhotosAlbum({
- filePath: tempFilePath,
- success: function () {
- uni.showToast({
- title: "保存成功",
- icon: "none",
- });
- },
- fail: function () {
- uni.showToast({
- title: "保存失败",
- icon: "none",
- });
- },
- });
- }
- },
- });
- }
- },
- });
- }
- export function preview(url) {
- url = methods.splitImgHost(url);
- // #ifdef H5
- uni.navigateTo({
- url: `/pages/webview/sdlink?url=https://preview.xyyxt.net?src=${url}`,
- });
- // #endif
- // #ifdef MP-WEIXIN
- uni.downloadFile({
- url: url,
- success: ({ tempFilePath }) => {
- uni.openDocument({
- filePath: tempFilePath,
- showMenu: true,
- success: (res) => {},
- fail: (err) => {
- uni.showToast({
- icon: "none",
- title: "文档地址错误",
- });
- },
- });
- },
- fail: (err) => {
- this.downLoading = false;
- uni.showModal({
- title: "提示",
- content: "文档错误," + err.errMsg,
- showCancel: false,
- });
- },
- });
- // #endif
- }
|