angular-service.js /** * 使用:引入该JS,再angular.module('..',['sfService']) * 然后在Controller中注入'sfHttp' * 例子:controller('..',function (sfHttp){}); */var _sfService = angular.module('sfService',[]);_sfService.factory('sf
/**
* 使用:引入该JS,再angular.module('..',['sfService'])
* 然后在Controller中注入'sfHttp'
* 例子:controller('..',function (sfHttp){});
*/
var _sfService = angular.module('sfService',[]);
_sfService.factory('sfHttp',['$http',function($http){
//公共参数获取,直接传入搜索的值就行,sfHttp.getProValue('dns',function (data){});
return {
fromGet:function (data) {
var data = data || {};//仿照jQuery,为JSON格式
return $http({
method: 'GET',
params:data.data,
url: data.url
}).then(function (result) {
if(typeof data.success === "function"){
if(result.data.data){
data.success(result.data.data,result);
}else if(result.data){
data.success(result.data,result);
}else{
data.success(result);
}
}
},function (errMsg) {
typeof data.error === "function" && data.error(errMsg);
});
},
fromPost:function (data) {
var data = data || {};//仿照jQuery,为JSON格式
data.params = data.params?false:true;
return $http({
url: data.url,
method: 'POST',
data:data.data,
params:data.params || data.data,
headers : data.headers,
}).then(function (result) {
if(typeof data.success === "function"){
if(result.data.data){
data.success(result.data.data,result);
}else if(result.data){
data.success(result.data,result);
}else{
data.success(result);
}
}
},function (errMsg) {
typeof data.error === "function" && data.error(errMsg);
});
},
publicParam: function(proKey, suFn,erFn) {
//获取公共参数
return this.fromPost({
url:this.getRootPath()+'/common/getPageData',
data:{proKey:proKey},
success:function (data) {
var flay = true;
angular.forEach(data.rows,function (v) {
if(v.proKey === proKey){
typeof suFn === 'function' && suFn(v.proValue);
flay = false;
return false;
}
});
flay && typeof erFn === 'function' && erFn(data);
},
error:function (msg) {
typeof erFn === 'function' && erFn(msg);
}
});
},
getRootPath:function () {
return _sfService.getRootPath();
},
getCmdbPath:function (suFn,erFn) {
return this.fromGet({
url: _sfService.getRootPath()+'/common/findByKey/vishnu_cmdb_url',
success:function (data) {
typeof suFn === "function" && suFn(data);
},
error:function (msg) {
typeof erFn === "function" && erFn(msg);
console.info('获取CMDB地址失败!');
}
});
},
getRemotePath:function () {
return _sfService.getRootPath();
}
}
}])
.factory('commonService',['$rootScope',function($rootScope){
return {
getEnvTitle:function(env){
var envTitle='';
if(env=='PRD'){
envTitle='生产&容灾';
}else if(env=='SIT'){
envTitle='测试';
}else if(env=='STG'){
envTitle='准生产';
}else if(env=='DR'){
envTitle='容灾';
}
return envTitle;
},
queryCommonConfig:function(key){
var commonConfig='';
restGet($rootScope.contextPath+"/common/findByKey/"+key,function(data){
commonConfig=data;
},function(data){
$rootScope.createAlert({html:'您所访问的数据库中不存在'+key+'对应这个字段,请添加了之后再操作!',div:$("body"),state:"alert-danger"});
})
return commonConfig;
}
}
}]);
_sfService.rootPath = null;//|| 'http://10.202.78.5:8080/vishnu-web';//为以后跨前后端分离留接口
_sfService.remotePath = 'http://10.202.78.5:8080/vishnu-web';
//获取项目路径
_sfService.getRootPath = function () {
if (_sfService.rootPath) {
return _sfService.rootPath;
} else {
return _sfService.rootPath =
(window.document.location.href.substring(0,
window.document.location.href
.indexOf(window.document.location.pathname))
+ window.document.location.pathname
.substring(0, window.document.location.pathname
.substr(1).indexOf('/') + 1));
}
}
_sfService.provider('vishnu',[function () {//能注入config()的参数中去,例子:vishnuProvider
this.rootPath = _sfService.getRootPath();
this.sayTime = new Date();
this.sayHello = function (name) {
return 'Hello '+name;
}
this.$get = function ($http) {
return {
'rootPath':this.rootPath,
'sayTime':this.sayTime,
'sayHello':this.sayHello,
'sfHttp':function (data) {//在config中不存在该函数,在run等方法中存在
$http({
method: 'GET',
params: data.data,
url: data.url
}).then(function (result) {
if (typeof data.success === "function") {
if (result.data.data) {
data.success(result.data.data, result);
} else if (result.data) {
data.success(result.data, result);
} else {
data.success(result);
}
}
}, function (errMsg) {
typeof data.error === "function" && data.error(errMsg);
});
},
};
}
}]);
_sfService.provider('vishnu2',{//不能注入config中去,可以注入controller,如vishnu2
$get:function () {
return {
sayTime:new Date(),
sayHello:'Hello World Angular!'
}
}
})
