Vue封装hbuilder热更新

main.js
import AutoUpdateApp from './common/AutoUpdateApp/AutoUpdateApp' //我存放AutoUpdateApp.js的目录
Vue.use(AutoUpdateApp)
AutoUpdateApp.js
export default {
    install(Vue,options)
    {
        // 蓝牙搜索方法
        Vue.prototype.AutoUpdateApp = function () {
            return {
                // 获取本地应用资源版本号
                getVersion: function (CallBack) {
                    var wgtVer = null;
                    //异步操作
                    plus.runtime.getProperty(plus.runtime.appid,(info) => {
                        wgtVer = info.version;
                        CallBack(wgtVer);
                    });
                },
                //下载wgt文件
                downWgt: function (wgtUrl) {
                    var self = this;
                    plus.nativeUI.showWaiting("下载更新文件...");
                    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
                        if ( status == 200 ) { 
                            console.log("下载更新文件成功:" + d.filename);
                            self.installWgt(d.filename); // 安装wgt包
                        } else {
                            plus.nativeUI.alert("下载失败!");
                        }
                        plus.nativeUI.closeWaiting();
                    }).start();
                },
                // 更新应用资源
                installWgt: function (path) {
                    plus.nativeUI.showWaiting("安装更新文件...");
                    plus.runtime.install(path,{},function(){
                        plus.nativeUI.closeWaiting();
                        plus.nativeUI.alert("应用资源更新完成!",function(){
                            plus.runtime.restart();
                        });
                    },function(e){
                        plus.nativeUI.closeWaiting();
                        plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
                        if(e.code == 10){
                            plus.nativeUI.alert('请清除临时目录');
                        }
                    });
                }
            }
        };
    }

}
vuex的store.js文件的actions
autoupdateapp: function (context) {
            let AutoUpdateApp = new window.Vue.AutoUpdateApp();
            let wgtVer = plus.runtime.version;

            axios.post('/point-api-autoupdateapp',{
                appid:plus.runtime.appid,
                appname:'queue'
            }).then(function(response){    
                let newVer = response;
                //检查更新
                if(wgtVer && newVer && (wgtVer.substring(0,3) < newVer.versionName.substring(0,3))){
                    if(window.confirm('检测到更新,是否更新?')){
                        AutoUpdateApp.downWgt(newVer.apk);  
                    }
                } 
            }, function(response){
                mui.toast('网络错误');
            });
        }
在需要的组件的 mounted或created调用
// 初始化创建对象
    mui.plusReady(() => {
      this.$store.dispatch('autoupdateapp');
    });

php的models代码

public static function AutoUpdateApp($params) 
{
    extract($params);
    $versionName = '';
    if($appname == 'queue')
    {
        $versionName = '1.2.0';
    }elseif($appname == 'waiter')
    {
        $versionName = '1.2.0';
    }elseif($appname == 'pos')
    {
        $versionName = '1.2.0';
    }
    $data = [
        'versionName' => $versionName,
        'apk' => $SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/app/'.$appid.'.wgt',//存放更新资源的目录
    ];
    return self::formatBody('data' => $data);
}

猜你喜欢

转载自blog.csdn.net/qq_39702364/article/details/81231594
今日推荐