common.js 21 KB

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