common.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /**
  2. * 通用方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. $(function() {
  6. // layer扩展皮肤
  7. if (window.layer !== undefined) {
  8. layer.config({
  9. extend: 'moon/style.css',
  10. skin: 'layer-ext-moon'
  11. });
  12. }
  13. // 回到顶部绑定
  14. if ($.fn.toTop !== undefined) {
  15. $('#scroll-up').toTop();
  16. }
  17. // select2复选框事件绑定
  18. if ($.fn.select2 !== undefined) {
  19. $.fn.select2.defaults.set( "theme", "bootstrap" );
  20. $("select.form-control:not(.noselect2)").each(function () {
  21. $(this).select2().on("change", function () {
  22. $(this).valid();
  23. })
  24. })
  25. }
  26. // iCheck单选框及复选框事件绑定
  27. if ($.fn.iCheck !== undefined) {
  28. $(".check-box:not(.noicheck),.radio-box:not(.noicheck)").each(function() {
  29. $(this).iCheck({
  30. checkboxClass: 'icheckbox-blue',
  31. radioClass: 'iradio-blue',
  32. })
  33. })
  34. }
  35. // 气泡弹出框特效(移到元素时)
  36. $(document).on("mouseenter", '.table [data-toggle="popover"]', function() {
  37. var _this = this;
  38. $(this).popover("show");
  39. $(".popover").on("mouseleave", function() {
  40. $(_this).popover('hide');
  41. });
  42. })
  43. // 气泡弹出框特效(离开元素时)
  44. $(document).on("mouseleave", '.table [data-toggle="popover"]', function() {
  45. var _this = this;
  46. setTimeout(function() {
  47. if (!$(".popover:hover").length) $(_this).popover("hide");
  48. }, 100);
  49. });
  50. // 取消回车自动提交表单
  51. $(document).on("keypress", ":input:not(textarea):not([type=submit])", function(event) {
  52. if (event.keyCode == 13) {
  53. event.preventDefault();
  54. }
  55. });
  56. // laydate 时间控件绑定
  57. if ($(".select-time").length > 0) {
  58. layui.use('laydate', function() {
  59. var laydate = layui.laydate;
  60. var startDate = laydate.render({
  61. elem: '#startTime',
  62. max: $('#endTime').val(),
  63. theme: 'molv',
  64. trigger: 'click',
  65. done: function(value, date) {
  66. // 结束时间大于开始时间
  67. if (value !== '') {
  68. endDate.config.min.year = date.year;
  69. endDate.config.min.month = date.month - 1;
  70. endDate.config.min.date = date.date;
  71. } else {
  72. endDate.config.min.year = '';
  73. endDate.config.min.month = '';
  74. endDate.config.min.date = '';
  75. }
  76. }
  77. });
  78. var endDate = laydate.render({
  79. elem: '#endTime',
  80. min: $('#startTime').val(),
  81. theme: 'molv',
  82. trigger: 'click',
  83. done: function(value, date) {
  84. // 开始时间小于结束时间
  85. if (value !== '') {
  86. startDate.config.max.year = date.year;
  87. startDate.config.max.month = date.month - 1;
  88. startDate.config.max.date = date.date;
  89. } else {
  90. startDate.config.max.year = '2099';
  91. startDate.config.max.month = '12';
  92. startDate.config.max.date = '31';
  93. }
  94. }
  95. });
  96. });
  97. }
  98. // laydate time-input 时间控件绑定
  99. if ($(".time-input").length > 0) {
  100. layui.use('laydate', function () {
  101. var com = layui.laydate;
  102. $(".time-input").each(function (index, item) {
  103. var time = $(item);
  104. // 控制控件外观
  105. var type = time.attr("data-type") || 'date';
  106. // 控制回显格式
  107. var format = time.attr("data-format") || 'yyyy-MM-dd';
  108. // 控制日期控件按钮
  109. var buttons = time.attr("data-btn") || 'clear|now|confirm', newBtnArr = [];
  110. // 日期控件选择完成后回调处理
  111. var callback = time.attr("data-callback") || {};
  112. if (buttons) {
  113. if (buttons.indexOf("|") > 0) {
  114. var btnArr = buttons.split("|"), btnLen = btnArr.length;
  115. for (var j = 0; j < btnLen; j++) {
  116. if ("clear" === btnArr[j] || "now" === btnArr[j] || "confirm" === btnArr[j]) {
  117. newBtnArr.push(btnArr[j]);
  118. }
  119. }
  120. } else {
  121. if ("clear" === buttons || "now" === buttons || "confirm" === buttons) {
  122. newBtnArr.push(buttons);
  123. }
  124. }
  125. } else {
  126. newBtnArr = ['clear', 'now', 'confirm'];
  127. }
  128. com.render({
  129. elem: item,
  130. theme: 'molv',
  131. trigger: 'click',
  132. type: type,
  133. format: format,
  134. btns: newBtnArr,
  135. done: function (value, data) {
  136. if (typeof window[callback] != 'undefined'
  137. && window[callback] instanceof Function) {
  138. window[callback](value, data);
  139. }
  140. }
  141. });
  142. });
  143. });
  144. }
  145. // tree 关键字搜索绑定
  146. if ($("#keyword").length > 0) {
  147. $("#keyword").bind("focus", function focusKey(e) {
  148. if ($("#keyword").hasClass("empty")) {
  149. $("#keyword").removeClass("empty");
  150. }
  151. }).bind("blur", function blurKey(e) {
  152. if ($("#keyword").val() === "") {
  153. $("#keyword").addClass("empty");
  154. }
  155. $.tree.searchNode(e);
  156. }).bind("input propertychange", $.tree.searchNode);
  157. }
  158. // tree表格树 展开/折叠
  159. var expandFlag;
  160. $("#expandAllBtn").click(function() {
  161. var dataExpand = $.common.isEmpty(table.options.expandAll) ? true : table.options.expandAll;
  162. expandFlag = $.common.isEmpty(expandFlag) ? dataExpand : expandFlag;
  163. if (!expandFlag) {
  164. $.bttTable.bootstrapTreeTable('expandAll');
  165. } else {
  166. $.bttTable.bootstrapTreeTable('collapseAll');
  167. }
  168. expandFlag = expandFlag ? false: true;
  169. })
  170. // 按下ESC按钮关闭弹层
  171. $('body', document).on('keyup', function(e) {
  172. if (e.which === 27) {
  173. $.modal.closeAll();
  174. }
  175. });
  176. });
  177. (function ($) {
  178. 'use strict';
  179. $.fn.toTop = function(opt) {
  180. var elem = this;
  181. var win = (opt && opt.hasOwnProperty('win')) ? opt.win : $(window);
  182. var doc = (opt && opt.hasOwnProperty('doc')) ? opt.doc : $('html, body');
  183. var options = $.extend({
  184. autohide: true,
  185. offset: 50,
  186. speed: 500,
  187. position: true,
  188. right: 15,
  189. bottom: 5
  190. }, opt);
  191. elem.css({
  192. 'cursor': 'pointer'
  193. });
  194. if (options.autohide) {
  195. elem.css('display', 'none');
  196. }
  197. if (options.position) {
  198. elem.css({
  199. 'position': 'fixed',
  200. 'right': options.right,
  201. 'bottom': options.bottom,
  202. });
  203. }
  204. elem.click(function() {
  205. doc.animate({
  206. scrollTop: 0
  207. }, options.speed);
  208. });
  209. win.scroll(function() {
  210. var scrolling = win.scrollTop();
  211. if (options.autohide) {
  212. if (scrolling > options.offset) {
  213. elem.fadeIn(options.speed);
  214. } else elem.fadeOut(options.speed);
  215. }
  216. });
  217. };
  218. })(jQuery);
  219. /** 刷新选项卡 */
  220. var refreshItem = function(){
  221. var topWindow = $(window.parent.document);
  222. var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-id');
  223. var target = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow);
  224. var url = target.attr('src');
  225. target.attr('src', url).ready();
  226. }
  227. /** 关闭选项卡 */
  228. var closeItem = function(dataId){
  229. var topWindow = $(window.parent.document);
  230. if($.common.isNotEmpty(dataId)){
  231. window.parent.$.modal.closeLoading();
  232. // 根据dataId关闭指定选项卡
  233. $('.menuTab[data-id="' + dataId + '"]', topWindow).remove();
  234. // 移除相应tab对应的内容区
  235. $('.mainContent .RuoYi_iframe[data-id="' + dataId + '"]', topWindow).remove();
  236. return;
  237. }
  238. var panelUrl = window.frameElement.getAttribute('data-panel');
  239. $('.page-tabs-content .active i', topWindow).click();
  240. if($.common.isNotEmpty(panelUrl)){
  241. $('.menuTab[data-id="' + panelUrl + '"]', topWindow).addClass('active').siblings('.menuTab').removeClass('active');
  242. $('.mainContent .RuoYi_iframe', topWindow).each(function() {
  243. if ($(this).data('id') == panelUrl) {
  244. $(this).show().siblings('.RuoYi_iframe').hide();
  245. return false;
  246. }
  247. });
  248. }
  249. }
  250. /** 创建选项卡 */
  251. function createMenuItem(dataUrl, menuName) {
  252. var panelUrl = window.frameElement.getAttribute('data-id');
  253. dataIndex = $.common.random(1, 100),
  254. flag = true;
  255. if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;
  256. var topWindow = $(window.parent.document);
  257. // 选项卡菜单已存在
  258. $('.menuTab', topWindow).each(function() {
  259. if ($(this).data('id') == dataUrl) {
  260. if (!$(this).hasClass('active')) {
  261. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  262. scrollToTab(this);
  263. $('.page-tabs-content').animate({ marginLeft: ""}, "fast");
  264. // 显示tab对应的内容区
  265. $('.mainContent .RuoYi_iframe', topWindow).each(function() {
  266. if ($(this).data('id') == dataUrl) {
  267. $(this).show().siblings('.RuoYi_iframe').hide();
  268. return false;
  269. }
  270. });
  271. }
  272. flag = false;
  273. return false;
  274. }
  275. });
  276. // 选项卡菜单不存在
  277. if (flag) {
  278. var str = '<a href="javascript:;" class="active menuTab noactive" data-id="' + dataUrl + '" data-panel="' + panelUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  279. $('.menuTab', topWindow).removeClass('active');
  280. // 添加选项卡对应的iframe
  281. var str1 = '<iframe class="RuoYi_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" data-panel="' + panelUrl + '" seamless></iframe>';
  282. $('.mainContent', topWindow).find('iframe.RuoYi_iframe').hide().parents('.mainContent').append(str1);
  283. window.parent.$.modal.loading("数据加载中,请稍后...");
  284. $('.mainContent iframe:visible', topWindow).load(function () {
  285. window.parent.$.modal.closeLoading();
  286. });
  287. // 添加选项卡
  288. $('.menuTabs .page-tabs-content', topWindow).append(str);
  289. scrollToTab($('.menuTab.active', topWindow));
  290. }
  291. return false;
  292. }
  293. // 滚动到指定选项卡
  294. function scrollToTab(element) {
  295. var topWindow = $(window.parent.document);
  296. var marginLeftVal = calSumWidth($(element).prevAll()),
  297. marginRightVal = calSumWidth($(element).nextAll());
  298. // 可视区域非tab宽度
  299. var tabOuterWidth = calSumWidth($(".content-tabs", topWindow).children().not(".menuTabs"));
  300. //可视区域tab宽度
  301. var visibleWidth = $(".content-tabs", topWindow).outerWidth(true) - tabOuterWidth;
  302. //实际滚动宽度
  303. var scrollVal = 0;
  304. if ($(".page-tabs-content", topWindow).outerWidth() < visibleWidth) {
  305. scrollVal = 0;
  306. } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
  307. if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
  308. scrollVal = marginLeftVal;
  309. var tabElement = element;
  310. while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content", topWindow).outerWidth() - visibleWidth)) {
  311. scrollVal -= $(tabElement).prev().outerWidth();
  312. tabElement = $(tabElement).prev();
  313. }
  314. }
  315. } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
  316. scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
  317. }
  318. $('.page-tabs-content', topWindow).animate({ marginLeft: 0 - scrollVal + 'px' }, "fast");
  319. }
  320. //计算元素集合的总宽度
  321. function calSumWidth(elements) {
  322. var width = 0;
  323. $(elements).each(function() {
  324. width += $(this).outerWidth(true);
  325. });
  326. return width;
  327. }
  328. /** 密码规则范围验证 */
  329. function checkpwd(chrtype, password) {
  330. if (chrtype == 1) {
  331. if(!$.common.numValid(password)){
  332. $.modal.alertWarning("密码只能为0-9数字");
  333. return false;
  334. }
  335. } else if (chrtype == 2) {
  336. if(!$.common.enValid(password)){
  337. $.modal.alertWarning("密码只能为a-z和A-Z字母");
  338. return false;
  339. }
  340. } else if (chrtype == 3) {
  341. if(!$.common.enNumValid(password)){
  342. $.modal.alertWarning("密码必须包含字母以及数字");
  343. return false;
  344. }
  345. } else if (chrtype == 4) {
  346. if(!$.common.charValid(password)){
  347. $.modal.alertWarning("密码必须包含字母、数字、以及特殊符号<font color='red'>~!@#$%^&*()-=_+</font>");
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. // 日志打印封装处理
  354. var log = {
  355. log: function(msg) {
  356. console.log(msg);
  357. },
  358. info: function(msg) {
  359. console.info(msg);
  360. },
  361. warn: function(msg) {
  362. console.warn(msg);
  363. },
  364. error: function(msg) {
  365. console.error(msg);
  366. }
  367. };
  368. // 本地缓存处理
  369. var storage = {
  370. set: function(key, value) {
  371. window.localStorage.setItem(key, value);
  372. },
  373. get: function(key) {
  374. return window.localStorage.getItem(key);
  375. },
  376. remove: function(key) {
  377. window.localStorage.removeItem(key);
  378. },
  379. clear: function() {
  380. window.localStorage.clear();
  381. }
  382. };
  383. // 主子表操作封装处理
  384. var sub = {
  385. editColumn: function() {
  386. var count = $("#" + table.options.id).bootstrapTable('getData').length;
  387. var params = new Array();
  388. for (var dataIndex = 0; dataIndex <= count; dataIndex++) {
  389. var columns = $('#' + table.options.id + ' tr[data-index="' + dataIndex + '"] td');
  390. var obj = new Object();
  391. for (var i = 0; i < columns.length; i++) {
  392. var inputValue = $(columns[i]).find('input');
  393. var selectValue = $(columns[i]).find('select');
  394. var key = table.options.columns[i].field;
  395. if ($.common.isNotEmpty(inputValue.val())) {
  396. obj[key] = inputValue.val();
  397. } else if ($.common.isNotEmpty(selectValue.val())) {
  398. obj[key] = selectValue.val();
  399. } else {
  400. obj[key] = "";
  401. }
  402. }
  403. params.push({ index: dataIndex, row: obj });
  404. }
  405. $("#" + table.options.id).bootstrapTable("updateRow", params);
  406. },
  407. delColumn: function(column) {
  408. sub.editColumn();
  409. var subColumn = $.common.isEmpty(column) ? "index" : column;
  410. var ids = $.table.selectColumns(subColumn);
  411. if (ids.length == 0) {
  412. $.modal.alertWarning("请至少选择一条记录");
  413. return;
  414. }
  415. $("#" + table.options.id).bootstrapTable('remove', { field: subColumn, values: ids });
  416. },
  417. addColumn: function(row, tableId) {
  418. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  419. table.set(currentId);
  420. var count = $("#" + currentId).bootstrapTable('getData').length;
  421. sub.editColumn();
  422. $("#" + currentId).bootstrapTable('insertRow', {
  423. index: count + 1,
  424. row: row
  425. });
  426. }
  427. };
  428. // 动态加载css文件
  429. function loadCss(file, headElem) {
  430. var link = document.createElement('link');
  431. link.href = file;
  432. link.rel = 'stylesheet';
  433. link.type = 'text/css';
  434. if (headElem) headElem.appendChild(link);
  435. else document.getElementsByTagName('head')[0].appendChild(link);
  436. }
  437. // 动态加载js文件
  438. function loadJs(file, headElem) {
  439. var script = document.createElement('script');
  440. script.src = file;
  441. script.type = 'text/javascript';
  442. if (headElem) headElem.appendChild(script);
  443. else document.getElementsByTagName('head')[0].appendChild(script);
  444. }
  445. /** 设置全局ajax处理 */
  446. $.ajaxSetup({
  447. complete: function(XMLHttpRequest, textStatus) {
  448. if (textStatus == 'timeout') {
  449. $.modal.alertWarning("服务器超时,请稍后再试!");
  450. $.modal.enable();
  451. $.modal.closeLoading();
  452. } else if (textStatus == "parsererror" || textStatus == "error") {
  453. $.modal.alertWarning("服务器错误,请联系管理员!");
  454. $.modal.enable();
  455. $.modal.closeLoading();
  456. }
  457. }
  458. });