小程序多张递归图上传

//index.js
//获取应用实例
const app = getApp()

Page({
data: {
motto: ‘Hello World’,
userInfo: {},
userid:’-1’,
username:‘本人’,
hasUserInfo: false,
items: [ /数组中的元素是对象/
{ name: ‘早稻’, value: ‘早稻’},
{ name: ‘中稻’, value: ‘中稻’ },
{ name: ‘晚稻’, value: ‘晚稻’ },
],
lzflag:0,
passtype: [
{ value: 0, name: ‘否’ },
{ value: 1, name: ‘是’ }
],
orderno:’’,
idcard: ‘’,
mobile: ‘’,
realname: ‘’,
area_address: ‘’,
bank_name: ‘’,
bank_cardno: ‘’,
crop_name: ‘111’,
insured_areasize: 0,
direct_areasize: ‘’,
insured_amount:0,
rate_value: ‘’,
premium: ‘’,
is_landcircle: ‘否’,
premium_sum: 0,
self_premium: 0,
allowance_amount: 0,
imageurl: [],
imgData: [],
dao1: 0,
dao2: ‘’,
dao3: 0,
dao4: ‘’,
dao5: 0,
dao6: ‘’,
isShow1:false,
isShow2:false,
isShow3:false
},

//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: ‘…/logs/logs’
})
},
onLoad: function (options) {

var parameter = JSON.parse(options.parameter);
this.setData({
  orderno: parameter.orderno,
  idcard: parameter.cardNo,
  mobile: parameter.telephone,
  realname: parameter.userName,
  area_address: parameter.plantAddress,
  bank_name: parameter.openingBank,
  bank_cardno:parameter.bankNo,
  rate_value: parameter.rate_value,
  dao2: parameter.unit_premium,
  dao4: parameter.unit_premium,
  dao6: parameter.unit_premium,
  insured_amount: parameter.insured_amount,
  allowance_amount: parameter.allowance_amount
})
// this.ctx = wx.createCameraContext();
// var userid = wx.getStorageSync('userInfo').userid;
// if (userid) {
//   this.setData({
//     username: wx.getStorageSync('userInfo').username,
//     userid: wx.getStorageSync('userInfo').userid
//   })
// } else {
//   wx.navigateTo({
//     url: '/pages/logn/index',
//   })
// }

},
getUserInfo: function(e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
watchUsername: function (event) {
this.setData({
idcard: event.detail.value
})
},
bindPickerChange: function (e) {
console.log(e);
console.log(‘picker发送选择改变,携带值为’, e.detail.value)
if (e.detail.value==0){
// 0 否 1 是
this.setData({
is_landcircle:‘否’,
index: e.detail.value,
lzflag:0,
imgData:’’
})
} else if (e.detail.value == 1){
this.setData({
is_landcircle:‘是’,
index: e.detail.value,
lzflag: 1
})
}
},
/**

  • 上传照片//选择图片时限制9张,如需超过9张,同理亦可参照此方法上传多张照片
    /
    uploadImg: function () {
    var that = this;
    wx.chooseImage({
    count: 9,
    sizeType: [‘original’, ‘compressed’],
    sourceType: [‘album’, ‘camera’],
    success: function (res) {
    var successUp = 0; //成功
    var failUp = 0; //失败
    var length = res.tempFilePaths.length; //总数
    var count = 0; //第几张
    that.setData({
    imageurl: res.tempFilePaths
    })
    that.uploadOneByOne(res.tempFilePaths, successUp, failUp, count, length);
    },
    });
    },
    /
    *

    • 采用递归的方式上传多张
      */
      uploadOneByOne(imgPaths, successUp, failUp, count, length) {
      var that = this;
      wx.showLoading({
      title: ‘正在上传第’ + count + ‘张’,
      })
      wx.uploadFile({
      url: app.globalData.url + ‘UpLoad’,
      filePath: imgPaths[count],
      name: ‘file’,
      formData: {
      file: ‘file’,
      openid: app.globalData.openid,
      idcard: that.data.idcard
      },
      complete: function (e) {
      count++;//下一张
      if (count == length) {
      //上传完毕,作一下提示
      console.log(‘上传成功’ + successUp + ‘,’ + ‘失败’ + failUp);
      wx.showToast({
      title: ‘上传成功’ + successUp,
      icon: ‘success’,
      duration: 2000
      })
      } else {
      //递归调用,上传下一张
      that.uploadOneByOne(imgPaths, successUp, failUp, count, length);
      console.log(‘正在上传第’ + count + ‘张’);
      }
      },
      success(res) {
      var arr = that.data.imgData;
      arr.push(JSON.parse(res.data).data);
      that.setData({
      imgData: arr
      })
      successUp++;//成功+1
      },
      fail: function () {
      failUp++;
      }
      })
      },
      previewImage: function (e) {
      var that = this;
      var index = e.currentTarget.dataset.order;
      var imgarr = that.data.imageurl;
      debugger;
      wx.previewImage({
      current: imgarr[index],
      urls: imgarr
      })
      },
      delete: function (e) {
      var that = this;
      var index = e.currentTarget.dataset.order;
      var imgarr = that.data.imageurl;
      imgarr.splice(index, 1);
      that.setData({
      imageurl: imgarr
      })
      return false;

    console.log(‘img:’, imgarr[index])
    // imgarr.splice(index, 1);
    // that.setData({
    // imageurl: imgarr
    // })
    wx.showLoading({
    title: ‘加载中…’,
    mask: true
    })
    wx.request({
    url: app.globalData.url + ‘Delete’,
    data: {
    fileid: imgarr[index].fileid,
    filepath: imgarr[index].filepath
    },
    header: {
    ‘content-type’: ‘application/json’ // 默认值
    },
    success: function (res) {
    wx.hideLoading();
    if (res.data.code == 200) {
    imgarr.splice(index, 1);
    that.setData({
    imageurl: imgarr
    })
    }
    else {
    wx.showModal({
    content: res.data.msg,
    showCancel: false
    })
    }
    },
    fail: function () {
    wx.hideLoading();
    wx.showModal({
    content: ‘请求数据失败’,
    showCancel: false
    })
    }
    })
    },

rate_value:function(e){
this.setData({
rate_value: e.detail.value

})

},

userInfo1: function(e){
this.setData({
userInfo1: e.detail.value
})
},

createInsurance: function () {
debugger;
var that = this;
that.setData({
maskFlag: false
})
wx.showLoading({
title: ‘加载中…’,
mask: false
})

var caseDate = {
  orderno: that.data.orderno,
  openid: app.globalData.openid,
  ywyid: that.data.userid,
  ywyname: that.data.username,
  idcard: that.data.idcard,
  mobile: that.data.mobile,
  realname: that.data.realname,
  area_address: that.data.area_address,
  bank_name: that.data.bank_name,
  bank_cardno: that.data.bank_cardno,
  crop_name: that.data.crop_name,
  insured_areasize: that.data.insured_areasize,
  direct_areasize: that.data.direct_areasize,
  insured_amount: that.data.insured_amount,
  rate_value: that.data.rate_value,
  premium: that.data.premium,
  is_landcircle: that.data.is_landcircle,
  premium_sum: that.data.premium_sum,
  self_premium: that.data.self_premium,
  imgData: JSON.stringify(that.data.imgData),
  allowance_amount: this.data.allowance_amount,
  area1 : that.data.dao1,
  area2 : that.data.dao3,
  area3 : that.data.dao5,
}


if (this.data.is_landcircle == 1 && this.data.imageurl.length == 0) {
  wx.showModal({
    content: '请先上传证明图片',
    showCancel: false
  })
  return;
}
wx.request({
  url: app.globalData.url + 'CreateInsurance',
  data: caseDate,
  header: {
    'content-type': 'application/json' // 默认值
  },
  success: function (res) {
    that.setData({
      maskFlag: true
    })
    wx.hideLoading();
    if (res.data.code == 200) {
      wx.setStorageSync('orderno',"");
      wx.showToast({
        title: '操作成功',
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../insurancelist/index',
        })
      }, 1000)
    }
  }
});

},

