chenken 7 gadi atpakaļ
vecāks
revīzija
3c1f244ced
2 mainītis faili ar 85 papildinājumiem un 58 dzēšanām
  1. 2 0
      src/main.js
  2. 83 58
      src/views/index.vue

+ 2 - 0
src/main.js

@@ -8,6 +8,8 @@ import './assets/bootstrap/css/bootstrap.min.css'
 import './assets/css/common.css'
 import './assets/css/index.css'
 
+const fs = nodeRequire("fs");
+
 
 Vue.config.productionTip = false;
 Vue.use(VueResource)

+ 83 - 58
src/views/index.vue

@@ -10,8 +10,8 @@
 			<div id='import-div'>
 	            <a href='javascript:;' class='file'>
 	            	<!-- webkitdirectory-->
-	            	<input id='photos' :disabled='uploadDisabled'  multiple="true" title=' ' name='files' type='file'  v-on:change='importPhotos'/>
-	            	<button  id='import-btn'>
+	            	<!--<input id='photos' :disabled='uploadDisabled'  multiple="true" title=' ' name='files' type='file'  v-on:change='importPhotos'/>-->
+	            	<button  id='import-btn' @click="importPhotos">
 		                	上传照片
 		            </button>
 	            </a>
@@ -47,6 +47,7 @@
 <script>
     const CryptoJS = require('crypto-js');
     const Base64 = require('js-base64').Base64;
+    const fs = nodeRequire("fs");
 	export default {
 		data() {
 			return {
@@ -75,23 +76,21 @@
 			},
 			importPhotos() {
 				this.init();
-				var photos = document.getElementById('photos');
-				for(var i = 0;i<photos.files.length;i++){
-					this.photoList.push(photos.files[i]);
-				}
+				var electron = nodeRequire('electron');
+				let dialog = electron.remote.dialog;
+		        dialog.showOpenDialog({
+		            title: '选择照片所在目录',
+		            properties: ["openDirectory"]
+		        }, (folderPaths) => {
+		            if (folderPaths) {
+		            	this.processQueue(folderPaths[0]);
+		            }
+		        });
+			},
+			processQueue(folderPath){
+				this.readFiles(folderPath);
 				this.allNum = this.photoList.length;
 				var _this = this;
-				if(this.allNum > this.photoQuantityLimit){
-					this.$alert('单次上传文件数量不能大于'+this.photoQuantityLimit+',当前数量为'+this.allNum, '提示', {
-		        		showClose:false,
-		                confirmButtonText:'确定',
-		                callback: action => {
-		                    _this.init();
-	    					photos.files = null;
-		                }
-		            })
-					return;
-				}
 				//初始化this.concurrency个到photoLine中,开始处理
 				this.photoLine = this.photoList.splice(0,this.concurrency);
 		    	for(var i = 0;i<this.photoLine.length;i++){
@@ -113,16 +112,50 @@
 		            }
 		            if (_this.photoList.length < 1) {
 		                console.log('photoList处理完毕');
+		                _this.completeShow = true;
 		                clearInterval(uploadTime);
 		            }
 		        },500);
 			},
