Michael Wang il y a 5 ans
Parent
commit
99774851af
4 fichiers modifiés avec 126 ajouts et 51 suppressions
  1. 1 1
      .postcssrc.js
  2. 16 15
      src/main.js
  3. 105 35
      src/views/index.vue
  4. 4 0
      vue.config.js

+ 1 - 1
.postcssrc.js

@@ -2,4 +2,4 @@ module.exports = {
   plugins: {
     autoprefixer: {}
   }
-}
+};

+ 16 - 15
src/main.js

@@ -12,6 +12,7 @@ Vue.config.productionTip = false;
 // Vue.use(VueResource);
 Vue.use(ElementUI);
 
+const SAVE_API = "/api/ecs_outlet/facepp/add";
 const qmInstance = axios.create({});
 let wk_token;
 let wk_key;
@@ -39,7 +40,7 @@ qmInstance.interceptors.request.use(
       }
     }
 
-    if (config.url.startsWith("https://api-cn.faceplusplus.com")) {
+    if (config.url.includes(SAVE_API)) {
       window.requestInProcessingTotal++;
     }
 
@@ -68,7 +69,7 @@ function calcErrorPerMinute(now) {
 //响应拦截
 qmInstance.interceptors.response.use(
   response => {
-    if (response.config.url.startsWith("https://api-cn.faceplusplus.com")) {
+    if (response.config.url.includes(SAVE_API)) {
       window.requestInProcessingTotal--;
     }
 
@@ -78,16 +79,16 @@ qmInstance.interceptors.response.use(
     return response;
   },
   error => {
-    if (error.config.url.startsWith("https://api-cn.faceplusplus.com")) {
+    if (error.config.url.includes(SAVE_API)) {
       window.requestInProcessingTotal--;
     }
     // 这里是返回状态码不为200时候的错误处理
     const status = error.response.status;
     if (status != 200) {
       console.log(error);
-      if (error.response.data.error_message) {
-        console.log(error.response.data.error_message);
-        if (error.response.data.error_message.toLowerCase().includes("limit")) {
+      if (error.response.data.desc) {
+        console.log(error.response.data.desc);
+        if (error.response.data.desc.toLowerCase().includes("系统")) {
           // 这是facepp并发错误的response特征
           const now = Date.now();
           faceppConCurrencyErrorArray.push(now);
@@ -145,15 +146,15 @@ qmInstance.defaults.timeout = 10000; //超时时间
 qmInstance.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"; //标识这是一个 ajax 请求
 
 axiosRetry(qmInstance, {
-  retries: 5, // 重试5
-  retryCondition: err => {
-    // 根据err的特征来执行请求,目前是全部请求
-    console.log(
-      `below error status code: ${err.response.status} and retry ...`
-    );
-    console.log(err);
-    return true;
-  }
+  retries: 3 // 重试3
+  // retryCondition: err => {
+  //   // 根据err的特征来执行请求,目前是全部请求
+  //   console.log(
+  //     `below error status code: ${err.response.status} and retry ...`
+  //   );
+  //   console.log(err);
+  //   return true;
+  // }
 });
 
 Vue.prototype.$http = qmInstance;

+ 105 - 35
src/views/index.vue

@@ -45,7 +45,7 @@
               <span style="color:black;">{{batchConcurrency}}</span>
             </span>
             <span>
-              并发请求:
+              实时并发:
               <span style="color:black;">{{reqNum}}</span>
             </span>
             <span>
@@ -80,7 +80,7 @@
           :key="msgInfo.fileName"
         >
           <p class='console-line'>
-            <span>{{msgInfo.fileName}}:{{msgInfo.msg}}</span>
+            <span class="filename">{{msgInfo.fileName}}</span><span>{{msgInfo.msg}}</span>
           </p>
         </div>
       </div>
@@ -89,7 +89,7 @@
 </template>
 
 <script>
-const CryptoJS = require("crypto-js");
+// const CryptoJS = require("crypto-js");
 const Base64 = require("js-base64").Base64;
 const fs = window.nodeRequire("fs");
 const path = window.nodeRequire("path");
@@ -247,17 +247,19 @@ export default {
         return;
       }
       const photoFile = fs.readFileSync(studentPhotoPath);
+      const photoFileName =path
+        .basename(studentPhotoPath) 
       const rootOrgId = localStorage.getItem("rootOrgId");
 
       //生成新名称
-      let serverPhotoPath = null;
-      const upyunPhotoPath = studentId => {
-        const md5Hash = CryptoJS.MD5(
-          Base64.encode(studentId + new Date().getTime())
-        ).toString();
-        serverPhotoPath = md5Hash + fileSuffix;
-        return rootOrgId + "/" + studentId + "/" + md5Hash + fileSuffix;
-      };
+      // let serverPhotoPath = null;
+      // const upyunPhotoPath = studentId => {
+      //   const md5Hash = CryptoJS.MD5(
+      //     Base64.encode(studentId + new Date().getTime())
+      //   ).toString();
+      //   serverPhotoPath = md5Hash + fileSuffix;
+      //   return rootOrgId + "/" + studentId + "/" + md5Hash + fileSuffix;
+      // };
 
       // 核心流程:
       // 1. get studentId from ecs
@@ -269,24 +271,29 @@ export default {
       // 每一步出错都会保存到错误日志
       try {
         // 不用Promise.all的原因是每一步失败就不用进行下一步了
-        let studentId = await this.getStudentId(rootOrgId, identityNumber);
-        let faceToken = await this.detectFace(photoFile);
-        this.faceSetToken = await this.getFaceSetToken();
-        let faceCount = await this.addFaceToSet(this.faceSetToken, faceToken);
-
-        await this.saveImageToUpyun({
-          upyunPhotoPath: upyunPhotoPath(studentId),
-          photoFile
-        });
-        const photoInfo = {
-          studentId: studentId,
-          faceSetToken: this.faceSetToken,
-          faceToken: faceToken,
-          rootOrgId: rootOrgId,
-          faceCount,
-          photoName: serverPhotoPath
-        };
-        await this.saveStudentFaceInfoByPut(photoInfo);
+        // let studentId = await this.getStudentId(rootOrgId, identityNumber);
+        // let faceToken = await this.detectFace(photoFile);
+        // this.faceSetToken = await this.getFaceSetToken();
+        // let faceCount = await this.addFaceToSet(this.faceSetToken, faceToken);
+
+        // await this.saveImageToUpyun({
+        //   upyunPhotoPath: upyunPhotoPath(studentId),
+        //   photoFile
+        // });
+        // const photoInfo = {
+        //   studentId: studentId,
+        //   faceSetToken: this.faceSetToken,
+        //   faceToken: faceToken,
+        //   rootOrgId: rootOrgId,
+        //   faceCount,
+        //   photoName: serverPhotoPath
+        // };
+        // await this.saveStudentFaceInfoByPut(photoInfo);
+        let studentInfo = await this.getStudentInfo(rootOrgId, identityNumber);
+
+        if (studentInfo.photoPath) {
+          await this.saveStudentBasePhoto(photoFile, photoFileName);
+        }
         this.finishOnePhotoSuccess("处理成功", studentPhotoPath);
 
         {
@@ -327,6 +334,65 @@ export default {
           });
       });
     },
+    async getStudentInfo(rootOrgId, identityNumber) {
+      return new Promise((resolve, reject) => {
+        this.$http
+          .get(
+            "/api/ecs_core/student/getStudentInfo?orgId=" +
+              rootOrgId +
+              "&identityNumber=" +
+              identityNumber
+          )
+          .then(res => {
+            var studentFaceInfo = res.data;
+            if (studentFaceInfo && studentFaceInfo.id) {
+              resolve(studentFaceInfo);
+            } else {
+              reject("查询身份证不存在");
+            }
+          })
+          .catch(err => {
+            console.log(err);
+            reject("根据身份证号码查询失败");
+          });
+      });
+    },
+    async saveStudentBasePhoto(file, filename) {
+      let config = {
+        headers: {
+          "Content-Type": "multipart/form-data",
+        }
+      };
+
+      let formData = new FormData();
+
+      let fileBlob = new Blob([file]);
+      formData.append("file", fileBlob, filename);
+      // formData.append("filename", filename);
+      // formData.append("Content-Type", "image/jpeg");
+
+      return new Promise((resolve, reject) => {
+        this.$http
+          .post("/api/ecs_outlet/facepp/add", formData, config)
+          .then(res => {
+            // var data = res.data;
+            // console.log(data);
+            // if (data) {
+              resolve('保存成功');
+            // } else {
+            //   reject("保存失败");
+            // }
+          })
+          .catch(err => {
+            console.log(err);
+            if(err.response && err.response.data && err.response.data.desc) {
+              reject(err.response.data.desc)
+            } else {
+              reject("保存失败---未知错误");
+            }
+          });
+      });
+    },
     //保存文件至又拍云
     async saveImageToUpyun({ upyunPhotoPath, photoFile }) {
       const url = process.env.VUE_APP_UPYUN_BUCKETURL + upyunPhotoPath;
@@ -619,15 +685,19 @@ export default {
 
 .console-panel .console-line {
   font-size: 14px;
-  text-align: center;
+  /* text-align: center; */
   display: flex;
-  justify-content: center;
-  align-items: center;
+  /* justify-content: center; */
+  /* align-items: center; */
 }
-
-.console-panel .console-line > span {
-  margin: 0 10px;
+.console-line .filename {
+  text-align: right;
+  width: 300px;
+  margin-right: 10px;
 }
+/* .console-panel .console-line > span {
+  margin: 0 10px;
+} */
 
 .console-panel .console-line > span.red {
   color: #fc7156;

+ 4 - 0
vue.config.js

@@ -3,6 +3,10 @@ let proxy = {
     target: process.env.VUE_APP_ECS_CORE, //代理跨域转地址,基础信息
     changeOrigin: true
   },
+  "/api/ecs_outlet/": {
+    target: process.env.VUE_APP_ECS_CORE, //代理跨域转地址,照片上传
+    changeOrigin: true
+  },
   "/facepp/v3": {
     secure: false,
     target: "https://api-cn.faceplusplus.com",