common.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. // 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. // laydate 时间控件绑定
  32. if ($(".select-time").length > 0) {
  33. layui.use('laydate', function() {
  34. var laydate = layui.laydate;
  35. var startDate = laydate.render({
  36. elem: '#startTime',
  37. max: $('#endTime').val(),
  38. theme: 'molv',
  39. trigger: 'click',
  40. done: function(value, date) {
  41. // 结束时间大于开始时间
  42. if (value !== '') {
  43. endDate.config.min.year = date.year;
  44. endDate.config.min.month = date.month - 1;
  45. endDate.config.min.date = date.date;
  46. } else {
  47. endDate.config.min.year = '';
  48. endDate.config.min.month = '';
  49. endDate.config.min.date = '';
  50. }
  51. }
  52. });
  53. var endDate = laydate.render({
  54. elem: '#endTime',
  55. min: $('#startTime').val(),
  56. theme: 'molv',
  57. trigger: 'click',
  58. done: function(value, date) {
  59. // 开始时间小于结束时间
  60. if (value !== '') {
  61. startDate.config.max.year = date.year;
  62. startDate.config.max.month = date.month - 1;
  63. startDate.config.max.date = date.date;
  64. } else {
  65. startDate.config.max.year = '';
  66. startDate.config.max.month = '';
  67. startDate.config.max.date = '';
  68. }
  69. }
  70. });
  71. });
  72. }
  73. // laydate time-input 时间控件绑定
  74. if ($(".time-input").length > 0) {
  75. layui.use('laydate', function () {
  76. var com = layui.laydate;
  77. $(".time-input").each(function (index, item) {
  78. var time = $(item);
  79. // 控制控件外观
  80. var type = time.attr("data-type") || 'date';
  81. // 控制回显格式
  82. var format = time.attr("data-format") || 'yyyy-MM-dd';
  83. // 控制日期控件按钮
  84. var buttons = time.attr("data-btn") || 'clear|now|confirm', newBtnArr = [];
  85. // 日期控件选择完成后回调处理
  86. var callback = time.attr("data-callback") || {};
  87. if (buttons) {
  88. if (buttons.indexOf("|") > 0) {
  89. var btnArr = buttons.split("|"), btnLen = btnArr.length;
  90. for (var j = 0; j < btnLen; j++) {
  91. if ("clear" === btnArr[j] || "now" === btnArr[j] || "confirm" === btnArr[j]) {
  92. newBtnArr.push(btnArr[j]);
  93. }
  94. }
  95. } else {
  96. if ("clear" === buttons || "now" === buttons || "confirm" === buttons) {
  97. newBtnArr.push(buttons);
  98. }
  99. }
  100. } else {
  101. newBtnArr = ['clear', 'now', 'confirm'];
  102. }
  103. com.render({
  104. elem: item,
  105. theme: 'molv',
  106. trigger: 'click',
  107. type: type,
  108. format: format,
  109. btns: newBtnArr,
  110. done: function (value, data) {
  111. if (typeof window[callback] != 'undefined'
  112. && window[callback] instanceof Function) {
  113. window[callback](value, data);
  114. }
  115. }
  116. });
  117. });
  118. });
  119. }
  120. // tree 关键字搜索绑定
  121. if ($("#keyword").length > 0) {
  122. $("#keyword").bind("focus", function focusKey(e) {
  123. if ($("#keyword").hasClass("empty")) {
  124. $("#keyword").removeClass("empty");
  125. }
  126. }).bind("blur", function blurKey(e) {
  127. if ($("#keyword").val() === "") {
  128. $("#keyword").addClass("empty");
  129. }
  130. $.tree.searchNode(e);
  131. }).bind("input propertychange", $.tree.searchNode);
  132. }
  133. // tree表格树 展开/折叠
  134. var expandFlag;
  135. $("#expandAllBtn").click(function() {
  136. var dataExpand = $.common.isEmpty($.table._option.expandAll) ? true : $.table._option.expandAll;
  137. expandFlag = $.common.isEmpty(expandFlag) ? dataExpand : expandFlag;
  138. if (!expandFlag) {
  139. $.bttTable.bootstrapTreeTable('expandAll');
  140. } else {
  141. $.bttTable.bootstrapTreeTable('collapseAll');
  142. }
  143. expandFlag = expandFlag ? false: true;
  144. })
  145. // 按下ESC按钮关闭弹层
  146. $('body', document).on('keyup', function(e) {
  147. if (e.which === 27) {
  148. $.modal.closeAll();
  149. }
  150. });
  151. });
  152. /** 刷新选项卡 */
  153. var refreshItem = function(){
  154. var topWindow = $(window.parent.document);
  155. var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-id');
  156. var target = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow);
  157. var url = target.attr('src');
  158. target.attr('src', url).ready();
  159. }
  160. /** 关闭选项卡 */
  161. var closeItem = function(dataId){
  162. var topWindow = $(window.parent.document);
  163. if($.common.isNotEmpty(dataId)){
  164. window.parent.$.modal.closeLoading();
  165. // 根据dataId关闭指定选项卡
  166. $('.menuTab[data-id="' + dataId + '"]', topWindow).remove();
  167. // 移除相应tab对应的内容区
  168. $('.mainContent .RuoYi_iframe[data-id="' + dataId + '"]', topWindow).remove();
  169. return;
  170. }
  171. if(window.frameElement!=null){
  172. var panelUrl = window.frameElement.getAttribute('data-panel');
  173. $('.page-tabs-content .active i', topWindow).click();
  174. if($.common.isNotEmpty(panelUrl)){
  175. $('.menuTab[data-id="' + panelUrl + '"]', topWindow).addClass('active').siblings('.menuTab').removeClass('active');
  176. $('.mainContent .RuoYi_iframe', topWindow).each(function() {
  177. if ($(this).data('id') == panelUrl) {
  178. $(this).show().siblings('.RuoYi_iframe').hide();
  179. return false;
  180. }
  181. });
  182. }
  183. }else if(window.frameElement==null){
  184. window.close();
  185. }
  186. }
  187. /** 创建选项卡 */
  188. function createMenuItem(dataUrl, menuName) {
  189. var panelUrl = window.frameElement.getAttribute('data-id');
  190. dataIndex = $.common.random(1,100),
  191. flag = true;
  192. if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;
  193. var topWindow = $(window.parent.document);
  194. // 选项卡菜单已存在
  195. $('.menuTab', topWindow).each(function() {
  196. if ($(this).data('id') == dataUrl) {
  197. if (!$(this).hasClass('active')) {
  198. $(this).addClass('active').siblings('.menuTab').removeClass('active');
  199. $('.page-tabs-content').animate({ marginLeft: ""}, "fast");
  200. // 显示tab对应的内容区
  201. $('.mainContent .RuoYi_iframe', topWindow).each(function() {
  202. if ($(this).data('id') == dataUrl) {
  203. $(this).show().siblings('.RuoYi_iframe').hide();
  204. return false;
  205. }
  206. });
  207. }
  208. flag = false;
  209. return false;
  210. }
  211. });
  212. // 选项卡菜单不存在
  213. if (flag) {
  214. var str = '<a href="javascript:;" class="active menuTab" data-id="' + dataUrl + '" data-panel="' + panelUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  215. $('.menuTab', topWindow).removeClass('active');
  216. // 添加选项卡对应的iframe
  217. 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>';
  218. $('.mainContent', topWindow).find('iframe.RuoYi_iframe').hide().parents('.mainContent').append(str1);
  219. window.parent.$.modal.loading("数据加载中,请稍后...");
  220. $('.mainContent iframe:visible', topWindow).load(function () {
  221. window.parent.$.modal.closeLoading();
  222. });
  223. // 添加选项卡
  224. $('.menuTabs .page-tabs-content', topWindow).append(str);
  225. }
  226. return false;
  227. }
  228. //日志打印封装处理
  229. var log = {
  230. log: function(msg) {
  231. console.log(msg);
  232. },
  233. info: function(msg) {
  234. console.info(msg);
  235. },
  236. warn: function(msg) {
  237. console.warn(msg);
  238. },
  239. error: function(msg) {
  240. console.error(msg);
  241. }
  242. };
  243. //本地缓存处理
  244. var storage = {
  245. set: function(key, value) {
  246. window.localStorage.setItem(key, value);
  247. },
  248. get: function(key) {
  249. return window.localStorage.getItem(key);
  250. },
  251. remove: function(key) {
  252. window.localStorage.removeItem(key);
  253. },
  254. clear: function() {
  255. window.localStorage.clear();
  256. }
  257. };
  258. /** 设置全局ajax处理 */
  259. $.ajaxSetup({
  260. complete: function(XMLHttpRequest, textStatus) {
  261. if (textStatus == 'timeout') {
  262. $.modal.alertWarning("服务器超时,请稍后再试!");
  263. $.modal.enable();
  264. $.modal.closeLoading();
  265. } else if (textStatus == "parsererror" || textStatus == "error") {
  266. $.modal.alertWarning("服务器错误,请联系管理员!");
  267. $.modal.enable();
  268. $.modal.closeLoading();
  269. }
  270. }
  271. });