//升級程序 .factory('UpdateService', function ($rootScope, $cordovaAppVersion, $cordovaFileTransfer, $timeout, $ionicLoading, $cordovaFileOpener2, $http, $ionicPopup, xc) { //檢查更新 var checkUpdate = function () { $cordovaAppVersion.getVersionNumber().then(function (v) { var url = xc.server.domain + 'update/checkNew/' + xc.app.id + '/android/' + v; $http.get(url) .error(function () { console.log('從服務器獲取數據失敗!'); }) .then(function (response) { var data = response.data; var compatible_binary = data.compatible_binary; var update_available = data.update_available; if (compatible_binary && update_available) { var url = xc.server.domain + data.update.url; showUpdateConfirm(url, data.info || ''); } } ); }); }; /** * * @param url * @param info */ var showUpdateConfirm = function (url, info) { var confirmPopup = $ionicPopup.confirm({ title: '版本升級', template: info.toString().replace(/\n/g, '<br />'), cancelText: '取消', okText: '升級' }); confirmPopup.then(function (res) { if (res) { $rootScope.process = 0; $ionicLoading.show({ template: '<ion-spinner icon="bubbles" class="spinner-assertive spinner spinner-bubbles"></ion-spinner><br>已經下載:{{process}}%' }); var targetPath = cordova.file.externalDataDirectory + "update.apk";//APP下載存放的路徑,可使用cordova file插件進行相關配置 var trustHosts = true; var options = {}; $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) { // 打開下載下來的APP //console.log(JSON.stringify(result)); $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive' ).then(function () { }, function (err) { }); $ionicLoading.hide(); }, function (err) { alert('下載失敗'); }, function (progress) { //進度,這裏使用文字顯示下載百分比 $timeout(function () { var downloadProgress = (progress.loaded / progress.total) * 100; $rootScope.process = Math.floor(downloadProgress); if (downloadProgress > 99) { $ionicLoading.hide(); } }) }); } else { // 取消更新 } }); }; return { checkUpdate: checkUpdate } })