app.controller('agentsManageCtrl', ['$scope', '$http', "$state", '$timeout', 'uiGridConstants', 'i18nService', 'FileUploader', 'toaster', 'md5', function ($scope, $http, $state, $timeout, uiGridConstants, i18nService, FileUploader, toaster, MD5) { 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); } }); } }; //查询条件 var condition = $scope.condition = { searchKey: "" }; //枚举常量 $scope.enum = {}; // 事件 $scope.ngEvent = { 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); } } }); } }, deletePic: function (index) { $scope.dialogData.bannerImgList.splice(index, 1); }, editPicUrl: function (index) { if ($scope.nowClickPicIndex == index) { $scope.nowClickPicIndex = null } else { $scope.nowClickPicIndex = index } }, addPic: function () { var imgUpload = $("#addImg"); var file = imgUpload[0].files[0]; this.compressorImg(file, function (formData) { var url = '/common/upload?type=banner'; $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.bannerImgList.push({img: url}); //重置文件路径,避免选中重复不触发onchange var imgUpload = $("#addImg"); imgUpload[0].value = ""; }, function (response) { toaster.pop("error", "提示", "上传失败!"); }); }) }, saveBanner: function () { $http({ method: 'POST', url: "/manager/setAgentBanner", data: { id: $scope.dialogData.id, bannerImgList: $scope.dialogData.bannerImgList, bannerName: $scope.dialogData.bannerName, } }).then(function (response) { if (response.data.result === 1) { toaster.pop("success", "提示", "保存成功!") } }, function (response) { toaster.pop("error", "提示", "保存失败!"); }); } }; //事件 $scope.event = { search: function () { $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize); }, generateNewAppKeyPair: function () { var dialogData = $scope.dialogData if (!dialogData.aliPayApp.appid) { toaster.pop("warning", "提示", "请填写支付宝的APP ID!"); return } $http({ method: 'POST', url: '/common/generateNewAppKeyPair', data: { appid: dialogData.aliPayApp.appid, } }).then(function (response) { if (response.data.result) { toaster.pop("success", "提示", "生成成功!"); $scope.dialogData.aliPayApp.app_private_key_path = response.data.payload.path; $scope.dialogData.aliPayApp.hasAlipayAppKeyPair = true; $scope.dialogData.aliPayApp.appPublicKeyContent = response.data.payload.appPublicKeyContent; } else { toaster.pop("error", "提示", "生成失败"); } }, function (response) { toaster.pop("error", "提示", "生成失败!"); }); }, }; function setColumnDefs() { $scope.gridOptions.columnDefs = [ { field: 'nickname', displayName: '代理商名称', cellTemplate: '
{{row.entity.nickname}}
', }, {field: 'username', displayName: '联系方式'}, { field: 'dateTimeAdded', displayName: '注册时间', cellTemplate: '
' }, { field: 'dealerTotal', displayName: '经销商总数', cellTemplate: '
' }, { field: 'annualTrafficCost', displayName: '流量卡最低费用', cellTemplate: '
' }, { field: 'withdrawFeeRatio', displayName: '最低提现费率', cellTemplate: '
{{row.entity.withdrawFeeRatio}}‰
' }, { field: 'pay_config', displayName: '资金池', cellTemplate: '
暂无
', enableFiltering: false, enableSorting: false }, { field: 'detail', displayName: '详细信息', cellTemplate: '
{{row.entity.detail}}
' }, { field: 'operation', displayName: '操作', enableFiltering: false, enableSorting: false, enableHiding: false,//禁止在列选择器中隐藏 enableColumnMenu: false,// 是否显示列头部菜单按钮 minWidth: 160, // 注意,主代理商自己不能分配设备类型 cellTemplate: '
' + '' + '
' }, ]; var fields = $scope.gridOptions.columnDefs; for (var index in fields) { var item = fields[index]; if (item && item['minWidth'] == null) { item['minWidth'] = 100; } } } 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.goDealer = function (entity) { $scope.nowAgentName = entity.nickname; $("#agentDealerList").modal(); $timeout(function () { // random避免刷新后显示问题 $state.go('app.user.agents.dealer', { agentId: entity.id, v: "v" + new Date().getTime() }); }); }; $scope.closeDealer = function () { $("#agentDealerList").modal('hide'); }; $scope.setPagingData = function (data, curPage, pageSize) { var firstRow = (curPage - 1) * pageSize; var pagedData = data.data.dataList; $scope.myData = pagedData; $scope.gridOptions.totalItems = data.data.total; }; $scope.getPagedDataAsync = function (curPage, pageSize, searchText) { if ($scope.gridOptionsLoading) { return; } var params = { pageSize: pageSize, pageIndex: curPage }; if (condition.searchKey != "") { params.searchKey = condition.searchKey } $scope.gridOptionsLoading = true; $http.get('/agent/getAgentsDetailList', { params: params }).then(function (data) { $scope.gridOptionsLoading = false; $scope.setPagingData(data.data, curPage, pageSize); }).catch(function (data) { toaster.pop("error", "提示", "获取数据列表失败"); }); }; function initDataGrid() { //首次加载表格 $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); } setColumnDefs(); initDataGrid(); //展示详情 $scope.showInfoDetail = function (title, content) { $scope.infoDetail = {title: title, content: content}; $("#detailInfoPanel").modal(); }; $scope.closeDetailInfoPanel = function () { $("#detailInfoPanel").modal("hide"); }; /********************链接拷贝面板***********************/ var copyLinkData = $scope.copyLinkData = {}; var currentDomain = location.protocol + "//" + location.host; //生成推广链接 $scope.openLinkPanel = function () { var entity = getOneRow(); if (!entity) { return } $("#copyLinkPanel").modal(); //经销商注册链接 var url = currentDomain + "/dealerAccess?agentId=" + entity.id; copyLinkData.link1 = url; // 个人主页链接 var url = currentDomain + "/userLogin?agentId=" + entity.id + "&redirect=" + encodeURIComponent("/user/index.html#/user/me"); copyLinkData.link2 = url; // 用户地图链接 var url = currentDomain + "/userLogin?agentId=" + entity.id + "&redirect=" + encodeURIComponent("/user/index.html#/user/index"); copyLinkData.link3 = url; // 用户扫码 var url = currentDomain + "/userLogin?agentId=" + entity.id + "&redirect=" + encodeURIComponent("/user/index.html#/dev"); copyLinkData.link4 = 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"); } }); // 必须先声明,否则ui-select无法双绑 $scope.dialogData = {}; //添加 $scope.add = function () { //重置表单状态 $scope.agentsForm.$setPristine(); $scope.agentsForm.$setUntouched(); $scope.dialogName = "代理商开户"; $scope.dialogData = {isNew: true,}; $("#agentsPanel").modal(); }; //编辑 $scope.edit = function () { //重置表单状态 $scope.agentsForm.$setPristine(); $scope.agentsForm.$setUntouched(); $scope.dialogName = "编辑代理商信息"; var row = getOneRow(); if (!row) { return } $scope.dialogData = $.extend({}, row);//非深度克隆的优点是:如果不小心取消了,下次编辑时,子对象的数据还在 $("#agentsPanel").modal(); }; //编辑 $scope.editOtherAPI = function () { //重置表单状态 $scope.otherAPIForm.$setPristine(); $scope.otherAPIForm.$setUntouched(); var row = getOneRow(); if (!row) { return } $scope.dialogFireData = $.extend(true, {id: row.id}, row.ZJFirePlatform); $("#otherAPIForm").modal(); }; $scope.saveOtherAPIForm = function () { if ($scope.otherAPIForm.$invalid) { return; } var url = "/manager/editAgentZJFirePlatform"; $http({ method: 'POST', url: url, data: $scope.dialogFireData }).then(function (response) { $('#otherAPIForm').modal('hide'); $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); }, function (response) { toaster.pop("error", "提示", "保存失败!"); }); } $scope.editBanner = function () { //重置表单状态 $scope.bannerForm.$setPristine(); $scope.bannerForm.$setUntouched(); $scope.dialogName = "编辑代理商Banner"; var row = getOneRow(); if (!row) { return } $scope.dialogData = $.extend({bannerImgList: []}, row);//非深度克隆的优点是:如果不小心取消了,下次编辑时,子对象的数据还在 $("#bannerForm").modal(); }; //编辑密码 默认123456 $scope.editPassword = function () { //重置表单状态 $scope.passwordForm.$setPristine(); $scope.passwordForm.$setUntouched(); var row = getOneRow(); if (!row) { return } $scope.passwordData = $.extend({password: 123456}, row); $("#passwordPanel").modal(); }; //解锁经销商的登录限制,如密码输错后导致的锁 $scope.unlock = function () { var row = getOneRow(); if (!row) { return } var url = "/manager/unlockAgent"; $http({ method: 'POST', url: url, data: { id: row.id } }).then(function (response) { toaster.pop("info", "提示", "解锁成功!") $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); }, function (response) { toaster.pop("error", "提示", "解锁失败!"); }); } //提交表单保存 $scope.saveData = function () { var url = ""; if ($scope.dialogData.id == null) { url = "/manager/addAgents"; } else { url = "/manager/editAgents"; } var data = { nickname: $scope.dialogData.nickname, username: $scope.dialogData.username, annualTrafficCost: parseFloat($scope.dialogData.annualTrafficCost),//最低流量卡年费 withdrawFeeRatio: parseFloat($scope.dialogData.withdrawFeeRatio), managerProfitShare: parseFloat($scope.dialogData.managerProfitShare), }; if ($scope.dialogData.id != null) { data.id = $scope.dialogData.id } else { //新建 data.password = MD5.createHash($scope.dialogData.password + ''); } $http({ method: 'POST', url: url, data: data }).then(function (response) { $('#agentsPanel').modal('hide');//弹窗消失 $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); }, function (response) { toaster.pop("error", "提示", "保存失败!"); }); }; /*****************公众号*********************/ $scope.editWechat = function () { //重置表单状态 $scope.wechatForm.$setPristine(); $scope.wechatForm.$setUntouched(); var row = getOneRow(); if (!row) { return } $scope.dialogData = $.extend({user: {}, dealer: {}}, row);//非深度克隆的优点是:如果不小心取消了,下次编辑时,子对象的数据还在 $("#wechatForm").modal(); } $scope.saveWechat = function () { var url = "/manager/bind_wechat_cust"; var data = {}; // 防止为空没填报错 $scope.dialogData.dealer = $scope.dialogData.dealer || {}; $scope.dialogData.user = $scope.dialogData.user || {}; //编辑 $.extend(data, { 'id': $scope.dialogData.id, customizedDealerGzhAllowable: $scope.dialogData.customizedDealerGzhAllowable, dealer: { appid: $scope.dialogData.dealer.appid, secret: $scope.dialogData.dealer.secret, templateIdMap: $scope.dialogData.dealer.templateIdMap, }, customizedUserGzhAllowable: $scope.dialogData.customizedUserGzhAllowable, user: { appid: $scope.dialogData.user.appid, secret: $scope.dialogData.user.secret, templateIdMap: $scope.dialogData.user.templateIdMap, }, }) $http({ method: 'POST', url: url, data: data }).then(function (response) { $('#wechatForm').modal('hide');//弹窗消失 $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); }, function (response) { toaster.pop("error", "提示", "操作失败!"); }); }; /*****************资金池*********************/ $scope.cashPoolWechat = function () { //重置表单状态 $scope.cashPoolWechatForm.$setPristine(); $scope.cashPoolWechatForm.$setUntouched(); var row = getOneRow(); if (!row) { return } $scope.dialogData = $.extend({wechatPayApp: {}}, row);//非深度克隆的优点是:如果不小心取消了,下次编辑时,子对象的数据还在 $("#cashPoolWechatForm").modal(); }; $scope.cashPoolAlipay = function () { //重置表单状态 $scope.cashPoolAlipayForm.$setPristine(); $scope.cashPoolAlipayForm.$setUntouched(); var row = getOneRow(); if (!row) { return } $scope.dialogData = $.extend({aliPayApp: {}}, row);//非深度克隆的优点是:如果不小心取消了,下次编辑时,子对象的数据还在 $("#cashPoolAlipayForm").modal(); }; $scope.changeFile = function (files,key) { if (files.length) { var file = files[0]; var reader = new FileReader(); // 文件不能大于1M if (file.size > 1000 * 1024) { toaster.pop("info", "提示", "文件太大,请重新选择"); } else { reader.onload = function() { if(reader.result) { var text = reader.result $scope.dialogData.wechatPayApp[key] = btoa(text) $scope.$apply(); } }; reader.readAsText(file); } } } $scope.saveCashPoolAlipay = function () { var url = "/manager/bind_ali_app"; var data = {}; // 防止未空没填报错 $scope.dialogData.aliPayApp = $scope.dialogData.aliPayApp || {}; //编辑 $.extend(data, { 'id': $scope.dialogData.id, customizedAlipayCashflowAllowable: $scope.dialogData.customizedAlipayCashflowAllowable, aliPayApp: { appid: $scope.dialogData.aliPayApp.appid, alipayPublicKeyContent: $scope.dialogData.aliPayApp.alipayPublicKeyContent, app_private_key_path: $scope.dialogData.aliPayApp.app_private_key_path, appPublicKeyContent: $scope.dialogData.aliPayApp.appPublicKeyContent, shadow: $scope.dialogData.aliPayApp.shadow, public_key_path: $scope.dialogData.aliPayApp.public_key_path } }) $http({ method: 'POST', url: url, data: data }).then(function (response) { $('#cashPoolAlipayForm').modal('hide');//弹窗消失 $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize); }, function (response) { toaster.pop("error", "提示", "保存失败!"); }); } //修改密码提交 $scope.savePassword = function () { //表单未校验通过不能提交 if ($scope.passwordForm.$invalid) { return; } var url = "/manager/editAgentPassword"; 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", "提示", "修改密码失败!"); }); }; /***************** 分配厂商的设备类型弹窗**********************/ $scope.editDeviceType = function () { var entity = getOneRow(); if (!entity) { return } if (entity.isPrimary) { toaster.pop("warning", "提示", "该角色是主代理商,不能配置!"); return } $("#editDeviceTypePanel").modal(); $scope.dialogName = entity.nickname + "配置设备类型"; $("#dealerDevList").modal(); // todo 由于ui-grid 之前隐藏,表格渲染有问题 setTimeout(function () { // 刷新设备 $scope.$broadcast("initDriverCodeList", {id: entity.id, role: "agent"}); }, 100); }; $scope.closeDeviceConfig = function (entity) { $("#editDeviceTypePanel").modal('hide'); }; }]);