Jelajahi Sumber

保存分享码

lifanagju_citu 7 bulan lalu
induk
melakukan
a0c8aed370
2 mengubah file dengan 38 tambahan dan 30 penghapusan
  1. 13 7
      pages/index/my.vue
  2. 25 23
      utils/base64src.js

+ 13 - 7
pages/index/my.vue

@@ -48,7 +48,7 @@
 					<view class="qrCode">
 						<image :src="shareUrl" style="width: 200px;height: 200px;"></image>
 					</view>
-					<!-- <button v-if="shareUrl" class="send-button ss-m-b-30" @tap="handleSaveShareUrl">保存</button> -->
+					<view v-if="shareUrl" class="saveImg" @tap="handleSaveShareUrl">保存图片</view>
 				</view>
 			</uni-popup>
 		</view>
@@ -65,7 +65,7 @@ import layoutPage from '@/layout'
 import { showAuthModal } from '@/hooks/useModal'
 import { onShow } from '@dcloudio/uni-app'
 import { getJobAdvertisedShareQrcode } from '@/api/user'
-// import { base64src } from '@/utils/base64src.js'
+import { base64src } from '@/utils/base64src.js'
 // 设置自定义tabbar选中值
 onShow(() => {
     const currentPage = getCurrentPages()[0];  // 获取当前页面实例
@@ -134,12 +134,16 @@ const shareUrl = ref()
 // 生成分享二维码
 const handleShareCode = async () => {
 	const userId = useUserStore?.accountInfo?.userId || ''
+	console.log(1, 'userId', userId)
 	if (!userId) {
 		uni.showToast({
 			title: '请先登录',
 			icon: 'none'
 		})
+		showAuthModal()
+		return
 	}
+  // if (!getAccessToken())
 	const query = {
 		scene: 'shareId=' + userId,
 		path: 'pages/login/index',
@@ -155,7 +159,7 @@ const handleShareCode = async () => {
 
 // 保存到本地
 const handleSaveShareUrl = async () => {
-	const convertData = await base64src(shareUrl.value, '我的分享码')
+	base64src(shareUrl.value, '我的分享码')
 }
 
 // 登录
@@ -223,10 +227,7 @@ const handleLogoutConfirm = () => {
 	margin: 0 0 20rpx 30rpx;
 }
 .shareQrCodePopupContent {
-	// width: 80vw;
-	// display: flex;
-	// flex-direction: column;
-	margin: 30rpx;
+	width: 80vw;
 	padding: 50rpx;
 	text-align: center;
 	background-color: #fff;
@@ -236,5 +237,10 @@ const handleLogoutConfirm = () => {
 	.qrCode {
 		margin-left: 4px;
 	}
+	.saveImg {
+		text-align: center;
+		margin-top: 10px;
+		color: #00897B;
+	}
 }
 </style>

+ 25 - 23
utils/base64src.js

@@ -1,26 +1,28 @@
-const fsm = wx.getFileSystemManager();
 
-function base64src(base64data, fileName) {
-  fileName = fileName || 'file_base64src'; //自定义文件名
-  const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
-  if (!format) {
-    return (new Error('ERROR_BASE64SRC_PARSE'));
-  }
-  const filePath = `${wx.env.USER_DATA_PATH}/${fileName}.${format}`;
-  const buffer = wx.base64ToArrayBuffer(bodyData);
-  return new Promise((resolve, reject) =>{
-    fsm.writeFile({
-        filePath,
-        data: buffer,
-        encoding: 'binary',
-        success() {
-            resolve(filePath);
-        },
-        fail() {
-            reject(new Error('ERROR_BASE64SRC_WRITE'));
-        },
-    });
-  })
-};
+function base64src(data, fileName) {
+	const fsm = wx.getFileSystemManager()
+	if (!data) return
+	fsm.writeFile({
+		filePath: `wx.env.USER_DATA_PATH+'/${fileName}.png'`,
+		data: data.slice(22),
+		encoding:'base64',
+		success: res => {
+			wx.saveImageToPhotosAlbum({
+				filePath: `wx.env.USER_DATA_PATH + '/${fileName}.png'`,
+				success: function (res) {
+					wx.showToast({
+						title: '保存成功',
+					})
+				},
+				fail: function (err) {
+					console.log(err)
+				}
+			})
+			// console.log(res)
+		}, fail: err => {
+			console.log(err)
+		}
+	})
+}
 
 export { base64src };