jeeplus.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /**
  2. * 工具组件 对原有的工具进行封装,自定义某方法统一处理
  3. *
  4. * @author lgf 2018-4-10
  5. * @Url: http://www.jeeplus.org
  6. * @version 2.0v
  7. */
  8. (function() {
  9. jp = {
  10. /**使用jp.open代替top.layer.open,参数使用完全一致,请参照layer官网, 不在直接暴露layer在,jeeplus对layer进行统一封装**/
  11. open:top.layer.open,
  12. /**通知方法,不阻塞浏览器当前窗口,四个级别 info,warning,error,success,图标不同,其余用法完全相同*/
  13. info:function(msg){
  14. return top.layer.msg(msg);
  15. },
  16. warning: function(msg){//通知
  17. return top.layer.msg(msg, {icon:0});
  18. },
  19. success:function(msg){
  20. return top.layer.msg(msg, {icon:1});
  21. },
  22. error:function(msg){
  23. return top.layer.msg(msg, {icon:2,time: 2000});
  24. },
  25. //layer之外的另一个选择toast风格消息提示框,直接使用jp.toastr调用
  26. toastr:(function(){
  27. top.toastr.options = {
  28. "closeButton": true,
  29. "debug": false,
  30. "progressBar": true,
  31. "positionClass": "toast-top-right",
  32. "onclick": null,
  33. "showDuration": "400",
  34. "hideDuration": "5000",
  35. "timeOut": "10000",
  36. "extendedTimeOut": "1000",
  37. "showEasing": "swing",
  38. "hideEasing": "linear",
  39. "showMethod": "fadeIn",
  40. "hideMethod": "fadeOut"
  41. }
  42. return top.toastr;
  43. })(),
  44. //页面提示声音
  45. voice:function() {
  46. var audio = document.createElement("audio");
  47. audio.src = ctxStatic+"/common/voice/default.wav";
  48. audio.play();
  49. },
  50. /**加载层,一直阻塞浏览器窗口,必须手动调用close方法关闭*/
  51. loading:function(msg){
  52. if(!msg){
  53. msg = '正在提交,请稍等...';
  54. }
  55. var index = top.layer.msg(msg, {
  56. icon: 16
  57. ,shade: 0.01,
  58. time:999999999//设置超长时间
  59. });
  60. return index;
  61. },
  62. close:function(index){
  63. if(index){
  64. top.layer.close(index);
  65. }else{
  66. top.layer.closeAll();
  67. }
  68. },
  69. /**alert弹出框,阻塞浏览器窗口*/
  70. alert:function(msg){
  71. top.layer.alert(msg, {
  72. skin: 'layui-layer-lan'
  73. ,area:['auto', 'auto']
  74. ,icon: 0
  75. ,closeBtn: 0
  76. ,anim: 4 //动画类型
  77. });
  78. },
  79. /**询问框,阻塞浏览器窗口*/
  80. confirm:function(msg, succFuc, cancelFuc){//msg:询问信息, succFuc:点‘是’调用的函数, errFuc:点‘否’调用的函数
  81. top.layer.confirm(msg,
  82. {icon: 3, title:'系统提示', btn: ['是','否'] //按钮
  83. }, function(index){
  84. if (typeof succFuc == 'function') {
  85. succFuc();
  86. }else{
  87. location = succFuc;
  88. jp.success("操作成功!", {icon:1});
  89. }
  90. top.layer.close(index);
  91. }, function(index){
  92. if(cancelFuc)
  93. cancelFuc();
  94. top.layer.close(index);
  95. });
  96. return false;
  97. },
  98. prompt:function (title, href) {
  99. var index = top.layer.prompt({title: title, formType: 2}, function(text){
  100. if (typeof href == 'function') {
  101. href(text);
  102. }else{
  103. location = href + encodeURIComponent(text);
  104. }
  105. top.layer.close(index);
  106. });
  107. },
  108. //打开一个窗体
  109. windowOpen:function(url, name, width, height){
  110. var top=parseInt((window.screen.height-height)/2,10),left=parseInt((window.screen.width-width)/2,10),
  111. options="location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,"+
  112. "resizable=yes,scrollbars=yes,"+"width="+width+",height="+height+",top="+top+",left="+left;
  113. window.open(url ,name , options);
  114. },
  115. // //打开对话框(添加修改)
  116. // openDialog: function(title,url,width,height, $table){
  117. // var auto = true;//是否使用响应式,使用百分比时,应设置为false
  118. // if(width.indexOf("%")>=0 || height.indexOf("%")>=0 ){
  119. // auto =false;
  120. // }
  121. // top.layer.open({
  122. // type: 2,
  123. // area: [width, height],
  124. // title: title,
  125. // auto:auto,
  126. // maxmin: true, //开启最大化最小化按钮
  127. // content: url ,
  128. // btn: ['确定', '关闭'],
  129. // yes: function(index, layero){
  130. // var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象,执行iframe页的方法:iframeWin.method();
  131. // if(!$table){//如果不传递table对象过来,按约定的默认id获取table对象
  132. // $table = $('#table')
  133. // }
  134. // iframeWin.contentWindow.doSubmit($table, index);
  135. // },
  136. // cancel: function(index){
  137. // }
  138. // });
  139. // },
  140. //
  141. // //打开对话框(查看)
  142. // openDialogView :function(title,url,width,height){
  143. // var auto = true;//是否使用响应式,使用百分比时,应设置为false
  144. // if(width.indexOf("%")>=0 || height.indexOf("%")>=0 ){
  145. // auto =false;
  146. // }
  147. // top.layer.open({
  148. // type: 2,
  149. // area: [width, height],
  150. // title: title,
  151. // auto:auto,
  152. // maxmin: true, //开启最大化最小化按钮
  153. // content: url ,
  154. // btn: ['关闭'],
  155. // cancel: function(index){
  156. // }
  157. // });
  158. //
  159. // },
  160. /**打开图片预览框**/
  161. showPic:function(url){
  162. var json = {
  163. "data": [ //相册包含的图片,数组格式
  164. {
  165. "src": url, //原图地址
  166. }
  167. ]
  168. };
  169. top.layer.photos({
  170. photos: json
  171. ,anim: 0 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  172. });
  173. },
  174. /**用户选择框**/
  175. openUserSelectDialog:function(isMultiSelect, yesFuc){
  176. top.layer.open({
  177. type: 2,
  178. area: ['900px', '560px'],
  179. title:"选择用户",
  180. auto:true,
  181. maxmin: true, //开启最大化最小化按钮
  182. content: ctx+"/sys/user/userSelect?isMultiSelect="+isMultiSelect,
  183. btn: ['确定', '关闭'],
  184. yes: function(index, layero){
  185. var ids = layero.find("iframe")[0].contentWindow.getIdSelections();
  186. var names = layero.find("iframe")[0].contentWindow.getNameSelections();
  187. var loginNames = layero.find("iframe")[0].contentWindow.getLoginNameSelections();
  188. if(ids.length ==0){
  189. jp.warning("请选择至少一个用户!");
  190. return;
  191. }
  192. // 执行保存
  193. yesFuc(ids.join(","), names.join(","), loginNames.join(","));
  194. top.layer.close(index);
  195. },
  196. cancel: function(index){
  197. //取消默认为空,如需要请自行扩展。
  198. top.layer.close(index);
  199. }
  200. });
  201. },
  202. /**角色选择框**/
  203. openRoleSelectDialog:function(isMultiSelect, yesFuc){
  204. var url = ctx + "/sys/role/data";
  205. var fieldLabels = "角色名|英文名";
  206. var fieldKeys = "name|enname";
  207. top.layer.open({
  208. type: 2,
  209. area: ['800px', '500px'],
  210. title:"角色选择",
  211. auto:true,
  212. name:'friend',
  213. content: ctx+"/tag/gridselect?url="+encodeURIComponent(url)+"&fieldLabels="+encodeURIComponent(fieldLabels)+"&fieldKeys="+encodeURIComponent(fieldKeys)+"&searchLabels="+encodeURIComponent(fieldLabels)+"&searchKeys="+encodeURIComponent(fieldKeys)+"&isMultiSelected="+isMultiSelect,
  214. btn: ['确定', '关闭'],
  215. yes: function(index, layero){
  216. var iframeWin = layero.find('iframe')[0].contentWindow; //得到iframe页的窗口对象,执行iframe页的方法:iframeWin.method();
  217. var items = iframeWin.getSelections();
  218. if(items == ""){
  219. jp.warning("必须选择一条数据!");
  220. return;
  221. }
  222. var ids = [];
  223. var names = [];
  224. for(var i=0; i<items.length; i++){
  225. var item = items[i];
  226. ids.push(item.id);
  227. names.push(item.enname)
  228. }
  229. yesFuc(ids.join(","), names.join(","));
  230. top.layer.close(index);//关闭对话框。
  231. },
  232. cancel: function(index){
  233. }
  234. });
  235. },
  236. dateFormat:function (timestamp, format) {
  237. var _this = new Date(timestamp);
  238. var o = {
  239. "M+": _this.getMonth() + 1,
  240. // month
  241. "d+": _this.getDate(),
  242. // day
  243. "h+": _this.getHours(),
  244. // hour
  245. "m+": _this.getMinutes(),
  246. // minute
  247. "s+": _this.getSeconds(),
  248. // second
  249. "q+": Math.floor((_this.getMonth() + 3) / 3),
  250. // quarter
  251. "S": _this.getMilliseconds()
  252. // millisecond
  253. };
  254. if (/(y+)/.test(format) || /(Y+)/.test(format)) {
  255. format = format.replace(RegExp.$1, (_this.getFullYear() + "").substr(4 - RegExp.$1.length));
  256. }
  257. for (var k in o) {
  258. if (new RegExp("(" + k + ")").test(format)) {
  259. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  260. }
  261. }
  262. return format;
  263. },
  264. escapeHTML: function(a){
  265. a = "" + a;
  266. return a.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");;
  267. },
  268. /**
  269. * @function unescapeHTML 还原html脚本 < > & " '
  270. * @param a -
  271. * 字符串
  272. */
  273. unescapeHTML: function(a){
  274. a = "" + a;
  275. return a.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(/&apos;/g, "'");
  276. },
  277. //获取字典标签
  278. getDictLabel : function(data, value, defaultValue){
  279. for (var i=0; i<data.length; i++){
  280. var row = data[i];
  281. if (row.value == value){
  282. return row.label;
  283. }
  284. }
  285. return defaultValue;
  286. },
  287. post:function(url,data,callback){
  288. $.ajax({
  289. url:url,
  290. method:"post",
  291. data:data,
  292. error:function(xhr,textStatus){
  293. if(xhr.status == 0){
  294. jp.info("连接失败,请检查网络!")
  295. }else if(xhr.status == 404){
  296. var errDetail ="<font color='red'>404,请求地址不存在!</font>";
  297. top.layer.alert(errDetail , {
  298. icon: 2,
  299. area:['auto','auto'],
  300. title:"请求出错"
  301. })
  302. }else if(xhr.status && xhr.responseText){
  303. var errDetail ="<font color='red'>"+ xhr.responseText.replace(/[\r\n]/g,"<br>").replace(/[\r]/g,"<br>").replace(/[\n]/g,"<br>")+"</font>";
  304. top.layer.alert(errDetail , {
  305. icon: 2,
  306. area:['80%','70%'],
  307. title:xhr.status+"错误"
  308. })
  309. }else{
  310. var errDetail ="<font color='red'>未知错误!</font>";
  311. top.layer.alert(errDetail , {
  312. icon: 2,
  313. area:['auto','auto'],
  314. title:"真悲剧,后台抛出异常了"
  315. })
  316. }
  317. },
  318. success:function(data,textStatus,jqXHR){
  319. if(data.indexOf == "_login_page_"){//登录超时
  320. location.reload(true);
  321. }else{
  322. callback(data);
  323. }
  324. }
  325. });
  326. },
  327. get:function(url,callback){
  328. $.ajax({
  329. url:url,
  330. method:"get",
  331. error:function(xhr,textStatus){
  332. if(xhr.status == 0){
  333. jp.info("连接失败,请检查网络!")
  334. }else if(xhr.status == 404){
  335. var errDetail ="<font color='red'>404,请求地址不存在!</font>";
  336. top.layer.alert(errDetail , {
  337. icon: 2,
  338. area:['auto','auto'],
  339. title:"请求出错"
  340. })
  341. }else if(xhr.status && xhr.responseText){
  342. var errDetail ="<font color='red'>"+ xhr.responseText.replace(/[\r\n]/g,"<br>").replace(/[\r]/g,"<br>").replace(/[\n]/g,"<br>")+"</font>";
  343. top.layer.alert(errDetail , {
  344. icon: 2,
  345. area:['80%','70%'],
  346. title:xhr.status+"错误"
  347. })
  348. }else{
  349. var errDetail ="<font color='red'>未知错误!</font>";
  350. top.layer.alert(errDetail , {
  351. icon: 2,
  352. area:['auto','auto'],
  353. title:"真悲剧,后台抛出异常了"
  354. })
  355. }
  356. },
  357. success:function(data,textStatus,jqXHR){
  358. if(data.indexOf == "_login_page_"){//返回首页内容代表登录超时
  359. top.layer.alert("登录超时!")
  360. location.reload(true);
  361. }else{
  362. callback(data);
  363. }
  364. }
  365. });
  366. },
  367. /*
  368. * params:
  369. * id: 表单的jQuery ID 例如"#inputForm"
  370. * succFuc, 成功后执行的回调函数
  371. * beformSubmit:执行提交操作前执行的方法
  372. */
  373. ajaxForm:function(id, succFuc, beforeSubmit){
  374. $(id).ajaxForm({
  375. dataType: 'json',
  376. beforeSubmit: function (arr, form, options) {
  377. if(beforeSubmit){
  378. beforeSubmit();
  379. }
  380. var isValidate = jp.validateForm(id);//校验表单
  381. if(isValidate){
  382. form.find("button:submit").button("loading");
  383. }else{
  384. return false;
  385. }
  386. },
  387. error:function(xhr,textStatus){
  388. if(xhr.status == 0){
  389. jp.info("连接失败,请检查网络!")
  390. }else if(xhr.status == 404){
  391. var errDetail ="<font color='red'>404,请求地址不存在!</font>";
  392. top.layer.alert(errDetail , {
  393. icon: 2,
  394. area:['auto','auto'],
  395. title:"请求出错"
  396. })
  397. }else if(xhr.status && xhr.responseText){
  398. var errDetail ="<font color='red'>"+ xhr.responseText.replace(/[\r\n]/g,"<br>").replace(/[\r]/g,"<br>").replace(/[\n]/g,"<br>")+"</font>";
  399. top.layer.alert(errDetail , {
  400. icon: 2,
  401. area:['80%','70%'],
  402. title:xhr.status+"错误"
  403. })
  404. }else{
  405. var errDetail ="<font color='red'>未知错误!</font>";
  406. top.layer.alert(errDetail , {
  407. icon: 2,
  408. area:['auto','auto'],
  409. title:"真悲剧,后台抛出异常了"
  410. })
  411. }
  412. $(id).find("button:submit").button("reset");
  413. },
  414. success: function (data, statusText, xhr, form) {
  415. succFuc(data);
  416. // form.find("button:submit").button("reset");
  417. }
  418. });
  419. },
  420. //打开对话框(添加修改)
  421. openSaveDialog: function(title,url,width,height){
  422. var auto = true;//是否使用响应式,使用百分比时,应设置为false
  423. if(width.indexOf("%")>=0 || height.indexOf("%")>=0 ){
  424. auto =false;
  425. }
  426. top.layer.open({
  427. type: 2,
  428. area: [width, height],
  429. title: title,
  430. auto:auto,
  431. maxmin: true, //开启最大化最小化按钮
  432. content: url ,
  433. btn: ['确定', '关闭'],
  434. yes: function(index, layero){
  435. var iframeWin = layero.find('iframe')[0]; //得到弹出的窗口对象,执行窗口内iframe页的方法:iframeWin.method();
  436. iframeWin.contentWindow.save();//调用保存事件,在 弹出页内,需要定义save方法。处理保存事件。
  437. },
  438. cancel: function(index){
  439. }
  440. });
  441. },
  442. //打开对话框(查看)
  443. openViewDialog :function(title,url,width,height){
  444. var auto = true;//是否使用响应式,使用百分比时,应设置为false
  445. if(width.indexOf("%")>=0 || height.indexOf("%")>=0 ){
  446. auto =false;
  447. }
  448. top.layer.open({
  449. type: 2,
  450. area: [width, height],
  451. title: title,
  452. auto:auto,
  453. maxmin: true, //开启最大化最小化按钮
  454. content: url ,
  455. btn: ['关闭'],
  456. cancel: function(index){
  457. }
  458. });
  459. },
  460. //打开子对话框(仅仅用作 父子layer弹窗之间交互数据使用)
  461. openChildDialog: function(title,url,width,height, parentObj){
  462. var auto = true;//是否使用响应式,使用百分比时,应设置为false
  463. if(width.indexOf("%")>=0 || height.indexOf("%")>=0 ){
  464. auto =false;
  465. }
  466. top.layer.open({
  467. type: 2,
  468. area: [width, height],
  469. title: title,
  470. auto:auto,
  471. maxmin: true, //开启最大化最小化按钮
  472. content: url ,
  473. btn: ['确定', '关闭'],
  474. yes: function(index, layero){
  475. var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象,执行iframe页的方法:iframeWin.method();
  476. iframeWin.contentWindow.save(parentObj);//在子窗口定义save方法,负责实际业务逻辑的执行
  477. },
  478. cancel: function(index){
  479. }
  480. });
  481. },
  482. validateForm: function (id) {
  483. return $(id).validate({
  484. errorPlacement: function(error, element) {
  485. if (element.is(":checkbox")||element.is(":radio")){
  486. error.appendTo(element.parent().parent().parent().parent());
  487. }else if (element.parent().is(".form_datetime") ||element.parent().is(".input-append") || element.is(".mydatepicker")){
  488. error.appendTo(element.parent().parent());
  489. }else{
  490. error.insertAfter(element);
  491. }
  492. }
  493. }).form();
  494. },
  495. go:function(url){//在当前选项卡跳转页面
  496. window.location.href = url;
  497. },
  498. openTab:function(url,title, isNew){//isNew 为true时,打开一个新的选项卡;为false时,如果选项卡不存在,打开一个新的选项卡,如果已经存在,使已经存在的选项卡变为活跃状态。
  499. top.openTab(url,title,isNew);
  500. },
  501. /**
  502. * Ajax上传文件
  503. * @param fileObj
  504. * @param url
  505. * @param callback
  506. */
  507. uploadFile: function (fileObj, url,callback) {
  508. var data = new FormData(fileObj);
  509. // data.append("CustomField", "This is some extra data, testing");//如果要添加参数
  510. $.ajax({
  511. type: "POST",
  512. enctype: 'multipart/form-data',
  513. url: url,
  514. data: data,
  515. processData: false, //prevent jQuery from automatically transforming the data into a query string
  516. contentType: false,
  517. cache: false,
  518. timeout: 600000,
  519. success: function (result) {
  520. callback(result);
  521. },
  522. error:function(xhr, textStatus){
  523. if(xhr.status == 0){
  524. jp.info("连接失败,请检查网络!")
  525. }else if(xhr.status == 404){
  526. var errDetail ="<font color='red'>404,请求地址不存在!</font>";
  527. top.layer.alert(errDetail , {
  528. icon: 2,
  529. area:['auto','auto'],
  530. title:"请求出错"
  531. })
  532. }else if(xhr.status && xhr.responseText){
  533. var errDetail ="<font color='red'>"+ xhr.responseText.replace(/[\r\n]/g,"<br>").replace(/[\r]/g,"<br>").replace(/[\n]/g,"<br>")+"</font>";
  534. top.layer.alert(errDetail , {
  535. icon: 2,
  536. area:['80%','70%'],
  537. title:xhr.status+"错误"
  538. })
  539. }else{
  540. var errDetail =xhr.responseText=="<font color='red'>未知错误!</font>";
  541. top.layer.alert(errDetail , {
  542. icon: 2,
  543. area:['auto','auto'],
  544. title:"真悲剧,后台抛出异常了"
  545. })
  546. }
  547. }
  548. })
  549. },
  550. downloadFile: function(url, name) {
  551. var $a = $("<a></a>").attr("href", url).attr("download", name);
  552. $a[0].click();
  553. },
  554. /**
  555. * 返回当前活跃的tab页面关联的iframe的Windows对象,方便layer弹窗调用父页面的方法。
  556. */
  557. getParent: function () {
  558. return top.getActiveTab()[0].contentWindow;
  559. }
  560. }
  561. })(jQuery);