From f226d5fe6327e31bb471a96b7370cf94689c6608 Mon Sep 17 00:00:00 2001
From: quanwei <419654421@qq.com>
Date: Fri, 31 Oct 2025 16:54:48 +0800
Subject: [PATCH] 名片设置 名片置顶 名片分享
---
mobile/common/activityFile.js | 121 ++++++++++++++++++++++++++++------------
1 files changed, 85 insertions(+), 36 deletions(-)
diff --git a/mobile/common/activityFile.js b/mobile/common/activityFile.js
index 208724b..2505884 100644
--- a/mobile/common/activityFile.js
+++ b/mobile/common/activityFile.js
@@ -95,20 +95,20 @@
file_path = await this.getFilePath(file_id);
}
// 生成文件名
- const file_name = new Date().toISOString().replace(/[-:T.Z]/g, '').slice(0, 14) +
+ const file_name = new Date().toISOString().replace(/[-:T.Z]/g, '').slice(0, 14) +
Math.random().toString().slice(2, 6);
-
+
// #ifdef H5
// 在H5环境中使用Blob方式下载
return await this.downloadViaBlob(file_path, file_name, points);
// #endif
-
+
// #ifdef APP-PLUS || MP-WEIXIN
// 在App和微信环境中使用uni.downloadFile
- return await this.downloadInApp(file_path, points);
+ return await this.downloadInApp(file_path, points, file_type);
// #endif
}
-
+
/**
* H5环境下的安全下载
*/
@@ -117,51 +117,59 @@
const response = await fetch(file_path);
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
-
+
const a = document.createElement('a');
a.href = objectUrl;
a.download = file_name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
-
+
setTimeout(() => URL.revokeObjectURL(objectUrl), 1000);
-
+
await uni.hideLoading();
-
+
// 扣除积分
await this.deductPoints(points);
-
+
uni.showToast({
title: '下载成功',
icon: 'success'
})
-
+
} catch (error) {
await uni.hideLoading();
throw new Error(`下载失败: ${error.message}`);
}
}
-
- static async downloadInApp(file_path, points) {
+
+ static async downloadInApp(file_path, points, file_type) {
try {
- // 下载文件到临时路径
- const downloadResult = await uni.downloadFile({
- url: file_path
+ // 下载到临时文件
+ const downloadResult = await new Promise((resolve, reject) => {
+ uni.downloadFile({
+ url: file_path,
+ success: (res) => {
+ if (res.statusCode === 200) {
+ resolve(res)
+ } else {
+ reject(new Error(`下载失败,状态码: ${res.statusCode}`))
+ }
+ },
+ fail: (err) => reject(err)
+ })
})
-
- if (downloadResult.statusCode !== 200) {
- throw new Error('下载文件失败')
- }
-
- const { tempFilePath } = downloadResult;
-
+
+ const {
+ tempFilePath
+ } = downloadResult
+
// 检查相册权限
const hasPermission = await this.checkPhotoAlbumPermission()
if (!hasPermission) {
throw new Error('无相册权限')
}
-
+
if (file_type = 'image') {
// 保存到相册
await uni.saveImageToPhotosAlbum({
@@ -173,17 +181,20 @@
filePath: tempFilePath
})
}
-
+
await uni.hideLoading();
-
+
// 扣除积分
await this.deductPoints(points);
-
+
uni.showToast({
title: '下载成功',
icon: 'success'
- })
-
+ });
+
+ // 清理临时文件
+ this.cleanTempFile(tempFilePath)
+
} catch (error) {
await uni.hideLoading();
throw error
@@ -195,10 +206,29 @@
// #ifdef MP-WEIXIN
uni.getSetting({
success: (res) => {
- if (res.authSetting['scope.writePhotosAlbum']) {
- resolve(true)
+ if (res.authSetting['scope.writePhotosAlbum'] === false) {
+ // 用户之前拒绝过授权
+ uni.showModal({
+ title: '需要相册权限',
+ content: '需要您授权相册权限才能保存图片',
+ success: (modalRes) => {
+ if (modalRes.confirm) {
+ uni.openSetting({
+ success: (settingRes) => {
+ resolve(!!settingRes
+ .authSetting[
+ 'scope.writePhotosAlbum'
+ ])
+ },
+ fail: () => resolve(false)
+ })
+ } else {
+ resolve(false)
+ }
+ }
+ })
} else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
- // 首次尝试授权
+ // 首次申请权限
uni.authorize({
scope: 'scope.writePhotosAlbum',
success: () => resolve(true),
@@ -208,14 +238,21 @@
}
})
} else {
- this.showAuthGuide()
- resolve(false)
+ // 已有权限
+ resolve(true)
}
- }
+ },
+ fail: () => resolve(false)
})
// #endif
- // #ifdef APP-PLUS || H5
+ // #ifdef APP-PLUS
+ // App端默认有权限或使用原生权限申请
+ resolve(true)
+ // #endif
+
+ // #ifdef H5
+ // H5端不需要相册权限
resolve(true)
// #endif
})
@@ -246,4 +283,16 @@
);
})
}
+
+ /**
+ * 清理临时文件
+ */
+ static cleanTempFile(tempFilePath) {
+ uni.getFileSystemManager().unlink({
+ filePath: tempFilePath,
+ fail: (err) => {
+ console.warn('清理临时文件失败:', err)
+ }
+ })
+ }
}
\ No newline at end of file
--
Gitblit v1.9.2