Нет описания

addOrEditNewss.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /**
  2. * 重要事项增加或编辑(正式)
  3. * */
  4. var bigLogoImg; //保存重要事项logo大图
  5. var smallLogoImg; //保存重要事项logo小图
  6. var proimglist = []; //保存附件的数组
  7. var uploader; //上传实例
  8. //var limitNum = 6; //限制数量
  9. $(function() {
  10. autosize($('textarea'));
  11. var n_id = helper.request.queryString("edit_id");
  12. if(n_id) { //修改
  13. //项目所在地 重要事项类型下拉
  14. getProLocations($('#pro_province'), $('#pro_city'), $('#pro_title'))
  15. .then(function() {
  16. getNews(n_id);
  17. });
  18. /* $.when(helper.getDropList.getlistDropByDic($('#news_property'), 'XWLX'),
  19. getProLocations($('#pro_province'), $('#pro_city'), $('#pro_title')))
  20. .done(function() {
  21. getNews(n_id);
  22. });*/
  23. } else {
  24. // helper.getDropList.getlistDropByDic($('#news_property'), 'XWLX'); //重要事项类型下拉
  25. getProLocations($('#pro_province'), $('#pro_city'), $('#pro_title')); //项目所在地
  26. $(".image-crop > img").attr('src', '');
  27. // imgEditor(); //重要事项logo裁剪
  28. // uploaderImages(); //初始化附件上传
  29. }
  30. //添加编辑保存按钮点击
  31. $('#news_save').on('click', saveNews);
  32. });
  33. //获取详情
  34. function getNews(ids) {
  35. $.getJSON(huayi.config.callcenter_url + "messageapi/api/News/getdetails", {
  36. id: ids,
  37. }, function(data) {
  38. if(data.state == "success") {
  39. var res = data.data;
  40. $('#news_title').val(res.title); //重要事项标题
  41. // if(decodeURIComponent(res.content) != "null") {
  42. // $('#news_content').val(decodeURIComponent(res.content)); //重要事项内容
  43. // }
  44. $('#news_sort').val(res.sort); //排序
  45. // $('#news_property').selectpicker('val', res.typeid).trigger('change'); //重要事项类型
  46. // $('#news_level').selectpicker('val', res.level).trigger('change'); //重要事项紧急程度
  47. $('#pro_province').selectpicker('val', res.province).trigger('change'); //省
  48. $('#pro_city').selectpicker('val', res.city).trigger('change'); //市
  49. $('#pro_title').selectpicker('val', res.projectid).trigger('change'); //项目
  50. // //重要事项logo
  51. // if(res.headimg) {
  52. // $('.crop_left img').attr('src', res.headimg);
  53. // } else {
  54. // $('.crop_left img').attr('src', '../../img/pro_default.png');
  55. // }
  56. // imgEditor();
  57. //附件
  58. // proimglist = res.files;
  59. // proimglist = proimglist == null ? [] : proimglist;
  60. // // limitNum -= proimglist.length;
  61. // uploaderImages(); //附件上传
  62. // if(proimglist.length > 0) {
  63. // $('#fileNum').text(proimglist.length);
  64. // var $list = $("#fileList");
  65. // $.each(proimglist, function(i, v) {
  66. // if(v.filesmallurl) {
  67. // //原来的图片
  68. // var $li = $(
  69. // '<li><span class="img_mask"><i class="img_del"></i></span>' +
  70. // '<img src="' + v.filesmallurl + '" title="原来的图片"/>' +
  71. // '<span class="file_name">' + v.filename + '</span>' +
  72. // '</li>'
  73. // );
  74. // $list.append($li);
  75. // } else {
  76. // var $liFile = $(
  77. // '<li><span class="img_mask"><i class="img_del"></i></span>' +
  78. // '<div class="noThumb" title="原来的文件"><i class="glyphicon glyphicon-paperclip"></i><p>无法预览</p></div>' +
  79. // '<span class="file_name">' + v.filename + '</span>' +
  80. // '</li>'
  81. // );
  82. // $list.append($liFile);
  83. // }
  84. // });
  85. // $list.find('li').off('click');
  86. // $list.find('li').on('click', '.img_del', function() {
  87. // $(this).parent().parent().remove();
  88. // var itemFileName = $(this).parent().parent().find('.file_name').text();
  89. // $.each(proimglist, function(i, v) {
  90. // if(v && v.filename == itemFileName) {
  91. // proimglist.splice(i, 1);
  92. // //helper.methods.delImgs(v.filesmallurl, v.fileurl);
  93. // }
  94. // });
  95. // $('#fileNum').text(proimglist.length);
  96. // // limitNum = 6 - proimglist.length;
  97. // uploaderImages();
  98. // });
  99. // }
  100. //
  101. }
  102. });
  103. }
  104. //保存重要事项
  105. function saveNews() {
  106. var n_id = helper.request.queryString("edit_id");
  107. var wUrl, loadIndex;
  108. if(!$.trim($('#news_title').val())) {
  109. layer.confirm('重要事项标题不能为空', {
  110. icon: 2,
  111. btn: ['确定'] //按钮
  112. });
  113. return;
  114. }
  115. // if(!$('#news_property').val()) {
  116. // layer.confirm('请选择重要事项类型', {
  117. // icon: 2,
  118. // btn: ['确定'] //按钮
  119. // });
  120. // return;
  121. // }
  122. //
  123. // if(!Number($('#news_level').val())) {
  124. // layer.confirm('请选择重要事项紧急程度', {
  125. // icon: 2,
  126. // btn: ['确定'] //按钮
  127. // });
  128. // return;
  129. // }
  130. if(!$('#pro_province').val()) {
  131. layer.confirm('请选择所属项目所在地省份!', {
  132. icon: 2,
  133. btn: ['确定'] //按钮
  134. });
  135. return;
  136. }
  137. if(!$('#pro_city').val()) {
  138. layer.confirm('请选择所属项目所在地市!', {
  139. icon: 2,
  140. btn: ['确定'] //按钮
  141. });
  142. return;
  143. }
  144. if(!$('#pro_title').val()) {
  145. layer.confirm('请选择所属项目!', {
  146. icon: 2,
  147. btn: ['确定'] //按钮
  148. });
  149. return;
  150. }
  151. // if(!$.trim($('#news_content').val())) {
  152. // layer.confirm('重要事项内容不能为空', {
  153. // icon: 2,
  154. // btn: ['确定'] //按钮
  155. // });
  156. // return;
  157. // }
  158. if(!regexs.integerReg.test($.trim($('#news_sort').val()))) {
  159. layer.confirm('请输入有效的排序编号(正整数、负整数、0)', {
  160. icon: 2,
  161. btn: ['确定'] //按钮
  162. });
  163. return;
  164. }
  165. if(n_id) {
  166. wURL = "messageapi/api/News/update";
  167. } else {
  168. wURL = "messageapi/api/News/add";
  169. }
  170. $.ajax({
  171. type: "post",
  172. url: huayi.config.callcenter_url + wURL,
  173. dataType: 'json',
  174. async: true,
  175. beforeSend: function() { //触发ajax请求开始时执行
  176. $('#news_save').attr("disabled", true);
  177. $('#news_save').text('保存中...');
  178. loadIndex = layer.load();
  179. },
  180. data: {
  181. id: n_id, // 是 string 重要事项id
  182. title: $('#news_title').val(), // 是 string 标题
  183. // typeid: $('#news_property').val(), // 是 string 类型id
  184. // level: $('#news_level').val(), // 是 int 级别:1最大
  185. province: $('#pro_province').val(), //是 string 省code
  186. city: $('#pro_city').val(), // 是 string 市code
  187. projectid: $('#pro_title').val(), //是 string 项目id
  188. // content: $('#news_content').val(), // 是 string 类型
  189. sort: $('#news_sort').val(), // 是 int 排序字段
  190. // headimg: bigLogoImg, // 否 string 图片
  191. // headsmallimg: smallLogoImg, //否 string 缩略图
  192. // files: proimglist, // 否 list 附件json
  193. },
  194. success: function(data) {
  195. layer.close(loadIndex);
  196. if(data.state == "success") {
  197. var index = parent.layer.getFrameIndex(window.name);
  198. parent.layer.close(index);
  199. parent.$('#table_all').bootstrapTable('refresh');
  200. parent.layer.msg("保存成功");
  201. }
  202. },
  203. error: function(textStatus) {
  204. layer.close(loadIndex);
  205. layer.confirm('网络繁忙,请稍后再试...', {
  206. closeBtn: 0,
  207. btn: ['确定'] //按钮
  208. });
  209. $('#news_save').text('保存');
  210. $('#news_save').attr("disabled", false);
  211. },
  212. complete: function(XMLHttpRequest, textStatus) {
  213. layer.close(loadIndex);
  214. if(textStatus == 'timeout') {
  215. var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
  216. xmlhttp.abort();
  217. layer.confirm('网络超时,请稍后再试...', {
  218. closeBtn: 0,
  219. btn: ['确定'] //按钮
  220. });    
  221. }
  222. $('#news_save').text('保存');
  223. $('#news_save').attr("disabled", false);
  224. },
  225. });
  226. }
  227. //重要事项logo剪切
  228. function imgEditor() {
  229. var o = $(".image-crop > img");
  230. o.cropper("destroy");
  231. o.cropper({
  232. aspectRatio: 1 / 1,
  233. resizable: true,
  234. dragCrop: true,
  235. preview: ".img-preview",
  236. done: function() {
  237. }
  238. });
  239. var r = $("#inputImage");
  240. var fileName = $("#file_name").val();
  241. if(window.FileReader) {
  242. r.change(function() {
  243. $('.progress-bar').css('width', '0%');
  244. $('.progress-bar').text('0%');
  245. var e, i = new FileReader,
  246. t = this.files;
  247. if(t.length && (e = t[0], /^image\/\w+$/.test(e.type))) {
  248. i.readAsDataURL(e);
  249. fileName = e.name;
  250. i.onload = function() {
  251. r.val("");
  252. o.cropper("reset", !0).cropper("replace", this.result);
  253. }
  254. } else {
  255. layer.msg("请选择本地图片文件");
  256. }
  257. });
  258. $("#setDrag").click(function() {
  259. var self = $(this);
  260. o.cropper("setDragMode", "crop");
  261. var imgUrl = o.cropper("getCroppedCanvas").toDataURL('image/png');
  262. var filesjson;
  263. var list = [],
  264. listItem = {};
  265. listItem.filename = fileName;
  266. listItem.fileurl = imgUrl;
  267. list.push(listItem);
  268. filesjson = JSON.stringify(list);
  269. upLoadFile(filesjson, self);
  270. });
  271. $("#zoomIn").click(function() {
  272. $('.progress-bar').css('width', '0%');
  273. $('.progress-bar').text('0%');
  274. o.cropper("zoom", .1)
  275. });
  276. $("#zoomOut").click(function() {
  277. $('.progress-bar').css('width', '0%');
  278. $('.progress-bar').text('0%');
  279. o.cropper("zoom", -.1)
  280. });
  281. $("#rotateLeft").click(function() {
  282. $('.progress-bar').css('width', '0%');
  283. $('.progress-bar').text('0%');
  284. o.cropper("rotate", 45)
  285. });
  286. $("#rotateRight").click(function() {
  287. $('.progress-bar').css('width', '0%');
  288. $('.progress-bar').text('0%');
  289. o.cropper("rotate", -45)
  290. });
  291. } else {
  292. r.addClass("hide");
  293. }
  294. }
  295. /**
  296. * 图片的上传
  297. * fjson 上传图片 的数据
  298. * self 调用者的this
  299. */
  300. function upLoadFile(fjson, self) {
  301. var formData = new FormData();
  302. formData.append("uploadtype", 'proManagement');
  303. formData.append("filesjson", fjson);
  304. $.ajax({
  305. type: "post",
  306. url: huayi.config.callcenter_url + "fileserverapi/Api/Upload",
  307. data: formData, //这里上传的数据使用了formData 对象
  308. processData: false,
  309. contentType: false, //必须false才会自动加上正确的Content-Type
  310. xhr: function() { //这是关键 获取原生的xhr对象 做以前做的所有事情
  311. var xhr = jQuery.ajaxSettings.xhr();
  312. xhr.upload.onload = function() {
  313. //alert('finish downloading')
  314. }
  315. xhr.upload.onprogress = function(ev) {
  316. //console.log(ev);
  317. //if(ev.lengthComputable) {
  318. var percent = 100 * ev.loaded / ev.total;
  319. //console.log(percent, ev);
  320. $('.progress-bar').css('width', percent + '%');
  321. $('.progress-bar').text(percent + '%');
  322. //}
  323. }
  324. return xhr;
  325. },
  326. async: true,
  327. beforeSend: function() { //触发ajax请求开始时执行
  328. self.attr("disabled", true);
  329. self.text('图片上传中...');
  330. $('.anniu').find('.btnn').css('backgroundColor', '#778592');
  331. },
  332. // data: {
  333. // uploadtype: 'proManagement',
  334. // filesjson: fjson,
  335. // },
  336. success: function(result) {
  337. result = $.parseJSON(result);
  338. self.attr("disabled", false);
  339. self.text('上传图片');
  340. $('.anniu').find('.btnn').css('backgroundColor', '#2f4050');
  341. /*验证请求*/
  342. if(result.state.toLowerCase() == "success") {
  343. bigLogoImg = result.data[0].fileurl;
  344. smallLogoImg = result.data[0].filesmallurl;
  345. layer.msg("图片上传成功");
  346. } else {
  347. layer.msg(result.message);
  348. }
  349. },
  350. error: function(textStatus) {
  351. $('.progress-bar').css('width', '0%');
  352. $('.progress-bar').text('0%');
  353. layer.confirm('网络繁忙,请稍后再试...', {
  354. btn: ['确定'] //按钮
  355. });
  356. self.text('上传图片');
  357. self.attr("disabled", false);
  358. $('.anniu').find('.btnn').css('backgroundColor', '#2f4050');
  359. },
  360. complete: function(XMLHttpRequest, textStatus) {
  361. //$('.progress-bar').css('width', '0%');
  362. //$('.progress-bar').text('0%');
  363. if(textStatus == 'timeout') {
  364. var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
  365. xmlhttp.abort();
  366. layer.confirm('网络超时,请稍后再试...', {
  367. btn: ['确定'] //按钮
  368. });    
  369. }
  370. self.text('上传图片');
  371. self.attr("disabled", false);
  372. $('.anniu').find('.btnn').css('backgroundColor', '#2f4050');
  373. },
  374. });
  375. }
  376. //上传附件
  377. function uploaderImages() {
  378. if(uploader) {
  379. uploader.destroy();
  380. }
  381. uploader = WebUploader.create({
  382. auto: true, // 选完文件后,是否自动上传
  383. swf: '../plugins/webuploader/Uploader.swf', // swf文件路径
  384. server: huayi.config.callcenter_url + "fileserverapi/Api/Upload", // 文件接收服务端
  385. pick: '#imgPicker', // 选择文件的按钮。可选
  386. formData: {
  387. uploadtype: 'proDrawings'
  388. },
  389. // 只允许选择图片文件。
  390. accept: {
  391. // title: 'Images',
  392. // extensions: 'gif,jpg,jpeg,bmp,png',
  393. // mimeTypes: 'image/*'
  394. },
  395. compress: false, //webuploader默认压缩图片,设置compress:false,可以按照原始比例上传图片
  396. //fileVal: "upFile",//设置文件上传域的name。
  397. // threads: limitNum, //上传并发数。允许同时最大上传进程数,为了保证文件上传顺序
  398. // fileNumLimit: limitNum,
  399. fileSizeLimit: 50 * 1024 * 1024, // 50 M
  400. fileSingleSizeLimit: 20 * 1024 * 1024 // 20 M
  401. });
  402. uploader.on('fileQueued', function(file) {
  403. var $list = $("#fileList"),
  404. $li = $(
  405. '<li id="' + file.id + '"><span class="img_mask"><i class="img_del"></i></span>' +
  406. '<img/>' +
  407. '<span class="img_state"><i></i></span>' +
  408. '<span class="file_name">' + file.name + '</span>' +
  409. '</li>'
  410. ),
  411. $img = $li.find('img');
  412. // $list为容器jQuery实例
  413. $list.append($li);
  414. // 创建缩略图
  415. uploader.makeThumb(file, function(error, src) {
  416. if(error) {
  417. $img.replaceWith('<div class="noThumb"><i class="glyphicon glyphicon-paperclip"></i><p>无法预览</p></div>');
  418. return;
  419. }
  420. $img.attr('src', src);
  421. }, 109, 109); //109*109为缩略图尺寸
  422. //绑定删除
  423. $('#' + file.id).on('click', '.img_del', function() {
  424. uploader.removeFile(file);
  425. var $fileLi = $('#' + file.id);
  426. $fileLi.off().find('.img_del').off().end().remove();
  427. var itemFileName = $(this).parent().parent().find('.file_name').text();
  428. $.each(proimglist, function(i, v) {
  429. if(v && v.filename == itemFileName) {
  430. proimglist.splice(i, 1);
  431. helper.methods.delImgs(v.filesmallurl, v.fileurl);
  432. }
  433. });
  434. $('#fileNum').text(proimglist.length);
  435. });
  436. });
  437. // 文件上传过程中创建进度条实时显示。
  438. uploader.on('uploadProgress', function(file, percentage) {
  439. var $li = $('#' + file.id),
  440. $percent = $li.find('.progress span');
  441. // 避免重复创建
  442. if(!$percent.length) {
  443. $percent = $('<div class="progress" style="">' +
  444. '<span class="text">0%</span>' +
  445. '<span class="percentage"></span>' +
  446. '</div>')
  447. .appendTo($li)
  448. .find('span');
  449. }
  450. $percent.eq(0).text(Math.round(percentage * 100) + '%');
  451. $percent.eq(1).css('width', Math.round(percentage * 100) + '%');
  452. });
  453. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  454. uploader.on('uploadSuccess', function(file, res) {
  455. proimglist.push(res.data[0]);
  456. $('#fileNum').text(proimglist.length);
  457. $('#' + file.id).addClass('upload-state-done');
  458. });
  459. // 文件上传失败,显示上传出错。
  460. uploader.on('uploadError', function(file) {
  461. var $li = $('#' + file.id),
  462. $error = $li.find('div.error');
  463. // 避免重复创建
  464. if(!$error.length) {
  465. $error = $('<div class="error"></div>').appendTo($li);
  466. }
  467. $error.text('上传失败');
  468. });
  469. // 完成上传完了,成功或者失败,先删除进度条。
  470. uploader.on('uploadComplete', function(file) {
  471. $('#' + file.id).find('.progress').remove();
  472. });
  473. uploader.onError = function(code) {
  474. //console.log('Error:' + code);
  475. switch(code) {
  476. // case "Q_EXCEED_NUM_LIMIT":
  477. // layer.msg('只能上传六个文件。');
  478. // break;
  479. case "Q_TYPE_DENIED":
  480. layer.msg('图片类型不正确。');
  481. break;
  482. case "F_DUPLICATE":
  483. layer.msg('该文件已上传,请选择其它文件。');
  484. break;
  485. case "F_EXCEED_SIZE":
  486. layer.msg('单文件大小不能超过20M。');
  487. break;
  488. case "Q_EXCEED_SIZE_LIMIT":
  489. layer.msg('总文件大小不能超过50M。');
  490. break;
  491. default:
  492. break;
  493. }
  494. };
  495. }
  496. ////项目所在地 省/市/项目 联动
  497. function getProLocations(obj, obj1, obj2) {
  498. var dtd = $.Deferred(); //在函数内部,新建一个Deferred对象
  499. obj.empty();
  500. obj1.empty();
  501. obj2.empty();
  502. obj.append('<option selected="selected" value="">请选择所在省</option>');
  503. obj1.append('<option selected="selected" value="">请选择所在市</option>');
  504. obj2.append('<option selected="selected" value="">请选择项目</option>');
  505. $.ajax({
  506. type: "get",
  507. url: huayi.config.callcenter_url + "equipmentapi/api/equipment/getprojecttree",
  508. dataType: 'json',
  509. async: true,
  510. success: function(data) {
  511. var content = data.data; //省信息
  512. var cityInfos = []; //市信息
  513. var proInfos = []; //项目信息
  514. //初始化省
  515. $(content).each(function(i, n) {
  516. if(n.parentcode === -1) {
  517. $("<option value='" + n.code + "'>" + n.name + "</option>").appendTo(obj);
  518. if(n.entityJson != -1 && n.entityJson.length > 0) {
  519. cityInfos.push(n.entityJson);
  520. }
  521. }
  522. });
  523. obj.selectpicker('refresh');
  524. //市
  525. obj.change(function() {
  526. obj1.empty();
  527. for(var i = 0; i < cityInfos.length; i++) {
  528. $(cityInfos[i]).each(function(j, v) {
  529. if(v.parentcode > 0) {
  530. if(obj.find("option:selected").val() == v.parentcode) {
  531. $("<option value='" + v.code + "'>" + v.name + "</option>").appendTo(obj1);
  532. if(v.entityJson != -1 && v.entityJson.length > 0) {
  533. proInfos.push(v.entityJson);
  534. }
  535. }
  536. }
  537. });
  538. }
  539. obj1.selectpicker('refresh');
  540. if(obj.find("option:selected").val() == '') {
  541. obj1.append('<option selected="selected" value="">请选择所在市</option>');
  542. }
  543. if(obj1.val() == -1) {
  544. obj1.append('<option selected="selected" value="">没有数据</option>');
  545. }
  546. obj1.trigger('change');
  547. });
  548. //项目
  549. obj1.change(function() {
  550. obj2.empty();
  551. for(var j = 0; j < proInfos.length; j++) {
  552. $(proInfos[j]).each(function(i, n) {
  553. if(n.parentcode > 0) {
  554. if(obj1.find("option:selected").val() == n.parentcode) {
  555. $("<option value='" + n.code + "'>" + n.name + "</option>").appendTo(obj2);
  556. }
  557. }
  558. });
  559. }
  560. obj2.selectpicker('refresh');
  561. if(obj1.find("option:selected").val() == '') {
  562. obj2.append('<option selected="selected" value="">请选择项目</option>');
  563. }
  564. if(obj2.val() == -1 || obj1.val() == '') {
  565. obj2.append('<option selected="selected" value="">没有项目</option>');
  566. }
  567. });
  568. dtd.resolve(); // 改变Deferred对象的执行状态
  569. },
  570. });
  571. return dtd.promise(); // 返回promise对象
  572. }