common.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. var topWin = (function (p, c) {
  2. while (p != c) {
  3. c = p
  4. p = p.parent
  5. }
  6. return c
  7. })(window.parent, window);
  8. /**
  9. * 初始化 Layui.Table
  10. */
  11. $(document).ready(function () {
  12. topWin.hideLoadLayer(); //关闭加载层
  13. layui.table.init("devTable", {
  14. skin: 'line',
  15. even: 'even',
  16. size: 'lg',
  17. text: {
  18. none: ''
  19. }
  20. });
  21. initContentHeight();
  22. initNoDataDefaultShow();
  23. });
  24. window.onbeforeunload = function () {
  25. topWin.showLoadLayer();
  26. };
  27. /**
  28. * 初始化主题内容高度
  29. */
  30. function initContentHeight() {
  31. if ($(".bottom-div").length == 0) {
  32. return;
  33. }
  34. $(".layui-anim-fadein").height(($("body").innerHeight() - 40) + "px");
  35. $(".layui-anim-fadein").css("overflow", "auto");
  36. }
  37. /**
  38. * 弹出层:ifarme
  39. * @param src 页面路径
  40. * @param title 弹层标题
  41. * @param width 宽度
  42. * @param height 高度
  43. */
  44. function layAlertPage(src, title, w, h, reload, end) {
  45. topWin.showLoadLayer();
  46. var param = {
  47. width: w.toString(),
  48. height: h.toString(),
  49. frameContentChangeFlag: false // 窗口内容改变标记,如改变为真,否则为假
  50. };
  51. // 处理百分比写法,比如:60%
  52. if (param.width && param.height) {
  53. if (param.width.indexOf("%") != -1) {
  54. var _width = param.width.replace("%", "");
  55. param.width = $(top).width() * _width / 100;
  56. }
  57. if (param.height.indexOf("%") != -1) {
  58. var _height = param.height.replace("%", "");
  59. param.height = $(top).height() * _height / 100;
  60. }
  61. }
  62. // 弹层宽度,缺省值960px,如为0,则适应屏幕宽度, 如open为假,则该参数无效。
  63. var width = param.width == null ? 960 : param.width;
  64. if (width == 0) {
  65. width = $(top).width() - 10;
  66. }
  67. //弹层宽度若超过浏览器的宽度,改为浏览器的宽度
  68. if (width > $(top).width()) {
  69. width = $(top).width();
  70. }
  71. // 弹层高度,缺省值680px,如为0,则适应屏幕高度,如open为假,则该参数无效。
  72. var height = param.height == null ? 680 : param.height;
  73. if (height == 0) {
  74. height = $(top).height() - 10;
  75. }
  76. //弹层高度若超过浏览器的高度,改为浏览器的高度
  77. if (height > $(top).height()) {
  78. height = $(top).height();
  79. }
  80. var index = topWin.layer.getFrameIndex(window.name);
  81. src += src.indexOf('?') == -1 ? ("?layerIndex=" + index) : ("&layerIndex=" + index);
  82. topWin.layer.open({
  83. type: 2,
  84. title: title,
  85. fixed: false,
  86. anim: '5',
  87. isOutAnim: false,
  88. maxmin: false,
  89. resize: false,
  90. shade: [0.1, '#fff'],
  91. area: [width + 'px', height + 'px'],
  92. content: src,
  93. success: function (layero, index) {
  94. var win = layero[0].getElementsByTagName('iframe')[0].contentWindow;
  95. // 监听表单是否提交
  96. win.$("form").on("submit", function () {
  97. param.frameContentChangeFlag = true;
  98. })
  99. win.isLayer = true;
  100. win.layerIndex = index;
  101. win.parentWin = window;
  102. if (win.layerReady) {
  103. win.layerReady();
  104. }
  105. },
  106. end: function () {
  107. topWin.hideLoadLayer();
  108. if (reload && param.frameContentChangeFlag) {
  109. location.reload(true);
  110. }
  111. if (end) {
  112. end();
  113. }
  114. }
  115. });
  116. }
  117. /**
  118. * 全屏弹层
  119. * @param content 正文
  120. * @param title 标题
  121. */
  122. function layAlertMaxPage(content) {
  123. var index = topWin.layer.open({
  124. type: 1,
  125. isOutAnim: false,
  126. title: '全屏查看',
  127. offset: 't',
  128. content: content,
  129. area: ['100%', '100%']
  130. });
  131. parent.layer.iframeAuto(index);
  132. parent.layer.full(index);
  133. }
  134. function reqDevService(url) {
  135. location.href = url;
  136. }
  137. /*
  138. * 头部通知
  139. * tip.green(msg,time);
  140. * msg为通知文字
  141. * time为显示时间(毫秒),,默认3秒关闭
  142. */
  143. var tip = {
  144. success: function (msg, time) {
  145. suiTip(msg, "success", time);
  146. },
  147. error: function (msg, time) {
  148. suiTip(msg, "error", time);
  149. },
  150. warning: function (msg, time) {
  151. suiTip(msg, "warning", time);
  152. },
  153. stop: function (msg, time) {
  154. suiTip(msg, "stop", time);
  155. },
  156. notice: function (msg, time) {
  157. suiTip(msg, "notice", time);
  158. },
  159. tips: function (msg, time) {
  160. suiTip(msg, "tips", time);
  161. },
  162. info: function (msg, time) {
  163. suiTip(msg, "info", time)
  164. },
  165. question: function (msg, time) {
  166. suiTip(msg, "question", time)
  167. }
  168. }
  169. function suiTip(msg, type, time) {
  170. topWin.tips(msg, type, time);
  171. }
  172. /**
  173. * 当前窗口内提示消息
  174. */
  175. var msg = function (msg) {
  176. layer.msg(msg, {
  177. anim: 1,
  178. offset: 't'
  179. });
  180. }
  181. /**
  182. * 关闭当前窗体
  183. */
  184. function closeLayDialog() {
  185. var index = topWin.layer.getFrameIndex(window.name); //获取窗口索引
  186. topWin.layer.close(index);
  187. }
  188. /**
  189. * 判断对象是否为空,为空返回true,不为空返回false
  190. * @param {Object} obj
  191. */
  192. function isEmpty(obj) {
  193. if (typeof obj == "undefined" || obj == null || obj == "") {
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }
  199. /**
  200. * 通过正则表达式获取浏览器的参数
  201. * @param name
  202. * @returns
  203. */
  204. function getQueryString(name) {
  205. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  206. var r = window.location.search.substr(1).match(reg);
  207. if (r != null) return unescape(r[2]);
  208. return null;
  209. }
  210. /**
  211. * 加密字符串
  212. * @param {Object} str
  213. */
  214. function compileStr(str) {
  215. var c = String.fromCharCode(str.charCodeAt(0) + str.length);
  216. for (var i = 1; i < str.length; i++) {
  217. c += String.fromCharCode(str.charCodeAt(i) + str.charCodeAt(i - 1));
  218. }
  219. return escape(c);
  220. }
  221. /**
  222. * 解密字符串
  223. */
  224. function uncompileStr(str) {
  225. str = unescape(str);
  226. var c = String.fromCharCode(str.charCodeAt(0) - str.length);
  227. for (var i = 1; i < str.length; i++) {
  228. c += String.fromCharCode(str.charCodeAt(i) - c.charCodeAt(i - 1));
  229. }
  230. return c;
  231. }
  232. /**
  233. * 初始化无数据时的默认显示
  234. */
  235. function initNoDataDefaultShow() {
  236. var defaultText = "<span class='no-data-text'>(null)</span>";
  237. // LayTable
  238. var cellAry = $(".layui-table-body").find(".layui-table-cell");
  239. for (var i = 0; i < cellAry.length; i++) {
  240. if (space.utils.isNull($(cellAry[i]).html())) {
  241. $(cellAry[i]).html(defaultText);
  242. }
  243. }
  244. // 查看
  245. var tdAry = $(".sui-table").find("td");
  246. for (var i = 0; i < tdAry.length; i++) {
  247. if (isNull($(tdAry[i]).html())) {
  248. $(tdAry[i]).html(defaultText);
  249. }
  250. }
  251. }
  252. function isNull(obj) {
  253. return (typeof obj == "undefined" || obj == null || obj.trim() == "");
  254. }
  255. /**
  256. * 列表排序
  257. */
  258. space.plugins.sortable = {
  259. init: function () {
  260. new Sortable(sortable, {
  261. sort: true,
  262. group: 'shared',
  263. swapThreshold: 0.5,
  264. animation: 150,
  265. chosenClass: "sortable-chosen", // 选中时的DOM节点的class名称
  266. dragClass: "sortable-drag", // 拖放时的DOM节点的class名称
  267. onStart: function (evt) { // 拖拽开始
  268. },
  269. onEnd: function (evt) { // 拖拽结束
  270. $("#sortNum li").removeClass("highlight");
  271. },
  272. onChoose: function ( /**Event*/ evt) { //点击选择
  273. $("#sortNum li").removeClass("highlight");
  274. $("#sortNum li").eq(evt.oldIndex).addClass("highlight");
  275. },
  276. onMove: function ( /**Event*/ evt, /**Event*/ originalEvent) { //拖拽移动
  277. var numIndex = $(evt.related).index();
  278. $("#sortNum li").removeClass("highlight");
  279. $("#sortNum li").eq(numIndex).addClass("highlight");
  280. }
  281. });
  282. },
  283. getData: function () {
  284. var dataIds = "";
  285. var itemAry = $("#sortable").find("li");
  286. for (var i = 0; i < itemAry.length; i++) {
  287. dataIds += $(itemAry[i]).attr("data-id") + ",";
  288. }
  289. return dataIds;
  290. }
  291. }
  292. function getStringAsEmpty(str) {
  293. return isEmpty(str) ? "" : str;
  294. }
  295. /**
  296. * 异步请求工具类
  297. */
  298. space.utils.ajax = function (setting) {
  299. if (setting.success) {
  300. console.error("success 函数由 callback 代替,请将 success 函数修改为 callback 后重试");
  301. return;
  302. }
  303. var index = layer.load(2);
  304. setting.complete = function () {
  305. layer.close(index);
  306. }
  307. setting.error = function (xhr, status, error) {
  308. console.error(xhr);
  309. console.error(status);
  310. console.error(error);
  311. layer.msg("系统繁忙,请稍后再试");
  312. if (setting.err) {
  313. setting.err();
  314. layer.close(index);
  315. }
  316. };
  317. setting.success = function (data) {
  318. try {
  319. if (data != undefined && data.code == "error") {
  320. layer.msg(data.msg);
  321. layer.close(index);
  322. }
  323. } catch (e) {
  324. }
  325. if (setting.callback) {
  326. setting.callback(data);
  327. }
  328. }
  329. try {
  330. $.ajax(setting);
  331. } catch (e) {
  332. console.error(e);
  333. layer.close(index);
  334. }
  335. }
  336. /**
  337. * 深复制一个对象
  338. */
  339. space.utils.cloneObj = function (obj) {
  340. var newObj = {};
  341. if (obj instanceof Array) {
  342. newObj = [];
  343. }
  344. for (var key in obj) {
  345. var val = obj[key];
  346. if (typeof val === 'undefined') {
  347. newObj[key] = "undefined";
  348. continue;
  349. }
  350. // arguments.callee 在哪一个函数中运行,它就代表哪个函数, 一般用在匿名函数中。
  351. newObj[key] = typeof val === 'object' ? space.utils.cloneObj(val) : val;
  352. }
  353. return newObj;
  354. }
  355. /**
  356. * 下载文件
  357. * @param {*} fileName 文件名
  358. * @param {*} content 文件内容
  359. */
  360. space.utils.download = function (fileName, content) {
  361. var eleLink = document.createElement('a');
  362. eleLink.download = fileName;
  363. eleLink.style.display = 'none';
  364. var blob = new Blob([content]);
  365. eleLink.href = URL.createObjectURL(blob);
  366. document.body.appendChild(eleLink);
  367. eleLink.click();
  368. document.body.removeChild(eleLink);
  369. }
  370. /**
  371. * 对象转字符串,保留函数体
  372. * @param {*} o
  373. */
  374. space.utils.objToStr = function (o) {
  375. var r = [];
  376. if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g,
  377. "\\r").replace(/(\t)/g, "\\t") + "\"";
  378. if (typeof o == "object") {
  379. if (!o.sort) {
  380. for (var i in o)
  381. r.push(i + ":" + space.utils.objToStr(o[i]));
  382. if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
  383. r.push("toString:" + o.toString.toString());
  384. }
  385. r = "{" + r.join() + "}"
  386. } else {
  387. for (var i = 0; i < o.length; i++)
  388. r.push(space.utils.objToStr(o[i]))
  389. r = "[" + r.join() + "]"
  390. }
  391. return r;
  392. }
  393. return o.toString();
  394. }