faq.js 11 KB

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