import.js
8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Created by DEV003 on 2015/1/14.
*/
angular.module("xn/template/importButton.html", []).run(["$templateCache", function ($templateCache) {
"use strict";
$templateCache.put("xn/template/importButton.html",
"<div class=\"mb_10 pr_10 clearfix\" >"+
"<a class=\"btn btn-primary btn-sm f-right\" ng-click=\"openImport()\">{{importLine.title}}</a>"+
"</div>");
}]);
angular.module("xn/template/importBox.html", []).run(["$templateCache", function ($templateCache) {
"use strict";
$templateCache.put("xn/template/importBox.html",
"<div class=\"modal-content xn-import\" >"+
"<div class=\"modal-header\">"+
" <h3 class=\"modal-title ng-binding\">导入</h3>"+
"</div>"+
"<div class=\"xn-input-line mt_20 clearfix\">"+
"<label for=\"inputFile\" class=\"col-sm-offset-2 col-sm-2 f-left l-h-34 mr_10 \">选择导入文件:</label>"+
"<div class=\"xn-input\">"+
"<div class=\"input-group\">"+
"<input type=\"text\" class=\"form-control\" id=\"{{textId}}\" placeholder=\"导入文件\" name=\"textFile\" />"+
"<input type=\"file\" class=\"none\" id=\"{{selectItemId}}\" ng-click=\"upFileClick()\" contentEditable=\"false\" accept=\"textml\" />"+
"<label class=\"input-group-addon xn-cursor\" for=\"{{selectItemId}}\">选择</label>"+
"</div>"+
"</div>"+
"</div>"+
"<div class=\"xn-input-line clearfix\">"+
"<a class=\"col-sm-2 col-sm-offset-2 l-h-34\" href=\"javascript:;\" ng-click=\"downloadTemplate()\">下载模板</a>"+
"<a ng-if=\"isImportError\" class=\"col-sm-2 l-h-34 \" href=\"javascript:;\" ng-click=\"downloadError()\">下载错误信息</a>"+
"<a ng-if=\"lineData\" class=\"col-sm-2 l-h-34 \" href=\"javascript:;\" ng-click=\"importData()\">导入正确数据</a>"+
"</div>"+
"<div class=\"form-group clearfix\">"+
"<div class=\"form-group clearfix\">"+
"<div class=\"hint col-md-6 col-sm-offset-2\">注意!导入前请仔细检查导入数据的准确性!</div>"+
"</div>"+
"</div>"+
"<div class=\"modal-footer\">"+
"<button class=\"btn btn-primary\" ng-click=\"upload()\">上传</button>"+
"<button class=\"btn btn-warning\" ng-click=\"cancel()\">取消</button>"+
"</div>"+
"<div>");
}]);
var xnImportLine = function ($http,$modal,FoundationService,dialogService) {
return {
restrict: "A",
templateUrl: "xn/template/importButton.html",
scope: {importLine: '=ngModel'},
replace: true,
require: "?ngModel",
link: function (scope,ngModel) {
if (!ngModel) return;
var importLine = scope.importLine;
scope.openImport = function(){
importLine.flag = true;
var modalInstance = $modal.open({
templateUrl: "xn/template/importBox.html",
controller: ImportLineController,
size:"lg",
resolve: {
items: function () {
return {
uploadMethod:importLine.uploadMethod,
downLoadUrl:importLine.downLoadUrl,
downLoadMethod:importLine.downLoadMethod,
params:importLine.params
};
}
}
});
modalInstance.result.then(function (data) {
importLine.data = data;
}, function () {
});
}
}
};
};
var ImportLineController =["$http","$scope","$modalInstance","items","dialogService",
function ($http,$scope, $modalInstance,items,dialogService) {
var uploadMethod = items.uploadMethod;
var downLoadUrl = items.downLoadUrl;
var downLoadMethod = items.downLoadMethod;
var json = items.params;
$scope.selectItemId = "AA"+new Date().getTime();
var selectItem = document.getElementById($scope.selectItemId);
var i = 0;
while(selectItem != null){
$scope.selectItemId = "AA"+new Date().getTime()+i;
selectItem = document.getElementById($scope.selectItemId);
i++;
}
$scope.textId = "BB"+new Date().getTime();
var textItem = document.getElementById($scope.textId);
while(textItem != null){
$scope.textId = "BB"+new Date().getTime()+i;
textItem = document.getElementById($scope.textId);
i++;
}
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
$scope.upFileClick=function(){
var upImportFileId=document.getElementById($scope.selectItemId);
upImportFileId.onchange=function(){
var upImportFile;
var upImportFileList =upImportFileId.files;
var upImportFilename;
if(upImportFileList){
upImportFile = upImportFileList[0];
upImportFilename=upImportFile.name;
}else{
upImportFilename =upImportFileId.value;
}
var str =upImportFilename.substr(upImportFilename.indexOf(".")+1);
if(!(str=="xls" || str=="xlsx" || str=="et")){
$("#"+$scope.textId).val("");
dialogService.tip([{"message":"请上传Excel支持的文件(xls,xlsx,et等)!" }]);
}else{
$("#"+$scope.textId).val(upImportFilename);
}
};
};
$scope.upload = function(){
$scope.importList(json).success(function(data){
$scope.lineData = null;
if(data.errors === null || data.errors.length > 0){
dialogService.tip([{"message":"存在错误数据!" }]);
if(data.url != null){
$scope.isImportError = true;
$scope.errotUrl = data.url;
}
if(data.result != null){
if(data.result.length>0){
$scope.lineData = data;
}
}
}else{
$scope.isImportError = false;
$scope.errotUrl = null;
dialogService.tip([{"message": "导入成功!"}]);
$modalInstance.close(data);
}
});
};
$scope.downloadTemplate = function(){
$scope.outputTemplate().success(function(data){
if(data.errors === null || data.errors.length > 0){
dialogService.tip(data.errors);
} else {
window.open(data.url,"_self");
}
});
};
$scope.importData = function(){
dialogService.tip([{"message": "导入成功!"}]);
$modalInstance.close($scope.lineData);
};
//导入列表
$scope.importList = function(json){
var file = document.getElementById($scope.selectItemId).files[0];
var url="/api/fileImport.do";
var fd = new FormData();
fd.append("file",file);
fd.append("method", uploadMethod);
fd.append("json",JSON.stringify(json));
return $http({
method : 'POST',
url : url,
headers: {'Content-Type': undefined },
transformRequest: angular.identity,
data: fd
});
};
//导出列表
$scope.outputTemplate = function(){
return $http({
method: 'POST',
url: downLoadUrl,
params: {"method": downLoadMethod}
});
};
//下载错误
$scope.downloadError = function(){
window.open($scope.errotUrl,"_self");
}
}];
angular.module("xn.directive.import", ["ng","xn.service.foundation","xn/template/importButton.html","xn/template/importBox.html"])
.directive('xnImportLine', ["$http","$modal","FoundationService","dialogService", xnImportLine]);