Browse Source

二维码下载

zouling 3 months ago
parent
commit
68e76a479f
1 changed files with 89 additions and 26 deletions
  1. 89 26
      property_ui/manage/pages/community/sqmoneydetail.vue

+ 89 - 26
property_ui/manage/pages/community/sqmoneydetail.vue

@@ -110,32 +110,95 @@
 			},
 			getDown(urls){
 				// var url=this.baseUrl+urls
-				var url=this.yy_code;
-				// uni.downloadFile({
-				// 	url: url,//文件的下载路径
-				// 	success(res) {
-				// 		if (res.statusCode === 200) {
-				// 		uni.saveImageToPhotosAlbum({ //保存图片到系统相册。
-				// 			filePath: res.tempFilePath, //图片文件路径
-				// 			success: function() {
-				// 				uni.showToast({
-				// 					title: '图片保存成功',
-				// 					icon: 'none',
-				// 				});
-				// 			},
-				// 			fail: function(e) {
-				// 				uni.showToast({
-				// 					title: '图片保存失败',
-				// 					icon: 'none',
-				// 				});
-				// 			}
-				// 		});
-				// 	}
-				// 	},
-				// 	fail(res) {
-				// 		uni.hideLoading()
-				// 	}
-				// })
+				var imgbase64url=this.yy_code;
+				// #ifdef H5
+					var fileName =  `/code${new Date().getTime()}.png`;
+					var arr = imgbase64url.split(','),
+					        mime = arr[0].match(/:(.*?);/)[1],
+					        bstr = atob(arr[1]),
+					        n = bstr.length,
+					        u8arr = new Uint8Array(n);
+					while (n--) {
+					    u8arr[n] = bstr.charCodeAt(n);
+					}
+					var blob = new Blob([u8arr], { type: mime });
+										
+					// 创建一个下载链接
+					var a = document.createElement('a');
+					a.download = fileName; // 设置下载文件名
+					a.href = URL.createObjectURL(blob); // 将Blob对象转换为URL
+					a.style.display = 'none'; // 隐藏下载链接
+					document.body.appendChild(a); // 将下载链接添加到文档中
+										
+					// 模拟点击下载链接
+					a.click();
+										
+					// 清理
+					URL.revokeObjectURL(a.href); // 释放URL对象
+					document.body.removeChild(a); // 移除下载链接
+				// #endif
+				// #ifdef MP-WEIXIN
+					let base64 = imgbase64url.replace(/^data:image\/\w+;base64,/, ""); //图片替换
+					let filePath = wx.env.USER_DATA_PATH + `/qrcode_id_${new Date().getTime()}.png`;
+					uni.getFileSystemManager().writeFile({
+						filePath: filePath, //创建一个临时文件名
+						data: base64, //写入的文本或二进制数据
+						encoding: 'base64', //写入当前文件的字符编码
+						success: (res) => {
+							uni.saveImageToPhotosAlbum({
+								filePath: filePath,
+								success: () => {
+									uni.showToast({
+										title: "图片保存成功,请到手机相册查看",
+										icon: "none",
+										duration: 5000
+									})
+								},
+								fail: (err) => {
+									console.log(err);
+									uni.showToast({
+										title: '图片保存失败',
+										icon: "none",
+										duration: 5000
+									})
+								}
+							})
+						},
+						fail: (err) => {
+							console.log(err)
+						}
+					})
+					// #endif
+					// #ifdef APP-PLUS
+					const bitmap = new plus.nativeObj.Bitmap("test");
+					bitmap.loadBase64Data(imgbase64url, function () {
+						const url = `_doc/qrcode_id_${new Date().getTime()}.png`; // url为时间戳命名方式
+						bitmap.save(url, {
+							overwrite: true,  // 是否覆盖
+							// quality: 'quality'  // 图片清晰度
+						}, (i) => {
+							uni.saveImageToPhotosAlbum({
+								filePath: url,
+								success: function () {
+									uni.showToast({
+										title: "图片保存成功,请到手机相册查看",
+										position: "center",
+										icon: "none",
+									})
+									bitmap.clear()
+								}
+							});
+						}, (e) => {
+							uni.showToast({
+								title: '图片保存失败',
+								icon: 'none'
+							})
+							bitmap.clear()
+						});
+					}, (e) => {
+						bitmap.clear()
+					});
+					// #endif
 			},
 			getPreview(idx,arr) {
 				var newArr=[];