uni-app

获取缓存数据并清除缓存

//清除缓存
clearCache() {
                let that = this;
                let os = plus.os.name;
                if (os == 'Android') {
                    let main = plus.android.runtimeMainActivity();
                    let sdRoot = main.getCacheDir();
                    let files = plus.android.invoke(sdRoot, "listFiles");
                    let len = files.length;
                    for (let i = 0; i < len; i++) {
                        let filePath = '' + files[i]; // 没有找到合适的方法获取路径,这样写可以转成文件路径  
                        plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
                            if (entry.isDirectory) {
                                entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
                                    uni.showToast({
                                        title: '缓存清理完成',
                                        duration: 2000
                                    });
                                    that.formatSize(); // 重新计算缓存  
                                }, function(e) {
                                    console.log(e.message)
                                });
                            } else {
                                entry.remove();
                            }
                        }, function(e) {
                            console.log('文件路径读取失败')
                        });
                    }
                } else { // ios  
                    plus.cache.clear(function() {
                        uni.showToast({
                            title: '缓存清理完成',
                            duration: 2000
                        });
                        that.formatSize();
                    });
                }
            },
//获取缓存数据
formatSize() {
                let that = this;
                plus.cache.calculate(function(size) {
                    let sizeCache = parseInt(size);
                    console.log(sizeCache, 'sizeCache')
                    if (sizeCache == 0) {
                        that.fileSizeString = "0B";
                    } else if (sizeCache < 1024) {
                        that.fileSizeString = sizeCache + "B";
                    } else if (sizeCache < 1048576) {
                        that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
                    } else if (sizeCache < 1073741824) {
                        that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
                    } else {
                        that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
                    }
                    console.log(that.fileSizeString, 'that.fileSizeString')
                });
            },

uni-热更新

在APP.vue里面

onLaunch: function() {
            // let uniIdToken = uni.getStorageSync('uniIdToken');
            // if (uniIdToken) {
            //  this.login(uni.getStorageSync('username'));
            // }
            console.log('App Launch');
            var that = this;
            // #ifdef APP-PLUS
            plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
                // console.log(widgetInfo, '版本');
                uni.request({
                    url: 'http://10.168.1.204:9092/charge_app/update/info',
                    data: {
                        version: widgetInfo.version,
                        appid: widgetInfo.appid
                    },
                    method: 'POST',
                    success: result => {
                        console.log(result, '更新结果');
                        var data = result.data.result;
                        if (Number(data.status) && data.url) {
                            that.wgtUrl = data.url; //保存下载地址
                            uni.showModal({
                                title: '发现新版本',
                                content: '确认下载更新',
                                success: res => {
                                    if (res.confirm) {
                                        /* 下载软件 */
                                        uni.showLoading({
                                            title: '正在更新,请稍后'
                                        });
                                        plus.downloader
                                            .createDownload(
                                                data.url, {
                                                    filename: '_doc/update/' + widgetInfo.name + '/' + new Date().getTime() + '/'
                                                },
                                                function(res, code) {
                                                    let filePath = res.filename;
                                                    plus.runtime.install(
                                                        filePath, {
                                                            force: false
                                                        },
                                                        res => {
                                                            /* 重新启动APP资源 */
                                                            uni.hideLoading();
                                                            uni.showModal({
                                                                title: '',
                                                                content: '更新成功,确定现在重启吗?',
                                                                confirmText: '重启',
                                                                confirmColor: '#5480F9',
                                                                success: function(res) {
                                                                    if (res.confirm) {
                                                                        // console.log('重启', res);
                                                                        plus.runtime.restart();
                                                                    }
                                                                }
                                                            });

                                                        },
                                                        e => {
                                                            console.log(e);
                                                            /* 更新失败做的操作 */
                                                        }
                                                    );
                                                }
                                            )
                                            .start();
                                        // that.doUpData();
                                    } else if (res.cancel) {
                                        // console.log('用户点击取消');
                                    }
                                }
                            });
                        }
                    }
                });
            });
        },

3.整体更新 需要跳转页面

//检查更新
        checkUpdate() {
            var that = this;
            // #ifdef APP-PLUS
            plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
                uni.request({
                    url: 'http://10.168.1.204:9092/charge_app/update/info',
                    data: {
                        version: widgetInfo.version,
                        appid: widgetInfo.appid
                    },
                    method: 'POST',
                    success: result => {
                        console.log(result, '更新结果');
                        var data = result.data.result;
                        if (Number(data.status)) {
                            uni.showModal({
                                title: '发现新版本',
                                content: '是否前往下载',
                                success: res => {
                                    if (res.confirm) {
                                        plus.runtime.openURL('http://10.168.1.204:8080/rtdownload/__UNI__096D2C0.wgt');
                                    } else {
                                        console.log('用户取消');
                                    }
                                }
                            });
                        } else {
                            uni.showToast({
                                title: '当前版本为最新版本',
                                icon: 'none'
                            });
                        }
                    }
                });
            });
            // #endif
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容