下面一段代碼給大家分享基于Vue+mui實(shí)現(xiàn)圖片的本地緩存,具體代碼如下所示:
在延邊朝鮮族等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),延邊朝鮮族網(wǎng)站建設(shè)費(fèi)用合理。const menu = { state: { products: {}, GLOBAL_CONFIG:GLOBAL_CONFIG['GLOBAL_CONFIG'] }, mutations: { get_product: function (state, products) { //商品列表 state.products = products; for(let i = 0; i < state.products.length; i++){ if(state.products[i]['image'] != null){ // state.products[i]['image'] = state.GLOBAL_CONFIG['base64Header'] + state.products[i]['image']; //下載圖片到本地 this.commit('imgCache',state.products[i]); }else{ //添加默認(rèn)圖片 state.products[i]['image'] = require("../assets/file.png"); } } }, imgCache: function (state,imgObj) { mui.plusReady(function(){ // 1. 轉(zhuǎn)換網(wǎng)絡(luò)圖片地址為本地緩存圖片路徑,判斷該圖片是否存在本地緩存 // http://...jpg -> md5 // 緩存目錄 _downloads/image/(md5).jpg let image_url = imgObj.image; let image_md5 = md5(image_url); // 緩存本地圖片url let local_image_url = '_downloads/image/'+image_md5+'.jpg'; // 平臺絕對路徑 let absolute_image_path = plus.io.convertLocalFileSystemURL(local_image_url); console.log(absolute_image_path); // 判斷本地是否存在該文件,存在就就直接使用,否則就下載 plus.io.resolveLocalFileSystemURL( absolute_image_path, function( entry ) { if(entry){ imgObj.image = plus.io.convertLocalFileSystemURL(local_image_url); }else{ download_img(); } }, function ( e ) { console.log("Resolve file URL failed: "); download_img(); } ); function download_img(){ // filename:下載任務(wù)在本地保存的文件路徑 let download_task = plus.downloader.createDownload(image_url, { filename: local_image_url }, function(download, status) { // 下載失敗,刪除本地臨時文件 if(status != 200){ console.log('下載失敗,status'+status); if(local_image_url != null){ plus.io.resolveLocalFileSystemURL(local_image_url, function(entry) { entry.remove(function(entry) { console.log("臨時文件刪除成功" + local_image_url); // 重新下載圖片 download_img(); }, function(e) { console.log("臨時文件刪除失敗" + local_image_url); }); }); } }else{ // 把下載成功的圖片顯示 // 將本地URL路徑轉(zhuǎn)換成平臺絕對路徑 console.log("下載成功" + local_image_url); imgObj.image = plus.io.convertLocalFileSystemURL(local_image_url); } }); download_task.start(); } }); } }, actions: { } }