会社のAngularプロジェクトであるappService.jsは、angularjsインスタンスの宣言、構成、およびその他のファイルです。
今のところ、インターフェイスサーバーのドメイン名アドレスを構成し、トークン(インターセプター)を使用してリクエストを送信し、httpリクエストをカプセル化し、get、post、delete、put、DownloadFileメソッドをサポートします。次のコードは、
var app = angular.module('app', [ /声明一个angular模块
'ngAnimate', // 动画模块
'ngSanitize', // $sanitize服务依赖于此模块,具体查百度
'ui.router', // 路由模块
'ui.bootstrap', // 基于angular的bootstrap
'ui.jq', // 基于angular的juqery
'ntt.TreeDnD', // 基于angular的树插件所依赖的模块
'treeControl', // 基于angular的Angular-tree-control所依赖的模块
'ngCookies', // $cookie所依赖的模块
'ui.select', // 基于Angular中ui-select控件所依赖的模块
'angularFileUpload', // 基于Angular的angular-file-upload控件所依赖的模块
'ngDragDrop' // 拖拽功能所依赖的模块(ng-drag等,以后会详谈);
]);
//配置接口服务器域名地址(属于服务,使用的地方注入serverDomain即可)
app.value("serverDomain", "http://localhost:21888/");
//app.value("serverDomain", "http://10.5.253.86:21888/");
//发送请求统一带上token
app.factory('authInterceptor', ["$rootScope", "$cookies", "$injector", "$q", "$location", function ($rootScope, $cookies, $injector, $q, $location) {
return {
request: function (config) { // 请求拦截
config.headers = config.headers || {};
var headerName = 'Authorization';
var cookieName = 'XSRF-TOKEN';
//检查cookie是否存在
if ($cookies.get(cookieName) == undefined || $cookies.get(cookieName) == '') {
$location.path('/Index');
}
else {
config.headers[headerName] = 'Bearer ' + $cookies.get(cookieName);
}
//设置不缓存
config.headers["X-Requested-With"] = 'XMLHttpRequest';
config.headers["Cache-Control"] = "no-cache";
config.headers['Pragma'] = 'no-cache';
return config;
},
responseError: function (response) { //请求错误异常处理拦截
abp.ui.clearBusy("#mianBody");
abp.ui.clearBusy($('.modal-content'));//清除Modal遮罩层
//401服务端返回授权失败
if (response.status == 401) {
var msg = "您访问的接口未授权,请联系管理员";// response.data.error.message ||
abp.message.warn(msg, "提示");
} else if (response.status == 400) {
abp.message.warn("您输入的参数不符合规范,请重新核对", "提示");
} else if (response.status == 403) {
abp.message.warn("您没有接口访问权限,请联系管理员", "提示");
} else if (response.status == 500 && response.data.error.code == 200)//code为200需要提示到界面的错误信息
{
abp.message.warn(response.data.error.message, "提示");
}
else {
var errorData = "<div><p>" + response.status + ":" + response.statusText + "</p>" +
"<p>请求接口为:" + response.config.url + "</p>"
+ "</div>"
if (response.data.error && response.data.error.message) {
errorData += "<p>" + response.data.error.message + "<p>";
}
var topMenuId = $location.search().topMenuId;
$location.path("/error").search({ topMenuId: topMenuId, errorData: errorData });
return $q.reject(response);
}
//else
// if (response.status === 404) {
// $location.path('/Index');
// return $q.reject(response);
//}
},
response: function (response) { // 响应拦截
return response;
}
};
}]);
//封装http请求,支持get,post,delete,put方式
app.factory('httpService', function ($http) {
var factory = {
Get: function (url, data, success_CallBack, error_CallBack) {
$http.get(url, { params: data }).then(function successCallback(response) {
success_CallBack(response.data);
}, function errorCallback(response) {
// 请求失败执行代码
error_CallBack(response);
});
},
Post: function (url, data, success_CallBack, error_CallBack) {
$http.post(url, data).then(function successCallback(response) {
success_CallBack(response.data);
}, function errorCallback(response) {
// 请求失败执行代码
error_CallBack(response);
});
},
Delete: function (url, success_CallBack, error_CallBack) {
$http.delete(url).then(function successCallback(response) {
success_CallBack(response.data);
}, function errorCallback(response) {
// 请求失败执行代码
error_CallBack(response);
});
},
Put: function (url, data, success_CallBack, error_CallBack) {
$http.put(url, data).then(function successCallback(response) {
success_CallBack(response.data);
}, function errorCallback(response) {
// 请求失败执行代码
error_CallBack(response);
});
},
DownloadFile: function (url, data) {
//增加loading
abp.ui.setBusy($("#mianBody"));
$http({
url: url,
method: "GET",
params: data,
responseType: "blob"
}).then(function (response, status, header, config, statusText) {
var fileName = response.headers("Content-Disposition").split(";")[1].split("filename=")[1];
var fileNameUnicode = response.headers("Content-Disposition").split("filename*=")[1];
if (fileNameUnicode) {//当存在 filename* 时,取filename* 并进行解码(为了解决中文乱码问题)
fileName = decodeURIComponent(fileNameUnicode.split("''")[1]);
}
var blob = response.data;
if ('msSaveOrOpenBlob' in navigator) {//IE导出
window.navigator.msSaveOrOpenBlob(blob, fileName);
}
else {
//var reader = new FileReader();
//reader.readAsDataURL(blob); // 转换为base64,可以直接放入a表情href
//reader.onload = function (e) {
// // 转换完成,创建一个a标签用于下载
// var a = document.createElement('a');
// a.download = fileName;
// a.href = e.target.result;
// $("body").append(a);
// a.click();
// $(a).remove();
//}
var a = document.createElement('a');
a.download = fileName;
a.href = URL.createObjectURL(blob);
$("body").append(a);
a.click();
$(a).remove();
}
//清除loading
abp.ui.clearBusy("#mianBody");
});
}
}
return factory;
});