Browse Source

solve bugs on me

StephenArk30 6 years ago
parent
commit
ddb96b0627

+ 122 - 33
sourcecode/h5app/vue/src/components/schoolCard.vue

@@ -61,45 +61,68 @@
                     this.loading=false;
                     return;
                 }
-                let that=this;
-                console.log('------- photo -------');
-                console.log(this.photo);
-                let formData = new FormData();
-                formData.append('file', this.photo);
-                BasicFunction.get_data("fileserver/upload?sign=abcdefg", function (resUploadPic) {
-                    if (resUploadPic === undefined || resUploadPic.model === undefined || resUploadPic.model.length === 0 || resUploadPic.model.list === undefined) {
-                        Toast.fail('上传失败,请重试');
-                        return;
-                    }
+                this.compressImg();
+            },
+            compressImg: function () {
+                let that = this;
+                let fileObj = this.photo;
+                photoCompress(fileObj, {
+                    quality: 0.2
+                }, function (base64Codes) {
+                    let formData = new FormData();
+                    //console.log("压缩后:" + base.length / 1024 + " " + base);
+                    var bl = convertBase64UrlToBlob(base64Codes);
+                    // formData.append('file', fileObj);
+                    console.log(fileObj);
+                    formData.append("file", bl); // 文件对象
+                    console.log(bl);
+                    console.log("file_" + Date.parse(new Date()) + ".jpg");
+                    that.uploadImg(formData);
+                });
+            },
+            uploadImg(formData) {
+                let that = this;
+                BasicFunction.get_data("fileserver/upload?sign=abcdefg", function (res) {
                     try {
-                        let photoURL = resUploadPic.model;
-                        if (resUploadPic.ret === '10000') {
-                            let userid = localStorage.getItem('frontend-userid');
-                            BasicFunction.get_data('smartIdentity/list?userId=' + userid, function (resUploadPic) {
-                                // console.log('----- User Identity -----');
-                                // console.log(resUploadPic);
-                                if (resUploadPic.ret === '10000') {
-                                    let t = resUploadPic.model.list[0];
-                                    t.userSchoolCardPic = photoURL;
-                                    t.userReviewStatus = 100;
-                                    t.reviewerNote = "未审核";
-                                    t.reviewerNoteToUser = "未审核";
-                                    BasicFunction.get_data('smartIdentity/updateBatch', function (res) {
-                                        if (res.ret === '10000') {
-                                            Toast.success('上传成功');
-                                            location.reload();
-                                        }
-                                        else Toast.fail('上传失败,请重试');
-                                    }, [t]);
-                                } else Toast.fail('访问失败,请重试');
-                            }, {});
-                        } else Toast.fail('上传失败,请重试');
+                        if (res.ret === '10000' && res.model !== undefined && res.model.length !== 0) {
+                            let imgURL = res.model;
+                            that.updateInfo(imgURL);
+                        } else {
+                            Toast.fail('上传图片失败,请检查网络');
+                            that.loading = false;
+                        }
                     } catch (e) {
                         console.warn(e);
-                        Toast.fail('上传失败,请重试');
+                        Toast.fail('上传图片失败,请检查网络');
+                        that.loading = false;
                     }
                 }, formData);
             },
+            updateInfo(image) {
+                let that = this;
+                let useridentity = that.imgDo;
+                if (image != null) {
+                    useridentity.userSchoolCardPic = image;
+                }
+                else return;
+                useridentity.userSchoolCardPic = image;
+                useridentity.userReviewStatus = 100;
+                useridentity.reviewerNote = "等待审核";
+                useridentity.reviewerNoteToUser = "等待审核";
+                BasicFunction.get_data("smartIdentity/updateBatch", function (response) {
+                    // console.log(useridentity);
+                    // console.log(response);
+                    if (response.ret === "10000") {
+                        // console.log('----- User Info -----');
+                        Toast.success("上传成功");
+                        that.loading = false;
+                        location.reload();
+                    } else {
+                        Toast.fail("上传失败,请检查网络");
+                        that.loading = false;
+                    }
+                }, [useridentity]);
+            },
             onRead: function (e) {
                 this.photo = e;
             }
@@ -152,6 +175,72 @@
             }, {});
         }
     }
+
+    /*
+        三个参数
+        file:一个是文件(类型是图片格式),
+        w:一个是文件压缩的后宽度,宽度越小,字节越小
+        objDiv:一个是容器或者回调函数
+        photoCompress()
+         */
+    function photoCompress(file, w, objDiv) {
+        var ready = new FileReader();
+        /*开始读取指定的Blob对象或File对象中的内容. 当读取操作完成时,readyState属性的值会成为DONE,如果设置了onloadend事件处理程序,则调用之.同时,result属性中将包含一个data: URL格式的字符串以表示所读取文件的内容.*/
+        ready.readAsDataURL(file);
+        ready.onload = function () {
+            var re = this.result;
+            canvasDataURL(re, w, objDiv)
+        }
+    }
+
+    function canvasDataURL(path, obj, callback) {
+        var img = new Image();
+        img.src = path;
+        img.onload = function () {
+            var that = this;
+            // 默认按比例压缩
+            var w = that.width,
+                h = that.height,
+                scale = w / h;
+            w = obj.width || w;
+            h = obj.height || (w / scale);
+            var quality = 0.7;  // 默认图片质量为0.7
+            //生成canvas
+            var canvas = document.createElement('canvas');
+            var ctx = canvas.getContext('2d');
+            // 创建属性节点
+            var anw = document.createAttribute("width");
+            anw.nodeValue = w;
+            var anh = document.createAttribute("height");
+            anh.nodeValue = h;
+            canvas.setAttributeNode(anw);
+            canvas.setAttributeNode(anh);
+            ctx.drawImage(that, 0, 0, w, h);
+            // 图像质量
+            if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
+                quality = obj.quality;
+            }
+            // quality值越小,所绘制出的图像越模糊
+            var base64 = canvas.toDataURL('image/jpeg', quality);
+            // 回调函数返回base64的值
+            callback(base64);
+        }
+    }
+
+    /**
+     * 将以base64的图片url数据转换为Blob
+     * @param urlData
+     *            用url方式表示的base64图片数据
+     */
+    function convertBase64UrlToBlob(urlData) {
+        var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
+            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
+        while (n--) {
+            u8arr[n] = bstr.charCodeAt(n);
+        }
+        return new Blob([u8arr], {type: mime});
+    }
+
 </script>
 
 <style scoped>

+ 0 - 2
sourcecode/h5app/vue/src/views/SecurityVerify.vue

@@ -178,7 +178,6 @@
                     return;
                 }
 
-                let that = this;
                 this.compressImg();
             },
             compressImg: function () {
@@ -217,7 +216,6 @@
                 }, formData);
             },
             updateInfo(image) {
-                let userId = localStorage.getItem('frontend-userid');
                 let that = this;
                 let useridentity = that.identityDo;
                 useridentity.userWxAccount = that.wechat;