down_file: function() {
var b;
var _this = this;
for (let x = 0, y = 16; x < y; x++) {
for (let i = 0, j = 20; i < j; i++) {
if (((x == 4) || (x == 5)) & j > 8) {
break;
}
cc為拼起來的字符串
var cc = "this.data." + this.data.allArray[i];
switch (x) {
case 0:
b = this.data.eye111A[i];
// 這里原先有16個的
break;
}
var Array = [, , , , , , , , , , , , , , , , , , , ];
const downloadTask = wx.downloadFile({
url: b,
success: function(res) {
// 只要服務器有響應數據,就會把響應內容寫入文件並進入 success 回調,業務需要自行判斷是否下載到了想要的內容
console.log(res)
if (res.statusCode === 200) {
_this.setData({
img_l: res.tempFilePath,
})
Array[i] = res.tempFilePath;
// let chose = "eye111B"
let chose = _this.data.allArray[i];
var bb = Array;
if (Array.length == 20 && ((x == 4 || x == 5) && i == 8)) {
_this.setData({
[chose]: bb,
})
console.log("成功換皮");
}
}
},
fail: function(err) {
console.log(err)
}
});
}
}
},
這其中的
eye111B: ["https://xxxxxx.xyz/images/A/eye111/B/a32.png", ],
1.地址要用域名 不能用ip地址;
2.直接在this.setData里面push是不成功的所以新建了一個固定長度的數組然后for循環填充數組。
3.這里面因為轉換速度不定,導致前面的地址還沒下完,但是后面的地址已經替換所以,替換的時候要固定位置。
wx.
downloadFile會返回一個地址給你當你在小程序里面有很多圖片導致只能使用網絡圖片的時候,就可以依靠這樣的方法來替換圖片地址,而不會一次次的重復加載。