faq.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. app.controller('faqCtrl', ['$scope', '$http', '$timeout', 'uiGridConstants', 'i18nService', 'toaster', 'FileUploader', function ($scope, $http, $timeout, uiGridConstants, i18nService, toaster, FileUploader) {
  2. i18nService.setCurrentLang("zh-cn");
  3. $scope.gridOptions = {
  4. data: 'myData',
  5. showGridFooter: true, //是否显示grid footer
  6. //-------- 分页属性 ----------------
  7. paginationPageSizes: [10, 20, 50, 100], //每页显示个数可选项
  8. paginationCurrentPage: 1, //当前页码
  9. paginationPageSize: 10, //每页显示个数
  10. totalItems: 0,// 总数量
  11. useExternalPagination: true,//是否使用分页按钮
  12. //过滤
  13. enableFiltering: true,
  14. columnDefs: [],
  15. //---------------api---------------------
  16. onRegisterApi: function (gridApi) {
  17. $scope.gridApi = gridApi;
  18. gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
  19. if ($scope.setPagingData) {
  20. $scope.getPagedDataAsync(newPage, pageSize);
  21. }
  22. });
  23. }
  24. };
  25. //查询条件
  26. var condition = $scope.condition = {
  27. searchKey: ""
  28. };
  29. function setColumnDefs() {
  30. $scope.gridOptions.columnDefs = [
  31. {field: 'question', displayName: '问题'},
  32. {field: 'answer', displayName: '答案'},
  33. {field: 'devType', displayName: '设备类型'},
  34. ];
  35. var fields = $scope.gridOptions.columnDefs;
  36. for (var index in fields) {
  37. var item = fields[index];
  38. if (item && item['minWidth'] == null) {
  39. item['minWidth'] = 100;
  40. }
  41. }
  42. }
  43. function initDataGrid() {
  44. //首次加载表格
  45. $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
  46. }
  47. $scope.setPagingData = function (data, curPage, pageSize) {
  48. var pagedData = data.data.dataList;
  49. $scope.myData = pagedData;
  50. $scope.gridOptions.totalItems = data.data.total;
  51. };
  52. $scope.getPagedDataAsync = function (curPage, pageSize) {
  53. var params = {
  54. pageSize: pageSize,
  55. pageIndex: curPage
  56. };
  57. if (condition.searchKey != "") {
  58. params.searchKey = condition.searchKey
  59. }
  60. $http.get('/manager/getFAQDetailList', {
  61. params: params
  62. }).then(function (data) {
  63. $scope.setPagingData(data.data, curPage, pageSize);
  64. }).catch(function (data) {
  65. toaster.pop("error", "提示", "获取数据失败");
  66. });
  67. };
  68. setColumnDefs();
  69. initDataGrid();
  70. /***********************初始化设备类型*********************************/
  71. $scope.devTypeList = [];
  72. $http.get('/device/getDevTypeList', {
  73. params: {pageSize: 1000, pageIndex: 1}
  74. }).then(function (data) {
  75. data = data.data
  76. $scope.devTypeList = data.data.dataList;
  77. }).catch(function (data) {
  78. toaster.pop("error", "提示", "获取设备类型失败");
  79. });
  80. //问题类型:全部、经销商、用户、代理商
  81. $scope.targetList = [
  82. {text: "终端用户", value: "myuser"},
  83. {text: "经销商", value: "dealer"},
  84. {text: "代理商", value: "agent"},
  85. ];
  86. //事件
  87. $scope.event = {
  88. search: function () {
  89. $scope.getPagedDataAsync(1, $scope.gridOptions.paginationPageSize);
  90. }
  91. };
  92. //编辑弹窗需要的数据
  93. $scope.dialogName = "";
  94. $scope.dialogData = {
  95. images: [],
  96. videos: [],
  97. };
  98. //添加
  99. $scope.add = function () {
  100. //重置表单状态
  101. $scope.dataForm.$setPristine();
  102. $scope.dataForm.$setUntouched();
  103. //重置上传控件
  104. closeUploader();
  105. $scope.dialogName = "添加问答";
  106. $scope.dialogData = {
  107. question: "",
  108. answer: "",
  109. images: [],
  110. videos: [],
  111. };
  112. $("#dataPanel").modal();
  113. };
  114. //编辑
  115. $scope.edit = function () {
  116. //重置表单状态
  117. $scope.dataForm.$setPristine();
  118. $scope.dataForm.$setUntouched();
  119. //重置上传控件
  120. closeUploader();
  121. $scope.dialogName = "编辑公告";
  122. var rows = $scope.gridApi.selection.getSelectedRows();
  123. if (rows.length == 0) {
  124. toaster.pop("info", "提示", "请选择数据!");
  125. return;
  126. }
  127. if (rows.length > 1) {
  128. toaster.pop("info", "提示", "只能选中编辑一条数据");
  129. return;
  130. }
  131. var item = rows[0];
  132. var dialogData = $scope.dialogData = $.extend(true, {}, item);//深度克隆
  133. if (dialogData.images == null) {
  134. dialogData.images = [];
  135. }
  136. if (dialogData.videos == null) {
  137. dialogData.videos = [];
  138. }
  139. $("#dataPanel").modal();
  140. };
  141. //删除
  142. $scope.deleteData = function () {
  143. var rows = $scope.gridApi.selection.getSelectedRows();
  144. if (rows.length == 0) {
  145. toaster.pop("info", "提示", "请选择数据!");
  146. return;
  147. }
  148. var ids = [];
  149. for (var i = 0; i < rows.length; i++) {
  150. ids.push(rows[i].id);
  151. }
  152. $.confirm({
  153. content: '确定删除?',
  154. buttons: {
  155. ok: {
  156. btnClass: 'btn-red',
  157. action: function () {
  158. $http({
  159. method: 'POST',
  160. url: '/manager/deleteFAQ',
  161. data: {ids: ids}
  162. }).then(function (response) {
  163. initDataGrid();
  164. }, function (response) {
  165. toaster.pop("error", "提示", "删除失败!");
  166. });
  167. }
  168. },
  169. }
  170. });
  171. };
  172. //提交表单保存
  173. $scope.saveData = function () {
  174. //表单未校验通过不能提交
  175. if ($scope.dataForm.$invalid) {
  176. return;
  177. }
  178. var url = "";
  179. if ($scope.dialogData.id == null) {
  180. url = "/manager/addFAQ";
  181. } else {
  182. url = "/manager/editFAQ";
  183. }
  184. $http({
  185. method: 'POST',
  186. url: url,
  187. data: $scope.dialogData
  188. }).then(function (response) {
  189. $('#dataPanel').modal('hide');//弹窗消失
  190. $scope.getPagedDataAsync($scope.gridOptions.paginationCurrentPage, $scope.gridOptions.paginationPageSize);
  191. }, function (response) {
  192. toaster.pop("error", "提示", "保存失败!");
  193. });
  194. };
  195. //事件绑定
  196. $timeout(function () {
  197. $("#fileUpload").on("change", function () {
  198. var objUrl = getObjectURL(this.files[0]);
  199. if (objUrl) {
  200. $("#previewImg").attr("src", objUrl);
  201. }
  202. });
  203. $("#fileUploadValue").on("focus", function () {
  204. $(this).blur();
  205. });
  206. $("#fileUpload2").on("change", function () {
  207. var objUrl = getObjectURL(this.files[0]);
  208. if (objUrl) {
  209. $("#previewVideo").attr("src", objUrl);
  210. }
  211. });
  212. $("#fileUploadValue2").on("focus", function () {
  213. $(this).blur();
  214. })
  215. });
  216. $scope.removeImgPreview = function () {
  217. $("#fileUpload").val("");
  218. $("#previewImg").attr("src", "");
  219. imageUploader.clearQueue()
  220. };
  221. $scope.removeVideoPreview = function () {
  222. $("#fileUpload2").val("");
  223. $("#previewVideo").attr("src", "");
  224. videoUploader.clearQueue();
  225. };
  226. //建立一個可存取到該file的url
  227. function getObjectURL(file) {
  228. var url = null;
  229. if (window.createObjectURL != undefined) { // basic
  230. url = window.createObjectURL(file);
  231. } else if (window.URL != undefined) { // mozilla(firefox)
  232. url = window.URL.createObjectURL(file);
  233. } else if (window.webkitURL != undefined) { // webkit or chrome
  234. url = window.webkitURL.createObjectURL(file);
  235. }
  236. return url;
  237. }
  238. //图片提交控件
  239. var imageUploader = $scope.imageUploader = new FileUploader({
  240. url: '/common/upload?type=faqImg',
  241. queueLimit: 1, //文件个数
  242. removeAfterUpload: true //上传后删除文件
  243. });
  244. //注册上传事件
  245. imageUploader.onCompleteItem = function (fileItem, response, status, headers) {
  246. if (response.result) {
  247. toaster.pop("success", "提示", "上传成功!");
  248. $scope.dialogData.images.unshift(response.payload);
  249. $scope.dialogData.answer = $scope.dialogData.answer + '<br /> <img src="' + response.payload + '">';
  250. } else {
  251. toaster.pop("error", "失败", response.description);
  252. }
  253. };
  254. imageUploader.onErrorItem = function (fileItem, response, status, headers) {
  255. toaster.pop("error", "提示", "上传失败,请重试");
  256. };
  257. //视频提交控件
  258. var videoUploader = $scope.videoUploader = new FileUploader({
  259. url: '/common/upload?type=faqVideo',
  260. queueLimit: 1, //文件个数
  261. removeAfterUpload: true //上传后删除文件
  262. });
  263. //注册上传事件
  264. videoUploader.onCompleteItem = function (fileItem, response, status, headers) {
  265. if (response.result) {
  266. toaster.pop("success", "提示", "上传成功!");
  267. $scope.dialogData.videos.unshift(response.payload);
  268. $scope.dialogData.answer = $scope.dialogData.answer + '<br /> <a href="' + response.payload + '">播放视频</a>';
  269. } else {
  270. toaster.pop("error", "失败", response.description);
  271. }
  272. };
  273. videoUploader.onErrorItem = function (fileItem, response, status, headers) {
  274. toaster.pop("error", "提示", "上传失败,请重试");
  275. };
  276. /*关闭上传框窗口后恢复上传框初始状态*/
  277. function closeUploader() {
  278. $("#fileUpload").val("");
  279. $("#fileUpload2").val("");
  280. $("#previewImg").attr("src", "");
  281. $("#previewVideo").attr("src", "");
  282. imageUploader.clearQueue();
  283. imageUploader.cancelAll();
  284. videoUploader.clearQueue();
  285. videoUploader.cancelAll();
  286. }
  287. /*图片移除*/
  288. $scope.removeImg = function (index) {
  289. var dialogData = $scope.dialogData;
  290. if (dialogData.images && dialogData.images.length > 0) {
  291. dialogData.images.splice(index, 1);
  292. }
  293. };
  294. /*视频移除*/
  295. $scope.removeVideo = function (index) {
  296. var dialogData = $scope.dialogData;
  297. if (dialogData.videos && dialogData.videos.length > 0) {
  298. dialogData.videos.splice(index, 1);
  299. }
  300. }
  301. }]);