将图片转化为base64数组,上传
const app = getApp()
Page({
data: {
showtab:0, //顶部选项卡索引
showtabtype:'', //选中类型
showfootertab:0, //底部标签页索引
questionsall:[], //所有问题
questions:[], //问题列表
showquestionindex:null, //查看问题索引,
uploadimgs:[], //上传图片列表
editable: true,//是否可编辑
piyu_value:'',
id:''
},
onLoad: function (option) {
this.setData({
id:option.id,
uploadimgs:[]
})
console.log(this.data.id)
},
piyu: function(res){
console.log("输入的值为:"+res.detail.value);//打印输入的值
this.setData({
piyu_value: res.detail.value
})
},
chooseImage:function() {
let _this = this;
wx.showActionSheet({
itemList: ['从相册中选择', '拍照'],
itemColor: "#A10606",
success: function(res) {
if (!res.cancel) {
if(res.tapIndex == 0){
_this.chooseWxImage('album')
}else if(res.tapIndex == 1){
_this.chooseWxImage('camera')
}
}
}
})
},
chooseWxImage:function(type){
let _this = this;
wx.chooseImage({
sizeType: ['original', 'compressed'],
sourceType: [type],
success: function (res) {
_this.setData({
uploadimgs: _this.data.uploadimgs.concat(res.tempFilePaths)
})
}
})
},
editImage:function(){
this.setData({
editable: !this.data.editable
})
},
deleteImg:function(e){
console.log(e.currentTarget.dataset.index);
const imgs = this.data.uploadimgs
Array.prototype.remove = function(i){
const l = this.length;
if(l==1){
return []
}else if(i>1){
return [].concat(this.splice(0,i),this.splice(i+1,l-1))
}
}
this.setData({
uploadimgs: imgs.remove(e.currentTarget.dataset.index)
})
},
questionSubmit:function(){
var that = this;
var base64=[];
for(var i = 0; i < that.data.uploadimgs.length; i++)
{
base64.push(wx.getFileSystemManager().readFileSync(that.data.uploadimgs[i], "base64"));
}
console.log(base64);
wx.request({
url: app.globalData.globalReqUrl +'/api/material/base64',
data:{
photo: base64,
affair_id:that.data.id,
name:'微信上传',
remark:that.data.piyu_value
},
method:'POST',
header:{
'content-type':'application/x-www-form-urlencoded',
'token':app.globalData.token
},
success:function(res){
console.log(res);
let error = null;
if(res.data.status!="200"){
error = "系统错误 请稍后尝试"
}
if(error !== null){
wx.showToast({
title: error,
icon:'none',
duration:2000
})
}else{
wx.showToast({
title:'上传成功',
icon:'success',
duration:1000,
success:function(){
setTimeout(function(){
wx.navigateBack({
delta: 2,
})
},1000);
}
})
}
},
fail:function(res){
console.log(res);
}
})
},
})