场景下开发-$http服务封装2.0,短信验证码发送2.0
1.angular 服务公共函数提出 runService2
2.倒计时 updateSendMsg
3.sms2里倒映htp 服务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
sms2.send($scope, 'member.sendregistermsg', $scope.user, function( data) { $scope.user.msgid = data.msgid; return true; });
.factory('sms2',function($state, $ionicLoading,$ionicPopup, $timeout,$http,htp){ //================================================= // htp.runServiceWithSession($http, $ionicLoading, $state, // method, bizObj,successFun, errorFun,timeOut) // sms2.send($scope, method, params, checkFun, errFun) //================================================= // console.log('test'); var updateSendMsg = function($scope,begintime){ var smsTimeout = $scope.smsTimeout; var initTimeout = $scope.initTimeout; var canSend = $scope.canSend; var nowDate = new Date(); // smsTimeout--; smsTimeout = parseInt(initTimeout-(nowDate.getTime() - begintime.getTime())/1000); if (smsTimeout < 0) { canSend = true; smsTimeout = initTimeout; $scope.sendMsg = "获取验证码"; $scope.sendMsgBg = 'click'; return; } $scope.sendMsgBg = 'timing'; $scope.sendMsg = smsTimeout + "秒后重发"; canSend = false; $timeout(function(){ updateSendMsg($scope,begintime); }, 1000); }; var runSMSService = function($scope, method, params, checkFun, errFun,begintime){ var canSend = $scope.canSend; htp.runServiceWithSession($http, $ionicLoading, $state, method, params, function(data, status) { console.log(data); if (checkFun.call(this, data)) { // 开始计数 var date = new Date(); begintime = date; //发送短信的时候调用 updateSendMsg.call(this,$scope,begintime); } else { canSend = true; $ionicLoading.show({ template : "短信发送失败", duration : 2000 }); } }, function(data, status) { canSend = true; if (typeof (data) == 'string') { $ionicLoading.show({ template : data, duration : 2000 }); } else { if(data.solution!=''&&data.solution!=undefined){ $ionicLoading.show({ template : data.rtnmessage + "," + data.solution, duration : 2000 }); }else{ $ionicLoading.show({ template : data.rtnmessage, duration : 2000 }); } } errFun.call(this, data); }); } return{ // init: function(){}, send: function($scope, method, params, checkFun, errFun){ $scope.initTimeout = 120; $scope.smsTimeout = $scope.initTimeout; $scope.canSend = true; $scope.sendMsg = "获取验证码"; $scope.sendMsgBg = 'click'; var begintime = 0; $scope.sendSMS = function(parameter) { if ($scope.canSend == false){ return; } $scope.canSend = false; params = parameter ? parameter : params; // 发送短信 // $scope, method, params, checkFun, errFun,begintime runSMSService($scope, method, params, checkFun, errFun,begintime); }; } // END } }) .factory('htp',function($http, $ionicLoading, $ionicPopup){ //================================================= // htp.runServiceWithSession($http, $ionicLoading, $state, // method, bizObj,successFun, errorFun,timeOut) //================================================= // 错误处理 函数 function callError(errorFun, data, status, $ionicLoading,$ionicPopup){ if (errorFun) errorFun.call(this, data, status); else { // alert(msg); if (typeof (data) == 'string') { if ($ionicPopup) $ionicPopup.alert({ template : data, okText : "确定", okType : 'button-confirm', cancelType : 'button-cancel', cssClass : 'custom_popup' }); else if ($ionicLoading) $ionicLoading.show({ template : data, duration : 2000 }); } else { if(data.solution!=''&&data.solution!=undefined){ if ($ionicPopup) $ionicPopup.alert({ template : data.rtnmessage + "," + data.solution, okText : "确定", okType : 'button-confirm', cancelType : 'button-cancel', cssClass : 'custom_popup' }); else if ($ionicLoading) $ionicLoading.show({ template : data.rtnmessage + "," + data.solution, duration : 2000 }); }else{ if ($ionicPopup) $ionicPopup.alert({ template : data.rtnmessage, okText : "确定", okType : 'button-confirm', cancelType : 'button-cancel', cssClass : 'custom_popup' }); else if ($ionicLoading) $ionicLoading.show({ template : data.rtnmessage, duration : 2000 }); } } } } var runService2 = function($http, $ionicLoading, $ionicPopup, method, bizObj,successFun, errorFun,timeOut){ // console.log(bizObj); if(typeof ($ionicPopup) == 'string'){ timeOut = errorFun; errorFun = successFun; successFun = bizObj; bizObj = method; method = $ionicPopup; $ionicPopup = undefined; }; var timeOut =(timeOut)?timeOut:AmApp.config.ServiceTimout; if(navigator && navigator.connection && navigator.connection.type == 'none'){ }else{ // $http数据交互 $http({ method : 'POST', url : AmApp.config.ServiceRootPath + AmApp.config.RequestPath + "?rand=" + Math.random() + "&GID=" + method, data : bizObj, headers : { 'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8' }, transformRequest : function(obj) { var str = []; for ( var p in obj) str .push(encodeURIComponent(p) + "=" + encodeURIComponent(encodeURIComponent(obj[p]))); return str.join("&"); }, timeout : timeOut }) .success(function(data, status, header, config){ if ($ionicLoading) $ionicLoading.hide(); if (status == 200) { if (typeof (data) == 'string') { callError(errorFun, data, status, $ionicLoading, $ionicPopup); } else { if (data.issuccess == true) successFun.call(this, data.result, status); else callError(errorFun, data, status, $ionicLoading, $ionicPopup); } } else { callError(errorFun, "数据获取错误", status, $ionicLoading, $ionicPopup); } }) .error(function(data, status, header, config){ if ($ionicLoading) $ionicLoading.hide(); if (status == 500) callError(errorFun, "检测到服务状态异常", status, $ionicLoading, $ionicPopup); else if (status == 0) callError(errorFun, method + "连接超时", status, $ionicLoading, $ionicPopup); else callError(errorFun, "网络连接错误", status, $ionicLoading, $ionicPopup); }) } }; return{ // init: function(){}, runServiceWithSession: function($http, $ionicLoading, $state, method, bizObj,successFun, errorFun,timeOut){ runService2($http, $ionicLoading, method, bizObj, successFun, function(data, status) { if (data.rtncode == 'account.session.timeout' || data.rtncode == 'account.session.invalid' || (data.rtncode != undefined && data.rtncode .indexOf('need login') > -1)) { $state.go('logon', {}); } else { callError(errorFun, data, status, $ionicLoading); } },timeOut); }, runService: runService2, } })