app.controller('systemConfigCtrl', ['$scope', '$http', '$timeout', 'toaster', 'uiGridConstants', 'FileUploader', 'i18nService', function ($scope, $http, $timeout, toaster, uiGridConstants, FileUploader, i18nService) {
i18nService.setCurrentLang("zh-cn");//中文环境,如表格分页
/**配置特性列表**/
$scope.gridOptions = {
data: 'myData',
showGridFooter: true, //是否显示grid footer
//-------- 分页属性 ----------------
paginationPageSizes: [10, 20, 50, 100], //每页显示个数可选项
paginationCurrentPage: 1, //当前页码
paginationPageSize: 10, //每页显示个数
totalItems: 0,// 总数量
useExternalPagination: true,//是否使用分页按钮
//过滤
enableFiltering: true,
columnDefs: [],
//---------------api---------------------
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
if ($scope.setPagingData) {
$scope.getPagedDataAsync(newPage, pageSize);
}
});
}
};
//查询条件
var condition = $scope.condition = {
searchKey: ""
};
//枚举常量
$scope.enum = {
role: [
{value: "dealer", label: "经销商"},
{value: "agent", label: "代理商"},
{value: "manager", label: "厂商"},
]
};
function setColumnDefs() {
$scope.gridOptions.columnDefs = [
{
field: 'role', displayName: '所属角色',
filter: {
term: '',
type: uiGridConstants.filter.SELECT,
selectOptions: $scope.enum.role
},
cellTemplate: '
{{grid.appScope.formatterRole(row.entity.role)}}
'
},
{field: 'name', displayName: '特性名称'},
{field: 'key', displayName: '特性关键字'},
{
field: 'default', displayName: '特性默认值',
cellTemplate: '{{ grid.appScope.translateDefault(row.entity.default) }}
'
},
{
field: 'createdTime',
displayName: '创建时间',
cellTemplate: ''
},
{field: 'desc', displayName: '特性说明', minWidth: 420},
];
var fields = $scope.gridOptions.columnDefs;
for (var index in fields) {
var item = fields[index];
if (item && item['minWidth'] == null) {
item['minWidth'] = 100;
}
}
}
function initDataGrid() {
//首次加载表格
$scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
}
$scope.setPagingData = function (data, curPage, pageSize) {
var pagedData = data.data.dataList;
$scope.myData = pagedData;
$scope.gridOptions.totalItems = data.data.total;
};
$scope.getPagedDataAsync = function (curPage, pageSize) {
var params = {
pageSize: pageSize,
pageIndex: curPage
};
if (condition.searchKey != "") {
params.searchKey = condition.searchKey
}
$http.get('/superadmin/getFeatureList', {
params: params
}).then(function (data) {
data = data.data
$scope.setPagingData(data, curPage, pageSize);
}).catch(function (data) {
toaster.pop("error", "提示", "获取数据失败");
});
};
setColumnDefs();
initDataGrid();
$scope.translateDefault = function(flag){ return flag ? "开启" : "不开启" };
//角色转换
$scope.formatterRole = function (role) {
var list = $scope.enum.role;
for (var index in list) {
var item = list[index];
if (item.value == role) {
return item.label;
}
}
};
//事件
$scope.event = {
search: function () {
$scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize);
}
};
//编辑弹窗需要的数据
$scope.dialogName = "编辑";
$scope.dialogData = {};
//添加
$scope.addFeature = function () {
//重置表单状态
$scope.FeaturePanel.$setPristine();
$scope.FeaturePanel.$setUntouched();
$scope.dialogName = "添加";
$scope.dialogData = {};
$("#FeaturePanel").modal();
};
//编辑
$scope.editFeature = function () {
//重置表单状态
$scope.FeaturePanel.$setPristine();
$scope.FeaturePanel.$setUntouched();
$scope.dialogName = "编辑";
var rows = $scope.gridApi.selection.getSelectedRows();
if (rows.length == 0) {
toaster.pop("info", "提示", "请选择数据!");
return;
}
if (rows.length > 1) {
toaster.pop("info", "提示", "只能选中编辑一条数据");
return;
}
var item = rows[0];
$scope.dialogData = $.extend(true, {}, item);
$("#FeaturePanel").modal();
};
//提交表单保存
$scope.saveData = function () {
//表单未校验通过不能提交
if ($scope.FeaturePanel.$invalid) {
return;
}
var url = "";
if ($scope.dialogData.id == null) {
url = "/superadmin/addFeature";
} else {
url = "/superadmin/editFeature";
}
//深度克隆 避免操作原对象
var data = $.extend(true, {}, $scope.dialogData);
delete data.createdTime //创建时间后台修改
$http({
method: 'POST',
url: url,
data: data
}).then(function (response) {
$('#FeaturePanel').modal('hide');//弹窗消失
$scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
}, function (response) {
toaster.pop("error", "提示", "保存失败!");
});
};
//删除特性
$scope.deleteFeature = function () {
var rows = $scope.gridApi.selection.getSelectedRows();
if (rows.length == 0) {
toaster.pop("info", "提示", "请选择数据!");
return;
}
var ids = [];
for (var i = 0; i < rows.length; i++) {
ids.push(rows[i].id);
}
$.confirm({
content: '确定删除?',
buttons: {
ok: {
btnClass: 'btn-red',
action: function () {
$http({
method: 'POST',
url: '/superadmin/removeFeatures',
data: {ids: ids}
}).then(function (response) {
initDataGrid();
}, function (response) {
toaster.pop("error", "提示", "删除失败!");
});
}
},
}
});
};
/********默认充值优惠设置**********/
$scope.config = {
chargeList: [],
};
$http.get("/manager/getChargeList").then(function (data) {
data = data.data
$scope.config.chargeList = data.data;
$timeout(function () {
//由于出现滚动条,导致横向空间减少,导致表格控件多出横向滚动条,需要resize
$(window).trigger("resize");
})
});
$scope.deleteCharge = function (index) {
var chargeList = $scope.config.chargeList;
if (chargeList[index]) {
chargeList.splice(index, 1);
}
};
//充值套餐
$scope.addCharge = function () {
var chargeList = $scope.config.chargeList;
if (!$.isArray(chargeList)) {
chargeList = $scope.config.chargeList = [];
}
var length = chargeList.length;
var data = {};
if (length == 0) {
data = {
payAmount: 1,
coins: 1
};
} else {
data = {
payAmount: chargeList[length - 1]["payAmount"] * 2,
coins: chargeList[length - 1]["coins"] * 2
};
}
chargeList.push(data);
};
//保存充值优惠
$scope.saveChargeData = function () {
$http({
method: 'POST',
url: "/manager/saveChargeData",
data: $scope.config.chargeList
}).then(function (response) {
toaster.pop("success", "提示", "保存成功!");
}, function (response) {
toaster.pop("error", "提示", "保存失败!");
});
};
}]);