收藏
回答

微信小程序输入框带图片上传功能,现在的问题是点击第二次提交按钮PhotoName才有值提交到php?

//上传图片的函数,并把上传的图片地址保存在新的变量里面,后期方便保存在数据库同一列里面
djf1(imgphat,phatvar) {
    var that = this;
    console.log(imgphat)
    for (var i = 0; i < imgphat.length; i++) {
      console.log(i);
      wx.uploadFile({
        url: 'https://www.xxx.top:8082/imgup.php',
        filePath: imgphat[i],
        name: 'file',
        success: function (res) {
          var a = JSON.parse(res.data);
          if(phatvar=='PhotoName'){
            that.setData({
              PhotoName:that.data.PhotoName.concat('https://www.xxx.top:8082/'+a.data)
             
            })
          }
          if(phatvar=='PhotoName1'){
            that.setData({
              PhotoName1:that.data.PhotoName1.concat('https://www.xxx.top:8082/'+a.data)
            })
          }
     
          if (res) {
            wx.showToast({
              title: '已提交发布!',
              duration: 3000
            });
          }
        }
      })
    }
 
  },
//把表单的数据和图片在服务器上面的地址提交到php并保存,因为图片是可以为空
handleSubmit(){
      var  that = this;
      var fromex = that.data.value3 
      // if(fromex.id=="" ){
      //   this.djf2("条形码不能为空");
      
      // }
      if(fromex[0] =="" ){
        this.djf2("名称不能为空");
      }
      if(fromex[1] ==""){
        this.djf2("品牌不能为空");
      }
      if(fromex[2]=="" ){
        this.djf2("型号不能为空");
      }
      if(fromex[3]== "" ){
        this.djf2("类型不能为空");
      }
      if(fromex[4] == "" ){
        this.djf2("数量不能为空");
      }
      if(fromex[5] == "" ){
        this.djf2("单位不能为空");
      }
      if(fromex[6] == "" ){
        this.djf2("区域不能为空");
      }
      if(fromex[7] == "" ){
        this.djf2("时间不能为空");
      }
      if(fromex[8] =="" ){
        this.djf2("维保名不能为空");
      }


      if(that.data.value4.length != 0 ){
        that.djf1(that.data.value4,'PhotoName');
      }
      if( that.data.tempImageUrl.length != 0){
        that.djf1(that.data.tempImageUrl,'PhotoName1');
      }
      console.log("一下等于多少")
      console.log(this.data.PhotoName1.length)
      wx.request({
        url: 'https://www.xxx.top:8082/fromdata.php'//仅为示例,并非真实的接口地址
        method:'POST',
        data: {
         'a':fromex,
          'b':this.data.PhotoName,
          'c':this.data.PhotoName1,
        },
        header: {
          'content-type''application/x-www-form-urlencoded' // 默认值
        },
        success (res) {
          console.log(res)
        }
      })
      
    },

回答关注问题邀请回答
收藏

3 个回答

  • 我和你!
    我和你!
    2023-11-17

    该函数是异步执行的

    2023-11-17
    有用
    回复
  • J
    J
    2023-11-16

    wx.uploadFile函数的异步特性导致的,该函数是异步执行的,而且在djf1函数内部的循环启动了多个异步文件上传操作。由于wx.request函数没有等待所有上传完成就执行,可能导致PhotoName在第一次点击提交按钮时尚未完全赋值。你可以使用Promise或回调机制来确保在所有上传完成后再调用wx.request

    2023-11-16
    有用
    回复 2
    • M.
      M.
      2023-11-16
      2023-11-16
      回复
    • J
      J
      2023-11-16回复M.
      return new Promise((resolve, reject) => { 语法错误啊少了个括号
      2023-11-16
      回复
  • M.
    M.
    2023-11-16

    或者那位大佬可以给点思维我处理一下


    2023-11-16
    有用
    回复
登录 后发表内容