app.controller('moniListCtrl', ['$scope', '$http', "$state", '$timeout', 'uiGridConstants', 'i18nService', 'toaster', function ($scope, $http, $state, $timeout, uiGridConstants, i18nService, toaster) { i18nService.setCurrentLang("zh-cn"); moment.locale('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); } }); } }; //枚举常量 $scope.enum = { status: [ {value: "addingFans", label: "加粉中"}, {value: "fansFull", label: "粉丝已满"}, {value: "adminStop", label: "管理员暂停"}, {value: "dayFullStop", label: "日限额满停"}, {value: "appOver", label: "公众号不可用", disabled: true}, ], priority: [ {value: 5, label: "最高"}, {value: 4, label: "高"}, {value: 3, label: "一般"}, {value: 2, label: "低"}, {value: 1, label: "最低"}, ], }; //查询条件 $scope.query = { searchKey: "", }; $scope.ngEvent = { //查询 query: function () { $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize); }, compressorImg: function (file, callback) { if (file) { var imageCompressor = new Compressor(file, { quality: .7, // 图片尺寸 maxWidth: 1920, maxHeight: 1920, success: function (result) { var formData = new FormData(); formData.append('file', result, result.name); if (callback) { callback(formData); } } }); } }, addPic: function () { var imgUpload = $("#addImg"); var file = imgUpload[0].files[0]; this.compressorImg(file, function (formData) { var url = '/common/upload?type=moniQrCode'; $http({ method: 'POST', url: url, transformRequest: angular.identity, headers: { 'Content-Type': undefined// 禁止angular对data的处理 }, data: formData }).then(function (response) { var url = response.data.para; $scope.dialogData.qrCode = url; //重置文件路径,避免选中重复不触发onchange var imgUpload = $("#addImg"); imgUpload[0].value = ""; }, function (response) { toaster.pop("error", "提示", "上传失败!"); }); }) }, }; function setColumnDefs() { $scope.gridOptions.columnDefs = [ {field: 'appName', displayName: '公众号',}, {field: 'appId', displayName: 'appId',}, {field: 'rawAppId', displayName: '原始appId',}, {field: 'secret', displayName: 'secret',}, { field: 'priority', displayName: '优先级', cellTemplate: '
{{grid.appScope.formatterPriority(row.entity)}}
' }, { field: 'status', displayName: '状态', cellTemplate: '
{{grid.appScope.formatterStatus(row.entity)}}
' }, {field: 'title', displayName: '广告词', minWidth: 300,}, {field: 'desc', displayName: '说明',}, {field: 'maxDayAddingCount', displayName: '每日加粉限额',}, { field: 'operation', displayName: '操作', enableFiltering: false, enableSorting: false, enableHiding: false,//禁止在列选择器中隐藏 enableColumnMenu: false,// 是否显示列头部菜单按钮 minWidth: 150, cellTemplate: '
' + '' + '
' }, ]; var fields = $scope.gridOptions.columnDefs; for (var index in fields) { var item = fields[index]; if (item && item['minWidth'] == null) { item['minWidth'] = 100; } } } $scope.formatterStatus = function (entity) { var status = entity.status; var statusList = $scope.enum.status; var label = ""; for (var index = 0; index < statusList.length; index++) { var item = statusList[index]; if (item.value == status) { label = item.label } } return label; }; $scope.formatterPriority = function (entity) { var targetValue = entity.priority; var enumList = $scope.enum.priority; var label = ""; for (var index = 0; index < enumList.length; index++) { var item = enumList[index]; if (item.value == targetValue) { label = item.label } } return label; }; $scope.setPagingData = function (data) { 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 }; var query = $scope.query; params.searchKey = query.searchKey $http.get('/superadmin/getMoniApps', { params: params }).then(function (data) { data = data.data $scope.setPagingData(data, curPage, pageSize); }).catch(function (data) { toaster.pop("error", "提示", "获取数据列表失败"); }); }; function initDataGrid() { //首次加载表格 $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); } setColumnDefs(); initDataGrid(); $scope.goAgents = function (entity) { $state.go('app.user.agents', { moniAppId: entity.appId, managerId: "", }, { reload: true }); } function getOneRow() { var rows = $scope.gridApi.selection.getSelectedRows(); if (rows.length === 0) { toaster.pop("info", "提示", "请选择数据!"); return false; } if (rows.length > 1) { toaster.pop("info", "提示", "只能选中编辑一条数据"); return false; } return rows[0] } $scope.dialogData = {}; $scope.add = function () { $scope.dialogData = {}; $("#moniEdit").modal() } $scope.edit = function () { var row = getOneRow(); if (!row) { return } $("#moniEdit").modal() $scope.dialogData = $.extend(true, {}, row); } $scope.save = function () { //表单未校验通过不能提交 if ($scope.moniEdit.$invalid) { return; } $http({ method: 'POST', url: "/superadmin/editMoniApp", data: $scope.dialogData }).then(function (response) { $('#moniEdit').modal('hide');//弹窗消失 toaster.pop("success", "提示", "保存成功!"); $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); }, function (response) { toaster.pop("error", "提示", "保存失败!"); }); } $scope.delete = 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].appId); } $.confirm({ content: '确定删除?', buttons: { ok: { btnClass: 'btn-red', action: function () { $http({ method: 'POST', url: '/superadmin/deleteMoniApp', data: {ids: ids} }).then(function (response) { initDataGrid(); }, function (response) { toaster.pop("error", "提示", "删除失败!"); }); } }, } }); } }]);