版本以前富文本eidtor,不支持真機(jī)插入臨時(shí)地址,也就會(huì)寫(xiě)很多代碼邏輯塞俱!
經(jīng)真機(jī)測(cè)試確實(shí)支持臨時(shí)路徑了荐绝,這個(gè)非常實(shí)用可以在提交階段再上傳圖片,避免
二次上傳圖片髓考。
insertImage() {
let that = this;
uploadImg.localUpload_img(1).then(_res => {
let pathImgArr = _res[0], //得到原始圖片臨時(shí)路徑的數(shù)組
imgsizeArr = _res[1], //得到原始圖片縮放后的尺寸數(shù)組
canvasidArr = []; //存放動(dòng)態(tài)生成畫(huà)布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, //本地臨時(shí)圖片地址
tempimages = that.data.tempimages //返回?cái)?shù)據(jù)數(shù)組
tempimages.push([tempPath, cloudimg[0]])
that.setData({ tempimages})
}
})
}).catch(err => {
wx.showToast({ title: '上傳失敗', icon: 'none', duration: 1500 })
})
},
//上傳圖片到臨時(shí)文件夾
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;
})
},
精簡(jiǎn)一下
insertImage() {
let that = this;
uploadImg.localUpload_img(1).then(_res => {
let pathImgArr = _res[0], //得到原始圖片臨時(shí)路徑的數(shù)組
imgsizeArr = _res[1], //得到原始圖片縮放后的尺寸數(shù)組
canvasidArr = []; //存放動(dòng)態(tài)生成畫(huà)布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 })
})
},