keywords:html
ionic,phonegap,cordova,网络制式,动态切换,变动,API,服务器地址,$resource,localstorage,urlapi
场景promise
APP以项目的形式提供,一个客户须要部署一套服务器,一个APP,因此APP的后台服务器地址不能写死。并且要求若是有wifi且在内网,须要用内网服务器地址,若是用3G或者4G,则需切换为外网服务器地址服务器
需求网络
APP第一次运行,若是没有设置过服务器地址,须要设置好后台服务器地址(含内网和外网,外网是必选,内网是可选)ionic
APP运行过程当中,若是用户手机网络制式发生改变,则须要根据移动网络制式选择合适的服务器地址ide
原有URL的设置post
之前APP的API地址,是直接写到一个constant里面,例如:优化
(function () { "use strict"; angular.module("gaia.config", []) .constant("ENV", { "version": "1.0.1", "name": "production", "debug": true, "api": "http://111.111.111.111:8088/gaia/api", }); })();
resource的调用url
var resource = $resource(ENV.api + '/customer',{},{ login:{ method:'post', url:ENV.api + '/customer/login' }, accesstoken:{ method:'get', url:ENV.api + '/customer/accesstoken' } });
(后面实现方案有部分变更)
保留ENV,APP第一次启动将设置的url存到localstorage里面,在之后每次启动前,从localstorage里面取出来赋值给ENV.api,当网络制式发生改变时,再根据状况赋值改变ENV.api。全部与后台相关的Service 的$resource 的初始化都依赖于ENV.api。
设计的关键点:
设置页面
请忽略美观(-__-)b
Service $resource 声明方式的改造:
var resource = null; var _initResource = function(apiUrl){ resource = $resource(apiUrl + '/customer',{},{ login:{ method:'post', url:apiUrl + '/customer/login' }, accesstoken:{ method:'get', url:apiUrl + '/customer/accesstoken' } }); }; _initResource(ENV.api); return { initResource:function(url){ _initResource(url); }, .....
新增Service,统一变动$resource
每次变动就调用先设置ENV.api,再_Sys.changeResource(ENV.api);
(function () { 'use strict'; angular.module('gaia.services') .factory('_Sys', function (ENV, User, Projects, Plans) { return { changeResource: function (url) { User.initResource(url); Projects.initResource(url); Plans.initResource(url); } }; }); })();
网络制式切换,调整URL
关于网络制式的监控的相关细节见:ngcordova 监控网络制式改变
$rootScope.$on('$cordovaNetwork:online', function (event, networkState) { var type = $cordovaNetwork.getNetwork(); if (type == Connection.WIFI) { var wifiApi = Settings.getWIFIAPI(); if (wifiApi) { //test方法定义见方案二Setting定义 Settings.test(wifiApi).$promise.then(function (response) { if (response.success) { //切换内网服务器地址中.... _Sys.changeResource(wifiApi); } }, function (e) {}); } } else if (type == Connection.CELL_4G || type == Connection.CELL_3G) { var wlanApi = Settings.getAPI(); //切换移动网(3G|4G)服务器地址中.... _Sys.changeResource(wlanApi); } });
大致思路同方案一,只是在思想方案一过程当中,又是localstorage 又是ENV.api,各类判断,给绕晕了,后来一想,url最终仍是以localstorage里面存储的为准,何不抛弃ENV.api,实现大统一(●ˇ∀ˇ●)
设计思路改动点:
1.用户设置的API地址,存储到localstorage里面,定义两个基础Service,一个用于localstorage操做,一个用于APP参数设置
//Storage Service 定义
(function(){ 'use strict'; angular.module('gaia.services') .factory('Storage',function($log){ $log.debug("Storage Service init"); return { set:function(key,data){ return window.localStorage.setItem(key,window.JSON.stringify(data)); }, get:function(key){ return window.JSON.parse(window.localStorage.getItem(key)); }, remove:function(key){ return window.localStorage.removeItem(key); } }; }); })();
//Settings Service 定义
(function () { 'use strict'; angular.module('gaia.services') .factory('Settings', function ($resource, $log, Storage) { var storageKey = 'settings'; var _settings = Storage.get(storageKey) || {}; var addOn = '/gaia/api'; return { getSettings: function () { return _settings; }, save: function (settings) { Storage.set(storageKey, settings); _settings = settings;//改变内存中的_settings,便于每次调用getAPI时都能取到最新值 return settings; }, getAPI: function () { if (_settings.wlan) { return "http://" + _settings.wlan + addOn; } return ""; }, getWIFIAPI: function () { if (_settings.wifiMode && _settings.wifi) { return "http://" + _settings.wifi + addOn; } return ""; }, test: function (url) { var resource = $resource("http://" + url + addOn + "/test", {}, { query: { method: 'get', isArray: false, timeout: 2000 } }); return resource.query(); } }; }); })();
2.Service $resource 的部分改动
var resource = null; var _initResource = function(apiUrl){ resource = $resource(apiUrl + '/customer',{},{ login:{ method:'post', url:apiUrl + '/customer/login' }, accesstoken:{ method:'get', url:apiUrl + '/customer/accesstoken' } }); }; //将此处的ENV.api 换成Settings.getAPI() // _initResource(ENV.api); _initResource(Settings.getAPI());
3.检测是否存在API地址,不存在则弹出设置页面
$scope.api = Settings.getAPI(); $ionicModal.fromTemplateUrl('templates/urlSetting.html', { scope: $scope }).then(function (modal) { $scope.settingModal = modal; if ((!$scope.api) || ($scope.api.indexOf("http://") === -1)) { if (window.StatusBar) { StatusBar.styleDefault(); } $scope.settingModal.show(); } }); //保存设置值方法 $scope.saveSettings = function () { //1.save setting 到localstorage if (!$scope.settings.wifiMode) { $scope.settings.wifi = ""; } Settings.save($scope.settings); $ionicLoading.show({ noBackdrop: true, template: "<div><h1><i class='icon ion-checkmark-circled balanced'></i></h1>保存成功</div>", duration: 1000 }); $scope.api = Settings.getAPI(); //2.变动全部Service 中的resource _Sys.changeResource($scope.api); $scope.settingModal.hide(); };
为了实现APP的后台API地址动态改变,只要找到问题的核心,不外乎围绕着API_URL 的获取 和设置 来实现该功能
获取:
设置:
【原创】,转载请带上原地址http://www.cnblogs.com/sloong/p/5151019.html