angular.module('push')
.controller('importOrgInfoCtrl', function ($scope, $ionicPopup, $stateParams, UserService, taskModuleService, ConfigService, CommentService, UtilService, $timeout, CommonService) {
$scope.restype = $stateParams.restype;
var enterprise = [
{
title: '上传文件',
content: '[]',
type: 'upload',
needed: false,
imagelist: [],
imageSaveList: [],
imageDeleteList: [],
doctype: 4908,
remarkType: 'normal'
}
];
$scope.formList = enterprise;
taskModuleService.getOrgStatsDataList().then(function (res) {
if (res.code == 3350) {
$scope.orgnamelist = res.data;
}
})
$scope.sendOrgInfoToDB = function () {
angular.forEach($scope.formList, function (val) {
if (val.type == "upload") {
if (val.imageSaveList.length == 0 && val.imagelist.length != 0) {
val.imageSaveList = val.imagelist;
}
}
})
if (!$scope.formList[0].imageSaveList || $scope.formList[0].imageSaveList.length == 0) {
$ionicPopup.alert({
title: '提示',
template: '没有文件需要导入!'
})
return;
}
var imagelist = $scope.formList[0].imageSaveList;
for (var i = 0; i < imagelist.length; i++) {
var fileDiveded = imagelist[i].title.split(".");
if (fileDiveded.length > 0) {
if (fileDiveded[fileDiveded.length - 1] != "xls" && fileDiveded[fileDiveded.length - 1] != "xlsx") {
$ionicPopup.alert({
title: '提示',
template: '只能上传excel文件!'
})
return;
}
}
}
var params = {};
params.data = $scope.formList;
params.node = UserService.node;
params.userid = UserService.id;
params.path = ConfigService.imgurl;
params.restype = $scope.restype;
$scope.showLoadingToast();
//企业信息导入数据库
taskModuleService.importOrgInfoToDB(params).then(function (res) {
$scope.hideLoadingToast();
if (res.code == 3350) {
$ionicPopup.alert({
title: '提示',
template: '导入成功!'
})
} else {
$ionicPopup.alert({
title: '提示',
template: '导入失败!'
})
return;
}
})
}
$scope.addImage = function (index) {
listIndex = index;
if ($scope.app) {
var list = [
{text: '拍照'},
{text: '从相册上传'},
];
var clickfunction = function (indx) {
if (indx == 0) {
$scope.openCamera();
} else {
getPictures();
}
return true;
}
} else {
var files = document.getElementById('file');
files.click();
$(files).unbind().on('change', function (e) {
// console.log(e);
if ($scope.formList[listIndex].imagelist.length > 8) {
$ionicPopup.alert({
title: '提示',
template: '最多只能上传9个文件!'
});
return;
}
$.each(e.target.files, function (i, file) {
var url = null;
if (window.createObjectURL != undefined) { // basic
url = window.createObjectURL(file);
} else if (window.URL != undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file);
}
$scope.formList[listIndex].imagelist.push({title: file.name, photoName: url, originalName: url});
});
$scope.showLoadingToast();
var data = new FormData();
$.each(e.target.files, function (i, file) {
data.append('file', file);
});
CommonService.mutipleUploadImage(data).then(function (res) {
if (res.code == 3350) {
angular.forEach(res.data, function (value) {
$scope.formList[listIndex].imageSaveList.push({
id: 0,
title: value.title,
doctype: $scope.formList[listIndex].doctype,//文档类型 1图片,2文件,3pdf,4xls
isenabled: 1,//是否可用:0-不可用,1-可用,2-已停用
photoName: value.photoName,
originalName: value.originalName,
sourceName: value.sourceName,
sourceSize: value.sourceSize
})
})
}
// console.log(temp);
// console.log($scope.formList[listIndex]);
})
})
$scope.hideLoadingToast();
return true;
}
$ionicActionSheet.show({
buttons: list,
cancelText: '取消',
buttonClicked: clickfunction
})
}
$scope.openCamera = function () {
if ($scope.formList[listIndex].imagelist.length >= 9) {
if ($scope.app) {
UtilService.showMess("最多选取9张图片");
} else {
CommonService.showMessage("最多选取9张图片", $scope)
}
return;
}
UtilService.getPicture(1).then(function (results) {
$scope.showLoadingToast();
$scope.formList[listIndex].imagelist.push({photoName: results, originalName: results})
UtilService.uploadFile([results], 0, "image/jpeg").then(function (response) {
angular.forEach(response, function (value) {
$scope.formList[listIndex].imageSaveList.push({
id: 0,
title: value.title,
doctype: $scope.formList[listIndex].doctype,//文档类型 1图片,2文件,3pdf,4xls
isenabled: 1,//是否可用:0-不可用,1-可用,2-已停用
photoName: value.photoName,
originalName: value.originalName,
sourceName: value.sourceName,
sourceSize: value.sourceSize
})
})
$scope.hideLoadingToast();
}, function () {
$scope.hideLoadingToast();
if ($scope.app) {
UtilService.showMess("网络不给力,请重试");
} else {
CommonService.showMessage("网络不给力,请重试", $scope)
}
});
$ionicScrollDelegate.$getByHandle("scrollimage").resize();
}, function (err) {
});
}
var getPictures = function () {
if (device.platform == "Android") {
verifyStorage();
} else {
getPic();
}
};
var verifyStorage = function () {
window.imagePicker.verifyStorage(
function (results) {
if (results == "1") {
getPic();
}
}, function (error) {
}
);
};
var getPic = function () {
var imagelistLength = $scope.formList[listIndex].imagelist.length;
if (imagelistLength >= 9) {
if ($scope.app) {
UtilService.showMess("最多选取9个文件");
} else {
CommonService.showMessage("最多选取9个文件", $scope)
}
return;
}
UtilService.getPictureList(9 - imagelistLength).then(function (results) {
$scope.showLoadingToast();
angular.forEach(results, function (val, i) {
$scope.formList[listIndex].imagelist.push({photoName: val, originalName: val});
})
UtilService.uploadFile(results, 0, "image/jpeg").then(function (response) {
angular.forEach(response, function (value) {
if (value.status) {
$scope.formList[listIndex].imageSaveList.push({
id: 0,
title: "",
doctype: $scope.formList[listIndex].doctype,//文档类型 1图片,2文件,3pdf,4xls
isenabled: 1,//是否可用:0-不可用,1-可用,2-已停用
photoName: value.photoName,
originalName: value.originalName,
sourceName: value.sourceName,
sourceSize: value.sourceSize
});
}
})
$scope.hideLoadingToast();
}, function () {
$scope.hideLoadingToast();
if ($scope.app) {
UtilService.showMess("网络不给力,请重试");
} else {
CommonService.showMessage("网络不给力,请重试", $scope)
}
});
$ionicScrollDelegate.$getByHandle("scrollimage").resize();
}, function (err) {
});
};
//删除图片
$scope.deletePhoto = function (index, outerIndex) {
console.log($scope.formList[outerIndex]);
if ($scope.formList[outerIndex].imageSaveList[index].id != 0) {
$scope.formList[outerIndex].imageSaveList[index].isenabled = 2;
$scope.formList[outerIndex].imageDeleteList.push($scope.formList[outerIndex].imageSaveList[index]);
}
$scope.formList[outerIndex].imagelist.splice(index, 1);
$scope.formList[outerIndex].imageSaveList.splice(index, 1);
// console.log($scope.formList[outerIndex].imagelist);
// console.log($scope.formList[outerIndex].imageSaveList);
// console.log($scope.formList[outerIndex].imageDeleteList);
}
});