Нет описания

addOrEditStudyMaterial.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * 添加或编辑文件资料
  3. * */
  4. var proimglist = []; //保存项目图纸的数组
  5. var uploader; //上传实例
  6. //var limitNum = 6; //限制数量
  7. //编辑器配置
  8. KindEditor.ready(function(K) {
  9. window.editor = K.create('#pro_content', {
  10. imgMaxSize: 10 * 1024 * 1024,
  11. uploadJson: '/fileapi/Api/Upload',
  12. //fileManagerJson: '../../js/kindeditor/asp.net/file_manager_json.ashx',
  13. allowFileManager: false,
  14. allowFileManager: true,
  15. autoHeightMode: true,
  16. afterCreate: function() {
  17. this.loadPlugin('autoheight');
  18. },
  19. items: [
  20. 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
  21. 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
  22. 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
  23. 'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
  24. 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
  25. 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image',
  26. 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
  27. 'anchor', 'link', 'unlink', '|', 'about'
  28. ]
  29. });
  30. });
  31. $(function() {
  32. var fid = helper.request.queryString("edit_id");
  33. if(fid) {
  34. getBtnsInfo(fid);
  35. } else {
  36. uploaderImages();
  37. }
  38. $("#save_btns").on('click', saveStudyMaterial);
  39. });
  40. //获取详情
  41. function getBtnsInfo(fid) {
  42. $.ajax({
  43. type: "get",
  44. url: huayi.config.callcenter_url + "equipmentapi/api/StudyMaterialsInfo/getdetails",
  45. dataType: 'json',
  46. async: true,
  47. data: {
  48. id: fid
  49. },
  50. success: function(data) {
  51. /*验证请求*/
  52. if(data.state.toLowerCase() == "success") {
  53. var res = data.data;
  54. if(res) {
  55. $("#columsTitle").val(res.title);
  56. editor.html(decodeURIComponent(res.content));
  57. proimglist = res.studymaterfile;
  58. proimglist = proimglist == null ? [] : proimglist;
  59. // limitNum -= proimglist.length;
  60. uploaderImages(); //附件上传
  61. if(proimglist.length > 0) {
  62. // $('#fileNum').text(proimglist.length);
  63. var $list = $("#fileList");
  64. $.each(proimglist, function(i, v) {
  65. if(v.filesmallurl) {
  66. //原来的图片
  67. var $li = $(
  68. '<li><span class="img_mask"><i class="img_del"></i></span>' +
  69. '<img src="' + v.filesmallurl + '" title="原来的图片"/>' +
  70. '<span class="file_name">' + v.filename + '</span>' +
  71. '</li>'
  72. );
  73. $list.append($li);
  74. } else {
  75. var $liFile = $(
  76. '<li><span class="img_mask"><i class="img_del"></i></span>' +
  77. '<div class="noThumb" title="原来的文件"><i class="glyphicon glyphicon-paperclip"></i><p>无法预览</p></div>' +
  78. '<span class="file_name">' + v.filename + '</span>' +
  79. '</li>'
  80. );
  81. $list.append($liFile);
  82. }
  83. });
  84. $list.find('li').off('click');
  85. $list.find('li').on('click', '.img_del', function() {
  86. $(this).parent().parent().remove();
  87. var itemFileName = $(this).parent().parent().find('.file_name').text();
  88. $.each(proimglist, function(i, v) {
  89. if(v && v.filename == itemFileName) {
  90. proimglist.splice(i, 1);
  91. }
  92. });
  93. // $('#fileNum').text(proimglist.length);
  94. // limitNum = 6 - proimglist.length;
  95. uploaderImages();
  96. });
  97. }
  98. }
  99. }
  100. }
  101. });
  102. }
  103. //保存
  104. function saveStudyMaterial() {
  105. var fid = helper.request.queryString("edit_id");
  106. var wURL;
  107. if(!$.trim($('#columsTitle').val())) {
  108. layer.confirm('文件资料标题不能为空', {
  109. icon: 2,
  110. btn: ['确定'] //按钮
  111. });
  112. return;
  113. }
  114. if(!$.trim(encodeURIComponent(editor.html()))) {
  115. layer.confirm('文件资料内容不能为空', {
  116. icon: 2,
  117. btn: ['确定'] //按钮
  118. });
  119. return;
  120. }
  121. if(fid) {
  122. wURL = "equipmentapi/api/StudyMaterialsInfo/update";
  123. } else {
  124. wURL = "equipmentapi/api/StudyMaterialsInfo/add";
  125. }
  126. $.ajax({
  127. type: "post",
  128. url: huayi.config.callcenter_url + wURL,
  129. dataType: 'json',
  130. async: true,
  131. data: {
  132. id: fid,
  133. title: $("#columsTitle").val(), //string 文件资料标题 )
  134. content: encodeURIComponent(editor.html()), //string 文件资料内容
  135. studymaterfile: proimglist, // 否 List<FileBaseModel>
  136. },
  137. success: function(data) {
  138. /*验证请求*/
  139. if(data.state == "success") {
  140. var index = parent.layer.getFrameIndex(window.name);
  141. parent.layer.close(index);
  142. parent.$('#table_all').bootstrapTable('refresh');
  143. parent.layer.msg("保存成功");
  144. } else {
  145. layer.msg(data.message);
  146. }
  147. }
  148. });
  149. }
  150. //上传附件
  151. function uploaderImages() {
  152. if(uploader) {
  153. uploader.destroy();
  154. }
  155. uploader = WebUploader.create({
  156. auto: true, // 选完文件后,是否自动上传
  157. swf: '../plugins/webuploader/Uploader.swf', // swf文件路径
  158. server: huayi.config.callcenter_url + "fileserverapi/Api/Upload", // 文件接收服务端
  159. pick: '#imgPicker', // 选择文件的按钮。可选
  160. formData: {
  161. uploadtype: 'studyMaterialFile'
  162. },
  163. // 只允许选择图片文件。
  164. accept: {
  165. // title: 'Images',
  166. // extensions: 'gif,jpg,jpeg,bmp,png',
  167. // mimeTypes: 'image/*'
  168. },
  169. compress: false, //webuploader默认压缩图片,设置compress:false,可以按照原始比例上传图片
  170. //fileVal: "upFile",//设置文件上传域的name。
  171. // threads: limitNum, //上传并发数。允许同时最大上传进程数,为了保证文件上传顺序
  172. // fileNumLimit: limitNum,
  173. fileSizeLimit: 50 * 1024 * 1024, // 50 M
  174. fileSingleSizeLimit: 20 * 1024 * 1024 // 20 M
  175. });
  176. uploader.on('fileQueued', function(file) {
  177. var $list = $("#fileList"),
  178. $li = $(
  179. '<li id="' + file.id + '"><span class="img_mask"><i class="img_del"></i></span>' +
  180. '<img/>' +
  181. '<span class="img_state"><i></i></span>' +
  182. '<span class="file_name">' + file.name + '</span>' +
  183. '</li>'
  184. ),
  185. $img = $li.find('img');
  186. // $list为容器jQuery实例
  187. $list.append($li);
  188. // 创建缩略图
  189. uploader.makeThumb(file, function(error, src) {
  190. if(error) {
  191. $img.replaceWith('<div class="noThumb"><i class="glyphicon glyphicon-paperclip"></i><p>无法预览</p></div>');
  192. return;
  193. }
  194. $img.attr('src', src);
  195. }, 109, 109); //109*109为缩略图尺寸
  196. //绑定删除
  197. $('#' + file.id).on('click', '.img_del', function() {
  198. uploader.removeFile(file);
  199. var $fileLi = $('#' + file.id);
  200. $fileLi.off().find('.img_del').off().end().remove();
  201. var itemFileName = $(this).parent().parent().find('.file_name').text();
  202. $.each(proimglist, function(i, v) {
  203. if(v && v.filename == itemFileName) {
  204. proimglist.splice(i, 1);
  205. helper.methods.delImgs(v.filesmallurl, v.fileurl);
  206. }
  207. });
  208. // $('#fileNum').text(proimglist.length);
  209. });
  210. });
  211. // 文件上传过程中创建进度条实时显示。
  212. uploader.on('uploadProgress', function(file, percentage) {
  213. var $li = $('#' + file.id),
  214. $percent = $li.find('.progress span');
  215. // 避免重复创建
  216. if(!$percent.length) {
  217. $percent = $('<div class="progress" style="">' +
  218. '<span class="text">0%</span>' +
  219. '<span class="percentage"></span>' +
  220. '</div>')
  221. .appendTo($li)
  222. .find('span');
  223. }
  224. $percent.eq(0).text(Math.round(percentage * 100) + '%');
  225. $percent.eq(1).css('width', Math.round(percentage * 100) + '%');
  226. });
  227. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  228. uploader.on('uploadSuccess', function(file, res) {
  229. proimglist.push(res.data[0]);
  230. // $('#fileNum').text(proimglist.length);
  231. $('#' + file.id).addClass('upload-state-done');
  232. });
  233. // 文件上传失败,显示上传出错。
  234. uploader.on('uploadError', function(file) {
  235. var $li = $('#' + file.id),
  236. $error = $li.find('div.error');
  237. // 避免重复创建
  238. if(!$error.length) {
  239. $error = $('<div class="error"></div>').appendTo($li);
  240. }
  241. $error.text('上传失败');
  242. });
  243. // 完成上传完了,成功或者失败,先删除进度条。
  244. uploader.on('uploadComplete', function(file) {
  245. $('#' + file.id).find('.progress').remove();
  246. });
  247. uploader.onError = function(code) {
  248. //console.log('Error:' + code);
  249. switch(code) {
  250. // case "Q_EXCEED_NUM_LIMIT":
  251. // layer.msg('只能上传六个文件。');
  252. // break;
  253. case "Q_TYPE_DENIED":
  254. layer.msg('文件类型不正确。');
  255. break;
  256. case "F_DUPLICATE":
  257. layer.msg('该文件已上传,请选择其它文件。');
  258. break;
  259. case "F_EXCEED_SIZE":
  260. layer.msg('单文件大小不能超过20M。');
  261. break;
  262. case "Q_EXCEED_SIZE_LIMIT":
  263. layer.msg('总文件大小不能超过50M。');
  264. break;
  265. default:
  266. break;
  267. }
  268. };
  269. }