版本以前富文本eidtor,不支持真机插入临时地址,也就会写很多代码逻辑!
经真机测试确实支持临时路径了,这个非常实用可以在提交阶段再上传图片,避免
二次上传图片。
insertImage() {
let that = this;
uploadImg.localUpload_img(1).then(_res => {
let pathImgArr = _res[0], //得到原始图片临时路径的数组
imgsizeArr = _res[1], //得到原始图片缩放后的尺寸数组
canvasidArr = []; //存放动态生成画布id
that.setData({ canvasimg: imgsizeArr })
canvasidArr.push(wx.createCanvasContext(`tempcanvas`))
return uploadImg.getCanvaspath(0, pathImgArr, imgsizeArr, canvasidArr)//绘制图片
}).then(c_res => {
that.setData({ tempPath: c_res[0] })
return that.uploadCloudimg(c_res) //上传图片
}).then(cloudimg => {
that.editorCtx.insertImage({
src: cloudimg[0], //替换后图片下载地址
success: function () {
wx.hideLoading()
let tempPath = that.data.tempPath, //本地临时图片地址
tempimages = that.data.tempimages //返回数据数组
tempimages.push([tempPath, cloudimg[0]])
that.setData({ tempimages})
}
})
}).catch(err => {
wx.showToast({ title: '上传失败', icon: 'none', duration: 1500 })
})
},
//上传图片到临时文件夹
uploadCloudimg(imagesArr){
let timeRand = Number(new Date) + Math.ceil(Math.random() * 10000),
filePath = imagesArr, //本地路径
cloudPath = [], //云路径
type = filePath[0].match(/\.[^.]+?$/);
cloudPath.push(`temp/${timeRand}${type}`)
return uploadImg.cloudUpload_img(0, filePath, cloudPath).then(imgArr => {
return imgArr;
})
},
精简一下
insertImage() {
let that = this;
uploadImg.localUpload_img(1).then(_res => {
let pathImgArr = _res[0], //得到原始图片临时路径的数组
imgsizeArr = _res[1], //得到原始图片缩放后的尺寸数组
canvasidArr = []; //存放动态生成画布id
that.setData({ canvasimg: imgsizeArr })
canvasidArr.push(wx.createCanvasContext(`tempcanvas`))
return uploadImg.getCanvaspath(0, pathImgArr, imgsizeArr, canvasidArr)//绘制图片
}).then(c_res => {
that.editorCtx.insertImage({
src: c_res[0],
success: function () {
wx.hideLoading()
}
})
}).catch(err => {
wx.showToast({ title: '上传失败', icon: 'none', duration: 1500 })
})
},