ソースを参照

h5上传图片

xuqiaoying 2 年 前
コミット
868b0b48c8
2 ファイル変更53 行追加13 行削除
  1. 52 13
      pages3/polyv/detail.vue
  2. 1 0
      pages4/login/login.vue

+ 52 - 13
pages3/polyv/detail.vue

@@ -583,6 +583,7 @@
               <!-- #endif -->
               <!-- #ifdef H5 -->
               <video
+                :controls='false'
                 id="video"
                 width="400"
                 height="300"
@@ -957,8 +958,8 @@ export default {
     }
   },
   onShow() {
-    // this.photoPopup = true;
-    // this.isTaking = true;
+    this.photoPopup = true;
+    this.isTaking = true;
     // if (
     //   (window.navigator.mediaDevices &&
     //     window.navigator.mediaDevices.getUserMedia) ||
@@ -2831,23 +2832,58 @@ export default {
             return;
           }
           let filePath = "";
-          // #ifdef MP-WEIXIN
-          filePath = options;
-          // #endif
           // #ifdef H5
-          let bytes = window.atob(options.split(",")[1]);
-          let ab = new ArrayBuffer(bytes.length);
-          let ia = new Uint8Array(ab);
-          for (let i = 0; i < bytes.length; i++) {
-            ia[i] = bytes.charCodeAt(i);
+          var localData = options; //dataUrl为base64位
+          let base = atob(localData.substring(localData.indexOf(",") + 1)); // base是将base64编码解码,去掉data:image/png;base64部分
+          let length = base.length;
+          let url = new Uint8Array(length);
+          while (length--) {
+            url[length] = base.charCodeAt(length);
           }
-          filePath = new Blob([ab], { type: "image/jpeg" });
-          // #endif
+          filePath = new File([url], "a.jpg", {
+            type: "image/jpg",
+          });
           console.log("filePath:", filePath);
+
           uni.uploadFile({
             url: ossToken.host,
             name: "file",
-            filePath: filePath,
+            file: filePath,
+            fileType: "image",
+            header: {
+              AuthorizationToken: "WX " + uni.getStorageSync("token"),
+            },
+            formData: {
+              key: ossToken.dir,
+              OSSAccessKeyId: ossToken.accessid,
+              policy: ossToken.policy,
+              Signature: ossToken.signature,
+              callback: ossToken.callback,
+              success_action_status: 200,
+            },
+            success: (result) => {
+              this.$u.toast("上传成功");
+              self.ossAvatarUrl = ossToken.dir;
+              console.log("h5上传成功--:", this.ossAvatarUrl);
+              resolve();
+            },
+            fail: (error) => {
+              console.log("h5上传失败:", error)
+              uni.showToast({
+                title: "上传接口报错,请重新拍照上传" + error,
+                icon: "none",
+              });
+              this.openPhoto();
+              return;
+            },
+          });
+          // #endif
+
+          // #ifdef MP-WEIXIN
+          uni.uploadFile({
+            url: ossToken.host,
+            name: "file",
+            filePath: options,
             fileType: "image",
             header: {
               AuthorizationToken: "WX " + uni.getStorageSync("token"),
@@ -2884,6 +2920,7 @@ export default {
               return;
             },
           });
+          // #endif
         });
       });
     },
@@ -3307,6 +3344,7 @@ export default {
       let compareFaceData = await this.faceRecognition();
       console.log(compareFaceData, "compareFaceData");
       this.compareFaceData = compareFaceData;
+      const waitYS = await this.imageInfos();
       if (compareFaceData >= 80) {
         const waitYS = await this.imageInfos();
         this.postCoursePhotoRecord()
@@ -3334,6 +3372,7 @@ export default {
             }
           })
           .catch((err) => {
+            console.log('拍照记录接口的err', err);
             uni.showToast({
               title: "上传接口报错,请重新拍照上传" + err,
               icon: "none",

+ 1 - 0
pages4/login/login.vue

@@ -397,6 +397,7 @@ export default {
 								})
 								types == 'wxlogin' && this.$method.setUuid(new Date().valueOf() + "")
 							} else {
+								console.log('登录后跳转');
 								uni.removeStorageSync('h5_code')
 								uni.reLaunch({
 									url:'/pages/index/index'