-		    processStudentPhoto(photo) {
-	    		let photoNameArr = photo.name.split('.');
+			//读取文件
+			readFiles(folderPath) {
+		        let files = fs.readdirSync(folderPath);
+		        files.forEach((fileName)=> {
+		            let fullPath = folderPath + "\\" + fileName;
+		            /*var stat = fs.lstatSync(fullPath);
+		            if (stat.isDirectory()) {
+		                this.readFiles(fullPath);
+		            } else if (this.isImageFile(file)) {
+		            	var student = {}
+		                student.fileName = file;
+		                student.path = fullPath;
+		                this.photoList.push(student);
+		            }*/
+		           //只扫描当前文件夹,不递归扫描
+		            if (this.isImageFile(fileName)) {
+		            	var student = {}
+		                student.fileName = fileName;
+		                student.path = fullPath;
+		                this.photoList.push(student);
+		            }
+		        });
+		    },
+		    isImageFile(fileName) {
+		        let lowercaseName = fileName.toLowerCase();
+		        if (lowercaseName.endsWith(".jpg") || lowercaseName.endsWith(".jpeg") || lowercaseName.endsWith(".png")) {
+		            return true;
+		        } else {
+		            return false;
+		        }
+		    },
+		    processStudentPhoto(studentPhoto) {
+	    		let photoNameArr = studentPhoto.fileName.split('.');
 		        let fileName = photoNameArr[0];//文件名就是身份证号码
 		        let fileSuffix = photoNameArr[1];//文件后缀
+		        
+		        let photo = fs.readFileSync(studentPhoto.path);
 		        //生成新名称
-		        let md5Hash = CryptoJS.MD5(Base64.encode(photo.name+new Date().getTime())).toString();
+		        let md5Hash = CryptoJS.MD5(Base64.encode(studentPhoto.fileName+new Date().getTime())).toString();
 	    		let rootOrgId = localStorage.getItem('rootOrgId');
 		        //根据身份证号码查询学生信息
 		        this.$http.get('/api/ecs_core/studentFaceInfo/identityNumber?orgId='+rootOrgId+'&identityNumber='+fileName, {})
@@ -133,36 +166,37 @@
 		        			if(res.body.faces.length>=1){
 		        				let face_token = res.body.faces[0].face_token;
 		        				this.getFaceSetToken().then((res)=>{
-		        					//var face_set_token = res.bodyText;
-		        					var face_set_token = 'ee564a7abbde7f1287f668ddd945ccfa';
+		        					var face_set_token = res.bodyText;
+		        					//var face_set_token = 'ee564a7abbde7f1287f668ddd945ccfa';
 		        					this.addFaceToSet(face_set_token, face_token).then((res)=>{
 		        						this.saveStudentFaceInfo({
 		        							'studentFaceInfoId':studentFaceInfo.id,
 		        							'studentId':studentFaceInfo.student.id,
 		        							'face_set_token':face_set_token,
 		        							'face_token':face_token,
-		        							'fileName':fileName,
-		        							'file':photo,
+		        							'studentPhoto':studentPhoto,
+		        							'identityNumber':fileName,
 		        							'fileSuffix':fileSuffix,
-		        							'photoNewName':md5Hash+'.'+fileSuffix
+		        							'photoNewName':md5Hash+'.'+fileSuffix,
+		        							'file':photo
 			        					});
 		        					},(err)=>{
-		        						this.finishOne(false,fileName,'addFaceToSet失败',photo);
+		        						this.finishOne(false,'addFaceToSet失败',studentPhoto);
 		        					});
 		        				},(err)=>{
-		        					this.finishOne(false,fileName,'获取FaceSetToken失败',photo);
+		        					this.finishOne(false,'获取FaceSetToken失败',studentPhoto);
 		        				});
 		        			}else{
-		        				this.finishOne(false,fileName,'face++没有检测到人脸',photo);
+		        				this.finishOne(false,'face++没有检测到人脸',studentPhoto);
 		        			}
 		        		},(err)=>{
-		        			this.finishOne(false,fileName,'调用face++检测人脸失败',photo);
+		        			this.finishOne(false,'调用face++检测人脸失败',studentPhoto);
 		        		});
 		        	}else{
-                		this.finishOne(false,fileName,'查询身份证不存在',photo);
+                		this.finishOne(false,'查询身份证不存在',studentPhoto);
 		        	}
                 },(err)=>{
-                	this.finishOne(false,fileName,'根据身份证号码查询失败',photo);
+                	this.finishOne(false,'根据身份证号码查询失败',studentPhoto);
                 });
 			},
 			//保存照片信息
