huangmp 2 лет назад
Родитель
Сommit
2a1af2585d
1 измененных файлов с 47 добавлено и 63 удалено
  1. 47 63
      src/views/modules/prod/prodTemplate-add-or-update.vue

+ 47 - 63
src/views/modules/prod/prodTemplate-add-or-update.vue

@@ -120,72 +120,56 @@ export default {
         }
       })
     },
+    //验证图片尺寸
+    checkImageSize(imageStr, index) {
+      let width = 0;
+      let height = 0;
+      let templateUsage = this.usageList.join(",");
+      return new Promise((resolve, reject) => {
+        let image = new Image()
+        image.onload = function() {
+          if(templateUsage.indexOf("smallcard") !== -1 || templateUsage.indexOf("polaroid") !== -1){
+            width = 709;
+            height = 1087;
+          }else if(templateUsage.indexOf("stub") !== -1){
+            width = 780;
+            height = 1489;
+          }
+          if (image.width === width && image.height === height) {
+            resolve();
+          } else {
+            reject(index);
+          }
+        };
+        image.src = this.resourcesUrl + imageStr;
+      });
+    },
     // 表单提交
-    dataFormSubmit () {
-      let valid = true;
-        let promise = new Promise((resolve, reject) =>{
-            let imgUrlArr = this.dataForm.templatePic.split(",");
-            let width = 0;
-            let height = 0;
-            let templateUsage = this.usageList.join(",");
-            if(templateUsage.indexOf("smallcard") !== -1 || templateUsage.indexOf("polaroid") !== -1){
-              width = 709;
-              height = 1087;
-            }else if(templateUsage.indexOf("stub") !== -1){
-              width = 780;
-              height = 1489;
+    async dataFormSubmit () {
+      try{
+        let imgUrlArr = this.dataForm.templatePic.split(",");
+        await Promise.all(imgUrlArr.map(this.checkImageSize));
+        this.handleStyleName(this.dataForm.templateStyleId);
+        this.handleSeriesName(this.dataForm.templateSeriesId);
+        this.dataForm.templateUsage = this.usageList.join(",");
+        this.$http({
+          url: this.$http.adornUrl('/prod/prodTemplate'),
+          method: this.dataForm.templateId ? 'put' : 'post',
+          data: this.$http.adornData(this.dataForm)
+        }).then(({data}) => {
+          this.$message({
+            message: this.$i18n.t('publics.operation'),
+            type: 'success',
+            duration: 1500,
+            onClose: () => {
+              this.visible = false
+              this.$emit('refreshDataList')
             }
-            imgUrlArr.forEach((item, index) =>{
-              let img = new Image();
-              img.onload = () => {
-                valid = img.width == width && img.height == height;
-                if(valid){
-
-                }else{
-                  this.$message.error("上传的图片" + (parseInt(index) + 1) + "宽高必须是" + width + "*" + height);
-                  return Promise.reject();
-                }
-              };
-              img.src = this.resourcesUrl + item;
-            });
-          });
-        if(valid){
-          promise.then((res)=>{
-            this.handleStyleName(this.dataForm.templateStyleId);
-            this.handleSeriesName(this.dataForm.templateSeriesId);
-            this.dataForm.templateUsage = this.usageList.join(",");
-            this.$http({
-              url: this.$http.adornUrl('/prod/prodTemplate'),
-              method: this.dataForm.templateId ? 'put' : 'post',
-              data: this.$http.adornData(this.dataForm)
-            }).then(({data}) => {
-              this.$message({
-                message: this.$i18n.t('publics.operation'),
-                type: 'success',
-                duration: 1500,
-                onClose: () => {
-                  this.visible = false
-                  this.$emit('refreshDataList')
-                }
-              })
-            })
           })
-        }
-    },
-    getStyleList () {
-      this.$http({
-        url: this.$http.adornUrl('/prod/prodTemplateStyle/page'),
-        method: 'get',
-        params: this.$http.adornParams(
-                Object.assign({
-                  size: 100,
-                  shopId: this.$store.state.user.shopId
-                }
-                )
-        )
-      }).then(({data}) => {
-        this.styleList = data.records
-      })
+        })
+      }catch (error) {
+        this.$message.error(`第 ${error + 1} 张图片不符合尺寸要求。`)
+      }
     },
     getSeriesList () {
       this.$http({