做淘客網(wǎng)站需要企業(yè)的域名深圳網(wǎng)絡(luò)推廣公司排名
這里寫目錄標(biāo)題
- 微信小程序檢測是否有存儲權(quán)限
- wx.getSetting
- 圖片上傳
- 從HTML中提取img標(biāo)簽的src屬性
- 多圖片下載
微信小程序檢測是否有存儲權(quán)限
wx.getSetting
上傳前判斷是否開啟存儲權(quán)限,如果不檢測直接上傳會出現(xiàn)fail的情況
var _this = this
wx.getSetting({success(res) {if (res.authSetting['scope.writePhotosAlbum']) {// 已獲得存儲相冊授權(quán)_this.writeInImg()}else {// 未獲得存儲相冊授權(quán)wx.showModal({title: '提示',content: '需要您授權(quán)保存相冊',showCancel: false,success: () => {wx.openSetting({success(settingdata) {//再進(jìn)行圖片上傳操作_this.writeInImg()}})}})}}})
圖片上傳
從HTML中提取img標(biāo)簽的src屬性
后端返回一個(gè)字符串,內(nèi)容是標(biāo)簽,img,需要截取出src中的圖片鏈接,
getImage(){const srcRegex = /<img\s+(?:[^>]*?\s+)?src\s*=\s*(["'])((?:[^\1"]|\\\1|.)*?)\1/gconst result = [...this.data.details.goods_content.matchAll(srcRegex)]const imgSrcs = result.map(v => v[2])return imgSrcs[0]},
多圖片下載
Page({/*** 頁面的初始數(shù)據(jù)*/data: {imgList: ['https://tys.zye.com/upload/1/common/20231110/207169967318.jpg','https://tys.zye.com/upload/1/common/20231110/207169967318.jpg','https://tys.zye.com/upload/1/common/20231110/207169967318.jpg', ],
}writeInImg() {let imgList = this.data.imgList let imglength = imgList.length; // 要下載的總條數(shù)let index = 0;wx.showLoading({title: '加載中',})for (let i = 0; i < imgList.length; i++) {wx.downloadFile({url: imgList[i],success: function(res) {var temp = res.tempFilePathwx.saveImageToPhotosAlbum({filePath: temp,success(res) {index++;// 全部下載完后觸發(fā)if (index == imglength) {wx.hideLoading();wx.showToast({title:'下載成功',icon:'success',duration:2000})}},fail(err){wx.hideLoading();console.log('下載失敗')}})}})}},