@@ -175,12 +209,13 @@
                 		this.saveStudentFaceInfoByPut(photoInfo);
                 	}
 				 },(err)=>{
-				 	this.finishOne(false,photoInfo.fileName,'saveImageToUpyun失败',photoInfo.file);
+				 	this.finishOne(false,'saveImageToUpyun失败',photoInfo.studentPhoto);
 				 });
 		    },
 		    //保存文件至又拍云
 		    saveImageToUpyun(photoInfo){
-		    	var url = process.env.VUE_APP_UPYUN_BUCKETURL+'/student_base_photo/'+photoInfo.fileName+'/'+photoInfo.photoNewName;
+		    	let fileBlob = new Blob([photoInfo.file]);
+		    	var url = process.env.VUE_APP_UPYUN_BUCKETURL+'/student_base_photo/'+photoInfo.identityNumber+'/'+photoInfo.photoNewName;
 		        var authorization = 'Basic '+Base64.encode(process.env.VUE_APP_UPYUN_OPERATOR+':'+process.env.VUE_APP_UPYUN_PASSWORD);
         		var headers = {headers:{'Authorization':authorization,'Content-Type': 'image/jpeg'}};
 		        return this.$http.put(url,photoInfo.file,headers);
@@ -219,9 +254,9 @@
 		            'updateUser':localStorage.getItem('userName'),
 		            'createType':'IMPORT_TOOL'
 		        }).then((res)=>{
-		        	this.finishOne(true,photoInfo.fileName,'处理成功',photoInfo.file);
+		        	this.finishOne(true,'处理成功',photoInfo.studentPhoto);
 		        },(err)=>{
-		        	this.finishOne(false,photoInfo.fileName,'saveStudentFaceInfoByPost失败',photoInfo.file);
+		        	this.finishOne(false,'saveStudentFaceInfoByPost失败',photoInfo.studentPhoto);
 		        });
 		    },
 		    saveStudentFaceInfoByPut(photoInfo){
@@ -236,39 +271,29 @@
 		            'updateUser':localStorage.getItem('userName'),
 		            'createType':'IMPORT_TOOL'
 		        }).then((res)=>{
-		        	this.finishOne(true,photoInfo.fileName,'处理成功',photoInfo.file);
+		        	this.finishOne(true,'处理成功',photoInfo.studentPhoto);
 		        },(err)=>{
-		        	this.finishOne(false,photoInfo.fileName,'saveStudentFaceInfoByPut失败',photoInfo.file);
+		        	this.finishOne(false,'saveStudentFaceInfoByPut失败',photoInfo.studentPhoto);
 		        });
 		    },
 		    //成功或失败处理
-		    finishOne(isSuccess,fileName,msg,file){
-		    	file.finished = true;
+		    finishOne(isSuccess,msg,studentPhoto){
+		    	var fileName = studentPhoto.fileName;
+		    	studentPhoto.finished = true;
 		    	this.returnMsgList.push({'success':isSuccess,'fileName':fileName,'msg':msg});
 	        	if(isSuccess){
 	        		this.successNum++;
 	        	}else{
 	        		this.errorNum++;
-	        		//如果存在nodejs环境
-	        		if(typeof(nodeRequire) != "undefined"){
-	    				var fs = nodeRequire("fs");
-		        		if(fs){
-		        			var errorfilePath = file.path.substring(0,file.path.lastIndexOf("\\"))+"\\errorfiles";
-			        		if (!fs.existsSync(errorfilePath)) {
-		            			fs.mkdirSync(errorfilePath);
-		            		}
-			        		var readStream = fs.createReadStream(file.path);
-							var writeStream = fs.createWriteStream(errorfilePath+"\\"+file.name);
-							readStream.pipe(writeStream);
-							fs.appendFile(errorfilePath+'\\errorPhotos.txt',fileName+":"+msg+'\n',function (){});
-		        		}
-	        		}
+        			var errorfilePath = studentPhoto.path.substring(0,studentPhoto.path.lastIndexOf("\\"))+"\\errorfiles";
+	        		if (!fs.existsSync(errorfilePath)) {
+            			fs.mkdirSync(errorfilePath);
+            		}
+	        		var readStream = fs.createReadStream(studentPhoto.path);
+					var writeStream = fs.createWriteStream(errorfilePath+"\\"+fileName);
+					readStream.pipe(writeStream);
+					fs.appendFile(errorfilePath+'\\errorPhotos.txt',fileName+":"+msg+'\n',function (){});
 	        	}
-		    	if(this.errorNum+this.successNum == this.allNum){
-		    		var photos = document.getElementById('photos');
-		    		photos.files = null;
-		    		this.completeShow = true;
-		    	}
 		    },
 		    logout() {
 				localStorage.removeItem("rootOrgId");