ionic android升级检查

https://www.cnblogs.com/zxj159/p/4421578.html

坑:

放到cordova.file.DataDirectory下载异常? 只好cordova.file.externalDataDirectory

代码

        // 检查更新
        $scope.appUpdateCheck = function(init) {
            var init = init || 0;

            // appUpdateService.showUpdateConfirm({});
            // return;

            VersionService.get({ platform: ionic.Platform.platform() }, function(resp) {
                // alert(JSON.stringify(resp))
                if (resp.code == '000') {
                    // alert(resp.data.version)
                    // alert($cordovaAppVersion.getVersionNumber())
                    // alert($cordovaAppVersion.getVersionCode())

                    cordova.getAppVersion.getVersionNumber(function(version) {
                        // alert(version)
                        if (version == resp.data.version) {
                            if (!init) {
                                messageService.show('当前已是最新版本');
                            }
                            return;
                        }

                        $scope.hasNewVersion = 1;
                        $scope.device_items[1].name = $sce.trustAsHtml('升级检查<font style="color:#FD1F38"> - New!</font>');

                        if (!init) {
                            appUpdateService.showUpdateConfirm(resp.data);
                        }
                    });
                } else {
                    messageService.show(resp.msg);
                }
            });
        }
 ;
 (function(angular) {

    angular.module('mobile')

        .factory('appUpdateService', ['$cordovaFileTransfer', '$cordovaFileOpener2', '$ionicPopup', '$ionicLoading', '$timeout', 'messageService',
            function($cordovaFileTransfer, $cordovaFileOpener2, $ionicPopup, $ionicLoading, $timeout, messageService) {
                // 显示是否更新对话框
                function showUpdateConfirm(versionInfo) {
                    var versionInfo = versionInfo;
                    var downloadUrl = versionInfo.url,
                        updateLog = versionInfo.upgradeinfo,
                        targetPath = 'file:///storage/sdcard0/Download/XiaoShuTong.apk' //APP下载存放的路径,可以使用cordova file插件进行相关配置

                    // TODO 测试数据
                    // downloadUrl = 'http://imtt.dd.qq.com/16891/006473EB7690D8B89DEBD9613BF9E40E.apk?fsname=com.tencent.mm_6.6.0_1200.apk'
                    // updateLog = '1. 更新模块升级<br>2. 修复1个bug<br>'
                    targetPath = cordova.file.externalDataDirectory + 'XiaoShuTong.apk'

                    var confirmPopup = $ionicPopup.confirm({
                        title: '版本升级',
                        template: updateLog, //从服务端获取更新的内容
                        cancelText: '取消',
                        okText: '升级'
                    });
                    confirmPopup.then(function(res) {
                        if (res) {
                            // if (ionic.Platform.isIOS()) {
                            //     // 提示进入 APP store
                            //     messageService.show('请到App Store升级.');
                            // }
                            if (ionic.Platform.isAndroid() && downloadUrl != '') {
                                $ionicLoading.show({
                                    template: "已经下载:0%"
                                });
                                $cordovaFileTransfer.download(downloadUrl, targetPath, null, true).then(function(result) {
                                    // 打开下载下来的APP
                                    $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive').then(function() {
                                        // 成功
                                    }, function(err) {
                                        // 错误
                                        // alert('安装文件打开失败, 请尝试手动安装.');
                                        messageService.show('安装文件打开失败, 请尝试手动安装.');
                                    });
                                    $ionicLoading.hide();
                                }, function(error) {
                                    $ionicLoading.hide();
                                    // alert(JSON.stringify(error));
                                    // messageService.show(JSON.stringify(error));
                                    messageService.show('请设置应用文件操作权限, 然后重试.');
                                }, function(progress) {
                                    var downloadProgress = Math.floor((progress.loaded / progress.total) * 100);
                                    if (downloadProgress > 0) {
                                        $ionicLoading.show({
                                            template: '已经下载:' + downloadProgress + '%'
                                        });
                                    }
                                    if (downloadProgress > 99) {
                                        $ionicLoading.hide();
                                    }
                                });
                            }
                        } else {
                            // 取消更新
                            $ionicLoading.hide();
                        }
                    });
                }

                // return
                return {
                    showUpdateConfirm: showUpdateConfirm
                }
            }
        ]);

 })(angular);

posted @ 2017-12-27 18:45  CooMark  阅读(421)  评论(0编辑  收藏  举报