浏览代码

保存图片迁移到用户数据目录files下面

oyq28 3 周之前
父节点
当前提交
263927eba1
共有 2 个文件被更改,包括 97 次插入72 次删除
  1. 2 2
      pages/work/record/RecordList.uvue
  2. 95 70
      pages/work/record/camera-scan-code.uvue

+ 2 - 2
pages/work/record/RecordList.uvue

@@ -68,9 +68,9 @@
 			</view>
 			<view class="my-page">
 				<swiper class="swiper-box my-swiper" :current="0" :indicator-dots="true" v-if="item.urlpdt.length>0">
-					<swiper-item v-for="(url, index) in item.urlpdt" :key="index" @tap="preview(url)">
+					<swiper-item v-for="(url, index) in item.urlpdt" :key="index" @tap="preview(uni.env.USER_DATA_PATH+url)">
 						<view class="swiper-item ">
-							<image class="my-image" :src="url" mode="aspectFill" />
+							<image class="my-image" :src="uni.env.USER_DATA_PATH+url" mode="aspectFill" />
 						</view>
 						<view v-if="item.imgname.length>0">
 							<text class="demo-view-label">{{ item.imgname?.[index] ?? ''}}</text>

+ 95 - 70
pages/work/record/camera-scan-code.uvue

@@ -23,15 +23,15 @@
 		</view>
 
 		<view class="camera-scan-code-table">
-			<view class="camera-scan-code-table-pair" v-if="joinRecord.imgname!=''" >
+			<view class="camera-scan-code-table-pair" v-if="joinRecord.imgname!=''">
 				<view class="camera-scan-code-table-pair-label">
 					<text>已存图片(最多能存{{joinRecord.num}}张)</text>
 				</view>
-				<view class="camera-scan-code-table-pair-value">					
+				<view class="camera-scan-code-table-pair-value">
 					<text class="txt">{{joinRecord.imgname}}</text>
 				</view>
 			</view>
-			
+
 			<view class="camera-scan-code-table-pair">
 				<view class="camera-scan-code-table-pair-label">
 					<text>待存图片</text>
@@ -59,7 +59,7 @@
 		<view class="camera-scan-code-back-wrap">
 			<button type="default" class="btn bg-blue round" @click="navigateBack">返回拍照</button>
 		</view>
-		
+
 		<view class="camera-scan-code-back-wrap">
 			<button type="default" class="btn bg-blue round" @click="navigateExit">退出拍照</button>
 		</view>
@@ -93,15 +93,15 @@
 					senum: 0,
 					photoitem: "",
 					productno: "",
-					descb:"",
+					descb: "",
 					part: "",
 					date: "",
-					partno : "",
-					num : 0,
-					status : 1,
-					urlspl : "",
-					imgname : "",
-					urlpdt : "",
+					partno: "",
+					num: 0,
+					status: 1,
+					urlspl: "",
+					imgname: "",
+					urlpdt: "",
 					pid: 0,
 					workorder: "",
 					invname: ""
@@ -148,11 +148,11 @@
 								if (record != null) {
 									this.num = record.num;
 									this.dyImgName = record.workorder + record.invname + record.part + record.photoitem + record.partno;
-								    this.joinRecord = record
-									this.imgArrLen = record.imgname.indexOf(",")>-1? record.imgname.split(",").length : (record.imgname!=''?1:0)
+									this.joinRecord = record
+									this.imgArrLen = record.imgname.indexOf(",") > -1 ? record.imgname.split(",").length : (record.imgname != '' ? 1 : 0)
 									// 计算最小可用编号
 									this.calculateMinAvailableNumber();
-										
+
 								}
 							}
 						});
@@ -170,7 +170,7 @@
 					url: `/pages/work/record/Camera?id=${this.recordId}&num=${this.num}`
 				})
 			},
