Pārlūkot izejas kodu

更改上传图片张数

Limengbo 6 gadi atpakaļ
vecāks
revīzija
5ec5fecfde

+ 60 - 52
pages/input_content/input_content.js

@@ -17,6 +17,7 @@ Page({
     tempFilePath: [],
     imgId: [],
     textValue: '',
+    upload: true
     //type: '',
     //columnType: ''
   },
@@ -34,61 +35,68 @@ Page({
    * 图片上传
    */
   uploading: function () {
-    var that = this;  
-    wx.chooseImage({  
-      count: 2,  //最多可以选择的图片总数  
-      sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有  
-      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有  
-      success: function (res) {  
-        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片  
-        var tempFilePaths = res.tempFilePaths; 
-        //启动上传等待中...  
-        wx.showToast({  
-          title: '正在上传...',  
-          icon: 'loading',  
-          mask: true,  
-          duration: 1000 
-        })  
-        that.data.tempFilePath.push(tempFilePaths);
-        that.setData({
-          tempFilePath:  that.data.tempFilePath
-        })
-        var uploadImgCount = 0;  
-        for (var i = 0, h = tempFilePaths.length; i < h; i++) {  
-          //上传文件
-          wx.uploadFile({  
-            url: HOST + '/cms/file/upload',  
-            filePath: tempFilePaths[i],  
-            name: 'uploadfile_ant',  
-            header: {  
-              "Content-Type": "multipart/form-data"  
-            },  
-            success: function (res) {  
-              uploadImgCount++;  
-              let data = JSON.parse(res.data);
-              that.data.imgId.push(data.data.id)
-              that.setData({
-                imgId: that.data.imgId,
-              })
-              console.log(data);
-              //如果是最后一张,则隐藏等待中  
-              if (uploadImgCount == tempFilePaths.length) {  
+    const that = this;
+    const length = that.data.tempFilePath.length;
+    if(length < 2) {
+      wx.chooseImage({  
+        count: 2,  //最多可以选择的图片总数  
+        sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有  
+        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有  
+        success: function (res) {  
+          // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片  
+          var tempFilePaths = res.tempFilePaths; 
+          //启动上传等待中...  
+          wx.showToast({  
+            title: '正在上传...',  
+            icon: 'loading',  
+            mask: true,  
+            duration: 1000 
+          })  
+          that.data.tempFilePath.push(tempFilePaths);
+          that.setData({
+            tempFilePath:  that.data.tempFilePath
+          })
+          var uploadImgCount = 0;  
+          for (var i = 0, h = tempFilePaths.length; i < h; i++) {
+            //上传文件
+            wx.uploadFile({  
+              url: HOST + '/cms/file/upload',  
+              filePath: tempFilePaths[i],  
+              name: 'uploadfile_ant',  
+              header: {  
+                "Content-Type": "multipart/form-data"  
+              },  
+              success: function (res) {  
+                uploadImgCount++;  
+                let data = JSON.parse(res.data);
+                that.data.imgId.push(data.data.id)
+                that.setData({
+                  imgId: that.data.imgId,
+                })
+                console.log(data)
+                //如果是最后一张,则隐藏等待中  
+                if (uploadImgCount == tempFilePaths.length) {  
+                  wx.hideToast();  
+                }  
+              },  
+              fail: function (res) {  
                 wx.hideToast();  
+                wx.showModal({  
+                  title: '错误提示',  
+                  content: '上传图片失败',  
+                  showCancel: false,  
+                  success: function (res) { }  
+                })  
               }  
-            },  
-            fail: function (res) {  
-              wx.hideToast();  
-              wx.showModal({  
-                title: '错误提示',  
-                content: '上传图片失败',  
-                showCancel: false,  
-                success: function (res) { }  
-              })  
-            }  
-          });  
+            });  
+          }  
         }  
-      }  
-    }); 
+      }); 
+    }else {
+      this.setData({
+        upload: false
+      })
+    }
   },
   //点击发送
   send: function() {

+ 1 - 1
pages/input_content/input_content.wxml

@@ -10,7 +10,7 @@
             <block wx:for="{{tempFilePath}}" wx:key="{{index}}">
                 <image class="minImg" mode='aspectFill' src="{{item}}" bindtap="listenerButtonPreviewImage" data-index="{{index}}"/>
             </block>
-            <image src="../image/add.png" class="add" bindtap="uploading"/>
+            <image src="../image/add.png" class="add" bindtap="uploading" wx:if="{{upload}}"/>
         </view>
     </view>
 </view>