123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- app.controller('faqCtrl', ['$scope', '$http', '$timeout', 'uiGridConstants', 'i18nService', 'toaster', 'FileUploader', function ($scope, $http, $timeout, uiGridConstants, i18nService, toaster, FileUploader) {
- 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: ""
- };
- function setColumnDefs() {
- $scope.gridOptions.columnDefs = [
- {field: 'question', displayName: '问题'},
- {field: 'answer', displayName: '答案'},
- {field: 'devType', displayName: '设备类型'},
- ];
- 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('/manager/getFAQDetailList', {
- params: params
- }).then(function (data) {
- $scope.setPagingData(data.data, curPage, pageSize);
- }).catch(function (data) {
- toaster.pop("error", "提示", "获取数据失败");
- });
- };
- setColumnDefs();
- initDataGrid();
- /***********************初始化设备类型*********************************/
- $scope.devTypeList = [];
- $http.get('/device/getDevTypeList', {
- params: {pageSize: 1000, pageIndex: 1}
- }).then(function (data) {
- data = data.data
- $scope.devTypeList = data.data.dataList;
- }).catch(function (data) {
- toaster.pop("error", "提示", "获取设备类型失败");
- });
- //问题类型:全部、经销商、用户、代理商
- $scope.targetList = [
- {text: "终端用户", value: "myuser"},
- {text: "经销商", value: "dealer"},
- {text: "代理商", value: "agent"},
- ];
- //事件
- $scope.event = {
- search: function () {
- $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize);
- }
- };
- //编辑弹窗需要的数据
- $scope.dialogName = "";
- $scope.dialogData = {
- images: [],
- videos: [],
- };
- //添加
- $scope.add = function () {
- //重置表单状态
- $scope.dataForm.$setPristine();
- $scope.dataForm.$setUntouched();
- //重置上传控件
- closeUploader();
- $scope.dialogName = "添加问答";
- $scope.dialogData = {
- question: "",
- answer: "",
- images: [],
- videos: [],
- };
- $("#dataPanel").modal();
- };
- //编辑
- $scope.edit = function () {
- //重置表单状态
- $scope.dataForm.$setPristine();
- $scope.dataForm.$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];
- var dialogData = $scope.dialogData = $.extend(true, {}, item);//深度克隆
- if (dialogData.images == null) {
- dialogData.images = [];
- }
- if (dialogData.videos == null) {
- dialogData.videos = [];
- }
- $("#dataPanel").modal();
- };
- //删除
- $scope.deleteData = 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: '/manager/deleteFAQ',
- data: {ids: ids}
- }).then(function (response) {
- initDataGrid();
- }, function (response) {
- toaster.pop("error", "提示", "删除失败!");
- });
- }
- },
- }
- });
- };
- //提交表单保存
- $scope.saveData = function () {
- //表单未校验通过不能提交
- if ($scope.dataForm.$invalid) {
- return;
- }
- var url = "";
- if ($scope.dialogData.id == null) {
- url = "/manager/addFAQ";
- } else {
- url = "/manager/editFAQ";
- }
- $http({
- method: 'POST',
- url: url,
- data: $scope.dialogData
- }).then(function (response) {
- $('#dataPanel').modal('hide');//弹窗消失
- $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
- }, function (response) {
- toaster.pop("error", "提示", "保存失败!");
- });
- };
- //事件绑定
- $timeout(function () {
- $("#fileUpload").on("change", function () {
- var objUrl = getObjectURL(this.files[0]);
- if (objUrl) {
- $("#previewImg").attr("src", objUrl);
- }
- });
- $("#fileUploadValue").on("focus", function () {
- $(this).blur();
- });
- $("#fileUpload2").on("change", function () {
- var objUrl = getObjectURL(this.files[0]);
- if (objUrl) {
- $("#previewVideo").attr("src", objUrl);
- }
- });
- $("#fileUploadValue2").on("focus", function () {
- $(this).blur();
- })
- });
- $scope.removeImgPreview = function () {
- $("#fileUpload").val("");
- $("#previewImg").attr("src", "");
- imageUploader.clearQueue()
- };
- $scope.removeVideoPreview = function () {
- $("#fileUpload2").val("");
- $("#previewVideo").attr("src", "");
- videoUploader.clearQueue();
- };
- //建立一個可存取到該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 imageUploader = $scope.imageUploader = new FileUploader({
- url: '/common/upload?type=faqImg',
- queueLimit: 1, //文件个数
- removeAfterUpload: true //上传后删除文件
- });
- //注册上传事件
- imageUploader.onCompleteItem = function (fileItem, response, status, headers) {
- if (response.result) {
- toaster.pop("success", "提示", "上传成功!");
- $scope.dialogData.images.unshift(response.payload);
- $scope.dialogData.answer = $scope.dialogData.answer + '<br /> <img src="' + response.payload + '">';
- } else {
- toaster.pop("error", "失败", response.description);
- }
- };
- imageUploader.onErrorItem = function (fileItem, response, status, headers) {
- toaster.pop("error", "提示", "上传失败,请重试");
- };
- //视频提交控件
- var videoUploader = $scope.videoUploader = new FileUploader({
- url: '/common/upload?type=faqVideo',
- queueLimit: 1, //文件个数
- removeAfterUpload: true //上传后删除文件
- });
- //注册上传事件
- videoUploader.onCompleteItem = function (fileItem, response, status, headers) {
- if (response.result) {
- toaster.pop("success", "提示", "上传成功!");
- $scope.dialogData.videos.unshift(response.payload);
- $scope.dialogData.answer = $scope.dialogData.answer + '<br /> <a href="' + response.payload + '">播放视频</a>';
- } else {
- toaster.pop("error", "失败", response.description);
- }
- };
- videoUploader.onErrorItem = function (fileItem, response, status, headers) {
- toaster.pop("error", "提示", "上传失败,请重试");
- };
- /*关闭上传框窗口后恢复上传框初始状态*/
- function closeUploader() {
- $("#fileUpload").val("");
- $("#fileUpload2").val("");
- $("#previewImg").attr("src", "");
- $("#previewVideo").attr("src", "");
- imageUploader.clearQueue();
- imageUploader.cancelAll();
- videoUploader.clearQueue();
- videoUploader.cancelAll();
- }
- /*图片移除*/
- $scope.removeImg = function (index) {
- var dialogData = $scope.dialogData;
- if (dialogData.images && dialogData.images.length > 0) {
- dialogData.images.splice(index, 1);
- }
- };
- /*视频移除*/
- $scope.removeVideo = function (index) {
- var dialogData = $scope.dialogData;
- if (dialogData.videos && dialogData.videos.length > 0) {
- dialogData.videos.splice(index, 1);
- }
- }
- }]);
|