-			navigateExit(){
+			navigateExit() {
 				uni.navigateTo({
 					url: `/pages/work/record/InfoDetail?id=${this.joinRecord.pid}`
 				})
@@ -195,23 +195,23 @@
 			},
 			saveImage() {
 				//保存图片进入相册文件
-				if(this.path==''){
+				if (this.path == '') {
 					uni.showToast({
-					  title: '没有拍照文件',
-					  icon: 'error', 
-					  duration: 2000 
+						title: '没有拍照文件',
+						icon: 'error',
+						duration: 2000
 					});
 					return;
 				}
-				if(this.imgArrLen == this.joinRecord.num){
+				if (this.imgArrLen == this.joinRecord.num) {
 					uni.showToast({
-					  title: '拍照图片已满',
-					  icon: 'error', 
-					  duration: 2000 
+						title: '拍照图片已满',
+						icon: 'error',
+						duration: 2000
 					});
 					return;
 				}
-								
+				
 				/*
 				uni.saveImageToPhotosAlbum({
 					filePath: this.path,
@@ -227,73 +227,74 @@
 					},
 				}); */
 
-
 				let updateImgs = ''
 				let updateNames = ''
 				let updateStatus = ''
-				
+
 				// 动态生成最小的可用图片编号
 				const getMinAvailableNumber = () => {
-					if(this.imgArrLen === 0) return 1;
-					
+					if (this.imgArrLen === 0) return 1;
+
 					// 从已有的图片名称中提取编号
-				const existingNumbers = new Set<number>();
-				const imgNames = this.joinRecord.imgname.split(',');
-				
-				imgNames.forEach((name : string) => {
-					if(name != '') {
-						// 提取图片名称中的数字部分
-						const match = name.match(/-(\d+)\.jpg$/);
-						if(match != null && match[1] != null) {
-							const numStr = match[1] as string;
-							existingNumbers.add(parseInt(numStr));
+					const existingNumbers = new Set<number>();
+					const imgNames = this.joinRecord.imgname.split(',');
+
+					imgNames.forEach((name : string) => {
+						if (name != '') {
+							// 提取图片名称中的数字部分
+							const match = name.match(/-(\d+)\.jpg$/);
+							if (match != null && match[1] != null) {
+								const numStr = match[1] as string;
+								existingNumbers.add(parseInt(numStr));
+							}
 						}
-					}
-				});
-					
+					});
+
 					// 查找1到所需图片数量之间最小的未使用编号
-					for(let i = 1; i <= this.joinRecord.num; i++) {
-						if(!existingNumbers.has(i)) {
+					for (let i = 1; i <= this.joinRecord.num; i++) {
+						if (!existingNumbers.has(i)) {
 							return i;
 						}
 					}
-					
+
 					// 如果1到所需数量都被使用了,则使用当前数量+1
 					return this.imgArrLen + 1;
 				};
-				
+
 				const minAvailableNumber = getMinAvailableNumber();
-				const newImgName = this.dyImgName + "-" + minAvailableNumber + ".jpg";
-				
-				if(this.imgArrLen == 0){
-					updateImgs = this.path
+				const newImgName = this.dyImgName + "-" + minAvailableNumber + ".jpg";				
+				const relativePath = `/uploadImgs/${newImgName}`
+
+				if (this.imgArrLen == 0) {
+					updateImgs = relativePath
 					updateNames = newImgName;
-				}else{
-					updateImgs = this.joinRecord.urlpdt + "," + this.path					
+				} else {
+					updateImgs = this.joinRecord.urlpdt + "," + relativePath
 					updateNames = this.joinRecord.imgname + "," + newImgName;
 				}
-				
+
 				let updatedData = "imgname='" + updateNames + "',urlpdt='" + updateImgs + "'"
-				
-				if(this.joinRecord.num === this.imgArrLen + 1 ){				
+
+				if (this.joinRecord.num === this.imgArrLen + 1) {
 					updateStatus = "status='3'"
 					updatedData = updatedData + "," + updateStatus
-				}else if(this.joinRecord.status === 1){
+				} else if (this.joinRecord.status === 1) {
 					updateStatus = "status='2'"
 					updatedData = updatedData + "," + updateStatus
 				}
 				console.log(updatedData)
 				
 				updateData('app_media_record', updatedData, 'sxid', this.recordId.toString()).then((res : UTSJSONObject) => {
-					let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
-					if (dataList != null && dataList.length > 0) {
-						console.log(dataList[0])
+					let data = res?.['data'] as boolean ?? false
+					if (data != null && data== true) {
 						uni.showToast({
 							title: "保存成功!",
 						});
+						const newPath = `${uni.env.USER_DATA_PATH}/uploadImgs/${newImgName}`
+						this.renameFile(this.path, newPath)
 						/*
 						uni.reLaunch({
-						    url: '/pages/record/camera-scan-code?path='+this.path+'&recordId='+this.recordId+'&num='+this.num
+							url: '/pages/record/camera-scan-code?path='+this.path+'&recordId='+this.recordId+'&num='+this.num
 						}); */
 					}
 				});
@@ -306,6 +307,30 @@
 					loop: false // 是否可循环预览
 				});
 			},
+			renameFile(oldPath:string, newPath:string) {
+			    // 先拷贝文件到新路径
+			    uni.getFileSystemManager().copyFile({
+			        srcPath: oldPath,
+			        destPath: newPath,
+			        success: function () {
+			            // 可选:删除原文件,如果你需要释放空间的话
+			            uni.getFileSystemManager().unlink({
+			                filePath: oldPath,
+			                success: function () {
+			                    console.log('原文件已删除');
+			                },
+			                fail: function (unlinkErr) {
+			                    console.error('删除原文件失败', unlinkErr);
+			                }
+			            });
+			            console.log('文件重命名成功');
+			        },
+			        fail: function (copyErr) {
+			            console.error('拷贝文件失败', copyErr);
+			        }
+			    });
+			},
+			
 			handleScanCode(ev : UniCameraScanCodeEvent) {
 				const deatil = ev.detail;
 				this.result = {
@@ -315,34 +340,34 @@
 			},
 			// 计算最小的可用图片编号
 			calculateMinAvailableNumber() {
-				if(this.imgArrLen === 0) {
+				if (this.imgArrLen === 0) {
 					this.minAvailableNumber = 1;
 					return;
 				}
-				
+
 				// 从已有的图片名称中提取编号
 				const existingNumbers = new Set<number>();
 				const imgNames = this.joinRecord.imgname.split(',');
-				
+
 				imgNames.forEach((name : string) => {
-					if(name != '') {
+					if (name != '') {
 						// 提取图片名称中的数字部分
 						const match = name.match(/-(\d+)\.jpg$/);
-						if(match != null && match[1] != null) {
+						if (match != null && match[1] != null) {
 							const numStr = match[1] as string;
 							existingNumbers.add(parseInt(numStr));
 						}
 					}
 				});
-				
+
 				// 查找1到所需图片数量之间最小的未使用编号
-				for(let i = 1; i <= this.joinRecord.num; i++) {
-					if(!existingNumbers.has(i)) {
+				for (let i = 1; i <= this.joinRecord.num; i++) {
+					if (!existingNumbers.has(i)) {
 						this.minAvailableNumber = i;
 						return;
 					}
 				}
-				
+
 				// 如果1到所需数量都被使用了,则使用当前数量+1
 				this.minAvailableNumber = this.imgArrLen + 1;
 			},
@@ -378,9 +403,9 @@
 	.camera-scan-code-table-pair-value {
 		flex-grow: 2;
 	}
-	
+
 	.camera-scan-code-table-pair-value .txt {
-	  font-size: 14px;
+		font-size: 14px;
 	}
 
 	.camera-scan-code-table-top-line {