• uni-app 实现热更新


      前端打包 app 即把写好的静态资源文件套壳打包成 app ,而热更新即下载并替换 app 内部的静态资源文件,实现 app 的版本升级。

      在uni-app 中,我们是如何实现热更新的呢?下面来看代码

    // 检测升级
                    // #ifdef APP-PLUS
                    // var ver = plus.runtime.version;
                    var aid = plus.runtime.appid;
                    uni.request({
                        url: this.BaseUrl + '/api/update/version?version=' + this.localVersion + '&appid=' + aid + '&_t=' + new Date().getTime(),
                        method: 'GET',
                        success: result => {
                            var data = result.data;
                            if (data.update && data.wgtUrl) {
                                this.wgtUrl = data.wgtUrl;//保存下载地址
                                    uni.showModal({
                                        title: "发现新版本",
                                        content: "确认下载更新",
                                        success: (res) => {
                                            if (res.confirm) {//当用户确定更新,执行更新
                                                this.doUpData();
                                            } else if (res.cancel) {
                                                // console.log('用户点击取消');
                                            }
                                        }
                                    })
                            }
                        },
                        complete: () => {
                            that.total += 1;
                        }
                    });
                    // #endif
    doUpData() {
                    uni.showLoading({
                        title: '更新中……'
                    })
                    uni.downloadFile({//执行下载
                        url: this.wgtUrl,
                        success: downloadResult => {//下载成功
                            if (downloadResult.statusCode === 200) {
                                uni.showModal({
                                    title: '',
                                    content: '更新成功,确定现在重启吗?',
                                    confirmText: '重启',
                                    confirmColor: '#EE8F57',
                                    success: function(res) {
                                        if (res.confirm) {
                                            plus.runtime.install(//安装
                                                downloadResult.tempFilePath, {
                                                    force: true
                                                },
                                                function() {
                                                    // utils.showToast('更新成功,重启中');
                                                    plus.runtime.restart();
                                                },
                                                function(e) {
                                                    // utils.showToast('更新失败');
                                                }
                                            );
                                        }
                                    }
                                });
                            }
                        },
                        complete: () => {
                            uni.hideLoading();
                        }
                    });
                }
  • 相关阅读:
    UNIX网络编程之旅配置unp.h头文件环境[ 转]
    C++著名程序库
    开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo
    网络库介绍
    置顶问题
    最近做的一个项目
    Storm 遇到问题?
    海量算法视频下载
    Quartz.NET作业调度框架详解
    c#中的委托、事件、Func、Predicate、Observer设计模式以及其他
  • 原文地址:https://www.cnblogs.com/gitByLegend/p/11905194.html
Copyright © 2020-2023  润新知