checkChange: function (e) {
/获取的是一个数组, 包含的是选中的value的值, 如[‘早稻’, ‘中稻’], 即要传递的值/
this.setData({
crop_name: e.detail.value.join(’,’),
isShow1: false,
isShow2: false,
isShow3: false
})
/**
* 根据用户选择作物名称,展示相应模块得信息录入
*/

for (var i = 0; i < e.detail.value.length; i++){
if (e.detail.value[i] === ‘早稻’){
this.setData({
isShow1:true,
})
}else if(e.detail.value[i] === ‘中稻’){
this.setData({
isShow2: true,
})
}else{
this.setData({
isShow3: true,
})
}
}

if(this.data.isShow1 == false){
this.setData({
dao1: 0
})
}else if(this.data.isShow2 == false){
this.setData({
dao3: 0
})
}else if(this.data.isShow3 == false){
this.setData({
dao5: 0
})
}

/**

  • 当第二次选择作物品种得时候计算总面积
    */
    if (this.data.isShow1 == true && this.data.isShow2 == false && this.data.isShow3 == false){
    this.setData({
    insured_areasize: Number(this.data.dao1).toFixed(2)
    })
    this.premium_sum();
    } else if (this.data.isShow1 == true && this.data.isShow2 == true && this.data.isShow3 == false ){
    this.setData({
    insured_areasize: (Number(this.data.dao1) + Number(this.data.dao3)).toFixed(2)
    })
    this.premium_sum();
    } else if (this.data.isShow1 == false && this.data.isShow2 == true && this.data.isShow3 == true){
    this.setData({
    insured_areasize: (Number(this.data.dao3) + Number(this.data.dao5)).toFixed(2)
    })
    this.premium_sum();
    } else if (this.data.isShow1 == false && this.data.isShow2 == false && this.data.isShow3 == true ){
    this.setData({
    insured_areasize: Number(this.data.dao5).toFixed(2)
    })
    this.premium_sum();
    } else if (this.data.isShow1 == false && this.data.isShow2 == true && this.data.isShow3 == false){
    this.setData({
    insured_areasize: Number(this.data.dao3).toFixed(2)
    })
    this.premium_sum();
    } else if (this.data.isShow1 == true && this.data.isShow3 == true && this.data.isShow2 == false){
    this.setData({
    insured_areasize: (Number(this.data.dao1) + Number(this.data.dao5)).toFixed(2)
    })
    this.premium_sum();
    } else if (this.data.isShow1 == true && this.data.isShow3 == true && this.data.isShow2 == true ){
    this.setData({
    insured_areasize: (Number(this.data.dao1) + Number(this.data.dao5) + Number(this.data.dao3)).toFixed(2)
    })
    this.premium_sum();
    } else if (this.data.isShow1 == false && this.data.isShow3 == false && this.data.isShow2 == false ){
    this.setData({
    insured_areasize: Number(0)
    })
    this.premium_sum();
    }
    },

