app.controller('manufacturerCtrl', ['$scope', '$http', "$state", '$timeout', 'uiGridConstants', 'i18nService', 'toaster', 'md5', 'FileUploader', function ($scope, $http, $state, $timeout, uiGridConstants, i18nService, toaster, MD5, FileUploader) {
i18nService.setCurrentLang("zh-cn");
$scope.gridOptions = {
data: 'myData',
showGridFooter: true, //是否显示grid footer
// rowHeight: 80,
//-------- 分页属性 ----------------
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 = {};
$scope.enum.adShow = [
{value: "", label: "广告状态"},
{value: false, label: "无广告"},
{value: true, label: "有广告"}
];
//查询条件
var condition = $scope.condition = {
adShow: $.extend({}, $scope.enum.adShow[0]),
searchKey: "",
searchType: {value: "", text: "厂商信息"},
searchTypeList: [
{value: "", text: "厂商信息"},
{value: "dealer", text: "经销商查厂商"},
{value: "agent", text: "代理商查厂商"},
],
};
//事件
$scope.event = {
statusChange: function (key, item) {
condition[key].value = item.value;
condition[key].label = item.label;
this.search()
},
search: function () {
$scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize);
}
};
function setColumnDefs() {
$scope.gridOptions.columnDefs = [
{
field: 'nickname', displayName: '厂商名称',
cellTemplate: '
{{row.entity.nickname}}
',
},
{field: 'brandName', displayName: '品牌名称'},
{field: 'username', displayName: '联系方式'},
{
field: 'createdTime',
displayName: '注册时间',
cellTemplate: ''
},
{
field: 'agentTotal',
displayName: '代理商总数',
cellTemplate: ''
},
{
field: 'logo',
displayName: '厂商图标',
enableFiltering: false,
cellTemplate: '
'
},
{
field: 'withdrawFeeRatioCost',
displayName: '提现费率',
cellTemplate: '{{row.entity.withdrawFeeRatioCost}}‰
'
},
{
field: 'operation',
displayName: '操作',
minWidth: 680,
enableFiltering: false,
enableSorting: false,
enableHiding: false,//禁止在列选择器中隐藏
enableColumnMenu: false,// 是否显示列头部菜单按钮
cellTemplate: '' +
'' +
'' +
// '' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'
'
}
];
var fields = $scope.gridOptions.columnDefs;
for (var index in fields) {
var item = fields[index];
if (item && item['minWidth'] == null) {
item['minWidth'] = 100;
}
}
}
$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 = {
adShow: condition.adShow.value,
pageSize: pageSize,
pageIndex: curPage
};
// 查询关键字
if (condition.searchKey != "") {
params.searchKey = condition.searchKey
}
// 查询类型
if (condition.searchType.value != "") {
params.searchType = condition.searchType.value;
if (condition.searchKey == "") {
toaster.pop("warning", "提示", "请输入 " + condition.searchType.text + " 信息");
return
}
}
$http.get('/superadmin/getManagerList', {
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.ban = function (entity, targetStatus) {
var url = "/superadmin/toggleManufacturerStatus";
$.confirm({
content: '确定这样操作?',
buttons: {
ok: {
btnClass: 'btn-red',
action: function () {
$http({
method: 'POST',
url: url,
data: {
id: entity.id,
normal: targetStatus
}
}).then(function (response) {
entity.normal = targetStatus;
console.log(response);
if (response.data.result) {
toaster.pop("success", "提示", "修改状态成功!");
} else {
toaster.pop("error", "提示", "修改状态失败!");
}
}, function (response) {
toaster.pop("error", "提示", "修改状态失败!");
});
}
},
}
});
};
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]
}
// 解除5次错误限制
$scope.unfreeze = function (entity) {
var entity = getOneRow();
if (!entity) {
return
}
var url = "/superadmin/unfreeze";
$http({
method: 'POST',
url: url,
data: {
role: "manager",
id: entity.id,
}
}).then(function (response) {
if (response.data.result) {
toaster.pop("success", "提示", "解除限制成功!");
} else {
toaster.pop("error", "提示", "解除限制失败!");
}
}, function (response) {
toaster.pop("error", "提示", "解除限制失败!");
});
};
// 必须先声明,否则ui-select无法双绑
$scope.dialogData = {};
//添加
$scope.add = function () {
//重置表单状态
$scope.manageForm.$setPristine();
$scope.manageForm.$setUntouched();
//重置图片上传控件
closeUploader();
$scope.dialogName = "厂商开户";
$scope.dialogData = {isNew: true};
$("#manageForm").modal();
};
//编辑
$scope.edit = function () {
//重置表单状态
$scope.manageForm.$setPristine();
$scope.manageForm.$setUntouched();
//重置图片上传控件
closeUploader();
$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({}, item);
$("#manageForm").modal();
};
//提交表单保存
$scope.saveData = function () {
//表单未校验通过不能提交,上传文件提示
if ($scope.manageForm.$invalid) {
return
}
var url = "";
if ($scope.dialogData.id == null) {
url = "/superadmin/addManager";
} else {
url = "/superadmin/editManager";
}
var data = {
nickname: $scope.dialogData.nickname,
brandName: $scope.dialogData.brandName,
username: $scope.dialogData.username,
remarks: $scope.dialogData.remarks,
logo: $scope.dialogData.logo,
};
if ($scope.dialogData.id != null) {
//编辑
$.extend(data, {'id': $scope.dialogData.id})
} else {
//新建时才会保存密码
data.password = MD5.createHash($scope.dialogData.password + '');
// 新增时才会修改首席代理商信息
data.primaryAgentUsername = $scope.dialogData.primaryAgentUsername;
data.primaryAgentPassword = MD5.createHash($scope.dialogData.primaryAgentPassword + '');
data.primaryAgentNickName = $scope.dialogData.primaryAgentNickName;
data.trafficCardCost = $scope.dialogData.trafficCardCost;
data.withdrawFeeRatioCost = $scope.dialogData.withdrawFeeRatioCost;
}
$http({
method: 'POST',
url: url,
data: data
}).then(function (response) {
$('#manageForm').modal('hide');//弹窗消失
$scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
}, function (response) {
toaster.pop("error", "提示", "开户失败!");
});
};
$scope.editAddress = function () {
//重置表单状态
$scope.editAddressForm.$setPristine();
$scope.editAddressForm.$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 = item.exchangeAddress || {};
$scope.dialogData.managerId = item.id
$("#editAddressForm").modal();
};
$scope.saveAddress = function () {
//表单未校验通过不能提交,上传文件提示
if ($scope.editAddressForm.$invalid) {
return
}
var url = "/superadmin/saveFactoryAddress";
var data = $scope.dialogData
$http({
method: 'POST',
url: url,
data: data
}).then(function (response) {
$('#editAddressForm').modal('hide');
$scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
}, function (response) {
toaster.pop("error", "提示", "保存失败!");
});
};
//查看代理商
$scope.checkAgent = function (entity) {
$state.go('app.user.agents', {
moniAppId: '',
managerId: entity.id,
}, {
reload: true
});
};
//查看经销商
$scope.checkDealer = function (entity) {
$state.go('app.user.dealer', {
managerId: entity.id,
agentId: "",
}, {
reload: true
});
};
//查看设备
$scope.checkDevice = function (entity) {
$state.go('app.dev.devManage', {
managerId: entity.id,
agentId: "",
dealerId: "",
searchKey: "",
}, {
reload: true
});
};
$scope.goLogin = function (entity) {
var username = entity.username;
var url = "/1.0/index.html?username=" + username + "#/access/signin";
window.open(url);
};
//事件绑定
$timeout(function () {
$("#fileUpload").on("change", function () {
var objUrl = getObjectURL(this.files[0]);
if (objUrl) {
$("#previewImg").attr("src", objUrl);
}
});
$("#fileUploadValue").on("focus", function () {
$(this).blur();
})
});
//建立一個可存取到該file的url
function getObjectURL(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);
}
return url;
}
//图片提交控件
var uploader = $scope.uploader = new FileUploader({
url: '/common/upload?type=logo',
queueLimit: 1, //文件个数
removeAfterUpload: true //上传后删除文件
});
//注册上传事件
uploader.onCompleteItem = function (fileItem, response, status, headers) {
if (response.payload) {
//适配数据
response.data = response.payload;
}
if (response.result) {
toaster.pop("success", "提示", "上传成功!");
$scope.dialogData.logo = response.data;
} else {
toaster.pop("error", "失败", response.description);
}
};
uploader.onErrorItem = function (fileItem, response, status, headers) {
toaster.pop("error", "提示", "上传失败,请重试");
};
/*关闭上传框窗口后恢复上传框初始状态*/
function closeUploader() {
$("#fileUpload").val("");
$("#previewImg").attr("src", "");
uploader.clearQueue();
uploader.cancelAll();
}
/********************链接拷贝面板***********************/
var copyLinkData = $scope.copyLinkData = {};
var currentDomain = location.protocol + "//" + location.host;
//生成推广链接
$scope.openLinkPanel = function (entity) {
$("#copyLinkPanel").modal();
//代理商入口
var url = currentDomain + "/agents/index.html?managerId=" + entity.id;
copyLinkData.link1 = url;
};
// 关闭链接生成器
$scope.closeCopyLinkPanel = function () {
$("#copyLinkPanel").modal("hide");
};
var clipboard;
//事件绑定
$timeout(function () {
//在弹窗绑定 COPY组件
clipboard = new ClipboardJS('.copyLinkBtn');
clipboard.on('success', function (e) {
e.clearSelection();
toaster.pop("info", "提示", "复制成功");
$scope.$apply();
});
clipboard.on('error', function (e) {
toaster.pop("info", "提示", "请选择“拷贝”进行复制!");
$scope.$apply();
});
});
$scope.$on("$destroy", function () {
if (clipboard) {
clipboard.destroy();
console.log("clipboard destroy");
}
});
// 分配厂商的设备类型弹窗
$scope.editDeviceType = function (entity) {
$("#editDeviceTypePanel").modal();
$scope.dialogName = entity.nickname + "配置设备类型";
$("#dealerDevList").modal();
// todo 由于ui-grid 之前隐藏,表格渲染有问题
setTimeout(function () {
// 刷新设备
$scope.$broadcast("initDriverCodeList", {id: entity.id, role: "manager"});
}, 100);
};
$scope.closeDeviceConfig = function (entity) {
$("#editDeviceTypePanel").modal('hide');
};
//------------配置特性--------------------
$scope.editFeature = function (entity) {
$scope.dialogData = {
id: entity.id,
featureList: entity.featureList,
specialFeature: entity.specialFeature,
};
$("#featureForm").modal();
}
$scope.saveFeature = function () {
if ($scope.featureForm.$invalid) {
return
}
$.confirm({
content: '确定要变更特性?',
buttons: {
ok: {
btnClass: 'btn-red',
action: function () {
var url = "/superadmin/editManagerFeature";
$http({
method: 'POST',
url: url,
data: {
id: $scope.dialogData.id,
featureList: $scope.dialogData.featureList,
specialFeature: $scope.dialogData.specialFeature,
}
}).then(function (response) {
//保存成功 弹窗消失
$('#featureForm').modal('hide');
if (response.data.result) {
toaster.pop("info", "提示", "成功")
$scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
}
}, function (response) {
toaster.pop("error", "提示", "保存失败!");
});
}
},
}
});
};
//广告开关
$scope.selectedIds = []
$scope.editAdShow = function () {
$scope.dialogData = {};
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);
}
$scope.selectedIds = ids;
$("#editAdForm").modal();
}
$scope.saveAdEdit = function () {
var ids = $scope.selectedIds
if ($scope.editAdForm.$invalid) {
return
}
$http({
method: 'POST',
url: '/superadmin/editAdShow',
data: {ids: ids, adShow: $scope.dialogData.adShow, role: "manager"}
}).then(function (response) {
initDataGrid();
$('#editAdForm').modal('hide');
toaster.pop("success", "提示", "保存成功!");
}, function (response) {
toaster.pop("error", "提示", "保存失败!");
});
}
//编辑密码 默认123456
$scope.editPassword = function () {
//重置表单状态
$scope.passwordForm.$setPristine();
$scope.passwordForm.$setUntouched();
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.passwordData = $.extend({password: 123456}, item);
$("#passwordPanel").modal();
};
//修改密码提交
$scope.savePassword = function () {
//表单未校验通过不能提交
if ($scope.passwordForm.$invalid) {
return;
}
var url = "/superadmin/changeManagerPassword"
var password = MD5.createHash($scope.passwordData.password + '');
$http({
method: 'POST',
url: url,
data: {
id: $scope.passwordData.id,
password: password,
}
}).then(function (response) {
$('#passwordPanel').modal('hide');//弹窗消失
$scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
}, function (response) {
toaster.pop("error", "提示", "修改密码失败!");
});
};
}]);