Нет описания

addOrEditPro.js 16KB

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