/**

  • 总保费得和农户自费的费用计算
    */

premium_sum: function(){
// debugger;
var _rate_value = Number(this.data.rate_value.replace(’%’, ‘’)) / 100;
var _allowance_amount = Number(this.data.allowance_amount.replace(’%’, ‘’)) / 100;
this.setData({
premium_sum: (this.data.insured_areasize * _rate_value * Number(this.data.insured_amount)).toFixed(2),
self_premium: (this.data.insured_areasize * _rate_value * Number(this.data.insured_amount)
* (1 - _allowance_amount)).toFixed(2)
})
},

/**

  • 验证用户输入的值只能是数字并且是两位小数

*/
regInput: function(e) {
if (/^(\d?)+(.\d{0,2})?$/.test(e.detail.value)) {
return e.detail.value;
} else {
return e.detail.value.substring(0, e.detail.value.length - 1);
}
},

/**

  • 根据用户输入得单个作物投保面积,累计投保总面积
    */
    dao1: function (event) {
    var value = this.regInput(event);
    this.setData({
    dao1: value,
    insured_areasize: (Number(this.data.dao3) + Number(this.data.dao5) + Number(value)).toFixed(2),
    })
    this.premium_sum();
    },
    dao3: function (event) {
    var value = this.regInput(event);
    this.setData({
    dao3: value,
    insured_areasize: (Number(this.data.dao1) + Number(this.data.dao5) + Number(value)).toFixed(2),
    })
    this.premium_sum();
    },

dao5: function (event) {
var value = this.regInput(event);
this.setData({
dao5: value,
insured_areasize: (Number(this.data.dao1) + Number(this.data.dao3) + Number(value)).toFixed(2),
})
this.premium_sum();
},

})

你可能感兴趣的:(小程序多张递归图上传)